liveBook · Manning

Variable

As explained in Deep Learning with Python, Third Edition

A mutable TensorFlow value that stores learnable parameters such as weights and bias in this example.

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

How Deep Learning with Python, Third Edition covers it

How deep it goes
Defines mutability and demonstrates whole-value, subset, additive, and subtractive assignment before applying updates in a classifier.
Examples
Uses a random variable v and classifier parameters W and b, updated with assign, indexed assignment, assign_add, and assign_sub.
What it assumes
Assumes basic tensors and arithmetic, while contrasting immutable tensors with variables and demonstrating both from fundamentals.
What you can do after
Apply it via TensorFlow to create mutable parameters and update them during a handwritten training loop.
How it connects
Connects variables to TensorFlow tensors, GradientTape, gradients, gradient descent, and linear classifiers.

Also explained in

Compare how 2 books explain variable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.