Keras
Machine Learning
Model Training
Deep Learning
Epochs

How to disable printing reports after each epoch in Keras?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

In the context of deep learning using Keras, the practice of printing progress reports after each training epoch can often be helpful for monitoring model performance. However, for large-scale training, these verbose reports can clutter the output, making it challenging to track critical information. This article provides a detailed guide on how to disable the printing of these reports and customize verbosity to suit your needs.

Overview

In Keras, the verbosity of training can be controlled directly by modifying the fit method's verbose parameter. The fit method is used during model training to specify whether and how training progress is displayed.

Controlling Epoch Reporting with Verbose

The verbose parameter in Keras controls the level of logging and has three primary settings:

  • 0: Silent mode.
  • 1: Progress bar (default).
  • 2: One line per epoch.

Using these settings, you can adjust how much information you receive during model training.

For instance, to prevent Keras from printing any output during each epoch, you can set verbose=0.

python
1# Example of disabling print statements in Keras
2
3import tensorflow as tf
4from tensorflow.keras.models import Sequential
5from tensorflow.keras.layers import Dense
6from tensorflow.keras.optimizers import Adam
7
8# Create a simple model
9model = Sequential([
10    Dense(64, activation='relu', input_shape=(10,)),
11    Dense(1, activation='sigmoid')
12])
13
14# Compile the model
15model.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['accuracy'])
16
17# Create dummy data
18import numpy as np
19X = np.random.random((1000, 10))
20y = np.random.randint(2, size=(1000, 1))
21
22# Train the model with verbose=0 to suppress epoch reports
23history = model.fit(X, y, epochs=10, batch_size=32, verbose=0)

Why Disable Verbose Output?

Several reasons might motivate you to suppress epoch-level reporting:

  1. Performance Optimization: Printing to the console can slow down training, especially when the training process is significant or when logging is done over network interfaces (e.g., in a distributed setting).
  2. Clarity: In scenarios where you employ custom callbacks for logging or analysis, the default printing can clutter the interface and obscure important custom logs.
  3. Automation: When automating experiments or logging to files, excessive print statements can make the logs unwieldy.

Advanced Customization Using Callbacks

Apart from the verbosity setting, Keras offers advanced control over what gets logged via callbacks. You can define custom callback functions that print essential details or suppress logging further.

Here’s an example of defining a custom callback to limit output:

python
1# Define a custom callback class
2class CustomVerboseCallback(tf.keras.callbacks.Callback):
3    def on_epoch_end(self, epoch, logs=None):
4        if logs is not None:
5            # Print a summary or handle logs as needed without standard verbose output
6            print(f"Epoch {epoch+1}: Loss = {logs['loss']:.4f}, Accuracy = {logs['accuracy']:.4f}")
7
8# Use the custom callback during training
9custom_callback = CustomVerboseCallback()
10model.fit(X, y, epochs=10, batch_size=32, callbacks=[custom_callback], verbose=0)

Callback Summary with Control Settings

SettingDescriptionUse Case
verbose=0Silent modeAutomated or streamlined logging Performance optimization
verbose=1Progress bar displayed after each batchVisual prompt for interactive sessions
verbose=2One line per epochSummary style for compact datasets
Custom CallbackFlexible, custom loggingTailored monitoring with selective data

Conclusion

Adjusting the verbosity level and employing custom callbacks allows precise control over training output in Keras. These features are crucial for optimizing performance and clarity in experimentation, particularly when dealing with extensive data and prolonged training sessions.

By applying these techniques, you ensure that the training outcomes and progress are communicated in a format that best suits your project's needs, whether through the console, logs, or monitoring dashboards.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.