How can Tensorflow and pre-trained model be used to see the changes during training after every epoch?

TensorflowServer Side ProgrammingProgramming

Tensorflow and the pre-trained model can be used to see the changes during training after every epoch with the help of the ‘evaluate’ method and the ‘fit’ methods.

Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?

A neural network that contains at least one layer is known as a convolutional layer. We can use the Convolutional Neural Network to build learning model. 

We will understand how to classify images of cats and dogs with the help of transfer learning from a pre-trained network. The intuition behind transfer learning for image classification is, if a model is trained on a large and general dataset, this model can be used to effectively serve as a generic model for the visual world. It would have learned the feature maps, which means the user won’t have to start from scratch by training a large model on a large dataset.

Read More: How can a customized model be pre-trained?

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.

Example

print("The number of epochs have been defined")
initial_epochs = 10
print("The model is being evaluated")
loss0, accuracy0 = model.evaluate(validation_dataset)
print("Initial loss is: {:.2f}".format(loss0))
print("Initial accuracy is: {:.2f}".format(accuracy0))
history = model.fit(train_dataset,
epochs=initial_epochs,
validation_data=validation_dataset)

Code credit −https://www.tensorflow.org/tutorials/images/transfer_learning

Output

The number of epochs have been defined
The model is being evaluated
26/26 [==============================] - 15s 485ms/step - loss: 0.8892 - accuracy: 0.4216
Initial loss is: 0.92
Initial accuracy is: 0.40
Epoch 1/10
63/63 [==============================] - 53s 793ms/step - loss: 0.7830 - accuracy: 0.5455 - val_loss: 0.6227 - val_accuracy: 0.6213
Epoch 2/10
63/63 [==============================] - 50s 792ms/step - loss: 0.5893 - accuracy: 0.6770 - val_loss: 0.4499 - val_accuracy: 0.7525
Epoch 3/10
63/63 [==============================] - 51s 799ms/step - loss: 0.4645 - accuracy: 0.7565 - val_loss: 0.3484 - val_accuracy: 0.8317
Epoch 4/10
63/63 [==============================] - 51s 803ms/step - loss: 0.4004 - accuracy: 0.8095 - val_loss: 0.2806 - val_accuracy: 0.8725
Epoch 5/10
63/63 [==============================] - 51s 799ms/step - loss: 0.3424 - accuracy: 0.8325 - val_loss: 0.2412 - val_accuracy: 0.8936
Epoch 6/10
63/63 [==============================] - 50s 790ms/step - loss: 0.3094 - accuracy: 0.8655 - val_loss: 0.2075 - val_accuracy: 0.9146
Epoch 7/10
63/63 [==============================] - 50s 785ms/step - loss: 0.2819 - accuracy: 0.8745 - val_loss: 0.1789 - val_accuracy: 0.9257
Epoch 8/10
63/63 [==============================] - 51s 812ms/step - loss: 0.2508 - accuracy: 0.8870 - val_loss: 0.1638 - val_accuracy: 0.9282
Epoch 9/10
63/63 [==============================] - 50s 797ms/step - loss: 0.2413 - accuracy: 0.8965 - val_loss: 0.1560 - val_accuracy: 0.9332
Epoch 10/10
63/63 [==============================] - 52s 818ms/step - loss: 0.2324 - accuracy: 0.8995 - val_loss: 0.1336 - val_accuracy: 0.9493

Explanation

  • After training for 10 epochs, the accuracy on the validation set is computed.

  • This value is displayed on the console.

raja
Published on 13-Feb-2021 07:25:53
Advertisements