How can TensorFlow be used to create a plot that visualizes accuracy and loss with respect to time in IMDB dataset in Python?

PythonServer Side ProgrammingProgramming

Tensorflow is a machine learning framework that is provided by Google. It is an open-source framework used in conjunction with Python to implement algorithms, deep learning applications and much more. It is used in research and for production purposes.

The ‘IMDB’ dataset contains reviews of over 50 thousand movies. This dataset is generally used with operations associated with Natural Language Processing.

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.

Following is the code snippet to create a plot that visualizes accuracy and loss with respect to time in IMDB dataset −


history_dict = history.history
acc = history_dict['binary_accuracy']
val_acc = history_dict['val_binary_accuracy']
loss = history_dict['loss']
val_loss = history_dict['val_loss']

epochs = range(1, len(acc) + 1)

plt.plot(epochs, loss, 'ro', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss with respect to time')

Code credit



  • Once the data has been fit to the model, the actual values and the predicted values need to be compared.

  • The best way to do this is through visualization.

  • Hence, the ‘matplotlib’ library is used to plot the loss that occurred during training and validation with respect to time.

  • This is based on the number of steps (or epochs) taken to train the data to fit the model.

Published on 20-Jan-2021 12:20:09