Passing trainingtrue when using Tensorflow 2's Keras Functional API
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In TensorFlow 2.x, the training flag tells certain layers whether they should behave in training mode or inference mode. In a normal Keras Functional API model, you usually do not hardcode training=True while building the graph. Instead, you let Keras choose the correct mode through fit, evaluate, and predict.
Which Layers Care About training
Most layers ignore the training flag. A few important ones do not.
- '
Dropoutdrops activations during training and passes everything through during inference.' - '
BatchNormalizationupdates moving statistics during training and uses stored statistics during inference.'
That is why the flag matters. If those layers run in the wrong mode, the model can behave incorrectly even though the code still executes.
The Normal Functional API Pattern
In ordinary Functional API code, build the graph without forcing training mode.
With this model:
- '
model.fit(...)uses training behavior,' - '
model.evaluate(...)uses inference behavior for relevant layers,' - '
model.predict(...)uses inference behavior.'
That automatic mode switching is what you want most of the time.
When You Should Forward training
If you write a custom layer or custom model that internally uses dropout, batch normalization, or any other mode-sensitive sublayer, accept training in call and pass it through.
This is the correct place to deal with training: you are relaying Keras's mode decision, not overriding it globally.
Why Hardcoding training=True Is Usually Wrong
You can force training mode directly:
But now dropout stays active even during inference. That is usually a bug, because predictions become stochastic and systematically different from the intended deployment behavior.
The same kind of problem applies to batch normalization, where forcing training mode changes whether moving statistics are updated and whether stored statistics are used.
Legitimate Exceptions
There are special cases where forcing training behavior is intentional. A common example is Monte Carlo dropout, where you deliberately keep dropout active during inference to estimate uncertainty.
That is an advanced technique, not the default architecture pattern. If you are not explicitly trying to do that, do not hardcode training=True.
Custom Training Loops Follow the Same Rule
If you use a custom loop instead of model.fit, choose the mode at the call site:
This keeps the training-versus-inference decision explicit and avoids leaking training behavior into validation or serving.
Common Pitfalls
A common mistake is hardcoding training=True while building the Functional API graph and then wondering why predictions are unstable. Dropout and batch normalization are often the reason.
Another issue is forgetting to accept and forward training in custom layers that contain stateful sublayers.
Developers also sometimes assume every layer cares about the flag. Most do not, so the important question is whether the specific sublayers inside your model are mode-sensitive.
Summary
- In normal Functional API code, do not force
training=Trueduring model construction. - Let Keras manage training and inference mode through its standard APIs.
- Forward
trainingexplicitly inside customcallmethods when your layer contains mode-sensitive sublayers. - Hardcoding training mode is only appropriate for special cases such as deliberate Monte Carlo dropout.
- Understanding which layers depend on
trainingprevents subtle model bugs.

