What's the differences between tf.GraphKeys.TRAINABLE_VARIABLES and tf.GraphKeys.UPDATE_OPS in tensorflow?
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 1 graph mode, tf.GraphKeys.TRAINABLE_VARIABLES and tf.GraphKeys.UPDATE_OPS refer to very different things. TRAINABLE_VARIABLES holds variables that optimizers are expected to update through gradient descent, while UPDATE_OPS holds operations that should run during training but are not gradient-based parameter updates. The classic example is batch normalization moving statistics.
TRAINABLE_VARIABLES: Parameters Learned by the Optimizer
This collection contains variables marked as trainable. These are typically the weights and biases of your model.
Examples include:
- dense layer kernels
- convolution filters
- bias vectors
- trainable embeddings
A TensorFlow 1 style example:
When you call an optimizer's minimize method, TensorFlow computes gradients with respect to these trainable variables and applies updates to them.
UPDATE_OPS: Extra State Updates That Must Also Run
UPDATE_OPS contains graph operations that update internal state but are not themselves trainable variables.
The most famous example is batch normalization. During training, batch norm updates moving averages such as moving mean and moving variance. Those updates are not gradient descent on trainable weights. They are side-effect operations that need to be executed during training.
A simplified graph-mode example:
Those update ops are usually empty in very simple models and non-empty once layers with internal moving statistics are added.
Why the Difference Matters During Training
If you only minimize the loss and forget to run UPDATE_OPS, batch normalization and similar layers may not update their internal state correctly. The trainable weights still change, but the moving statistics stay stale.
That leads to training or inference behavior that can look mysteriously wrong.
The classic TensorFlow 1 training pattern is:
That control_dependencies block ensures the update ops run whenever train_op runs.
Mental Model
A useful mental model is:
- '
TRAINABLE_VARIABLESare things the optimizer learns' - '
UPDATE_OPSare side effects that training should execute'
They are related to training, but they do not represent the same kind of graph entity.
One is a collection of variables.
The other is a collection of operations.
TensorFlow 2 Note
This distinction is most visible in TensorFlow 1 graph-mode code. In TensorFlow 2 and modern Keras usage, many of these details are managed automatically by the higher-level training loop, so developers see GraphKeys much less often.
That does not make the concept unimportant. It just means modern APIs hide more of the explicit graph bookkeeping.
Common Pitfalls
A common mistake is assuming UPDATE_OPS are extra trainable parameters. They are not; they are operations.
Another mistake is collecting update ops but never attaching them to the training step in TensorFlow 1 graph mode.
People also sometimes expect every layer to contribute to UPDATE_OPS. Many layers do not. The collection becomes relevant only for layers that maintain extra internal state.
Finally, do not read TensorFlow 1 GraphKeys code as if it were the normal TensorFlow 2 style. The programming model is different.
Summary
- In TensorFlow 1 graph mode,
TRAINABLE_VARIABLESandUPDATE_OPSserve different training roles - '
TRAINABLE_VARIABLESare the optimizer-updated model parameters' - '
UPDATE_OPSare state-update operations such as batch norm moving-average updates' - A common TensorFlow 1 training pattern is to run
UPDATE_OPSviatf.control_dependencies - Forgetting
UPDATE_OPScan break layers that depend on internal state updates during training - In TensorFlow 2, these details are often handled automatically, so the collection names appear less often in everyday code
Related reading
- What's the purpose of keras.backend.function
- What's the purpose of tf.app.flags in TensorFlow?
- When are Model call and train_step called?
- When do I have to use TensorFlow's FileWriter.flush method?
- What's the major difference between glove and word2vec?
- What's the meaning of logistic regression dataset labels?
- When global_variables_initializer is actually required
- When I try to train tensorflow's object detection api I get CUDA_ERROR_ILLEGAL_INSTRUCTION
.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.