🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEtensorflow

tensorflow Documentation

LOADING ENGINE...

tf.Variable()

AI & DATA SCIENCE // tf-variable

tf.Variable() creates a mutable tensor whose value can be updated in place over time — the standard way to represent a neural network's trainable weights and biases.

Syntax

tf.Variable(initial_value, trainable=True, dtype=None)

Deep Dive Course

Unlike tf.constant(), a Variable's value can be changed after creation via methods like .assign(), .assign_add(), and .assign_sub(), which update its stored value in place rather than creating a brand-new tensor — this in-place mutability is exactly what lets an optimizer repeatedly nudge a model's weights during training. The trainable parameter, True by default, controls whether TensorFlow's automatic differentiation should track this variable for gradient computation; setting it to False creates a variable that persists and can still be manually updated, but is excluded from gradient-based training.

1Understanding tf.Variable()

Unlike tf.constant(), a Variable's value can be changed after creation via methods like .assign(), .assign_add(), and .assign_sub(), which update its stored value in place rather than creating a brand-new tensor — this in-place mutability is exactly what lets an optimizer repeatedly nudge a model's weights during training. The trainable parameter, True by default, controls whether TensorFlow's automatic differentiation should track this variable for gradient computation; setting it to False creates a variable that persists and can still be manually updated, but is excluded from gradient-based training.

💡

Use .assign(), or .assign_add()/.assign_sub(), to update a Variable's value in place — a plain Python reassignment doesn't update the Variable at all, it just rebinds the Python name to point at something else entirely.

editor.html
import tensorflow as tf

weight = tf.Variable(5.0)
weight.assign(10.0)
print(weight)
localhost:3000

2Practical Example

Here is a real-world application of tf.Variable() showing how it is used in production TensorFlow code.

editor.html
import tensorflow as tf

counter = tf.Variable(0)
counter.assign_add(1)
counter.assign_add(1)
print(counter.numpy())
localhost:3000

3Best Practices

Follow these guidelines when working with tf.Variable():

1. Use .assign()/.assign_add()/.assign_sub() to update a Variable's value in place, never a plain Python reassignment, which doesn't actually mutate the underlying Variable

2. Set trainable=False for variables that need to persist and update manually but shouldn't be included in gradient-based optimization, like a manually-tracked running statistic

3. Let Keras layers create and manage their own Variables automatically in most cases, rather than manually creating tf.Variable() objects for standard model weights

⚠️

Tip: Use .assign(), or .assign_add()/.assign_sub(), to update a Variable's value in place — a plain Python reassignment doesn't update the Variable at all, it just rebinds the Python name to point at something else entirely.

editor.html
import tensorflow as tf

weight = tf.Variable(5.0)
weight.assign(10.0)
print(weight)
localhost:3000

Examples

Example 01Basic Usage
import tensorflow as tf

weight = tf.Variable(5.0)
weight.assign(10.0)
print(weight)
Example 02Advanced Example
import tensorflow as tf

counter = tf.Variable(0)
counter.assign_add(1)
counter.assign_add(1)
print(counter.numpy())

Best Practices

  • Use .assign()/.assign_add()/.assign_sub() to update a Variable's value in place, never a plain Python reassignment, which doesn't actually mutate the underlying Variable
  • Set trainable=False for variables that need to persist and update manually but shouldn't be included in gradient-based optimization, like a manually-tracked running statistic
  • Let Keras layers create and manage their own Variables automatically in most cases, rather than manually creating tf.Variable() objects for standard model weights

Interview Question

Why doesn't reassigning a Python name that points to a tf.Variable actually update the Variable's stored value?

Hint: Think about the difference between rebinding a Python name and mutating the object that name currently points to.

A plain Python assignment simply makes the name point to a completely different object, the new tensor, in Python's own variable-name bookkeeping — it doesn't call any method on the original Variable object to actually change its internally stored value. Anything else in the program still holding a reference to the original Variable object, like an optimizer that's already tracking it, would still see its old, unchanged value. Only methods like .assign() actually mutate the Variable's internal state in place, which is why they're required for genuinely updating it rather than just reassigning a Python name.

Exercises

MediumPractice using tf.Variable() in a real scenario.
View Solution
import tensorflow as tf

weight = tf.Variable(5.0)
weight.assign(10.0)
print(weight)

Frequently Asked Questions

Why doesn't reassigning a Python name that points to a tf.Variable actually update the Variable's stored value?

A plain Python assignment simply makes the name point to a completely different object, the new tensor, in Python's own variable-name bookkeeping — it doesn't call any method on the original Variable object to actually change its internally stored value. Anything else in the program still holding a reference to the original Variable object, like an optimizer that's already tracking it, would still see its old, unchanged value. Only methods like .assign() actually mutate the Variable's internal state in place, which is why they're required for genuinely updating it rather than just reassigning a Python name.

Related Functions

tf-constantoptimizer-apply-gradientstf-gradienttape