Softmax Function: Turning Scores into Probabilities
Softmax is a function that converts raw scores (logits) into probabilities. It’s mainly used for multi-class classification.
How It Works
- Takes a list of numbers (logits).
- Exponentiates them (makes them positive & larger).
- Divides each by the sum of all exponentiated values.
- The result is a probability distribution (all values sum to 1).
1️⃣ Real-Life Example (Ice Cream Choice)
Imagine you walk into an ice cream shop and see three flavors with "popularity scores":
- 🍦 Vanilla: 8
- 🍫 Chocolate: 5
- 🍓 Strawberry: 2
Softmax converts these scores into probabilities:
- Vanilla: 70% chance you'll pick it
- Chocolate: 25% chance
- Strawberry: 5% chance
🔹 Higher scores get higher probabilities, but everything adds up to 100%.
2️⃣ PyTorch Example
import torch
import torch.nn.functional as F
scores = torch.tensor([8.0, 5.0, 2.0]) # Ice cream popularity
probabilities = F.softmax(scores, dim=0) # Apply softmax
print(probabilities)
✅ Output:
tensor([0.8438, 0.1142, 0.0420])
👉 Vanilla is most likely (84%), while Strawberry is least likely (4%).
3️⃣ Softmax in a Simple Model
You have a model predicting dog, cat, or rabbit from an image.
logits = torch.tensor([3.0, 1.0, 0.5]) # Model's raw scores
probs = F.softmax(logits, dim=0) # Convert to probabilities
print(probs)
🔹 The highest value gets the highest probability.
🔹 All values sum to 1 (like percentages).
Why Use Softmax?
- Converts raw scores into probabilities.
- Ensures that predictions sum to 1.
- Helps in training deep learning models by providing a clear probability distribution.
Where is Softmax Used?
- Output layer of classification networks (e.g., image classification, NLP).
- Neural network interpreters for probabilistic decision-making.
- Multi-class classification problems, paired with cross-entropy loss.
TL;DR
- Softmax = Converts scores into probabilities.
- Bigger score → Higher probability.
- Total probability = 1 (like percentages).
- Used in multi-class classification (dog, cat, rabbit, etc.).
Super simple, right? 😃