TensorFlow tensors are immutable, but training must update model state. A tf.Variable supplies that mutable state for learnable values such as weights and bias. section 3.3.1
Updating a linear classifier
A linear classifier creates W with random values and b with zeros, then computes predictions as tf.matmul(inputs, W) + b. During each training step, GradientTape computes gradients, and W.assign_sub(...) and b.assign_sub(...) update the variables. section 3.3.2
Use variable assignment methods when changing stored state:
- Call assign to replace the complete value. section 3.3.1
- Assign a subset through indexed assignment, such as v[0, 0].assign(3.). section 3.3.1
- Use assign_add and assign_sub for efficient incremental updates. section 3.3.1
Listing 3.5 Creating a tf.Variable
>>> v = tf.Variable(initial_value=tf.random.normal(shape=(3, 1))) >>> print(v) array([[-0.75133973], [-0.4872893 ], [ 1.6626885 ]], dtype=float32)>
Inspect a minimal tf.Variable creation example, then compare it with assign-based updates. section 3.3.1

