A mathematical function used at the output layer of neural networks to convert a vector of raw scores (logits) into a normalized probability distribution, where all outputs sum to exactly 1.
The final step that turns an AI’s raw math into actual percentages. If an AI is trying to guess the next word, Softmax takes its uncalculated scores and turns them into clear probabilities, like “70% chance it’s ‘the’, 20% ‘a’, 10% ‘an’”.
In classification and language modeling, the final linear layer outputs raw, unnormalized scores called logits. The Softmax function applies the exponential function to each logit and then normalizes them by dividing by the sum of all exponentials. This ensures that the output represents a valid probability distribution, making it possible to calculate the Cross-Entropy Loss during training and to sample tokens during inference.
A teacher grading a multiple-choice test. The raw scores are just the number of points earned. Softmax is the process of converting those raw points into a final percentage grade (e.g., 92%) that clearly shows how well the student did relative to the total possible score.
# Conceptual: Softmax in PyTorch
import torch
import torch.nn.functional as F
# Raw logits from the final layer of a neural network
logits = torch.tensor([2.0, 1.0, 0.1])
# Apply Softmax along the last dimension
probabilities = F.softmax(logits, dim=-1)
print(probabilities)
# Output: tensor([0.6590, 0.2424, 0.0986]) -> Sums to 1.0