I read this somwhere: when one logit is too large in softmax, gradient tend to be very…
- User
I read this somwhere: when one logit is too large in softmax, gradient tend to be very low and one thing we can do is to normalize logits.
- Assistant
I read this somwhere: when one logit is too large in softmax, gradient tend to be very low and one thing we can do is to normalize logits.
Yes, your observation is correct. When training neural networks that use the softmax activation function, having one logit (pre-softmax output) that is significantly larger than the others can lead to very small gradients during backpropagation. This phenomenon can slow down learning and hinder the model's ability to update the weights effectively. Normalizing the logits before applying the softmax function is a common technique to mitigate this issue. Let me explain why this happens and how normalization helps.
The softmax function is defined as:
[ \sigma(z)i = \frac{e^{z_i}}{\sum{j} e^{z_j}} ]
where ( z_i ) is the ( i )-th logit (input to the softmax), and ( \sigma(z)_i ) is the predicted probability for class ( i ).
Issue with Large Logits:
Suppose we use the cross-entropy loss for a target class ( t ). The loss ( L ) is:
[ L = -\log(\sigma(z)_t) ]
The gradient of the loss with respect to the logits ( z_i ) is:
[ \frac{\partial L}{\partial z_i} = \sigma(z)_i - y_i ]
where ( y_i ) is the true label (1 for the target class ( t ), 0 otherwise).
For the correct class (( i = t )):
[ \frac{\partial L}{\partial z_t} = \sigma(z)_t - 1 ]
For incorrect classes (( i \ne t )):
[ \frac{\partial L}{\partial z_i} = \sigma(z)_i ]
Effect of Large Logits:
To address this, normalizing the logits can help:
Subtracting the Maximum Logit:
Before applying softmax, subtract the maximum logit ( z_{\text{max}} = \max_i z_i ) from all logits:
[ \tilde{z}i = z_i - z{\text{max}} ]
This doesn't change the output of the softmax because:
[ \sigma(z)i = \frac{e^{z_i}}{\sum{j} e^{z_j}} = \frac{e^{z_i - z_{\text{max}}}}{\sum_{j} e^{z_j - z_{\text{max}}}} = \sigma(\tilde{z})_i ]
Subtracting the maximum logit prevents extremely large exponentials, reducing numerical instability and keeping the logits in a similar range.
Mean Normalization:
Subtract the mean of the logits:
[ \tilde{z}i = z_i - \frac{1}{N} \sum{j} z_j ]
Centering the logits can reduce the variance and prevent any single logit from dominating.
Batch Normalization:
Temperature Scaling:
Adjust the softmax function by introducing a temperature parameter ( T ):
[ \sigma(z)i = \frac{e^{z_i / T}}{\sum{j} e^{z_j / T}} ]
A higher temperature ( T > 1 ) softens the output probabilities, while ( T < 1 ) makes the distribution sharper.
This can help control the confidence of the model and improve gradient flow.
Label Smoothing:
Instead of using hard labels (0 or 1), assign a small probability to all classes:
[ y_i' = (1 - \epsilon) y_i + \frac{\epsilon}{K} ]
Here, ( \epsilon ) is a small constant, and ( K ) is the number of classes.
Label smoothing prevents the model from becoming overconfident, which can improve generalization and gradient flow.
When one logit is significantly larger than the others in a softmax operation:
Normalizing the logits before applying softmax helps by:
Would you like more details on any of these methods or additional strategies to improve training dynamics?
Not yet included in a published release.