Complete the code to record operations for automatic differentiation using GradientTape.
import tensorflow as tf x = tf.Variable(3.0) with tf.GradientTape() as tape: y = x * x grad = tape.[1](y, x)
The method to compute gradients in TensorFlow's GradientTape is gradient.
Complete the code to watch a tensor for gradient calculation inside GradientTape.
import tensorflow as tf x = tf.constant(4.0) tape = tf.GradientTape() tape.[1](x) y = x * x grad = tape.gradient(y, x)
To tell GradientTape to watch a tensor, use the watch method.
Fix the error in the code to correctly compute the gradient of y = x^3.
import tensorflow as tf x = tf.Variable(2.0) with tf.GradientTape() as tape: y = x[1]3 grad = tape.gradient(y, x)
In Python, exponentiation is done with **, not ^.
Fill both blanks to create a GradientTape context and compute the gradient of z = x * y.
import tensorflow as tf x = tf.Variable(3.0) y = tf.Variable(4.0) with tf.GradientTape([1]=True) as tape: z = x [2] y
Setting watch_accessed_variables=True makes tape watch variables automatically. The multiplication operator is *.
Fill all three blanks to compute the gradient of f = x^2 + y^2 with respect to x and y.
import tensorflow as tf x = tf.Variable(1.0) y = tf.Variable(2.0) with tf.GradientTape() as tape: f = x[1]2 + y[2]2 gradients = tape.gradient(f, [3])
Use ** for power, and pass a list of variables [x, y] to compute gradients for both.
