Neural Network Basics
Medium
Softmax Activation
Medium
~15 min
code completion
Softmax
Softmax converts a vector of raw scores (logits) into a probability distribution that sums to 1:
It's used in the output layer of multi-class classifiers.
Numerical stability: large values cause exp(z) to overflow. The standard fix is to subtract the maximum before exponentiating:
This leaves the output unchanged mathematically but avoids overflow.
Your task:
Implement softmax(z) using the numerically stable formula.
Example Tests
3 logits
Input: {"z":[1,2,3]}
Expected: [0.09003,0.24473,0.66524]
Equal logits: uniform distribution
Input: {"z":[0,0,0]}
Expected: [0.33333,0.33333,0.33333]
Output sums to 1
Input: {"z":[1,2,3,4]}
Expected: 1