Search code examples
pythontensorflowkerasdeep-learningneural-network

Moving averaging of Loss during Training in Keras


I am using Keras with TensorFlow to implement a deep neural network. When I plot the loss and number of iterations, there is a significant jump in loss after each epoch. In reality, the loss of each mini-batch should vary from each other, but Keras calculates the moving average of the loss over the mini-batches, that's why we obtain a smooth curve instead of an arbitrary one. The array of the moving average is reset after each epoch because of which we can observe a jump in the loss.

I would like to remove the functionality of moving average instead I would like to have raw loss values which will vary for each mini-batch. For now, I tried reduction in the loss function but it works only on the examples within the mini-batch. The following code sum losses of all the training examples within the mini-batch.

tf.keras.losses.BinaryCrossentropy(reduction = 'sum')

I also tried writing a custom loss function but that doesn't help either.


Solution

  • Keras in fact shows the moving average instead of the "raw" loss values. In order to acquire the raw loss values, one should implement a callback as shown below:

    class LossHistory(keras.callbacks.Callback):
        def on_train_begin(self, logs={}):
            #initialize a list at the begining of training
            self.losses = []
    
        def on_batch_end(self, batch, logs={}):
            self.losses.append(logs.get('loss'))
    
    mycallback = LossHistory()
    
    

    Then call it in model.fit

    model.fit(X, Y, epochs=epochs, batch_size=batch, shuffle=True, verbose = 0, callbacks=[mycallback])
    print(mycallback.losses)
    

    I tested with the following configuration

    Keras 2.3.1
    Tensorflow 2.1.0
    Python 3.7.9