Output multiple losses added by add_loss 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
add_loss in Keras is useful when part of your training objective comes from intermediate tensors, regularization terms, or custom constraints that are not tied directly to the main target passed to fit. The confusing part is that Keras aggregates those losses into the total training loss by default. If you want to see each component separately, you need to expose them as metrics or return them from a custom training step.
What add_loss Actually Does
When a layer or model calls self.add_loss, Keras stores that tensor as an extra contribution to the optimization objective.
When this layer participates in a model, its penalty becomes part of the total loss used during training. Keras does not automatically create a named output column for each extra term.
Use add_metric to Report Each Loss Component
If you want separate visibility in training logs, add a metric with a stable name alongside the loss.
Now the model can log both the total loss and the named penalty metric during fit. This is usually the simplest answer when the question is really about output, not optimization.
Example with Multiple Added Losses
You can add more than one extra loss term from different layers.
During training, Keras sums the base mse loss and both added penalties into one total loss value. The individual penalty values can still appear in logs if you registered them as metrics.
Inspect Added Losses Programmatically
At call time, a model exposes the currently collected extra losses in model.losses.
This is useful for debugging, but it is not a full training report by itself. The entries are tensors, not automatically formatted history columns with stable names.
It is also important to remember that model.losses is populated during a forward call. If you inspect it before the model has processed input, it may look empty even though your layers do call add_loss.
Use a Custom train_step for Full Control
If you need to log, weight, or print each loss component exactly the way you want, override train_step.
This is the right approach when metrics need custom grouping or when some losses should be reported separately but combined differently for optimization.
That matters in multi-objective training, where you may want one number for optimization, one number for regularization pressure, and another number for a task-specific penalty that you are still monitoring separately.
Common Pitfalls
- Expecting every
add_lossterm to appear automatically as a separate named value in training history. - Forgetting that Keras sums all extra losses into the total objective.
- Using
model.lossesfor debugging and assuming it is a permanent log format. - Adding custom losses without also exposing metrics when observability matters.
- Overriding
train_stepwithout keeping the total loss aligned with the optimization objective.
Summary
- '
add_losscontributes extra terms to the total training loss.' - Keras does not automatically label each extra loss as a separate history field.
- Use
add_metricwhen you want named logging for individual loss components. - Inspect
model.losseswhen debugging what is currently attached. - Override
train_stepwhen you need full control over reporting and aggregation.
Related reading
- Overfitting in Tensorflow Object detection API
- Oversampling functionality in Tensorflow dataset API
- Padding time-series subsequences for LSTM-RNN training
- padding'same' conversion to PyTorch padding
- overcome Graphdef cannot be larger than 2GB in tensorflow
- Parallel fitting of multiple Keras Models on single GPU
- Parallel processes in distributed tensorflow
- Parallelization strategies for deep learning
.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.