0% found this document useful (0 votes)
18 views7 pages

Deep Learning Model Training Guide

Uploaded by

Sahil Mhaske
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

Deep Learning Model Training Guide

Uploaded by

Sahil Mhaske
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

In [1]: import numpy as np

# Generate toy dataset


[Link](0)
X = [Link](100, 2) # 100 samples, 2 features
y = (X[:, 0] + X[:, 1] > 1).astype(int) # Binary target variable

print("Shape of X:", [Link])


print("Shape of y:", [Link])

Shape of X: (100, 2)
Shape of y: (100,)

Split Dataset
In [3]: from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y,


test_size=0.2, random_state=42)

print("Shape of X_train:", X_train.shape)


print("Shape of X_test:", X_test.shape)

Shape of X_train: (80, 2)


Shape of X_test: (20, 2)

Build Neural Network


In [5]: import tensorflow as tf
from [Link] import Sequential
from [Link] import Dense

# Define the model


model = Sequential([
Dense(10, activation='relu', input_shape=(2,)),
Dense(10, activation='relu'),
Dense(1, activation='sigmoid')
])

# Compile the model


[Link](optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])

# Print the model summary


[Link]()
WARNING:tensorflow:From C:\Users\ASHISH\anaconda3\Lib\site-packages\keras\src\losse
[Link]: The name [Link].sparse_softmax_cross_entropy is deprecated. Please use
[Link].sparse_softmax_cross_entropy instead.

WARNING:tensorflow:From C:\Users\ASHISH\anaconda3\Lib\site-packages\keras\src\backen
[Link]: The name tf.get_default_graph is deprecated. Please use [Link].v1.get_d
efault_graph instead.

WARNING:tensorflow:From C:\Users\ASHISH\anaconda3\Lib\site-packages\keras\src\optimi
zers\__init__.py:309: The name [Link] is deprecated. Please use [Link]
[Link] instead.

Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 10) 30

dense_1 (Dense) (None, 10) 110

dense_2 (Dense) (None, 1) 11

=================================================================
Total params: 151 (604.00 Byte)
Trainable params: 151 (604.00 Byte)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________

Train The model


In [7]: # Train the model
history = [Link](X_train, y_train, epochs=50, batch_size=32,
validation_data=(X_test, y_test))
Epoch 1/50
WARNING:tensorflow:From C:\Users\ASHISH\anaconda3\Lib\site-packages\keras\src\utils
\tf_utils.py:492: The name [Link] is deprecated. Please use tf.
[Link] instead.

WARNING:tensorflow:From C:\Users\ASHISH\anaconda3\Lib\site-packages\keras\src\engine
\base_layer_utils.py:384: The name tf.executing_eagerly_outside_functions is depreca
ted. Please use [Link].v1.executing_eagerly_outside_functions instead.

3/3 [==============================] - 2s 179ms/step - loss: 0.6949 - accuracy: 0.41


