Why compute softmax + cross-entropy jointly via log-sum-exp?
mediumAnswer
- Naive overflows for large logits.
- Compute (z) where (z) = (sum()).
- Numerically stable.
- Cross-entropy then becomes -sum(target * log-softmax) — computed in one op (nn.CrossEntropyLoss in PyTorch), avoiding a separate softmax that could overflow before the log.
Check yourself — multiple choice
- Only for aesthetic reasons
- Numerical stability — compute log-softmax directly with trick to avoid overflow
- It's faster on CPU only
- Because softmax is always non-differentiable
trick: (sum()) — no overflow.
#fundamentals#losses
Practise Deep Learning
214 interview questions in this topic.
Related questions
- How should the output head be designed for a regression task with a strictly positive target?
- How is the output head different for multi-label vs multi-class classification?
- MSE vs MAE for regression — when is each preferred?
- ReLU vs sigmoid vs GELU — when do you use each?
- In one sentence, what is backpropagation?
- State the universal approximation theorem in one sentence and its practical caveat.