ML MATH & LINEAR ALGEBRA ยท INTERVIEW QUESTION
What is the softmax function? Walk through a numerical example.
๐ข beginner ยท Calculus & Optimization
Hint: Exponentiate logits and normalize so they sum to 1.
Answer
Softmax converts raw logits into probabilities:
Properties: all probabilities and .
Numerical Example
Logits:
- Exponentiate: , ,
- Sum:
- Normalize: , ,
Result: โ model assigns ~66% to class 1.
python
import numpy as np
def softmax(z, temperature=1.0):
z = np.array(z) / temperature
exp_z = np.exp(z - np.max(z)) # subtract max for stability
return exp_z / exp_z.sum()
logits = [2.0, 1.0, 0.1]
print('Softmax:', softmax(logits)) # [0.659, 0.242, 0.099]
print('T=0.5:', softmax(logits, 0.5)) # sharper
print('T=2.0:', softmax(logits, 2.0)) # more uniformTemperature scaling: โ higher โ uniform, lower โ sharper.
Likely follow-up: What happens to softmax outputs as temperature approaches 0 vs infinity?
Related questions
- Derive the gradient of the cross-entropy loss for softmax output.
- What is the difference between convex and non-convex optimization? Why does it matter in deep learning?
- What are gradients, Jacobians, and Hessians? When do you use each in ML?
- What is gradient descent? Explain the intuition behind it.
- What is the vanishing/exploding gradient problem? How do modern architectures solve it?