
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How can Tensorflow be used to train the model using Python?
The model can be trained using the ‘train’ method in Tensorflow, where the epochs (number of times the data has to be trained to fit the model) and the training data are specified.
Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?
We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.
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 [==============================] - 94s 1s/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
Explanation
- The model is trained to fit the data.
- This is done using the 'fit' method.
- Related Articles
- How can TensorFlow used to train a linear model using Python?
- How can Tensorflow be used to train the model with the stackoverflow question dataset using Python?
- How can Keras be used to train the model using Python?
- How can Tensorflow be used to train and compile the augmented model?
- How can Tensorflow be used to compile the model using Python?
- How can Tensorflow be used to train the Illiad dataset using Python?
- How can TensorFlow be used to train the model for Fashion MNIST dataset in Python?
- How can Keras be used to train the model using Python Program?
- How can Tensorflow be used to train and compile a CNN model?
- After normalization, how can Tensorflow be used to train and build the model?
- How can Tensorflow be used to export the model built using Python?
- How can Tensorflow be used to export the built model using Python?
- How can Tensorflow be used to compile the exported model using Python?
- How can Tensorflow be used with Estimators to train the model for titanic dataset?
- How can Tensorflow and pre-trained model be used to compile the model using Python?

Advertisements