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 and two inputs, and . The model's prediction is and its target is . The loss is . Start with .
- Forward pass with no stop-gradient. and . The loss is . Check: the loss is positive because the prediction and the target disagree.
- Backward pass with no stop-gradient. Both and depend on , so the chain rule gives two contributions. The total is . Check: increasing increases the loss, because the function grows with . The derivative at agrees.
- Forward pass with stop-gradient on the target. Wrap the target: . The forward value is still , so the loss is still . Check: the stop-gradient changes nothing the forward pass can see.
- Backward pass with stop-gradient. The target contributes no gradient, so only the prediction branch remains: . Check: the sign is now negative, so raising lowers the loss, which is what we want from the prediction side.
- Update the weight. With the step rule of gradient descent and learning rate , . Check: moved toward the target value, and the target itself was not allowed to move.
- Loss after the update. The prediction is and the frozen target is still . The loss is , down from . 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 . Its forward value is the identity:
So the loss has the same value as . Nothing about the forward pass changes.
Step 2 in symbols: the full gradient
Without the stop-gradient, the chain rule gives
With and , the partial derivatives are and . Then , matching the walkthrough.
Step 4 in symbols: the stopped gradient
With the stop-gradient, the derivative of is zero, so
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:
The symbol 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, , the loss without stop-gradient is for every . The optimiser has no reason to change anything, and any constant representation achieves the minimum. With stop-gradient on one branch, the loss is , whose gradient with respect to is 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
Use , , , and loss .
- Prediction: .
- Target value: . It is wrapped in a stop-gradient.
- Loss: .
- Gradient through the prediction only: .
- The negative gradient means increasing reduces the loss.
Interpretation: the optimiser pushes the prediction toward the frozen target, and the target does not move in response.
Methods like BYOL keep a target weight that follows the online weight through an exponential moving average with decay , one of the values BYOL's ablations test. The target is also wrapped in a stop-gradient. Suppose and at the start of a step.
- The target update is .
- Compute the first term: .
- Compute the second term: .
- Add them: .
- The target moved from to , a small step toward the online weight.
- The stop-gradient means the loss gradient never touches . 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.
A small image model has an online weight and a target weight , with decay . The prediction is with , and the target output is with for a different augmentation of the same image.
- Prediction: .
- Target output: , wrapped in a stop-gradient.
- Loss: .
- Gradient with respect to through the prediction only: . The gradient is positive, so the update lowers .
- Learning rate : .
- Target moving average, with the online value now : .
- 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 and the target weight is with decay . Compute the new target after one update. Answer: .
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 to .
- 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
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
QuestionWhy does a stop-gradient alone not guarantee that a self-supervised model learns useful features?Show answer
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
Sources
- van den Oord, Vinyals, Kavukcuoglu, Neural Discrete Representation Learning, arXiv:1711.00937, https://arxiv.org/abs/1711.00937
- Grill et al., Bootstrap your own latent: A new approach to self-supervised Learning, arXiv:2006.07733, https://arxiv.org/abs/2006.07733
- Automatic differentiation (Wikipedia), https://en.wikipedia.org/wiki/Automatic_differentiation