We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← JAX Autodiff step 25 of 25
Medium
Primitives
Grad through stop_gradient
Why this matters
lax.stop_gradient(x) blocks autodiff from flowing through x. From
the perspective of the differentiation engine, the wrapped value is treated
as a constant — even though the forward computation is unchanged.
This is a foundational pattern in JAX because:
-
Target networks (DQN, DDPG) — the target Q-value should not receive
gradients; wrapping it with
stop_gradientenforces this cleanly. - Straight-through estimators — let a discrete forward pass influence the backward via a continuous surrogate, but stop the gradient from flowing back through the discrete op.
-
Freezing sub-networks — treat a pretrained encoder as a feature
extractor by
stop_gradient-ing its output before computing the loss.
Worked mini-example
import jax
import jax.numpy as jnp
from jax import lax
x = jnp.array([1.0, 2.0])
w = 3.0
def loss(w):
return jnp.sum((lax.stop_gradient(x) * w) ** 2)
g = jax.grad(loss)(w)
# grad w.r.t. w = 2 * w * sum(x²) = 2 * 3 * 5 = 30.0
# grad w.r.t. x would be 0 if we tried (stop_gradient blocks it)
Common pitfalls
-
Only blocks gradient — not the forward value —
stop_gradient(x)still returnsxin the forward pass; only autodiff is blocked. -
Wrong target — wrapping the wrong argument (e.g., wrapping
winstead ofx) blocks all gradient and defeats the purpose. -
Alternative:
jax.lax.stop_gradient— accessible as eitherjax.lax.stop_gradientorfrom jax import lax; lax.stop_gradient.
Problem
Implement grad_through_stop(x, w) that:
-
Defines
loss(w) = sum((stop_gradient(x) * w)²). -
Returns
jax.grad(loss)(w)— the gradient w.r.t.wonly.
-
x: 1-D jax array. -
w: scalar.
Returns: scalar — equals 2 * w * sum(x²).
Nothing accepted yet. When a submission passes, the code that passed shows up
here, one entry per mode.
Stuck?
JAX reference solution
Sign in to attempt this problem and reveal the reference solution.