How to print one log line per every 10 epochs when training models with tensorflow keras?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Keras prints a progress line for every epoch by default, which is often too noisy for long training runs. If you want one log line every 10 epochs, the clean solution is to suppress the built-in epoch logging and add a small callback that prints only at your chosen interval. That gives you predictable output without losing access to metrics such as loss, accuracy, or validation loss.
Disable the Default Per-Epoch Console Noise
If you keep the default Keras verbosity and also print from a custom callback, you will get duplicate output. Set verbose=0 or verbose=2 depending on how much built-in progress you still want.
For fully custom interval logging, verbose=0 is the simplest choice:
Now Keras trains silently unless a callback prints something.
Write a Callback That Logs Every 10 Epochs
Keras callbacks can inspect the logs dictionary at the end of each epoch. The only subtlety is that the epoch argument is zero-based, so epoch 10 is reported as epoch == 9.
Use it like this:
That produces one line at epochs 10, 20, 30, and so on.
Handle the Final Epoch Deliberately
Sometimes the total number of epochs is not a multiple of 10. In that case, you may still want one final summary line for the last epoch. Add a stored total and print on the last epoch as well.
That keeps the output sparse while ensuring the final state is still visible.
Log Only the Metrics You Actually Care About
The logs dictionary often contains more than you need. A concise training log is more useful when it highlights the metrics that drive training decisions.
Common choices:
lossval_lossaccuracyval_accuracy
For example:
This is often easier to scan than dumping the whole dictionary each time.
Prefer a Callback Over Manual Training Loops Unless Needed
You could also train one epoch at a time inside a Python loop and print every tenth iteration, but that is usually more awkward than necessary.
This works, but you lose some of the convenience of normal fit behavior. A callback keeps the training flow idiomatic and integrates better with other callbacks such as early stopping or model checkpointing.
Common Pitfalls
- Forgetting that the callback epoch index is zero-based and logging at the wrong intervals.
- Leaving
verbose=1enabled and then getting both default Keras logs and custom interval logs. - Assuming every metric exists in
logseven when validation data was not supplied. - Printing the entire
logsdictionary and recreating the same console noise you were trying to remove. - Rewriting the training loop manually when a small callback would handle the requirement cleanly.
Summary
- Suppress the default per-epoch output if you want clean interval-based logging.
- A custom
Callbackis the simplest way to print one line every 10 epochs. - Remember that
epochis zero-based insideon_epoch_end. - Print only the metrics that matter for monitoring the run.
- Optionally log the final epoch as well when the total epoch count is not divisible by the interval.

