Home / TensorFlow / Automatic differentiation with GradientTape
Gotcha: Tapes are single-use by default
When GradientTape.gradient
is called, all resources are released.
To compute multiple gradients, use multiple tapes or:
import tensorflow as tf
x1 = tf.Variable(1.)
x2 = tf.Variable(2.)
with tf.GradientTape(persistent=True) as tape:
y1 = x1 ** 2
y2 = x2 ** 2
dy_dx_1 = tape.gradient(y1, x1)
dy_dx_2 = tape.gradient(y2, x2)
del tape # NOTE Do not forget.
f"{dy_dx_1}, {dy_dx_2}"
2.0, 4.0