Softmax Function
The softmax function (also known as the normalized exponential function) is a mathematical function that converts a vector of arbitrary real numbers (which could be positive, negative, zero, or any magnitude) into a probability distribution over multiple classes or categories.
In simpler terms:
- It takes multiple values (e.g., scores, logits) as input.
- It squashes them into a vector of probabilities, where:
- Each value is between 0 and 1.
- The sum of all values is exactly 1.
The Softmax Formula
Given a vector of K real numbers (let's call it z):
z=[z1,z2,z3,…,zK
The softmax function computes a new vector σ(z) of the same length K, where each element σi is:
σi(z)=∑j=1
Here's what each part means:
- ezi is the exponential of the i-th element of z. The exponential function grows very rapidly for positive and shrinks rapidly toward 0 for negative .
Key Properties of Softmax
- Output is a Probability Distribution:
- 0≤σi(z)≤1 for all i.
- (the probabilities sum to 1).
Example: Softmax in Action
Suppose you're classifying an image into one of three categories: cat, dog, or bird. Your model outputs the following logits (unnormalized scores):
z=[2.0,1.0,0.1]
- z1=2.0 (score for "cat")
- z2=1.0 (score for "dog")
- (score for "bird")
Apply Softmax:
- Compute exponentials: [e2.0,e1.0,e0.1]≈[7.389
So, the softmax output is:
σ(z)≈[0.659,0.242,0.099]
Interpretation:
- There's a 65.9% chance the image is a cat.
- A 24.2% chance it's a dog.
- Only a 9.9% chance it's a bird.
Notice:
- The largest logit (2.0 for "cat") became the largest probability (0.659).
- The probabilities sum to 0.659+0.242+0.099=1.0.
Why is Softmax Useful?
- Multi-Class Classification:
- In problems with K>2 classes (e.g., handwritten digit recognition: 0–9), softmax ensures you get a probability for each class.
- Cross-entropy loss (common in classification) relies on softmax probabilities.
- Neural Networks:
- The final layer of a classification neural network often uses softmax to turn raw scores (logits) into probabilities.
- Example: In TensorFlow/PyTorch, it's
tf.nn.softmax or torch.nn.functional.softmax.
- Interpretable Outputs:
- Probabilities are easier to understand than arbitrary scores. "85% chance of cancer" is more meaningful than "logit = 3.4".
Softmax vs. Sigmoid
- Sigmoid (logistic function) maps one value to [0,1]:
sigmoid(x)=1+e
Used for (e.g., spam/not-spam).
Numerical Stability Trick
Computing ezi can cause overflow if zi is very large (e.g., ). To avoid this:
- Subtract the maximum logit from all logits:
z′=z−max(z)
- Then apply softmax. This doesn't change the result (due to the shift invariance property) but keeps values manageable.
For example, if z=[1000,200,300], compute:
z′=[1000−1000,200−1000,300−1000
Now, e0=1, e−800≈0, and softmax works safely.
In summary:
- Softmax turns arbitrary scores into a valid probability distribution over multiple classes.
- It's the go-to function for the output layer in multi-class neural networks.
- Combined with cross-entropy loss, it forms the backbone of modern classification systems.
Now you know why softmax is everywhere in AI, from image classifiers to language models!