significance of trainable and training flag in tf.layers.batch_normalization
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
In TensorFlow batch normalization, trainable and training sound similar, but they control different things. training decides whether the layer behaves like it is in training mode or inference mode for the current forward pass, while trainable controls whether the layer’s trainable parameters are updated by the optimizer.
What the training flag controls
The training argument affects runtime behavior. For batch normalization, that mainly means:
- '
training=True: use the current mini-batch statistics' - '
training=False: use the stored moving averages'
A simplified TensorFlow 1.x style example looks like this:
When you feed is_training=True, the layer behaves like training. When you feed False, it behaves like inference.
What the trainable flag controls
The trainable flag is about learnable variables such as gamma and beta. If trainable=False, those trainable parameters are frozen and should not receive gradient updates from the optimizer.
That is useful in transfer learning or when you want to freeze part of a model while fine-tuning other layers.
Why the two flags are not interchangeable
This is the key point:
- '
trainingis about the current forward-pass mode' - '
trainableis about whether the layer’s parameters participate in training updates'
You can imagine combinations such as:
- '
trainable=True,training=True' - '
trainable=False,training=False'
Those combinations do not mean the same thing. One concerns how statistics are used right now. The other concerns what the optimizer is allowed to change over time.
The TensorFlow 1.x update-ops detail
With tf.layers.batch_normalization, there is another classic TensorFlow 1.x detail: moving mean and moving variance updates are often stored in UPDATE_OPS, and your training op needs to run them.
If you forget this, batch normalization can appear to train, but the moving statistics used at inference time may never update correctly.
A practical transfer-learning rule
In practice, if you want to freeze a pretrained batch-normalization layer, you often set it up so it behaves in inference mode and does not keep adapting its internal statistics during fine-tuning. That is why frozen batch norm frequently involves both the mode decision and the trainability decision together.
The exact behavior can vary across TensorFlow and Keras versions, which is another reason to be explicit.
Why frozen batch norm still confuses people
Developers often freeze a pretrained model by setting layers non-trainable and then expect batch normalization to behave like a completely inert layer. In practice, batch norm is special because it has both trainable parameters and moving statistics, so you need to think about optimization and runtime mode separately.
That is why transfer-learning bugs around batch normalization are so common: the layer has more than one kind of state.
Common Pitfalls
- Confusing
training=Falsewithtrainable=False. - Forgetting
UPDATE_OPSin TensorFlow1.xtraining graphs. - Freezing a model but still running batch normalization in training mode by mistake.
- Assuming batch norm behaves like a stateless activation layer when it actually depends on runtime mode and moving statistics.
Summary
- '
trainingcontrols whether batch normalization uses batch statistics or stored moving statistics for the current pass.' - '
trainablecontrols whether gamma and beta are updated by the optimizer.' - In TensorFlow
1.x, batch norm also needs update ops to run during training. - Do not treat
trainableandtrainingas synonyms; they solve different problems.
Related reading
- Simple Keras Network in GradientTape LookupError No gradient defined for operation 'IteratorGetNext' op type IteratorGetNext
- Simple Keras neural network isn't learning
- Simple multi layer neural network implementation
- Simple Multilayer Perceptron model does not converge in TensorFlow
- Simple Keras Network in GradientTape LookupError No gradient defined for operation 'IteratorGetNext' op type IteratorGetNext
- Simple way to visualize a TensorFlow graph in Jupyter?
- Simple accord.net machine learning example
- Simple example using BernoulliNB naive bayes classifier scikit-learn in python - cannot explain classification
.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.