TL;DR
Many useful network operations are hard decisions: a threshold, a rounding step, a sampled category. Their true derivative is zero almost everywhere, so backpropagation learns nothing through them. The straight-through estimator keeps the hard operation in the forward pass, then in the backward pass treats it as if it were the identity, so the gradient reaches the inputs anyway.
Where you'll see it: training binarized and low-precision networks, the discrete codebook lookup in VQ-VAE, and sampling categorical latent variables in world models.
Intuition
A gate with a broken speedometer
Think of a light switch that reads the room brightness and turns on above some level. To tune the threshold you need to know whether nudging the brightness reading would change the output. For a switch, it almost never does: nudge the reading by a little and the light is still on or still off. The only moment anything changes is at the exact threshold, where the output jumps. Calculus sees a flat floor with a cliff at one point, and the flat part tells the learner nothing.
The straight-through estimator takes a shortcut. It runs the real switch on the forward pass, so the network does the real thing. On the backward pass it says: "pretend the switch was a wire." The error signal coming back from the output is handed straight through to the input, scaled by one. The learner gets a usable direction even though the true slope is useless.
The name comes from that picture. The gradient goes straight through the non-differentiable block as if it were not there.
Why we need it
Backpropagation is the Chain Rule applied backwards through a computation graph (see Backpropagation). Every link in the chain has to have a derivative. Three common operations break this:
- a hard threshold, which outputs 1 above zero and 0 below;
- rounding a real number to the nearest integer or to a low-precision grid;
- drawing a sample from a categorical distribution, because the sample is an integer.
Each of these is piecewise constant. Wherever the derivative exists it is zero, and at the jumps it does not exist at all. Without a workaround, the gradient reaching anything upstream of the operation is zero, and training stops. The straight-through estimator is the workaround most often used because it is a one-line change in the backward pass and it works surprisingly well.
The math
For a real input , the hard threshold returns if and otherwise. Its output is , and the loss depends on only through .
The chain rule and where it breaks
Write the loss as a composition . The chain rule gives
The factor is for every , because is flat there. At it is undefined, since jumps. So the gradient is almost everywhere, and the product with is always .
The estimator
The straight-through estimator replaces the undefined factor with a surrogate and keeps the rest of the chain rule:
The forward pass still computes . Only the backward pass changes. Three common choices of are:
- Identity: . The gradient passes through unchanged. This is the classic form.
- Clipped identity: , where is when the condition holds and otherwise. Inputs far outside the range stop receiving gradient, which keeps saturated units from drifting. Hubara et al. use this form for binarized activations.
- Sigmoid derivative: , where is the Sigmoid Function. Bengio et al. (2013) tried this as a variant for stochastic binary units, where the forward pass samples a bit from a sigmoid probability, and reported better results without multiplying by the sigmoid derivative than with it.
- forward: sign(x)
- STE backward: g = 1
- true derivative (0 away from 0)
Stochastic and categorical units
The same idea applies when the forward pass draws a random sample. Suppose is a Bernoulli sample with probability . The sampling step has no useful derivative with respect to . The plain estimator copies the gradient with respect to onto the pre-sigmoid input , . The variant above instead multiplies by the sigmoid slope, . For a categorical latent, the same rule is applied per category to the softmax probabilities, which is how the sampled categorical latents in some world models are trained.
Worked examples
Setup: , the target is , the loss is , the learning rate is , and the surrogate is .
- Forward: , so .
- True gradient: . Then at , so . The weight does not move.
- STE gradient: .
- Update: .
- Repeat. At the output is still , so and the STE gradient is again. Update: .
- At the threshold has flipped: , so and . The gradient , so the STE gradient is and learning stops.
Interpretation: the estimator moved the threshold across the decision boundary in two steps, which is the behaviour we wanted. It also shows the stopping condition: once the loss is zero, the signal is gone.
Setup: the forward activation is , which is or . The backward surrogate is the clipped identity . Two units have pre-activations and . The upstream gradient is for both.
- Unit 1: , so . The gradient is .
- Unit 2: , so . The gradient is .
- Interpretation: unit 2 is already far past the threshold. A small change in its pre-activation would not change its sign, so the estimator refuses to push it further. Unit 1 sits near the boundary, where the sign can still flip, and it receives the full signal.
This is why the clipped form is preferred for binarized networks: without it, units can drift into a range where their gradients are large but the output never changes.
This script runs the first example. It was executed for this lesson, and the output below is its real output.
def step(x):
return 1.0 if x > 0 else 0.0
def train(x, lr=0.1, target=0.0, steps=4, use_ste=True):
for t in range(steps):
h = step(x)
loss = (h - target) ** 2
dL_dh = 2 * (h - target)
dh_dx = 1.0 if use_ste else 0.0
grad = dL_dh * dh_dx
print(f"step {t}: x={x:.2f} h={h:.0f} loss={loss:.2f} dL/dh={dL_dh:.2f} dL/dx={grad:.2f}")
x = x - lr * grad
return x
print("with STE")
train(0.4)
print("true gradient")
train(0.4, use_ste=False)Output:
with STE
step 0: x=0.40 h=1 loss=1.00 dL/dh=2.00 dL/dx=2.00
step 1: x=0.20 h=1 loss=1.00 dL/dh=2.00 dL/dx=2.00
step 2: x=0.00 h=0 loss=0.00 dL/dh=0.00 dL/dx=0.00
step 3: x=0.00 h=0 loss=0.00 dL/dh=0.00 dL/dx=0.00
true gradient
step 0: x=0.40 h=1 loss=1.00 dL/dh=2.00 dL/dx=0.00
step 1: x=0.40 h=1 loss=1.00 dL/dh=2.00 dL/dx=0.00
step 2: x=0.40 h=1 loss=1.00 dL/dh=2.00 dL/dx=0.00
step 3: x=0.40 h=1 loss=1.00 dL/dh=2.00 dL/dx=0.00
The true-gradient run never moves, while the STE run crosses the threshold in two steps.
Tradeoffs and failure modes
The estimator is a cheap and often effective fix, but it is a heuristic. The table lists the situations where it helps and where it goes wrong.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Binary or low-bit weights and activations | Surrogate gradient ignores saturation, so units drift far from the boundary | Clipped identity, or a learned scale |
| Discrete latent codes (for example a codebook lookup) | Gradient for the codebook itself is missing, since the lookup is not reached by the STE | Add a separate codebook loss, as in VQ-VAE |
| Sampling from a categorical with many categories | High variance across samples, slow learning | Gumbel-softmax relaxation with a temperature, or a score-function estimator |
| A gradient that must be exact (for example, for a variational bound) | The estimate is biased, so the optimizer may converge to the wrong place | Reparameterization where the distribution allows it (see Reparameterization Trick), or a score-function estimator (see Score Function) |
| Rounding during quantization-aware training | Gradient ignores rounding error, so the model can settle where the rounded weights are poor | Keep a full-precision copy and round only in the forward pass, or fine-tune after rounding |
A practical warning: the STE does not compute a gradient of the loss, so a gradient check against finite differences will fail. The test should compare the STE against the surrogate network it actually implements, not against the true derivative of the hard model.
Another failure mode is the learning-rate interaction. Because the estimator moves inputs whose output does not change, a parameter can drift a long way with no effect on the loss. Gradient clipping and careful initialisation of pre-activations help keep the drift bounded.
History
The idea was proposed by Geoffrey Hinton in a 2012 lecture (lecture 15b of his neural networks course, as cited by Bengio et al.). Yoshua Bengio, Nicholas Léonard and Aaron Courville named it the straight-through estimator in 2013 and compared it with other estimators for stochastic binary neurons. They found it simple to implement and, in their small experiment, it gave the best validation and test error. Their motivation was conditional computation: they wanted networks that switch parts of themselves on and off, and a gate that must make a hard decision needs some gradient to learn from.
Hubara, Courbariaux and colleagues used it in 2016 to train networks whose weights and activations were binary, which makes the forward pass cheap but rules out exact gradients. Their version is the clipped form, which accounts for saturation. Van den Oord, Vinyals and Kavukcuoglu used the estimator in the VQ-VAE paper in 2017, where the nearest-codebook lookup has no derivative, and they copy the decoder's gradient to the encoder output.
Check yourself
QuestionA hard threshold H(x) outputs 1 when x is positive and 0 otherwise. What is its true derivative at x = 2, and why can't backpropagation use it to learn?Show answer
The true derivative is 0. H is constant on each side of zero, so a small change in x leaves the output unchanged, and the chain rule multiplies the upstream gradient by 0. Learning stops because nothing upstream of the threshold receives a usable signal. The straight-through estimator replaces the 0 with a surrogate, usually 1.
QuestionA unit has pre-activation x = 0.5, upstream gradient dL/dh = -0.6, and the clipped surrogate g(x) = 1 if |x| is at most 1, else 0. What gradient reaches x?Show answer
Since |0.5| is at most 1, g(0.5) = 1. The gradient is -0.6 times 1, which is -0.6. The clipped form lets the full signal through here, because the unit is close enough to the threshold for a small change to flip its sign.
QuestionWhy is the straight-through gradient not the gradient of the loss, and why does training still work?Show answer
The forward pass uses the hard step, but the backward pass uses a surrogate, usually the identity. So the estimate is the exact gradient of a different network, one with the step replaced by a wire. It is biased for inputs where the wire and the step disagree. It still works in practice because the direction it gives is usually correct, and the forward pass always reflects the real discrete model, so the loss we measure is the real one.
QuestionA categorical latent is sampled from a softmax with probabilities (0.2, 0.5, 0.3) and the sampled category is the second one. Under a sigmoid-style straight-through rule applied per category, what is the surrogate derivative of the sampled probability p = 0.5 with respect to its logit, using p(1 minus p)?Show answer
The derivative is p(1 - p) = 0.5 times 0.5, which is 0.25. The sampled category contributes its gradient scaled by 0.25. The rule uses the probability of the drawn category as if the draw were a smooth function of that probability, which is the sigmoid-derivative variant described in the math section.
QuestionName one situation where you would prefer the score-function estimator to the straight-through estimator, and say why.Show answer
When an unbiased gradient is needed, for example inside a variational bound where a biased gradient can move the optimizer to the wrong solution. The score-function estimator is unbiased but tends to have high variance, so the choice is between an exact-in-expectation but noisy estimate and a biased but low-variance one. The reparameterization trick is the other option when the distribution allows it.
Sources
- Bengio, Léonard, Courville (2013), Estimating or Propagating Gradients Through Stochastic Neurons for Conditional Computation: https://arxiv.org/abs/1308.3432
- Hubara, Courbariaux, Soudry, El-Yaniv, Bengio (2016), Binarized Neural Networks: https://arxiv.org/abs/1602.02830
- van den Oord, Vinyals, Kavukcuoglu (2017), Neural Discrete Representation Learning: https://arxiv.org/abs/1711.00937