Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can Tensorflow be used to train the model using Python?
TensorFlow provides the fit() method to train machine learning models. This method requires training data, validation data, and the number of epochs (complete passes through the dataset) to optimize the model's parameters.
Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?
Setting up the Environment
We are using Google Colaboratory to run the below code. Google Colab provides free access to GPUs and requires zero configuration, making it ideal for machine learning experiments.
Training the Model
The model.fit() method trains the neural network by iteratively adjusting weights based on training data ?
print("The model is being trained")
epochs = 12
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs
)
Code credit: https://www.tensorflow.org/tutorials/images/classification
Output
The model is being trained Epoch 1/12 92/92 [==============================] - 94s 1s/step - loss: 1.6007 - accuracy: 0.3411 - val_loss: 1.0708 - val_accuracy: 0.5627 Epoch 2/12 92/92 [==============================] - 92s 995ms/step - loss: 1.0138 - accuracy: 0.5843 - val_loss: 0.9451 - val_accuracy: 0.6458 Epoch 3/12 92/92 [==============================] - 91s 990ms/step - loss: 0.8382 - accuracy: 0.6767 - val_loss: 0.9054 - val_accuracy: 0.6471 Epoch 4/12 92/92 [==============================] - 90s 984ms/step - loss: 0.6362 - accuracy: 0.7580 - val_loss: 0.8872 - val_accuracy: 0.6540 Epoch 5/12 92/92 [==============================] - 91s 988ms/step - loss: 0.4125 - accuracy: 0.8572 - val_loss: 0.9114 - val_accuracy: 0.6676 Epoch 6/12 92/92 [==============================] - 91s 988ms/step - loss: 0.2460 - accuracy: 0.9207 - val_loss: 1.0891 - val_accuracy: 0.6757 Epoch 7/12 92/92 [==============================] - 91s 988ms/step - loss: 0.1721 - accuracy: 0.9532 - val_loss: 1.2619 - val_accuracy: 0.6635 Epoch 8/12 92/92 [==============================] - 90s 983ms/step - loss: 0.0658 - accuracy: 0.9823 - val_loss: 1.4119 - val_accuracy: 0.6703 Epoch 9/12 92/92 [==============================] - 90s 983ms/step - loss: 0.0556 - accuracy: 0.9865 - val_loss: 1.6113 - val_accuracy: 0.6090 Epoch 10/12 92/92 [==============================] - 91s 992ms/step - loss: 0.0805 - accuracy: 0.9729 - val_loss: 1.9744 - val_accuracy: 0.6390 Epoch 11/12 92/92 [==============================] - 90s 979ms/step - loss: 0.0545 - accuracy: 0.9838 - val_loss: 1.9303 - val_accuracy: 0.6662 Epoch 12/12 92/92 [==============================] - 96s 1s/step - loss: 0.0176 - accuracy: 0.9961 - val_loss: 1.8234 - val_accuracy: 0.6540
Understanding the Training Process
Key Parameters
- epochs ? Number of complete passes through the training dataset
- train_ds ? Training dataset containing input-output pairs
- validation_data ? Separate dataset used to evaluate model performance
Training Metrics
- loss ? How far the model's predictions are from actual values (lower is better)
- accuracy ? Percentage of correct predictions on training data
- val_loss ? Loss calculated on validation dataset
- val_accuracy ? Accuracy on validation dataset
Key Observations
From the training output, we can observe:
- Training accuracy improves from 34% to 99% over 12 epochs
- Validation accuracy plateaus around 65-67%, indicating potential overfitting
- The gap between training and validation accuracy suggests the model memorizes training data
Conclusion
The model.fit() method effectively trains neural networks by iteratively updating weights through multiple epochs. Monitor both training and validation metrics to detect overfitting and ensure good generalization performance.
