tensorflow
keras
training mode
neural networks
deep learning

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.

Practice ML system design

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.

python
1import tensorflow as tf
2
3layer = tf.keras.layers.Dropout(0.5)
4x = tf.ones((1, 6))
5
6print(layer(x, training=True).numpy())
7print(layer(x, training=True).numpy())
8print(layer(x, training=False).numpy())

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.

python
1import tensorflow as tf
2
3bn = tf.keras.layers.BatchNormalization()
4x = tf.constant([[1.0, 2.0], [3.0, 4.0]])
5
6_ = bn(x, training=True)
7print(bn.moving_mean.numpy())
8print(bn(x, training=False).numpy())

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:

python
y = model(x, training=True)

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.

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Dense(16, activation="relu"),
5    tf.keras.layers.Dropout(0.5),
6    tf.keras.layers.Dense(1),
7])
8
9optimizer = tf.keras.optimizers.Adam()
10loss_fn = tf.keras.losses.MeanSquaredError()
11
12x = tf.random.normal((8, 4))
13y_true = tf.random.normal((8, 1))
14
15with tf.GradientTape() as tape:
16    y_pred = model(x, training=True)
17    loss = loss_fn(y_true, y_pred)
18
19grads = tape.gradient(loss, model.trainable_variables)
20optimizer.apply_gradients(zip(grads, model.trainable_variables))

During validation in the same program, you would switch to training=False.

python
y_val = model(x, 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.

python
1import tensorflow as tf
2
3class MyBlock(tf.keras.layers.Layer):
4    def __init__(self):
5        super().__init__()
6        self.dropout = tf.keras.layers.Dropout(0.3)
7
8    def call(self, inputs, training=None):
9        return self.dropout(inputs, training=training)

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=True tells layers with phase-dependent behavior to act as if the model is in training mode.'
  • It mainly affects layers such as Dropout and BatchNormalization.
  • Keras manages the flag for fit, evaluate, and predict automatically.
  • In custom training loops, call the model with training=True during training and training=False during validation or inference.
  • Custom layers should accept and propagate the training argument when they wrap layers that depend on it.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.