How to record val_loss and loss per batch in keras
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When training machine learning models using Keras, monitoring the loss metrics during training is crucial for understanding how well a model is learning. Specifically, observing the loss (`loss`) and validation loss (`val_loss`) during each training batch can offer deeper insights than only looking at the metrics at the end of each epoch. This article will discuss how to record these metrics per batch using Keras.
Why Monitor `loss` and `val_loss` Per Batch
Understanding the `loss` and `val_loss` per batch provides:
- Real-Time Insights: Compare the model's real-time performance on both training and validation data.
- Early Stopping Criteria: By observing loss trends per batch, one can implement more dynamic early stopping.
- Fine-Tuning Learning Parameters: Insights into these metrics can prompt adjustments in learning rate and other hyperparameters.
Approach to Recording `loss` and `val_loss` Per Batch
Keras does not record validation loss for each batch by default. However, with a custom callback, one can achieve this. Let’s dive into the technical implementation.
Custom Callback for Batch-wise `Loss` Recording
Keras provides a powerful way to extend training functionalities through callbacks. Here, we'll create a custom callback to record `loss` and `val_loss` for each batch.
Implementing the Callback
- on_train_begin: Initializes lists to store losses per batch.
- on_batch_end: Captures the loss after each batch and stores it.
- on_epoch_end: Evaluates the validation loss for the epoch, distributing it across all batches.
- Trends in learning such as convergence or divergence.
- Overfitting, apparent when the training loss decreases while validation loss increases.
- Performance Overhead: Frequent evaluation on validation data can increase computational cost.
- Batch Size: Optimal batch sizes vary per dataset and can influence the granularity of insights.
- Early Stopping: Implementing early stopping based on batch-wise validation losses can be more responsive.

