What does trainingTrue mean when calling a TensorFlow Keras model?
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
When you call a Keras model as model(x, training=True), you are telling layers that behave differently during training and inference to use their training-time behavior. That mainly affects layers such as Dropout and BatchNormalization, and it matters a lot in custom training loops where Keras is not automatically managing the mode for you.
Why the training Flag Exists
Most layers do the same thing in both phases, but a few important ones do not.
Two common examples are:
- '
Dropout, which randomly drops activations only during training' - '
BatchNormalization, which uses batch statistics during training and moving averages during inference'
That is why the same model can produce different outputs depending on whether training=True or training=False is used.
See the Difference with Dropout
Here is a minimal example showing how training=True changes behavior.
With training=True, different elements are dropped on different calls. With training=False, dropout is disabled and the output stays deterministic.
That is exactly what you want during training versus inference.
BatchNormalization Is Also Sensitive to the Flag
Batch normalization behaves differently in a less obvious way. During training it updates moving statistics and uses the current batch. During inference it uses the stored moving averages.
If you pass the wrong training flag in custom code, batch-norm layers can behave incorrectly even though the model still runs.
Keras Handles This for fit, evaluate, and predict
If you use the standard high-level APIs, Keras usually passes the correct mode automatically:
- '
model.fit(...)uses training mode' - '
model.evaluate(...)uses inference mode' - '
model.predict(...)uses inference mode'
That means you often do not need to set training= manually in ordinary Keras workflows.
The flag becomes more important when you write code like this:
That form is common in custom training loops, subclassed models, and advanced layer implementations.
Use It Explicitly in Custom Training Loops
In a custom training step, you normally want the forward pass to happen in training mode.
During validation in the same program, you would switch to training=False.
That separation keeps training-specific behavior from leaking into evaluation.
Propagate the Flag in Custom Layers and Models
If you write a custom layer or subclassed model, accept the training argument and pass it down to child layers that need it.
If you ignore the flag in custom code, your model may silently use the wrong behavior during training or inference.
Common Pitfalls
The biggest mistake is assuming training=True means "compute gradients." It does not. It only tells certain layers which behavior to use. Another common issue is forgetting to pass the flag through custom layers, which breaks dropout or batch normalization in subtle ways. Developers also sometimes force training=True during validation or prediction, which makes reported metrics noisier and less trustworthy. Finally, if you use model.fit, model.evaluate, and model.predict, you usually do not need to set the flag manually at all.
Summary
- '
training=Truetells layers with phase-dependent behavior to act as if the model is in training mode.' - It mainly affects layers such as
DropoutandBatchNormalization. - Keras manages the flag for
fit,evaluate, andpredictautomatically. - In custom training loops, call the model with
training=Trueduring training andtraining=Falseduring validation or inference. - Custom layers should accept and propagate the
trainingargument when they wrap layers that depend on it.
Related reading
- What does unsqueeze do in Pytorch?
- What does view do in PyTorch?
- what does x tf.placeholdertf.float32, None, 784 means?
- what exactly does 'tf.contrib.rnn.DropoutWrapper'' in tensorflow do? three citical questions
- What does use_lockingTrue do in TensorFlow optimizers?
- What exactly is a device in TensorFlow?
- What exactly does the forward function output in Pytorch?
- What exactly is Keras's CategoricalCrossEntropy doing?
.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.