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

Time:10-23

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={}):
        self.history['loss'].append(logs.get('loss'))
        self.history['val_loss'].append(logs.get('val_loss'))
        self.history['accuracy'].append(logs.get('accuracy'))
        self.history['val_accuracy'].append(logs.get('val_accuracy'))

    def on_epoch_end(self, epoch, logs={}):
        self.history['loss_avg'].append(logs.get('loss'))
        self.history['val_loss_avg'].append(logs.get('val_loss'))
        self.history['accuracy_avg'].append(logs.get('accuracy'))
        self.history['val_accuracy_avg'].append(logs.get('val_accuracy'))

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.

CodePudding user response:

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.

  • Related