Inside a GradientTape's with block, every operation performed on a tracked tensor is recorded onto the tape, building up a record of the computation; afterward, calling tape.gradient(target, sources) uses that recorded computation to compute the derivative of target with respect to each tensor in sources, via automatic differentiation. Variables created with tf.Variable() are tracked automatically, while a plain tf.constant() tensor needs tape.watch() called on it explicitly if you need a gradient with respect to it. GradientTape is the fundamental building block underlying model.fit()'s automatic training loop, used directly whenever you need a custom training loop with full manual control.
1Understanding tf.GradientTape()
Inside a GradientTape's with block, every operation performed on a tracked tensor is recorded onto the tape, building up a record of the computation; afterward, calling tape.gradient(target, sources) uses that recorded computation to compute the derivative of target with respect to each tensor in sources, via automatic differentiation. Variables created with tf.Variable() are tracked automatically, while a plain tf.constant() tensor needs tape.watch() called on it explicitly if you need a gradient with respect to it. GradientTape is the fundamental building block underlying model.fit()'s automatic training loop, used directly whenever you need a custom training loop with full manual control.
tf.Variable() tensors are watched automatically by GradientTape, but a plain tf.constant() is not — call tape.watch(my_constant) explicitly inside the tape's context if you need a gradient with respect to a constant tensor.
import tensorflow as tf
x = tf.Variable(3.0)
with tf.GradientTape() as tape:
y = x ** 2
dy_dx = tape.gradient(y, x)
print(dy_dx.numpy())2Practical Example
Here is a real-world application of tf.GradientTape() showing how it is used in production TensorFlow code.
import tensorflow as tf
x = tf.constant(3.0)
with tf.GradientTape() as tape:
tape.watch(x)
y = x ** 3
dy_dx = tape.gradient(y, x)
print(dy_dx.numpy())3Best Practices
Follow these guidelines when working with tf.GradientTape():
1. Use GradientTape directly when writing a custom training loop that needs full manual control over the forward pass, loss computation, and gradient application
2. Remember tf.Variable() tensors are watched automatically, while tf.constant() tensors need an explicit tape.watch() call to compute gradients with respect to them
3. Keep the with block as short as possible, containing only the forward pass and loss computation, since everything inside it is recorded and consumes extra memory
Tip: tf.Variable() tensors are watched automatically by GradientTape, but a plain tf.constant() is not — call tape.watch(my_constant) explicitly inside the tape's context if you need a gradient with respect to a constant tensor.
import tensorflow as tf
x = tf.Variable(3.0)
with tf.GradientTape() as tape:
y = x ** 2
dy_dx = tape.gradient(y, x)
print(dy_dx.numpy())