Search

Search papers and lessons

Stop-Gradient

Level 3 · 11 min read

A stop-gradient passes a value forward unchanged but blocks the gradient from flowing back through it, so that value is treated as a constant during training.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Linear algebra
  • Neural network basics
  • Programming languages
  • Probability
  • ReadReady to readNot yet

TL;DR

A stop-gradient is an operation that does nothing to a value on the forward pass but blocks the gradient on the backward pass. The value flows through the network as usual, yet during training the optimiser treats it as a constant. This lets a model use one side of a comparison as a fixed target, which is useful when letting gradients flow through both sides would make training fail. It is one line of code, and it changes what the optimiser is allowed to adjust.

Builds on: Backpropagation, Automatic Differentiation · Leads to: Self-Supervised Learning methods such as BYOL, and the codebook update in VQ-VAE

Where it's used

Self-supervised image learning. BYOL trains two copies of a network, in the style of self-supervised image learning. The online copy is trained to predict the output of a target copy, and the target output is wrapped in a stop-gradient, so the loss cannot be lowered by changing the target. The paper states that BYOL's target parameters are not updated by the gradient of the loss; they follow an exponential moving average of the online parameters instead.

Discrete autoencoders. In VQ-VAE, an autoencoder's encoder output is snapped to the nearest entry of a learned codebook. The loss has three terms, and two of them use stop-gradient: one term moves the codebook entries toward the encoder output, and another moves the encoder output toward the chosen entry. The paper defines the stop-gradient operator as the identity in the forward pass, with zero partial derivatives, so the operand is effectively a non-updated constant for that term.

Reinforcement learning targets. Many value-learning methods, such as DQN with its target network, compute a bootstrapped target from the model's own predictions. The target is treated as fixed for the current update, so the update pushes the prediction toward the target and not the reverse.

How it works

