Difference between tf.assign and assignment operator
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
tf.assign and Python's = operator do completely different jobs. = only rebinds a Python name, while tf.assign or the modern variable.assign(...) updates the value stored inside a TensorFlow variable.
What = Does in Python
Python assignment does not mutate TensorFlow state by itself. It changes which object a Python variable name points to.
After the second line, the name v no longer refers to the original tf.Variable. It now refers to a constant tensor. The original variable was not updated. You just replaced the Python reference.
That is the core distinction. The = operator lives at the Python language level, not at the TensorFlow variable-state level.
What tf.assign Did in TensorFlow 1
In TensorFlow 1 graph mode, tf.assign created an operation in the computation graph that, when executed, updated the variable's stored value.
Notice the difference from Python assignment: nothing changes until the assign operation is actually run in the session.
The Modern TensorFlow 2 Way
In TensorFlow 2, the idiomatic API is the variable method assign:
This updates the TensorFlow variable in place. It is conceptually the successor to tf.assign, which mainly survives through tf.compat.v1 for older code.
Why This Difference Matters
This distinction shows up everywhere in training code. Optimizers, moving averages, counters, and custom state updates must modify TensorFlow variables, not just rebind Python names.
For example, this is wrong if you mean to update the model state:
That expression creates a new tensor object and rebinds step at the Python level. It does not perform an in-place variable update the way step.assign_add(1) does.
The correct stateful update is:
Compound Updates Need TensorFlow APIs Too
The same rule applies to increment and decrement patterns. In older TensorFlow 1 code you often saw tf.assign_add or tf.assign_sub. In TensorFlow 2 the modern equivalents are variable methods such as assign_add and assign_sub.
Those APIs matter because they preserve stateful semantics inside TensorFlow. A Python expression such as step = step + 1 creates a new tensor expression. step.assign_add(1) updates the variable the rest of the training system is already tracking.
Graph Mode Versus Eager Mode
The confusion is strongest when older TensorFlow 1 tutorials are read through a TensorFlow 2 mindset. In graph mode, tf.assign was an op that had to be executed. In eager mode, assign methods act immediately. But in both cases, Python = still only changes the Python binding.
That rule is stable across TensorFlow versions.
Common Pitfalls
- Using
=and expecting the underlying TensorFlow variable to change. - Forgetting to execute
tf.assignin TensorFlow 1 graph mode. - Reading old
tf.assignexamples and copying them into TensorFlow 2 code instead of usingvariable.assign. - Rebinding a variable name to a tensor and accidentally losing the original
tf.Variable. - Mixing Python object assignment with TensorFlow state updates in training loops.
Summary
- Python
=rebinds names; it does not update TensorFlow variable storage. - '
tf.assignin TensorFlow 1 created a graph operation that updated a variable when executed.' - In TensorFlow 2, prefer
variable.assign(...)andassign_add(...). - The difference is about Python references versus TensorFlow state.
- If you want model state to change, use TensorFlow variable update APIs, not plain
=.

