SECTION 03 / 4

Backpropagation — the Idea, and Gradients at the Output Layer

5 min read
On this page 7 headings
BUILD INTUITION WITH NUMBERS

See 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.

  1. Write the true class as a one-hot vector.

    y=[0,1]y=[0,1]
  2. For softmax with cross-entropy, the score gradient is prediction minus target.

    ∇aL=y^−y=[0.5,0.5]−[0,1]=[0.5,−0.5]\nabla_a L=\hat y-y=[0.5,0.5]-[0,1]=[0.5,-0.5]
  3. Take a learning step directly on these scores, with learning rate 0.2.

    anew=[0,0]−0.2[0.5,−0.5]=[−0.1,0.1]a_{new}=[0,0]-0.2[0.5,-0.5]=[-0.1,0.1]

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^−\hat{y} - (true label).

1. Focus on one weight

To learn W112W_{112} (one weight in the first layer) with gradient descent we need ∂L(θ)/∂W112\partial L(\theta )/\partial W_{112}. How does a weight deep inside the network affect the loss at the top?

2. Easy case: a deep but thin network

Imagine a network with one neuron per layer:x1→a11→h11→a21→h21→aL1→y^→L(θ).: x_{1} \to a_{11} \to h_{11} \to a_{21} \to h_{21} \to a_L1 \to \hat{y} \to L(\theta ).

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)\partial L/\partial W_{111} = (\partial L/\partial \hat{y}) \cdot (\partial \hat{y}/\partial a_L1) \cdot (\partial a_L1/\partial h_{21}) \cdot (\partial h_{21}/\partial a_{21}) \cdot (\partial a_{21}/\partial h_{11}) \cdot (\partial h_{11}/\partial a_{11}) \cdot (\partial a_{11}/\partial W_{111})

Compress it — each layer can reuse what the layer above already worked out:

  • ∂L/∂W111=(∂L/∂h11)⋅(∂h11/∂W111)\partial L/\partial W_{111} = (\partial L/\partial h_{11}) \cdot (\partial h_{11}/\partial W_{111})
  • ∂L/∂W211=(∂L/∂h21)⋅(∂h21/∂W211)\partial L/\partial W_{211} = (\partial L/\partial h_{21}) \cdot (\partial h_{21}/\partial W_{211})
  • ∂L/∂WL11=(∂L/∂aL1)⋅(∂aL1/∂WL11)\partial L/\partial W_L11 = (\partial L/\partial a_L1) \cdot (\partial a_L1/\partial W_L11)

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.

3. The blame story (intuition)

Blame flows downward through the layers — that is backpropagation.
Blame flows downward through the layers — that is backpropagation.
Open image ↗
The loss blames the output layer, which points to the weights and the hidden layer below, and so on down to the first layer
  1. We get a loss −log⁡y^-\log \hat{y}_ℓ at the output. Who is responsible?
  2. We tell the output layer: “You are not producing the right output — take responsibility.”
  3. 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)\hat{y} = O(W_L h_{L-1} + b_L).
  4. We ask WL,bLW_L, b_L and hLh_L what’s wrong. WLW_L and bLb_L take full responsibility. hLh_L says: “I’m only as good as my pre-activation layer.”
  5. The pre-activation says: “I’m only as good as the hidden layer and weights below me.”
  6. 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=\partial L/\partial W_{111} = ∂L/∂y^⋅∂y^/∂a3\partial L/\partial \hat{y} \cdot \partial \hat{y}/\partial a_{3} (talk to the output layer) · ∂a3/∂h2⋅∂h2/∂a2\partial a_{3}/\partial h_{2} \cdot \partial h_{2}/\partial a_{2} (previous hidden layer) · ∂a2/∂h1⋅∂h1/∂a1\partial a_{2}/\partial h_{1} \cdot \partial h_{1}/\partial a_{1} (previous hidden layer) · ∂a1/∂W111\partial a_{1}/\partial W_{111} (now talk to the weight)

Roadmap (with softmax output + cross-entropy loss):

  1. Gradient w.r.t. output units — this page.
  2. Gradient w.r.t. hidden units — next page.
  3. Gradient w.r.t. weights and biases — next page.

4. Gradient w.r.t. the output ŷ

Loss: L(θ)=−log⁡y^L(\theta ) = -\log \hat{y}_ℓ\ell (ℓ = true class).

∂L/∂y^i=∂(−log⁡y^ℓ)/∂y^i=\partial L/\partial \hat{y}_{i} = \partial (-\log \hat{y}_\ell )/\partial \hat{y}_{i} = −1/y^-1/\hat{y}_ℓ if i=ℓi = \ell , and 0 otherwise (the loss only contains y^\hat{y}_ℓ).

Compactly: ∂L/∂y^i=−\partial L/\partial \hat{y}_{i} = -𝟙(i=ℓ)/y^(i = \ell ) / \hat{y}_ℓ, where 𝟙(i=ℓ)(i = \ell ) is 1 if i=ℓi = \ell , else 0.

As a vector:

∇y^L(θ)=−(1/y^ℓ)⋅e(ℓ)\nabla \hat{y} L(\theta ) = -(1/\hat{y}_\ell ) \cdot e(\ell )

where e(ℓ) is a k-dimensional vector with 1 at position ℓ and 0 everywhere else (a one-hot vector).

5. Gradient w.r.t. the output pre-activation a_L (the key result)

