Challenge - 5 Problems
GradientTape Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of GradientTape with simple function
What is the output of the following TensorFlow code that uses GradientTape to compute the derivative of y = x² at x = 3?
TensorFlow
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())
Attempts:
2 left
💡 Hint
Recall the derivative of x squared is 2x.
✗ Incorrect
The derivative of y = x² is dy/dx = 2x. At x = 3, dy/dx = 6.0.
❓ Model Choice
intermediateChoosing correct GradientTape usage for multiple variables
You want to compute gradients of z = x * y + y² with respect to both x and y using TensorFlow GradientTape. Which code snippet correctly computes both gradients?
Attempts:
2 left
💡 Hint
To get gradients for multiple variables, pass them as a list to gradient().
✗ Incorrect
Passing [x, y] to tape.gradient returns gradients with respect to both variables. Passing only x or y returns gradient for that variable only.
❓ Hyperparameter
advancedEffect of persistent=True in GradientTape
What is the effect of setting persistent=True when creating a tf.GradientTape?
Attempts:
2 left
💡 Hint
By default, GradientTape is deleted after one gradient call.
✗ Incorrect
persistent=True keeps the tape active after calling gradient(), allowing multiple gradient computations from the same tape.
🔧 Debug
advancedIdentifying error in GradientTape usage
What error will this TensorFlow code raise and why?
import tensorflow as tf
x = tf.Variable(1.0)
with tf.GradientTape() as tape:
y = x * 2
print(tape.gradient(y, x))
print(tape.gradient(y, x))
Attempts:
2 left
💡 Hint
GradientTape is deleted after first gradient call unless persistent=True.
✗ Incorrect
By default, GradientTape is deleted after one call to gradient(), so the second call raises RuntimeError.
🧠 Conceptual
expertWhy use GradientTape for higher-order derivatives?
Why is tf.GradientTape useful for computing higher-order derivatives (like second derivatives) in TensorFlow?
Attempts:
2 left
💡 Hint
Think about nesting GradientTape contexts.
✗ Incorrect
To compute second derivatives, you nest GradientTape contexts so the inner tape records operations for the first derivative, and the outer tape computes gradients of those gradients.
