Custom training loops
Compute parameter derivatives of a loss during model training.
TensorFlow · Class
Records operations performed in a context so TensorFlow can compute gradients automatically. It watches trainable variables by default and supports manual watching, persistent tapes, and higher-order derivatives.
Use the tape as a context manager around the operations whose derivatives are needed. TensorFlow records eligible operations, then gradient() applies reverse-mode differentiation to a target with respect to selected sources; trainable variables are watched automatically unless that behavior is disabled.
tf.GradientTape(persistent=False, watch_accessed_variables=True)| Name | Type | Description |
|---|---|---|
persistent | bool | Whether to retain the tape for more than one gradient or Jacobian computation; defaults to False. |
watch_accessed_variables | bool | Whether to automatically watch trainable variables accessed while the tape is active; defaults to True. |
A GradientTape context-manager object; its gradient(), jacobian(), and related methods return derivatives for the requested targets and sources.
import tensorflow as tf
x = tf.Variable(3.0)
with tf.GradientTape() as tape:
y = x * x
dy_dx = tape.gradient(y, x)
print(dy_dx.numpy()) # 6.0Records the forward computation and obtains the derivative of y with respect to x.
Compute parameter derivatives of a loss during model training.
Differentiate a tensor that is not a trainable variable.
Calculate second-order derivatives or other nested derivatives.
GradientTape records differentiable operations executed in its context and uses those records to compute a target's gradient with respect to sources. Trainable variables accessed while the tape is active are watched automatically by default.
Follow-up: How would you differentiate with respect to a plain tensor? Call tape.watch(tensor) before using it in the recorded computation.
By default, a tape is released after gradient() is called, so it supports one gradient computation. Construct it with persistent=True when multiple gradient or Jacobian computations must reuse the same recorded work.
Follow-up: When is the persistent tape released? Its resources remain until the tape object is garbage-collected.
Set watch_accessed_variables=False to disable automatic tracking of trainable variables. Then explicitly watch each required variable while the tape is active; otherwise the requested gradient cannot be formed from that source.
Follow-up: What happens for disconnected sources? The gradient() method uses unconnected_gradients to control the result, with tf.UnconnectedGradients.NONE as the default.
tf.autodiff.GradientTapetf.GradientTape.gradienttf.GradientTape.jacobiantf.GradientTape.watchtf.UnconnectedGradients