BUILD INTUITION WITH NUMBERSSee what an output gradient asks us to change
Two softmax scores start at [0, 0], giving probabilities [0.5, 0.5]. The second class is correct. Use cross-entropy for one example.
Write the true class as a one-hot vector.
For softmax with cross-entropy, the score gradient is prediction minus target.
Take a learning step directly on these scores, with learning rate 0.2.
What changed? The first score falls and the correct class’s score rises. In a network, backpropagation passes this signal to the weights that created the scores.
Quick check: Is the second class now more or less likely?
More likely: softmax([-0.1, 0.1]) gives its probability about 0.5498.
Swipe long formulas, tables or code sideways to see the rest.
Start here The network made a mistake. Who
is to blame? Backpropagation answers this by passing the blame
backwards, layer by layer, using the chain rule. This page builds the
intuition and then derives the first and most famous result: at a
softmax + cross-entropy output, the gradient is simply y^− (true label).
To learn W112 (one weight in the first layer) with gradient descent we need ∂L(θ)/∂W112. How does a weight deep inside the
network affect the loss at the top?
Imagine a network with one neuron per layer:x1→a11→h11→a21→h21→aL1→y^→L(θ).
The chain rule gives the answer as one long
product:
∂L/∂W111=(∂L/∂y^)⋅(∂y^/∂aL1)⋅(∂aL1/∂h21)⋅(∂h21/∂a21)⋅(∂a21/∂h11)⋅(∂h11/∂a11)⋅(∂a11/∂W111)
Compress it — each layer can reuse what the layer
above already worked out:
- ∂L/∂W111=(∂L/∂h11)⋅(∂h11/∂W111)
- ∂L/∂W211=(∂L/∂h21)⋅(∂h21/∂W211)
- ∂L/∂WL11=(∂L/∂aL1)⋅(∂aL1/∂WL11)
Everyday example In a relay race, the time lost by
the first runner reaches the finish line through every later runner. To
find the first runner’s share, you go backwards from the finish, one
runner at a time.
- We get a loss −logy^_ℓ at the output. Who is responsible?
- We tell the output layer: “You are not producing
the right output — take responsibility.”
- The output layer says: “I take my share, but I am only as good as
the hidden layer and weights below me” — after all, y^=O(WLhL−1+bL).
- We ask WL,bL and hL what’s wrong. WL and bL take full responsibility. hL says: “I’m only as good as my pre-activation layer.”
- The pre-activation says: “I’m only as good as the hidden layer and
weights below me.”
- Continue down… the responsibility lies with all the
weights and biases.
But instead of talking to each weight directly, it
is easier to talk to them through the hidden and output
layers — and that is exactly what the chain rule does:
∂L/∂W111=∂L/∂y^⋅∂y^/∂a3 (talk to the output layer)
· ∂a3/∂h2⋅∂h2/∂a2 (previous hidden layer) ·
∂a2/∂h1⋅∂h1/∂a1 (previous hidden layer) ·
∂a1/∂W111 (now talk to the weight)
Roadmap (with softmax output + cross-entropy
loss):
- Gradient w.r.t. output units — this page.
- Gradient w.r.t. hidden units — next page.
- Gradient w.r.t. weights and biases — next
page.
Loss: L(θ)=−logy^_ℓ (ℓ = true class).
∂L/∂y^i=∂(−logy^ℓ)/∂y^i=−1/y^_ℓ if i=ℓ, and
0 otherwise (the loss only contains y^_ℓ).
Compactly: ∂L/∂y^i=−𝟙(i=ℓ)/y^_ℓ, where 𝟙(i=ℓ) is 1 if i=ℓ, else 0.
∇y^L(θ)=−(1/y^ℓ)⋅e(ℓ)
where e(ℓ) is a k-dimensional vector with 1
at position ℓ and 0 everywhere else (a one-hot vector).
What we really want is ∂L/∂aLi. Does y^_ℓ depend on aLi? Yes — through softmax, every y^ depends on every aL:
y^ℓ=eaLℓ/∑i′eaLi′
∂(−logy^ℓ)/∂aLi=−(1/y^ℓ)⋅∂y^ℓ/∂aLi
Use the quotient rule ∂/∂x[g/h]=(∂g/∂x)/h−g(∂h/∂x)/h2, with g=eaLℓ and h=∑i′eaLi′:
- ∂g/∂aLi=𝟙(ℓ=i)⋅eaLℓ
- ∂h/∂aLi=eaLi
∂y^ℓ/∂aLi=𝟙(ℓ=i)⋅eaLℓ/∑−(eaLℓ/∑)⋅(eaLi/∑)=𝟙(ℓ=i)⋅y^ℓ−y^ℓ⋅y^i
∂L/∂aLi=−(1/y^ℓ)⋅(𝟙(ℓ=i)y^ℓ−y^ℓy^i)=−(𝟙(ℓ=i)−y^i)
∇aLL(θ)=−(e(ℓ)−y^)=y^−e(ℓ)
Beautiful and simple: prediction minus the one-hot
truth.
Lecture example — with numbers y^=[0.659,0.242,0.099], true class = apple (ℓ=1), so e(ℓ)=[1,0,0]. ∇aLL=y^−e(ℓ)=[0.659−1,0.242−0,0.099−0]= [−0.341, 0.242,
0.099]. Meaning: increase the apple score (negative gradient →
gradient descent pushes it up), decrease the others.
Note: the entries of ∇aL always add up to 0 (∑y^i=1 and ∑e(ℓ)i=1).
In one line: Backpropagation passes blame from the loss down through the layers with the chain rule; at a softmax + cross-entropy output the gradient w.r.t. the pre-activation is just y^−e(ℓ).
Exam words: backpropagation, chain rule, deep thin network, gradient w.r.t. output units, one-hot vector e(ℓ), indicator 𝟙(i=ℓ), quotient rule, ∇aLL=−(e(ℓ)−y^).
- y^=[0.2,0.7,0.1], true class 2. Find ∇y^L.
Only position 2 is non-zero: −1/0.7=−1.429. So∇y^L=[0,−1.429,0].
- Same y^, true class 2. Find ∇aLL.
y^−e(2)=[0.2,0.7−1,0.1]=[0.2,−0.3,0.1].
- Why is ∂L/∂y^i=0 for i=ℓ, but ∂L/∂aLi=0?
The loss contains only y^_ℓ, so other y^idon’t appear directly. But softmax makes y^_ℓ depend on every aLi (through the denominator), so every aLi affects the loss.
- If the network predicts the true class with probability 1, what is ∇aLL?
y^=e(ℓ), so y^−e(ℓ)=0. No gradient — nothing to fix.
- In the blame story, what does hlsay when asked what is wrong?
“I am only as good as my pre-activation layer” — blame passes from h to
a, then to the weights and hidden layer below.