What we really want is ∂L/∂aLi\partial L/\partial a_{\mathrm{Li}}. Does y^\hat{y}_ℓ depend on aLia_{\mathrm{Li}}? Yes — through softmax, every y^\hat{y} depends on every aLa_L:

y^ℓ=eaLℓ/∑i′eaLi′\hat{y}_\ell = e^{a_L\ell } / \sum _{i}' e^{a_{\mathrm{Li}}'}

Derivation:

∂(−log⁡y^ℓ)/∂aLi=−(1/y^ℓ)⋅∂y^ℓ/∂aLi\partial (-\log \hat{y}_\ell )/\partial a_{\mathrm{Li}} = -(1/\hat{y}_\ell ) \cdot \partial \hat{y}_\ell /\partial a_{\mathrm{Li}}

Use the quotient rule ∂/∂x[g/h]=(∂g/∂x)/h−g(∂h/∂x)/h2\partial /\partial x [g/h] = (\partial g/\partial x)/h - g(\partial h/\partial x)/h^{2}, with g=eaLℓg = e^{a_L\ell } and h=∑i′eaLi′h = \sum _{i}' e^{a_{\mathrm{Li}}'}:

  • ∂g/∂aLi=𝟙(ℓ=i)⋅eaLℓ\partial g/\partial a_{\mathrm{Li}} = 𝟙(\ell = i) \cdot e^{a_L\ell }
  • ∂h/∂aLi=eaLi\partial h/\partial a_{\mathrm{Li}} = e^{a_{\mathrm{Li}}}

So

∂y^ℓ/∂aLi=𝟙(ℓ=i)⋅eaLℓ/∑−(eaLℓ/∑)⋅(eaLi/∑)=𝟙(ℓ=i)⋅y^ℓ−y^ℓ⋅y^i\partial \hat{y}_\ell /\partial a_{\mathrm{Li}} = 𝟙(\ell = i) \cdot e^{a_L\ell }/\sum - (e^{a_L\ell }/\sum ) \cdot (e^{a_{\mathrm{Li}}}/\sum ) = 𝟙(\ell = i) \cdot \hat{y}_\ell - \hat{y}_\ell \cdot \hat{y}_{i}

Put it back:

∂L/∂aLi=−(1/y^ℓ)⋅(𝟙(ℓ=i)y^ℓ−y^ℓy^i)=\partial L/\partial a_{\mathrm{Li}} = -(1/\hat{y}_\ell ) \cdot (𝟙(\ell = i) \hat{y}_\ell - \hat{y}_\ell \hat{y}_{i}) = −(𝟙(ℓ=i)−y^i)-(𝟙(\ell = i) - \hat{y}_{i})

As a vector:

∇aLL(θ)=−(e(ℓ)−y^)=y^−e(ℓ)\nabla a_L L(\theta ) = -(e(\ell ) - \hat{y}) = \hat{y} - e(\ell )

Beautiful and simple: prediction minus the one-hot truth.

Lecture example — with numbers y^=[0.659,0.242,0.099]\hat{y} = [0.659, 0.242, 0.099], true class == apple (ℓ=1)(\ell = 1), so e(ℓ)=[1,0,0]e(\ell ) = [1, 0, 0]. ∇aLL=y^−e(ℓ)=[0.659−1,0.242−0,0.099−0]=\nabla a_L L = \hat{y} - e(\ell ) = [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\nabla a_L always add up to 0 (∑y^i=1\sum \hat{y}_{i} = 1 and ∑e(ℓ)i=1\sum e(\ell )_{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(ℓ)\hat{y} - e(\ell ).

Exam words: backpropagation, chain rule, deep thin network, gradient w.r.t. output units, one-hot vector e(ℓ), indicator 𝟙(i=ℓ)(i = \ell ), quotient rule, ∇aLL=−(e(ℓ)−y^)\nabla a_L L = -(e(\ell ) - \hat{y}).

Quick check

  1. y^=[0.2,0.7,0.1]\hat{y} = [0.2, 0.7, 0.1], true class 2. Find ∇y^L\nabla \hat{y} L.
Only position 2 is non-zero: −1/0.7=−1.429-1/0.7 = -1.429. So∇y^L=[0,−1.429,0]So \nabla \hat{y} L = [0, -1.429, 0].
  1. Same y^\hat{y}, true class 2. Find ∇aLL\nabla a_L L.
y^−e(2)=[0.2,0.7−1,0.1]=[0.2,−0.3,0.1]. \hat{y} - e(2) = [0.2, 0.7 - 1, 0.1] = [0.2, -0.3, 0.1].
  1. Why is ∂L/∂y^i=0\partial L/\partial \hat{y}_{i} = 0 for i≠ℓi \ne \ell , but ∂L/∂aLi≠0\partial L/\partial a_{\mathrm{Li}} \ne 0?
The loss contains only y^\hat{y}_ℓ, so other y^idon\hat{y}_{i} don’t appear directly. But softmax makes y^\hat{y}_ℓ depend on every aLia_{\mathrm{Li}} (through the denominator), so every aLia_{\mathrm{Li}} affects the loss.
  1. If the network predicts the true class with probability 1, what is ∇aLL\nabla a_L L?
y^=e(ℓ)\hat{y} = e(\ell ), so y^−e(ℓ)=0\hat{y} - e(\ell ) = 0. No gradient — nothing to fix.
  1. In the blame story, what does hlsayh_{l} say 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.
YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