We will compare validation to the training loss by plotting a graph. We are going to use matplotlib for this:
import matplotlib.pyplot as plt
history_dict = history.history
loss_values = history_dict['loss']
val_loss_values = history_dict['val_loss']
epochs = range(1, len(loss_values)+ 1)
line1 = plt.plot(epochs, val_loss_values, label = 'Validation/Test Loss')
line2 = plt.plot(epochs, loss_values, label= 'Training Loss')
plt.setp(line1, linewidth=2.0, marker = '+', markersize=10.0)
plt.setp(line2, linewidth=2.0, marker= '4', markersize=10.0)
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.grid(True)
plt.legend()
plt.show()
Let's look at the output:
Fig 6.22: Validation versus training loss plot
The training loss started from 0.6 and ended at 0.16, and the validation loss started from 2.2 and ended at 0.06. Our model performed well, as the loss has decreased to a minimum.