from tensorflow.keras.callbacks import Callback
from IPython.display import clear_output
class TrainingPlot(Callback):
def on_train_begin(self, logs={}):
self.losses = []
self.val_losses = []
self.accuracies = []
self.val_accuracies = []
def on_epoch_end(self, epoch, logs={}):
self.losses.append(logs.get('loss'))
self.val_losses.append(logs.get('val_loss'))
self.accuracies.append(logs.get('accuracy'))
self.val_accuracies.append(logs.get('val_accuracy'))
clear_output(wait=True)
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)
plt.plot(self.losses, label='Training Loss')
plt.plot(self.val_losses, label='Validation Loss')
plt.legend()
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.title("Losses vs Epochs")
plt.subplot(1, 2, 2)
plt.plot(self.accuracies, label='Training Accuracy')
plt.plot(self.val_accuracies, label='Validation Accuracy')
plt.legend()
plt.xlabel("Epochs")
plt.ylabel("Accuracy")
plt.title("Accuracy vs Epochs")
plt.show()
plot_losses = TrainingPlot()
history = model.fit(X_train,y_train,epochs=100,batch_size =32,callbacks=[plot_losses])