25 - val_loss: 0.6920 - val_accuracy: 0.5500
Epoch 2/50
3/3 [==============================] - 0s 30ms/step - loss: 0.6929 - accuracy: 0.450
0 - val_loss: 0.6909 - val_accuracy: 0.5000
Epoch 3/50
3/3 [==============================] - 0s 30ms/step - loss: 0.6911 - accuracy: 0.512
5 - val_loss: 0.6897 - val_accuracy: 0.5000
Epoch 4/50
3/3 [==============================] - 0s 30ms/step - loss: 0.6893 - accuracy: 0.525
0 - val_loss: 0.6885 - val_accuracy: 0.5000
Epoch 5/50
3/3 [==============================] - 0s 50ms/step - loss: 0.6877 - accuracy: 0.525
0 - val_loss: 0.6873 - val_accuracy: 0.5000
Epoch 6/50
3/3 [==============================] - 0s 29ms/step - loss: 0.6859 - accuracy: 0.525
0 - val_loss: 0.6862 - val_accuracy: 0.5000
Epoch 7/50
3/3 [==============================] - 0s 31ms/step - loss: 0.6843 - accuracy: 0.525
0 - val_loss: 0.6851 - val_accuracy: 0.5000
Epoch 8/50
3/3 [==============================] - 0s 28ms/step - loss: 0.6825 - accuracy: 0.525
0 - val_loss: 0.6840 - val_accuracy: 0.5000
Epoch 9/50
3/3 [==============================] - 0s 28ms/step - loss: 0.6809 - accuracy: 0.525
0 - val_loss: 0.6829 - val_accuracy: 0.5000
Epoch 10/50
3/3 [==============================] - 0s 28ms/step - loss: 0.6790 - accuracy: 0.525
0 - val_loss: 0.6819 - val_accuracy: 0.5000
Epoch 11/50
3/3 [==============================] - 0s 33ms/step - loss: 0.6773 - accuracy: 0.525
0 - val_loss: 0.6808 - val_accuracy: 0.5000
Epoch 12/50
3/3 [==============================] - 0s 33ms/step - loss: 0.6757 - accuracy: 0.525
0 - val_loss: 0.6798 - val_accuracy: 0.5000
Epoch 13/50
3/3 [==============================] - 0s 33ms/step - loss: 0.6740 - accuracy: 0.525
0 - val_loss: 0.6787 - val_accuracy: 0.5000
Epoch 14/50
3/3 [==============================] - 0s 25ms/step - loss: 0.6721 - accuracy: 0.525
0 - val_loss: 0.6775 - val_accuracy: 0.5000
Epoch 15/50
3/3 [==============================] - 0s 29ms/step - loss: 0.6703 - accuracy: 0.525
0 - val_loss: 0.6764 - val_accuracy: 0.5000
Epoch 16/50
3/3 [==============================] - 0s 32ms/step - loss: 0.6684 - accuracy: 0.525
0 - val_loss: 0.6752 - val_accuracy: 0.5000
Epoch 17/50
3/3 [==============================] - 0s 31ms/step - loss: 0.6669 - accuracy: 0.525
0 - val_loss: 0.6740 - val_accuracy: 0.5000
Epoch 18/50
3/3 [==============================] - 0s 27ms/step - loss: 0.6648 - accuracy: 0.525
0 - val_loss: 0.6727 - val_accuracy: 0.5000
Epoch 19/50
3/3 [==============================] - 0s 28ms/step - loss: 0.6631 - accuracy: 0.525
0 - val_loss: 0.6714 - val_accuracy: 0.5000
Epoch 20/50
3/3 [==============================] - 0s 28ms/step - loss: 0.6610 - accuracy: 0.525
0 - val_loss: 0.6700 - val_accuracy: 0.5000
Epoch 21/50
3/3 [==============================] - 0s 28ms/step - loss: 0.6591 - accuracy: 0.537
5 - val_loss: 0.6686 - val_accuracy: 0.5000
Epoch 22/50
3/3 [==============================] - 0s 32ms/step - loss: 0.6570 - accuracy: 0.537
5 - val_loss: 0.6673 - val_accuracy: 0.5000
Epoch 23/50
3/3 [==============================] - 0s 28ms/step - loss: 0.6550 - accuracy: 0.537
5 - val_loss: 0.6659 - val_accuracy: 0.5000
Epoch 24/50
3/3 [==============================] - 0s 42ms/step - loss: 0.6527 - accuracy: 0.537
5 - val_loss: 0.6644 - val_accuracy: 0.5000
Epoch 25/50
3/3 [==============================] - 0s 29ms/step - loss: 0.6503 - accuracy: 0.537
5 - val_loss: 0.6627 - val_accuracy: 0.5000
Epoch 26/50
3/3 [==============================] - 0s 32ms/step - loss: 0.6477 - accuracy: 0.550
0 - val_loss: 0.6611 - val_accuracy: 0.5000
Epoch 27/50
3/3 [==============================] - 0s 28ms/step - loss: 0.6451 - accuracy: 0.550
0 - val_loss: 0.6595 - val_accuracy: 0.5000
Epoch 28/50
3/3 [==============================] - 0s 29ms/step - loss: 0.6421 - accuracy: 0.550
0 - val_loss: 0.6580 - val_accuracy: 0.5500
Epoch 29/50
3/3 [==============================] - 0s 29ms/step - loss: 0.6391 - accuracy: 0.587
5 - val_loss: 0.6562 - val_accuracy: 0.5500
Epoch 30/50
3/3 [==============================] - 0s 29ms/step - loss: 0.6359 - accuracy: 0.587
5 - val_loss: 0.6545 - val_accuracy: 0.5500
Epoch 31/50
3/3 [==============================] - 0s 30ms/step - loss: 0.6328 - accuracy: 0.600
0 - val_loss: 0.6525 - val_accuracy: 0.6000
Epoch 32/50
3/3 [==============================] - 0s 27ms/step - loss: 0.6295 - accuracy: 0.650
0 - val_loss: 0.6505 - val_accuracy: 0.6000
Epoch 33/50
3/3 [==============================] - 0s 33ms/step - loss: 0.6259 - accuracy: 0.675
0 - val_loss: 0.6484 - val_accuracy: 0.6000
Epoch 34/50
3/3 [==============================] - 0s 29ms/step - loss: 0.6223 - accuracy: 0.687
5 - val_loss: 0.6461 - val_accuracy: 0.6000
Epoch 35/50
3/3 [==============================] - 0s 26ms/step - loss: 0.6188 - accuracy: 0.687
5 - val_loss: 0.6437 - val_accuracy: 0.6000
Epoch 36/50
3/3 [==============================] - 0s 28ms/step - loss: 0.6151 - accuracy: 0.687
5 - val_loss: 0.6414 - val_accuracy: 0.6000
Epoch 37/50
3/3 [==============================] - 0s 44ms/step - loss: 0.6116 - accuracy: 0.687
5 - val_loss: 0.6393 - val_accuracy: 0.6500
Epoch 38/50
3/3 [==============================] - 0s 31ms/step - loss: 0.6082 - accuracy: 0.700
0 - val_loss: 0.6372 - val_accuracy: 0.7000
Epoch 39/50
3/3 [==============================] - 0s 27ms/step - loss: 0.6047 - accuracy: 0.712
5 - val_loss: 0.6351 - val_accuracy: 0.7000
Epoch 40/50
3/3 [==============================] - 0s 30ms/step - loss: 0.6011 - accuracy: 0.712
5 - val_loss: 0.6327 - val_accuracy: 0.7000
Epoch 41/50
3/3 [==============================] - 0s 31ms/step - loss: 0.5974 - accuracy: 0.712
5 - val_loss: 0.6304 - val_accuracy: 0.7000
Epoch 42/50
3/3 [==============================] - 0s 31ms/step - loss: 0.5938 - accuracy: 0.700
0 - val_loss: 0.6281 - val_accuracy: 0.7000
Epoch 43/50
3/3 [==============================] - 0s 28ms/step - loss: 0.5900 - accuracy: 0.700
0 - val_loss: 0.6258 - val_accuracy: 0.7000
Epoch 44/50
3/3 [==============================] - 0s 34ms/step - loss: 0.5862 - accuracy: 0.712
5 - val_loss: 0.6235 - val_accuracy: 0.7000
Epoch 45/50
3/3 [==============================] - 0s 34ms/step - loss: 0.5824 - accuracy: 0.725
0 - val_loss: 0.6213 - val_accuracy: 0.7000
Epoch 46/50
3/3 [==============================] - 0s 32ms/step - loss: 0.5784 - accuracy: 0.737
5 - val_loss: 0.6191 - val_accuracy: 0.7000
Epoch 47/50
3/3 [==============================] - 0s 28ms/step - loss: 0.5745 - accuracy: 0.737
5 - val_loss: 0.6170 - val_accuracy: 0.7000
Epoch 48/50
3/3 [==============================] - 0s 28ms/step - loss: 0.5705 - accuracy: 0.750
0 - val_loss: 0.6149 - val_accuracy: 0.7000
Epoch 49/50
3/3 [==============================] - 0s 31ms/step - loss: 0.5666 - accuracy: 0.787
5 - val_loss: 0.6128 - val_accuracy: 0.7000
Epoch 50/50
3/3 [==============================] - 0s 30ms/step - loss: 0.5625 - accuracy: 0.787
5 - val_loss: 0.6106 - val_accuracy: 0.7000

