Keras
_uses_learning_phase
deep learning
machine learning
neural networks

What is _uses_learning_phase 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.

Practice ML system design

Introduction

_uses_learning_phase comes from older Keras internals, where the framework needed to track whether a layer behaved differently during training and inference. If you saw it in model debugging, it usually meant the graph depended on concepts like dropout or batch normalization, which need to know whether the model is training.

What The Learning Phase Means

Some layers are mode-dependent:

  • 'Dropout randomly drops activations during training but not during inference.'
  • 'BatchNormalization uses batch statistics during training and moving statistics during inference.'

Older Keras represented this with a global "learning phase" concept. Internally, tensors and layers could be marked as depending on that phase. _uses_learning_phase was one of the internal flags involved in that bookkeeping.

In other words, the flag did not mean "this model is currently training". It meant "this output depends on whether the model is called in training mode or inference mode".

Why You Usually Should Not Touch It

The important word is internal. Application code was not supposed to set _uses_learning_phase manually. In modern Keras and tf.keras, the public mechanism is the training argument in call(), plus the built-in behavior of layers such as Dropout and BatchNormalization.

A modern custom layer should look like this:

python
1import keras
2
3class CustomDropout(keras.layers.Layer):
4    def __init__(self, rate, **kwargs):
5        super().__init__(**kwargs)
6        self.rate = rate
7        self.seed_generator = keras.random.SeedGenerator(1337)
8
9    def call(self, inputs, training=None):
10        if training:
11            return keras.random.dropout(
12                inputs, rate=self.rate, seed=self.seed_generator
13            )
14        return inputs

Here, you do not set _uses_learning_phase yourself. Keras understands that the layer behavior depends on training.

Legacy Keras Versus Modern Keras

This topic often appears in old answers written for graph-mode Keras or early tf.keras. Back then, code sometimes inspected attributes like uses_learning_phase or relied on backend utilities that exposed a global phase tensor.

Modern Keras documentation emphasizes a different approach:

  • built-in training loops such as fit() pass training mode automatically
  • custom layers can declare call(self, inputs, training=None)
  • inference mode can be forced by calling the model with training=False

That means if you encounter _uses_learning_phase today, it is usually one of three things:

  • legacy code
  • internal framework state surfaced in debugging output
  • an old Stack Overflow answer describing behavior from earlier Keras versions

How To Control Training And Inference Correctly

The right public API is to pass training where needed.

python
1import keras
2from keras import layers
3
4inputs = keras.Input(shape=(16,))
5x = layers.Dense(32, activation="relu")(inputs)
6x = layers.Dropout(0.5)(x)
7outputs = layers.Dense(1)(x)
8
9model = keras.Model(inputs, outputs)
10
11sample = keras.random.normal(shape=(4, 16))
12
13train_out = model(sample, training=True)
14test_out = model(sample, training=False)

The two outputs can differ because dropout is active only when training=True.

For custom models and layers, follow the same pattern:

python
1class NoiseLayer(keras.layers.Layer):
2    def call(self, inputs, training=None):
3        if training:
4            noise = keras.random.normal(shape=keras.ops.shape(inputs), stddev=0.1)
5            return inputs + noise
6        return inputs

This is much clearer than relying on hidden flags.

When People Run Into This Attribute

Developers usually discover _uses_learning_phase when:

  • printing layer internals while debugging
  • using old custom Lambda layers
  • loading older models or code samples
  • mixing low-level backend operations with high-level Keras layers

If the model behaves incorrectly between training and inference, the fix is rarely to modify _uses_learning_phase. The fix is usually to make sure the layer accepts training and forwards it correctly.

For example, when wrapping a submodel:

python
1class Wrapper(keras.layers.Layer):
2    def __init__(self, inner_model, **kwargs):
3        super().__init__(**kwargs)
4        self.inner_model = inner_model
5
6    def call(self, inputs, training=None):
7        return self.inner_model(inputs, training=training)

If you forget to forward training, nested dropout or batch normalization may behave unexpectedly.

Common Pitfalls

  • Treating _uses_learning_phase as a supported public API. It is an internal implementation detail.
  • Trying to set the attribute manually instead of accepting training in call().
  • Forgetting to pass training=training through wrapper layers or custom models.
  • Reading old answers without checking whether they describe legacy graph-mode behavior.
  • Assuming trainable=False and inference mode mean the same thing. They are related but not identical, especially for batch normalization.

Summary

  • '_uses_learning_phase is a legacy internal Keras concept for outputs that differ between training and inference.'
  • It commonly appears with layers like dropout and batch normalization.
  • Modern Keras code should use the public training argument, not hidden internal flags.
  • When debugging old code, treat this attribute as a clue about mode-dependent behavior, not as an API to manipulate.
  • If you write custom layers, accept training=None and pass it through explicitly.

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.