TensorFlow simple operations tensors vs Python variables
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
TensorFlow code often mixes Python variables, tf.Tensor objects, and tf.Variable objects in the same function. They are not interchangeable. Python variables are just names in your program, tensors are immutable values used by TensorFlow operations, and tf.Variable represents mutable state that can be updated during training.
Python Variables Are Names, Not TensorFlow State
In Python, a variable is simply a reference to an object. It does not tell TensorFlow anything about gradients, devices, or graph execution.
Here x is a Python variable whose value happens to be a tf.Tensor. Reassigning x in Python just changes what name points to what object.
That is normal Python behavior, not in-place mutation of the original tensor.
tf.Tensor Is an Immutable Value
Tensors are the primary data objects TensorFlow uses for computation. They support arithmetic and can flow through models, but they are immutable. An operation produces a new tensor rather than changing the old one.
t remains unchanged. This is important when reasoning about computation graphs and functional-style model code.
Use tf.Variable for Mutable Parameters
If a value should change over time, such as a model weight, use tf.Variable.
tf.Variable is what optimizers update during training. It behaves differently from a plain tensor because TensorFlow tracks it as mutable state.
Simple Operations and Gradient Tracking
TensorFlow operations work on tensors and variables, but only trainable variables are updated by optimizers. A plain Python number participates in math, yet it is not trainable state.
bias is just a Python float embedded into the computation. w is the tracked variable that can receive gradients and be updated.
If you need bias to be trainable, make it a tf.Variable too.
Eager Execution Makes This Easier to See
In modern TensorFlow, eager execution is on by default. That means tensors behave more like regular runtime values, and you can inspect them immediately with .numpy(). This makes the distinction easier:
- Python variable means a normal program reference
- '
tf.Tensormeans an immutable computed value' - '
tf.Variablemeans mutable TensorFlow state'
That separation becomes especially important when writing custom training loops, losses, or layers.
Common Pitfalls
- Calling a Python name a "TensorFlow variable" when it is only a reference to a tensor.
- Expecting a tensor to change in place after arithmetic.
- Using Python scalars for values that should be trainable model parameters.
- Forgetting that optimizers update
tf.Variable, not arbitrary Python objects. - Mixing TensorFlow state and Python control flow without understanding which parts are tracked.
Summary
- Python variables are names that reference objects.
- '
tf.Tensoris an immutable TensorFlow value used in computation.' - '
tf.Variableis mutable state used for trainable parameters and updates.' - Arithmetic on tensors creates new tensors rather than mutating old ones.
- The distinction matters most when training models, computing gradients, and managing state.
Related reading
- TensorFlow simple recurrent neural network
- Tensorflow simultaneous prediction on GPU and CPU
- Tensorflow slicing
- Tensorflow Slim TypeError Expected int32, got list containing Tensors of type ''_Message'' instead
- Tensorflow single sigmoid output with log loss vs two linear outputs with sparse softmax cross entropy loss for binary classification
- Tensorflow Slicing a Tensor into overlapping blocks
- Tensorflow slicing based on variable
- TensorFlow slow performance when getting gradients at inputs
.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.