Keras
Machine Learning
Training Progress
Progress Bar
Deep Learning

Show progress bar for each epoch during batchwise training 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 deep learning models using Keras, it's essential to monitor the progress of training to understand how the model is performing and adjust parameters as needed. While Keras provides a summary of training after each epoch by default, visualizing the progress for each epoch with more granularity can offer valuable insights. This is particularly useful when training on large datasets, where each epoch consists of numerous batches.

In this article, we will explore how to show a progress bar for each epoch during batchwise training in Keras. We'll also discuss the technical details, provide examples, and explore additional features that can enhance this process.

Understanding Batchwise Training in Keras

In Keras, training is often conducted in batches—a subset of the training dataset that the model processes in one iteration. This approach allows for more efficient use of computational resources:

  • Epoch: An epoch is one complete pass over the entire dataset.
  • Batch: A batch is a subset of the dataset. The dataset is divided into multiple batches for each epoch.

During training, the model parameters are updated after each batch, not just at the end of the epoch. This batchwise updating can lead to faster convergence to an optimal solution.

Implementing Progress Bars in Keras

To visualize the progress of training within each epoch, we can leverage Keras callbacks or external libraries like tqdm. Below, we'll discuss both approaches.

Using Keras callbacks

Keras provides a flexible callback system that can be extended to add various custom functionalities to the training process. Here's how you can implement a progress bar using a custom callback:

python
1import keras
2import numpy as np
3from keras.models import Sequential
4from keras.layers import Dense
5from keras.datasets import mnist
6from keras.utils import to_categorical
7
8class ProgressBarCallback(keras.callbacks.Callback):
9    def on_epoch_begin(self, epoch, logs=None):
10        self.batch_counter = 0
11        self.verbose = 1 if self.params.get('verbose', 0) == 0 else self.params['verbose']
12        self.seen = 0
13        self.target = self.params['steps'] * self.params['batch_size']
14
15    def on_batch_end(self, batch, logs=None):
16        self.batch_counter += 1
17        self.seen += logs.get('size', 0)
18        if self.verbose:
19            progress = f"{self.batch_counter}/{self.params['steps']}"
20            print(f"\rEpoch {self.epoch+1}/{self.params['epochs']} [{progress}] - {self.seen}/{self.target} samples", end='', flush=True)
21
22(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
23train_images = train_images.reshape((60000, 28 * 28)).astype('float32') / 255
24test_images = test_images.reshape((10000, 28 * 28)).astype('float32') / 255
25train_labels = to_categorical(train_labels)
26test_labels = to_categorical(test_labels)
27
28model = Sequential([
29    Dense(512, activation='relu', input_shape=(28 * 28,)),
30    Dense(10, activation='softmax'),
31])
32
33model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
34
35model.fit(train_images, train_labels, epochs=5, batch_size=128, callbacks=[ProgressBarCallback()], verbose=0)

Using TQDM

The tqdm library is a Python library for showing progress bars. It can be easily integrated with Keras training loops:

python
1from tqdm import tqdm
2import keras
3from keras.models import Sequential
4from keras.layers import Dense
5
6class TQDMCallback(keras.callbacks.Callback):
7    def on_epoch_begin(self, epoch, logs=None):
8        self.progbar = tqdm(total=int(self.params['steps']), desc=f"Epoch {epoch+1}/{self.params['epochs']}", unit='batch')
9
10    def on_batch_end(self, batch, logs=None):
11        self.progbar.update(1)
12        
13    def on_epoch_end(self, epoch, logs=None):
14        self.progbar.close()
15
16model = Sequential([
17    Dense(512, activation='relu', input_shape=(784,)),
18    Dense(10, activation='softmax'),
19])
20
21model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
22
23model.fit(train_images, train_labels, epochs=5, batch_size=128, callbacks=[TQDMCallback()], verbose=0)

Key Points Summary

Here's a summarized table of the key points discussed:

TopicDescription
EpochOne complete pass over the entire dataset
BatchA subset of the dataset used for a single gradient update
Progress with CallbacksUtilize Keras custom callbacks for custom logic
tqdm LibraryExternal library for easy progress bar integration
Callback SetupUse on_epoch_begin, on_batch_end for progress updates

Conclusion

Tracking the progress of each epoch during batchwise training offers valuable insights into model performance. By implementing progress bars using either Keras callbacks or the tqdm library, you gain real-time visibility into the training process, which can be crucial for debugging and optimizing training parameters. Whether you choose to utilize Keras' built-in capabilities or external libraries, the key is to choose a method that integrates well with your workflow and enhances your understanding of the training dynamics.


Course illustration
Course illustration

All Rights Reserved.