To update a model, you need to know how its loss changes with its weights. `tf.GradientTape` records TensorFlow operations performed inside its scope so you can compute those gradients afterward. Put the forward calculation inside `with tf.GradientTape() as tape:`, then call `tape.gradient(output, inputs)` outside it. section 3.3.1 section 7.4.2
Start with a square
Create `input_var = tf.Variable(3.0)`. Inside the tape scope, calculate `result = tf.square(input_var)`. Afterward, `tape.gradient(result, input_var)` gives the gradient of the result with respect to the variable. The important boundary is the tape scope: calculate the result while recording, then ask for its gradient. section 3.3.1
Two useful rules when moving beyond that first example:
- Variables work directly as gradient inputs. For a constant tensor, call `tape.watch(input_const)` inside the scope before calculating the result. section 3.3.1
- For a second-order gradient, nest two tapes: use the inner tape to differentiate once and the outer tape to differentiate that result. section 3.3.1
import tensorflow as tf model = get_mnist_model() loss_fn = keras.losses.SparseCategoricalCrossentropy() optimizer = keras.optimizers.Adam() def train_step(inputs, targets): with tf.GradientTape() as tape: #1 predictions = model(inputs, training=True) #2 loss = loss_fn(targets, predictions) #2 gradients = tape.gradient(loss, model.trainable_weights) #3 optimizer.apply(gradients, model.trainable_weights) #4 return loss
The complete TensorFlow training step places model predictions and loss calculation inside the tape, then obtains gradients with respect to `model.trainable_weights` and passes them to the optimizer. Use this pattern when writing a custom Keras training step. section 7.4.2
The same pattern can optimize an input instead of model weights. To increase a convolutional filter’s activation, watch the image, compute the activation-based loss inside the tape, and take its gradient with respect to the image. A `@tf.function` decorator can speed up the step. section 10.2.1

