TensorFlow Variables and Constants
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, both constants and variables hold tensor data, but they serve different roles. A constant is an immutable value used as fixed input or configuration, while a variable is mutable state that TensorFlow can update during training.
Create a Constant
Use tf.constant when a value should not change during the life of the computation.
Constants are useful for:
- fixed scalar values
- hard-coded lookup tensors
- input examples in small demos
- configuration values inside a computation
They can participate in math operations normally, but you cannot assign a new value to them.
Create a Variable
Use tf.Variable when the value must change over time, especially during optimization.
Variables are the natural choice for model parameters because training algorithms update them repeatedly.
Variables Can Be Updated
Unlike constants, variables support assignment methods:
That mutability is the core distinction. If your code needs state that changes, it should usually be a variable.
Variables in a Training Step
TensorFlow tracks variables during gradient-based optimization. A small example shows why variables matter for machine learning:
Here, w and b must be variables because the optimizer updates them on every step. If they were constants, training could not adjust them.
Constants Still Matter in Models
Constants are not just for toy scripts. They are often useful alongside variables for:
- fixed masks
- scaling factors
- constant embeddings or lookup tables that should not train
- static input tensors in tests
For example:
The important point is that constants can be part of a model pipeline even though they are not trainable state.
Variable Tracking in Keras Layers
In tf.keras, variables are usually created inside layers and models. TensorFlow automatically tracks them as trainable or non-trainable weights.
That tracking is built on top of tf.Variable. So even if you do not instantiate variables directly all the time, they are still the underlying state objects that hold learned parameters.
Common Pitfalls
The biggest mistake is using tf.constant for something that needs to change during training. Optimizers only update variables, not immutable tensors.
Another common issue is assuming variables and constants are separate data types in every mathematical sense. Both behave like tensors in computation, but only variables carry mutable state and assignment semantics.
People also forget that variables need an initial value. TensorFlow must know the starting tensor shape and dtype when the variable is created.
Finally, do not confuse "non-trainable" with "constant". A non-trainable variable can still be assigned manually, while a constant cannot be changed at all.
Summary
- Use
tf.constantfor fixed tensor values that should not change. - Use
tf.Variablefor mutable state, especially model parameters. - Variables support
assign,assign_add, and gradient-based optimization. - Constants still participate in normal TensorFlow computations.
- In machine learning code, learned weights are variables, not constants.
Related reading
- Tensorflow vocabularyprocessor
- tensorflow warning - Found untraced functions such as lstm_cell_6_layer_call_and_return_conditional_losses
- Tensorflow weight initialization
- Tensorflow What are the output_node_names for freeze_graph.py in the model_with_buckets model?
- Tensorflow VarLenFeature vs FixedLenFeature
- TensorFlow version 1.0.0-rc2 on Windows OpKernel 'op BestSplits device_type CPU' for unknown op BestSplits with test code
- Tensorflow vs OpenCV
- Tensorflow warning The graph couldn't be sorted in topological order?
.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.