Evaluate the model


In [9]: # Evaluate the model
loss, accuracy = [Link](X_test, y_test)
print(f"Test loss: {loss:.4f}")
print(f"Test accuracy: {accuracy:.4f}")
1/1 [==============================] - 0s 31ms/step - loss: 0.6106 - accuracy: 0.700
0
Test loss: 0.6106
Test accuracy: 0.7000

Visualize the history


In [11]: import [Link] as plt

# Plot training & validation accuracy values


[Link]([Link]['accuracy'])
[Link]([Link]['val_accuracy'])
[Link]('Model accuracy')
[Link]('Accuracy')
[Link]('Epoch')
[Link](['Train', 'Test'], loc='upper left')
[Link]()

# Plot training & validation loss values


[Link]([Link]['loss'])
[Link]([Link]['val_loss'])
[Link]('Model loss')
[Link]('Loss')
[Link]('Epoch')
[Link](['Train', 'Test'], loc='upper left')
[Link]()

Common questions

Powered by AI

Throughout the training process, as the number of epochs increases, both training and validation accuracy gradually improve, while training and validation loss decrease. This trend indicates that the model becomes better at classifying the data and reduces errors over time. Such a pattern is typical of learning, suggesting the model successfully abstracts features and parameters from the input data .

Model warnings indicate the use of outdated functions like 'tf.losses.sparse_softmax_cross_entropy' and suggest replacing them with updated alternatives like 'tf.compat.v1.losses.sparse_softmax_cross_entropy'. Addressing these warnings is important to maintain code compatibility with future updates of TensorFlow, ensuring reliability and leveraging new performance improvements or bug fixes .

