derivative
softmax
backpropagation
calculus
neural networks

how can i take the derivative of the softmax output in back-prop

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In the context of neural networks, the softmax function is widely used in the output layer of classification models. It converts raw scores from the network into probabilities, allowing us to interpret them meaningfully as class probabilities. When implementing backpropagation, understanding how to derive the gradient of the softmax function with respect to its inputs is crucial. This article delves into the mathematical underpinnings of taking the derivative of the softmax output and provides practical context for its use.

The Softmax Function

The softmax function is defined as follows for a vector of scores z=[z1,z2,...,zn]\mathbf{z} = [z_1, z_2, ..., z_n]:

σ(z)i=ez_ij=1nez_j\sigma(\mathbf{z})*i = \frac{e^{z\_i}}{\sum*{j=1}^{n} e^{z\_j}}

where ii denotes the index of a particular output.

The softmax function maps the input vector to a probability distribution, where the sum of all probabilities is 1.

Derivative of the Softmax

To compute the derivative of the softmax output with respect to its inputs, we examine how a small change in one input zkz_k affects the output σ(z)iσ(z)_i.

Derivative for the Same Index

For the derivative with respect to the same index ii, we compute:

σ(z)_iz_i=σ(z)_i(1σ(z)_i)\frac{\partial \sigma(\mathbf{z})\_i}{\partial z\_i} = \sigma(\mathbf{z})\_i (1 - \sigma(\mathbf{z})\_i)

Derivative for Different Indices

For the derivative with respect to a different index jj, where iji \neq j, we have:

σ(z)_iz_j=σ(z)_iσ(z)_j\frac{\partial \sigma(\mathbf{z})\_i}{\partial z\_j} = -\sigma(\mathbf{z})\_i \sigma(\mathbf{z})\_j

Combined Result

These derivatives are consolidated into a Jacobian matrix for the softmax function, which is crucial for computing the gradient:

σ(z)z=[σ(z)_1(1σ(z)_1)σ(z)_1σ(z)_2σ(z)_1σ(z)_nσ(z)_2σ(z)_1σ(z)_2(1σ(z)_2)σ(z)_2σ(z)_nσ(z)_nσ(z)_1σ(z)_nσ(z)_2σ(z)_n(1σ(z)_n)]\frac{\partial \sigma(\mathbf{z})}{\partial \mathbf{z}} = \begin{bmatrix} \sigma(\mathbf{z})\_1 (1 - \sigma(\mathbf{z})\_1) & -\sigma(\mathbf{z})\_1 \sigma(\mathbf{z})\_2 & \ldots & -\sigma(\mathbf{z})\_1 \sigma(\mathbf{z})\_n \\ -\sigma(\mathbf{z})\_2 \sigma(\mathbf{z})\_1 & \sigma(\mathbf{z})\_2 (1 - \sigma(\mathbf{z})\_2) & \ldots & -\sigma(\mathbf{z})\_2 \sigma(\mathbf{z})\_n \\ \vdots & \vdots & \ddots & \vdots \\ -\sigma(\mathbf{z})\_n \sigma(\mathbf{z})\_1 & -\sigma(\mathbf{z})\_n \sigma(\mathbf{z})\_2 & \ldots & \sigma(\mathbf{z})\_n (1 - \sigma(\mathbf{z})\_n) \end{bmatrix}

Backpropagation Context

In backpropagation, we often use the derivative of the loss function with respect to the network's outputs. Suppose we have a loss function L\mathcal{L}, such as cross-entropy. To propagate the gradients back through the network, we need the derivative:

Lz=Lσ(z)σ(z)z\frac{\partial \mathcal{L}}{\partial \mathbf{z}} = \frac{\partial \mathcal{L}}{\partial \sigma(\mathbf{z})} \cdot \frac{\partial \sigma(\mathbf{z})}{\partial \mathbf{z}}

For classification tasks using cross-entropy loss, this reduces computation as follows: if yiy_i is the true label, the gradient through softmax simplifies to the difference between probability and true label:

Lz_i=σ(z)_iy_i\frac{\partial \mathcal{L}}{\partial z\_i} = \sigma(\mathbf{z})\_i - y\_i

This simplification is due to the chain rule and the nature of the cross-entropy loss applied to the softmax output.

Key Points Summary

NotationExplanation
σ(z)i\sigma(\mathbf{z})_iSoftmax probability for index ii
σ(z)izi\frac{\partial \sigma(\mathbf{z})_i}{\partial z_i}Derivative of softmax wrt the same input index
$\frac\{\partial \sigma(\mathbf\{z\})_i\}\{\partial z_j\}$Derivative of softmax wrt a different input index $(i \neq j)$
zL\nabla_z \mathcal{L}Gradient of loss wrt inputs, key for backpropagation
Simplified $\quad
$ GradientFor cross-entropy loss: $\sigma(\mathbf\{z\})_i - y_i$

Conclusion

Understanding the derivatives of the softmax function is critical in the optimization of neural networks, particularly during the backpropagation stage. Armed with this mathematical foundation, one can implement efficient and accurate models capable of meaningful classification performance. By leveraging simplifications such as those seen with cross-entropy loss, neural networks can be optimized more effectively, making gradient descent-based approaches more powerful and efficient.


Course illustration
Course illustration

All Rights Reserved.