How do I print inside the loss function during training 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.
Introduction
Printing from inside a Keras loss function is usually a debugging task, not something you want to keep in normal training code. The main rule is that regular Python print is often the wrong tool inside TensorFlow execution, so the reliable options are tf.print, eager debugging, or moving the logging into a callback or custom training step.
Why Plain print Often Disappoints
Keras loss functions run inside TensorFlow execution, which may be graph-based or compiled. In that context, a normal Python print can run at trace time, run fewer times than you expect, or not reflect per-batch values in the way you intended.
That is why TensorFlow provides tf.print, which is itself a TensorFlow op and works during execution.
Use tf.print Inside the Loss Function
A simple custom loss can print tensors during training like this:
This is the standard answer when you truly want to observe tensors inside the loss calculation.
Use run_eagerly=True for Heavier Debugging
If you need to step through Python logic more directly, compile the model with eager execution enabled.
With eager execution, debugging becomes easier, but training is usually slower. This is a debugging mode, not the best production training configuration.
Consider a Callback Instead
Many times, printing inside the loss function is a sign that the real goal is to inspect per-batch or per-epoch values. A callback is often cleaner.
This avoids coupling debug output to the math of the loss function itself.
Custom Training Step for Advanced Inspection
If you want to inspect gradients, intermediate activations, or multiple loss components, a custom train_step is often the better design.
That approach is more flexible than forcing all debugging into the loss function.
Common Pitfalls
The most common mistake is using Python print and expecting it to behave like per-batch TensorFlow execution output.
Another issue is printing too much data and slowing training dramatically, especially with large tensors or many batches.
A third problem is debugging inside the loss function when the cleaner solution is actually a callback or a custom train_step.
Summary
- Use
tf.printinstead of Pythonprintinside TensorFlow loss code. - Turn on
run_eagerly=Truewhen you need easier debugging and can tolerate slower execution. - Prefer callbacks when you only need batch or epoch loss values.
- Use a custom
train_stepfor deeper inspection of training behavior. - Treat printing inside the loss function as temporary debugging, not as normal training design. If you need the same visibility for every experiment, it usually belongs in callbacks, metrics, or a custom training loop instead. That makes the debug signal easier to turn on and off without touching the loss math itself.
Related reading
- How do I profile a tf.data.Dataset?
- How do I resolve these tensorflow warnings?
- How do I save and load BatchNormalization Layer in this Tensorflow model?
- How do I select certain columns of a 2D tensor in TensorFlow?
- How do I print the model summary in PyTorch?
- How do I resolve one hot encoding if my test data has missing values in a col?
- How do I print my Java object without getting SomeType@2f92e0f4?
- How do I properly assert that an exception gets raised in pytest?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.