The model starts with low accuracy (0.4125) and a high loss (0.6949), indicating a need for more training or model adjustments. These can be addressed by tweaking the network architecture, such as adding more neurons or layers, adjusting learning rates, or using techniques like dropout to prevent overfitting. Such strategies could improve accuracy and reduce loss over epochs .

Given the simple nature of the dataset (two features), enhancing performance could involve increasing network complexity, such as adding more hidden layers, neurons, or applying dropout for regularization. Additionally, experimenting with different activation functions, learning rates, or introducing feature engineering steps to better capture data patterns could further improve predictions. Incorporating early stopping during training might also prevent overfitting .

The loss function, binary_crossentropy, measures the error for two-class predictions. Minimizing this error helps improve model predictions. The Adam optimizer, a variant of the stochastic gradient descent method, adapts the learning rate through the training process, facilitating an efficient convergence to a local minimum of the loss function. This combination allows the model to learn effectively from the data .

The model achieves a test accuracy of 0.7000 and a test loss of 0.6106, indicating moderate performance. The accuracy and loss indicate that the model successfully learned to make predictions on unseen data but may still have room for improvement. Its performance suggests it can generalize to an extent, but higher complexity tasks might require an enhanced model with additional features or tuning .

The network's three layers consist of 10 neurons each for the hidden layers and 1 neuron for the output layer. The parameter count, 151, indicates the total number of weights and biases in the model. The architecture's simplicity—with only two hidden layers and a modest number of neurons, suggests a model with limited complexity and capacity, suitable for small datasets or tasks with fewer features .

Visualizing training history, through plots of accuracy and loss over epochs, provides insights into how the model's learning improves. It helps identify trends, such as convergence or overfitting, by comparing training and validation metrics. Consistent improvement in validation metrics demonstrates effective learning; divergence might suggest overfitting or underlying issues with model setup or data .

The neural network uses the 'relu' activation function for the two hidden layers, which introduces non-linearity allowing the model to handle complex patterns in the input data. The final layer uses the 'sigmoid' activation function to convert outputs to a probability between 0 and 1, suitable for binary classification .

Splitting the dataset into training and test sets allows for an unbiased evaluation of the model's performance. The training set is used to learn the parameters, while the test set ensures that the model maintains its accuracy on unseen data, preventing overfitting where the model performs well only on the training data .

You might also like