TensorFlow
machine learning
debugging
training issues
duplicate output

Tensorflow prints the same info twice while training

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

TensorFlow is a popular open-source library used for numerical computation and machine learning. Developed by the Google Brain team, it has been widely adopted for building deep learning models. However, many users encounter a peculiar issue where TensorFlow prints the same training information twice during the model training process. This can be confusing and may appear to clutter the logs. Let's take a detailed look at why this happens, the technical explanations behind it, and how to potentially mitigate this issue.

Understanding the Issue

When running a TensorFlow model training script, you may see that TensorFlow prints the following kinds of repetitive messages:

  • TensorFlow provides `Callback` classes that allow users to perform actions at various stages during training (e.g., `on_epoch_begin`, `on_epoch_end`).
  • When a `Callback` is incorrectly implemented or added multiple times to the model's `fit` method, it may result in duplicate logs. Users should review their code to ensure callbacks are not duplicated implicitly or explicitly.
  • Many users operate TensorFlow within Jupyter Notebooks. Each time a kernel is restarted, it may rerun the initialization cells, potentially duplicating configurations or callbacks.
  • Check that cells are not executed redundantly, causing recurrent callbacks or logger setups.
  • The `verbose` parameter of the `model.fit()` method in TensorFlow controls logging. Setting it to higher verbosity levels can lead to more detailed logging, which may appear duplicative.
  • Adjusting the verbosity level to `1` (default) may prevent excessive logs. Here's a brief on verbosity:
    • `0` = silent
    • `1` = progress bar
    • `2` = one log line per epoch
  • If the dataset is being processed in parallel via `tf.data.Dataset` or similar constructs, there might be parallel threads logging the same epoch data.
  • Cross-verify this by examining your dataset's processing pipeline, paying special attention to parallel operations like `map` and `batch`.
  • Custom scripts or wrappers around the TensorFlow models may inadvertently print more logs than required. It's crucial to ensure that the custom printing logic is well-contained and doesn't replicate the internal reporting of TensorFlow.
  • Ensure callbacks are correctly implemented and not redundantly attached.
  • Confirm logger configurations in scripts, making sure no custom logger redundancies exist.
  • Test with a clean environment (especially in notebooks) to rule out execution artifacts.
  • Set the verbosity level as needed but minimal to avoid repeat logs.

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.