'Extract loss value from keras model periodically, not after each epoch (too few) and not after each batch (too many)

I wish to track the loss throughout my training, however the solutions I've seen floating around are either about recording it at the end of every batch or at the end of every epoch. I will only have 3-5 epochs so that is too few to properly plot or analyze, on the other hand there are millions of batches, which is way too many to handle.

My current simple method (after each epoch) works:

    csv_logger = CSVLogger(out_dir + 'log.csv', append = True, separator = '|')
    
    for epoch in range(epochs):
        
        np_in_data = remakedata(#some input)
        model.fit([np_in_data[:, 0], np_in_data[:, 1]], np_in_data[:, 2], 
                  batch_size = 256, callbacks = [csv_logger])

But I'm not sure where to start with a custom callback function like the one I described above.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source