Keras validation loss and accuracy metrics per batch produces a list of 'None'


I am currently trying to get the loss and accuracy of each batch for both the training and validation of my Keras Model. I have managed to successfully do so for the loss and accuracy training but am running into problems when trying to obtain the equivalent for the validation loss and accuracy.

I was basing my work of this query and have adapted the code slightly for my application. The issue is that I just receive a list of ‘None’ values.

I created my own LossHistory class shown below. I want to be able to get the metrics for each batch and then the each epoch.

class LossHistory(keras.callbacks.Callback):
    def on_train_begin(self, logs={}):
        self.history = {'loss':[],'val_loss':[], 'accuracy':[],'val_accuracy':[], 'loss_avg':[],'val_loss_avg':[], 'accuracy_avg':[],'val_accuracy_avg':[]}

    def on_batch_end(self, batch, logs={}):

    def on_epoch_end(self, epoch, logs={}):

I can still get the average values of the validation loss and accuracy. In other words, the validation metrics after each epoch. I am just not able to get those metrics for each batch.

Would anyone know why this is the case? I tried looking at the Keras documentation about customer callbacks but couldn’t make find anything much regarding the validation metrics.

Not sure if I’m missing anything obvious.


Validation loss and metrics are evaluated at the end of each epoch, there is no per-batch validation loss and metrics as computed by Keras.

Answered By – Dr. Snoopy

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published