The Gradient at the Output Layer

Derive the starting point of backpropagation for softmax with cross-entropy: the gradient is simply the prediction minus the target.

Deep Learning- Fundamentals to Advanced Concepts

Backpropagation needs a place to start, the gradient of the loss with respect to the last pre-activation aLa_L. For classification we use softmax with cross-entropy, and the result is unusually clean.

Setup

Let a=aLa = a_L be the vector of KK numbers before the output function. Then

y^j=eaj∑keak,L=−log⁡y^ℓ\hat y_j = \frac{e^{a_j}}{\sum_{k} e^{a_k}}, \qquad L = -\log \hat y_\ell

where ℓ\ell is the index of the true class. We want ∂L∂aj\dfrac{\partial L}{\partial a_j} for every jj. The chain passes through y^\hat y, so we proceed in two steps.

Step 1: the loss with respect to the prediction

The loss depends on y^\hat y only through the entry y^ℓ\hat y_\ell, so

∂L∂y^i={−1y^ℓi=ℓ0i≠ℓ\frac{\partial L}{\partial \hat y_i} = \begin{cases} -\dfrac{1}{\hat y_\ell} & i = \ell \\[6pt] 0 & i \ne \ell \end{cases}

Step 2: the softmax derivative

Each output y^i\hat y_i depends on all the entries of aa, because every aka_k appears in the denominator. Differentiating gives

∂y^i∂aj=y^i (1[i=j]−y^j)\frac{\partial \hat y_i}{\partial a_j} = \hat y_i\,\bigl(\mathbb{1}[i = j] - \hat y_j\bigr)

where 1[i=j]\mathbb{1}[i = j] is 1 if i=ji = j and 0 otherwise. In words:

  • for i=ji = j: y^i(1−y^i)\hat y_i(1 - \hat y_i),
  • for i≠ji \ne j: −y^i y^j-\hat y_i\,\hat y_j.

A quick derivation of the first case. Write S=∑keakS = \sum_k e^{a_k}, so y^i=eai/S\hat y_i = e^{a_i}/S. Then ∂y^i/∂ai=eai/S−eaieai/S2=y^i−y^i2\partial \hat y_i/\partial a_i = e^{a_i}/S - e^{a_i}e^{a_i}/S^2 = \hat y_i - \hat y_i^2. For i≠ji \ne j the numerator does not depend on aja_j, so only the denominator contributes, giving −eaieaj/S2=−y^iy^j-e^{a_i}e^{a_j}/S^2 = -\hat y_i \hat y_j.

Combine with the chain rule

The loss reaches aja_j through every y^i\hat y_i:

∂L∂aj=∑i∂L∂y^i ∂y^i∂aj\frac{\partial L}{\partial a_j} = \sum_i \frac{\partial L}{\partial \hat y_i}\,\frac{\partial \hat y_i}{\partial a_j}

Only the term i=ℓi = \ell survives, because every other ∂L/∂y^i\partial L/\partial \hat y_i is 0:

∂L∂aj=−1y^ℓ⋅y^ℓ (1[ℓ=j]−y^j)=y^j−1[ℓ=j]\frac{\partial L}{\partial a_j} = -\frac{1}{\hat y_\ell}\cdot \hat y_\ell\,\bigl(\mathbb{1}[\ell = j] - \hat y_j\bigr) = \hat y_j - \mathbb{1}[\ell = j]

The y^ℓ\hat y_\ell factors cancel, leaving a very clean result:

  ∇aLL=y^−eℓ  \boxed{\;\nabla_{a_L} L = \hat y - e_\ell\;}

where eℓe_\ell is the one-hot vector for the true class (1 in position ℓ\ell, 0 elsewhere). The gradient is simply the prediction minus the target.

What it means

  • For a wrong class jj, the gradient is y^j>0\hat y_j > 0. Gradient descent subtracts it, pushing that class's score down, more strongly the more probability the network wrongly gave it.
  • For the true class, the gradient is y^ℓ−1<0\hat y_\ell - 1 < 0. Subtracting it pushes the score up.
  • The entries always sum to 0 (the predictions sum to 1 and so does the one-hot vector), so the update moves probability between classes and does not create it.

Example. For the scores a=(−1,1,2,3)a = (-1, 1, 2, 3) we found y^=(0.012,0.089,0.242,0.657)\hat y = (0.012, 0.089, 0.242, 0.657). With the true class being the third, the gradient is

(0.012,  0.089,  0.242−1,  0.657)=(0.012,  0.089,  −0.758,  0.657)(0.012,\; 0.089,\; 0.242 - 1,\; 0.657) = (0.012,\; 0.089,\; -0.758,\; 0.657)

The fourth class has the highest predicted probability but is wrong, so it receives the strongest push down. The true class is pushed up.

The worked network from earlier. There y^=(0.376,0.624)\hat y = (0.376, 0.624) and the true class is 2, so ∇a2L=(0.376,  0.624−1)=(0.376,  −0.376)\nabla_{a_2} L = (0.376,\; 0.624 - 1) = (0.376,\; -0.376).

Regression gives the same shape

For regression with a linear output, y^=a\hat y = a, and squared error L=12∑j(y^j−yj)2L = \tfrac12\sum_j(\hat y_j - y_j)^2, we get

∂L∂aj=y^j−yj\frac{\partial L}{\partial a_j} = \hat y_j - y_j

It is again "prediction minus target". That is part of why softmax with cross-entropy and linear output with squared error are such natural pairings: the gradient at the output takes the same simple form in both.

EasySoftmaxGradient

The network outputs (0.7, 0.2, 0.1) and the true class is the first. What is the gradient with respect to a_L?

MediumSoftmaxGradient

Why do the entries of the softmax-cross-entropy gradient always sum to zero?

HardSoftmaxChain rule

Why do the y-hat-l factors cancel when combining Step 1 and Step 2?