How can Tensorflow be used to train the model using Python?

PythonServer Side ProgrammingProgrammingTensorflow

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.
raja
Published on 11-Feb-2021 09:33:29
Advertisements