Take one weight ww and two inputs, x=1x = 1 and x′=3x' = 3. The model's prediction is p=wxp = w x and its target is t=wx′t = w x'. The loss is L=(p−t)2L = (p - t)^2. Start with w=2w = 2.

  1. Forward pass with no stop-gradient. p=2×1=2p = 2 \times 1 = 2 and t=2×3=6t = 2 \times 3 = 6. The loss is (2−6)2=16(2 - 6)^2 = 16. Check: the loss is positive because the prediction and the target disagree.
  2. Backward pass with no stop-gradient. Both pp and tt depend on ww, so the chain rule gives two contributions. The total is dLdw=2(p−t)(x−x′)=2(−4)(−2)=16\frac{dL}{dw} = 2(p - t)(x - x') = 2(-4)(-2) = 16. Check: increasing ww increases the loss, because the function w2(1−3)2=4w2w^2(1-3)^2 = 4w^2 grows with ww. The derivative 8w=168w = 16 at w=2w = 2 agrees.
  3. Forward pass with stop-gradient on the target. Wrap the target: t=sg(wx′)t = \mathrm{sg}(w x'). The forward value is still 66, so the loss is still 1616. Check: the stop-gradient changes nothing the forward pass can see.
  4. Backward pass with stop-gradient. The target contributes no gradient, so only the prediction branch remains: dLdw=2(p−t) x=2(−4)(1)=−8\frac{dL}{dw} = 2(p - t)\,x = 2(-4)(1) = -8. Check: the sign is now negative, so raising ww lowers the loss, which is what we want from the prediction side.
  5. Update the weight. With the step rule of gradient descent and learning rate 0.10.1, w←2−0.1×(−8)=2.8w \leftarrow 2 - 0.1 \times (-8) = 2.8. Check: ww moved toward the target value, and the target itself was not allowed to move.
  6. Loss after the update. The prediction is 2.8×1=2.82.8 \times 1 = 2.8 and the frozen target is still 66. The loss is (2.8−6)2=10.24(2.8 - 6)^2 = 10.24, down from 1616. Check: the loss fell, and it fell because only the prediction moved.
flowchart LR
  X[input x] --> P[prediction branch] --> L[loss]
  Xp[other view x'] --> T[target branch] --> SG[stop-gradient] --> L
  L -. gradient .-> P
  SG -. blocked .-> T

The math

Step 1 in symbols: the forward value

Write the stop-gradient as a function sg(⋅)\mathrm{sg}(\cdot). Its forward value is the identity:

sg(u)=u.\mathrm{sg}(u) = u.

So the loss L=(p−sg(t))2L = (p - \mathrm{sg}(t))^2 has the same value as (p−t)2(p - t)^2. Nothing about the forward pass changes.

Step 2 in symbols: the full gradient

Without the stop-gradient, the chain rule gives

dLdw=2(p−t)(∂p∂w−∂t∂w).\frac{dL}{dw} = 2(p - t)\left(\frac{\partial p}{\partial w} - \frac{\partial t}{\partial w}\right).

With p=wxp = wx and t=wx′t = wx', the partial derivatives are ∂p/∂w=x\partial p / \partial w = x and ∂t/∂w=x′\partial t / \partial w = x'. Then 2(p−t)(x−x′)=2(−4)(1−3)=162(p - t)(x - x') = 2(-4)(1 - 3) = 16, matching the walkthrough.

Step 4 in symbols: the stopped gradient

With the stop-gradient, the derivative of sg(t)\mathrm{sg}(t) is zero, so

∂∂w sg(t)=0,dLdw=2(p−t) x=−8.\frac{\partial}{\partial w}\,\mathrm{sg}(t) = 0, \qquad \frac{dL}{dw} = 2(p - t)\,x = -8.

The one-line change removes the second term of the chain rule and keeps the first. This is the whole mechanism. Automatic-differentiation libraries implement it as a "detach" call that returns the same numbers with no recorded history, which is why the forward values are unchanged.

Step 5 in symbols: the update

Gradient descent then uses the stopped gradient:

w←w−η dLdw=2−0.1×(−8)=2.8.w \leftarrow w - \eta\, \frac{dL}{dw} = 2 - 0.1 \times (-8) = 2.8.

The symbol η\eta is the learning rate, a small positive number that sets the step size.

Why the trivial solution matters

If the two branches use the same input, x=x′x = x', the loss without stop-gradient is (wx−wx)2=0(wx - wx)^2 = 0 for every ww. The optimiser has no reason to change anything, and any constant representation achieves the minimum. With stop-gradient on one branch, the loss is (wx−sg(wx))2(wx - \mathrm{sg}(wx))^2, whose gradient with respect to ww is 2(wx−wx)x=02(wx - wx)x = 0 as well, so the stop-gradient alone does not prevent collapse here. Methods like BYOL combine the stop-gradient with a slowly moving target so that the target keeps changing in a way the prediction must chase.

Worked examples

Example
1. Tiny: one weight, one target

Use w=2w = 2, x=1x = 1, x′=3x' = 3, and loss L=(wx−sg(wx′))2L = (wx - \mathrm{sg}(wx'))^2.

  1. Prediction: p=2×1=2p = 2 \times 1 = 2.
  2. Target value: t=2×3=6t = 2 \times 3 = 6. It is wrapped in a stop-gradient.
  3. Loss: (2−6)2=16(2 - 6)^2 = 16.
  4. Gradient through the prediction only: 2×(2−6)×1=−82 \times (2 - 6) \times 1 = -8.
  5. The negative gradient means increasing ww reduces the loss.

Interpretation: the optimiser pushes the prediction toward the frozen target, and the target does not move in response.

Example
2. Standard: an exponential moving average target

Methods like BYOL keep a target weight ξ\xi that follows the online weight θ\theta through an exponential moving average with decay τ=0.99\tau = 0.99, one of the values BYOL's ablations test. The target is also wrapped in a stop-gradient. Suppose ξ=1.0\xi = 1.0 and θ=2.0\theta = 2.0 at the start of a step.

  1. The target update is ξ←τξ+(1−τ)θ\xi \leftarrow \tau \xi + (1 - \tau)\theta.
  2. Compute the first term: 0.99×1.0=0.990.99 \times 1.0 = 0.99.
  3. Compute the second term: 0.01×2.0=0.020.01 \times 2.0 = 0.02.
  4. Add them: ξ=0.99+0.02=1.01\xi = 0.99 + 0.02 = 1.01.
  5. The target moved from 1.01.0 to 1.011.01, a small step toward the online weight.
  6. The stop-gradient means the loss gradient never touches ξ\xi. Only the moving average changes it.

Interpretation: the target changes slowly and is never pulled directly by the loss, which is why the prediction cannot make the loss zero by dragging the target toward itself.

Example
3. Realistic: a training step with a moving target

A small image model has an online weight θ=3.0\theta = 3.0 and a target weight ξ=1.0\xi = 1.0, with decay τ=0.99\tau = 0.99. The prediction is p=θxp = \theta x with x=2x = 2, and the target output is t=sg(ξx′)t = \mathrm{sg}(\xi x') with x′=2x' = 2 for a different augmentation of the same image.

  1. Prediction: p=3.0×2=6.0p = 3.0 \times 2 = 6.0.
  2. Target output: t=1.0×2=2.0t = 1.0 \times 2 = 2.0, wrapped in a stop-gradient.
  3. Loss: (6.0−2.0)2=16.0(6.0 - 2.0)^2 = 16.0.
  4. Gradient with respect to θ\theta through the prediction only: 2×(6.0−2.0)×2=16.02 \times (6.0 - 2.0) \times 2 = 16.0. The gradient is positive, so the update lowers θ\theta.
  5. Learning rate 0.10.1: θ←3.0−0.1×16.0=1.4\theta \leftarrow 3.0 - 0.1 \times 16.0 = 1.4.
  6. Target moving average, with the online value now 1.41.4: ξ←0.99×1.0+0.01×1.4=0.99+0.014=1.004\xi \leftarrow 0.99 \times 1.0 + 0.01 \times 1.4 = 0.99 + 0.014 = 1.004.
  7. The target moved less than the online weight, which keeps the target stable while the prediction learns.

Interpretation: the stop-gradient and the slow target together turn the loss into a prediction problem with a moving but controlled goal.

Try it: the online weight is θ=3.0\theta = 3.0 and the target weight is ξ=1.01\xi = 1.01 with decay τ=0.99\tau = 0.99. Compute the new target after one update. Answer: 0.99×1.01+0.01×3.0=0.9999+0.03=1.02990.99 \times 1.01 + 0.01 \times 3.0 = 0.9999 + 0.03 = 1.0299.

Common mistakes and tradeoffs

It is tempting to think that a stop-gradient changes the value the model computes. In fact the forward value is identical. Only the gradient is different, so you can check a stop-gradient by comparing outputs with and without it.

It is tempting to think that a stop-gradient always prevents collapse. In fact it removes one route to a zero-gradient solution but does not guarantee good features. As the identical-input case in the math section showed, a stop-gradient on a shared-input branch can still leave a constant solution in place. Methods that use it pair it with a slowly moving target or a predictor network.

It is tempting to think that any copy of a value is a stop-gradient. In fact an ordinary copy in a differentiable framework keeps the link to the original, so gradients still flow back through it. Only a detach, which cuts that recorded link while keeping the number, gives the behaviour described here.

When to use it: one side of a loss should act as a fixed target for the current update, the other side should learn, and letting gradients through both sides leads to a trivial or unstable solution.

When not to use it: when both sides genuinely need to be optimised together, as in a symmetric distance where each argument is a free variable. Blocking a gradient that should exist silently biases training.

Alternatives: a fixed target network that is refreshed only at intervals, a contrastive loss with negative examples, or a target produced by a separate optimiser step.

History

Automatic differentiation made the stop-gradient easy to write, because the framework records which operations depend on which inputs. Before that, gradients were usually derived by hand and the question of which terms to drop was answered in the derivation itself.

In reinforcement learning, bootstrapped targets have been treated as fixed for each update for decades, because the target depends on the same estimates being learned. Deep reinforcement learning made the practice explicit when Deep Q-Networks copied their target network periodically to stabilise training.

Self-supervised vision methods then borrowed the idea. The VQ-VAE paper from 2017 used a stop-gradient in its codebook loss and defined the operator precisely. BYOL in 2020 showed that a stop-gradient on a target with a slowly moving average could learn image representations without negative examples. The idea caught on because the change is tiny and the effect on training stability is large.

Recap

  • A stop-gradient is the identity in the forward pass and has zero derivative in the backward pass.
  • Using it on one side of a loss makes that side a fixed target for the current update.
  • The gradient drops one of the two chain-rule terms, so the update in the example changed from +16+16 to −8-8.
  • It removes one path to a trivial solution, and methods like BYOL combine it with a moving target.
  • Read next: Self-Supervised Learning for the setting where targets are built from the data itself, and Exponential Moving Average for how a slow target is kept.

Check yourself

QuestionWhat is the value of sg(u) in the forward pass, and what is its derivative with respect to u?Show answer
In the forward pass sg(u) equals u, so the value is unchanged. Its derivative with respect to u is zero, so no gradient flows back through it.
QuestionWith w = 2, x = 1, x' = 3, and a stop-gradient on the target w x', what is the gradient of L = (wx - sg(wx'))^2 with respect to w?Show answer
The prediction term gives 2 times (wx - wx') times x = 2 times (2 - 6) times 1 = -8. The target term is removed. The answer is -8.
QuestionWhy does a stop-gradient alone not guarantee that a self-supervised model learns useful features?Show answer
Because the stop-gradient only removes one gradient path. When both branches see the same input, a constant output can still give zero loss, so the loss can be minimised without learning anything. Methods like BYOL add a slowly moving target and a predictor to keep the target changing and to avoid this trivial solution.
QuestionAn EMA target has decay 0.99. The target is 1.0 and the online weight is 4.0. What is the target after one update, and does the loss gradient change it?Show answer
The new target is 0.99 times 1.0 plus 0.01 times 4.0, which equals 0.99 + 0.04 = 1.03. The loss gradient does not change it, because the target is wrapped in a stop-gradient; only the moving-average rule updates it.

Sources