Welcome back. In our previous lesson, we derived the initial gradients for backpropagation in both regression and classification settings. We discovered the remarkably elegant result that for a softmax activation paired with cross-entropy loss, the gradient of the loss with respect to the pre-activation logits is simply . We arrived at this conclusion by using the individual components of the softmax Jacobian without deriving the full matrix itself.
Today, we will deliver on the promise to build that Jacobian from first principles. This lesson is dedicated to formally deriving the Jacobian of the softmax function and then using it to re-trace the steps that lead to the simplification. This will provide a complete, foundational understanding of one of the most critical gradient calculations in deep learning for classification.
1. The Jacobian of the Softmax Function
Let's begin by recalling the softmax function, which maps a vector of logits to a vector of probabilities :
Since softmax is a vector-valued function of a vector input, its derivative is a Jacobian matrix, , where each entry is the partial derivative .
To compute these entries, we must consider two distinct cases:
- Diagonal elements (): The derivative of a softmax output with respect to its corresponding logit.
- Off-diagonal elements (): The derivative of a softmax output with respect to a different logit.
The following article provides an exceptionally clear, step-by-step derivation for both cases using the quotient rule.
The Softmax function and its derivative - Eli Bendersky's website
This section from Eli Bendersky's article will guide us through the core calculus needed to build the Jacobian matrix. It's a classic first-principles derivation.
Please read the section titled Derivative of softmax. Pay close attention to how the quotient rule is applied differently for the case i=j versus i \neq j. This distinction is the key to understanding the structure of the resulting Jacobian.
Let's summarize the results from the reading. The partial derivatives that form the entries of the softmax Jacobian are:
Notice how the derivative of a softmax output always depends on the outputs themselves, not the inputs. This self-referential nature is common in functions involving exponentials.
- When , the derivative is positive. Increasing a logit will always increase its corresponding probability .
- When , the derivative is negative. Increasing a logit will increase , and to keep the total probability sum at 1, all other probabilities must decrease.
2. A Compact Matrix Representation
While the piecewise formula is correct, expressing the Jacobian in a single matrix equation can provide deeper insight. Given your background, you'll likely appreciate this more compact and algebraic view. We can unify the two cases using the Kronecker delta, (which is 1 if and 0 otherwise):
You can verify that this correctly collapses to when and to when .
This component-wise definition leads to a very elegant matrix representation. The entire Jacobian matrix can be written as:
Here:
- is the column vector of softmax outputs.
- is a diagonal matrix with the elements of on its main diagonal. This matrix captures all the terms, but only on the diagonal, since . To see this, expand . The first term corresponds to .
- is the outer product of the softmax output vector with itself. This produces a matrix where the entry at is . This captures the second term, .
This form, , is not only concise but also reveals that the softmax Jacobian is a symmetric matrix, since both and are symmetric. The following resource provides an excellent derivation that arrives at this matrix form directly.
This article by Matt Petersen takes a matrix-calculus approach and is a great companion to the previous reading. Note that it assumes row vectors for gradients, so the final matrix-vector products might appear transposed compared to our column-vector convention, but the logic and the final Jacobian structure are identical.
Focus on the section Jacobian of softmax. It shows the derivation of the diagonal and off-diagonal entries and then assembles them into the full Jacobian, explicitly showing the \text{diag}(\mathbf{s}) - \mathbf{s}\mathbf{s}^T form.
3. Re-deriving the Combined Gradient
Now we have all the machinery needed to formally prove the simplification we used in the last lesson. Our goal is to compute for the cross-entropy loss .
By the chain rule (using numerator layout, where gradients are column vectors):
Since is symmetric, , so we have:
Let's compute the two parts and then their product.
1. Gradient of Loss w.r.t. Softmax Outputs ()
The partial derivative of the loss with respect to a single softmax output is:
The gradient vector is therefore the column vector with entries .
2. The Matrix-Vector Product
Now we multiply the Jacobian by this gradient vector:
Let's evaluate each term:
-
First term: . This is a diagonal matrix multiplying a vector. The -th element of the resulting vector is the product of the -th diagonal element of the matrix () and the -th element of the vector ().
So, the first term simplifies to the vector .
-
Second term: . We can re-group this using associativity of matrix multiplication: . The term in the parentheses is a scalar dot product:
Since is a one-hot vector representing the true class, the sum of its elements is exactly 1. So the dot product is -1.
The second term thus becomes .
Combining the two terms, we get our final result:
Or using for the predicted probabilities , we have . This completes the formal derivation. The intricate dependencies within the softmax Jacobian perfectly cancel with the logarithmic derivatives from the cross-entropy loss, leaving a simple, intuitive error signal.
For a final reinforcement of these steps, the video below walks through the same derivation by combining the derivative terms element by element.
NN - 10 - Cross Entropy and Softmax - Derivatives
This video from Meerkat Statistics provides a clear, narrated walkthrough of the full derivation.
First, watch the derivation of the softmax derivative, which covers the same diagonal and off-diagonal cases we saw earlier. Next, see the derivation of the cross-entropy gradient. Finally, watch how they are put together to arrive at the final simplified result.
Conclusion
In this lesson, we moved from using a result to deriving it from scratch. We have now rigorously established the mathematical underpinnings of the gradient for the most common classification output layer.
Key Takeaways:
- The Jacobian of the softmax function has a specific structure: positive on the diagonal () and negative off the diagonal ().
- This structure can be expressed compactly in matrix form as , revealing its symmetry.
- The application of the chain rule to the composition of softmax and cross-entropy loss results in a cascade of cancellations, yielding the simple and computationally efficient gradient . This is a beautiful example of how mathematical components are designed to fit together elegantly in deep learning.
In our upcoming lessons, we will continue to build our matrix calculus toolkit. Our next topic will be deriving the derivatives for several critical matrix operations, such as those involving determinants and quadratic forms. These identities are not just theoretical curiosities; they are the essential tools needed to derive the objective functions for advanced generative models like Variational Autoencoders and Normalizing Flows, bringing us another step closer to implementing those papers.