liveBook · Manning

Softmax

As explained in Deep Learning with PyTorch, Second Edition

A normalization function that converts a vector of scores into a probability distribution by exponentiating and renormalizing the values.

A classifier can produce two scores for an image, but scores are not yet probabilities. To interpret them as the chances of “airplane” and “bird,” each output must lie between 0 and 1, and the two outputs must add up to 1. Softmax makes that conversion while remaining differentiable. section 7.2.3

Softmax exponentiates each score, then divides each result by the sum of all the exponentials. It preserves the scores’ ranking, though it changes their ratios. For scores [1, 2, 3], the result is approximately [0.0900, 0.2447, 0.6652]. section 7.2.4

# In[10]:
softmax = nn.Softmax(dim=1)

x = torch.tensor([[1.0, 2.0, 3.0],
                  [3.0, 2.0, 1.0]])

softmax(x)

# Out[10]:
tensor([[0.0900, 0.2447, 0.6652],
        [0.6652, 0.2447, 0.0900]])

Two rows of scores show why nn.Softmax(dim=1) matters: it normalizes each row separately, as it would for a batch of predictions. section 7.2.4

For image classification, a final nn.Softmax can turn a network’s two output scores into class probabilities. The class with the larger probability is its prediction. For training, the related nn.LogSoftmax can be paired with nn.NLLLoss; the softmax-and-negative-log-likelihood combination is cross-entropy loss. section 7.2.4 section 7.5

Softmax also has a different job in self-attention. Applied to query–key dot-product scores, it produces attention weights that sum to 1. Those weights determine how strongly value vectors contribute to a weighted sum. section 9.5.1

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Works through exponentiation, normalization, output values, and the dimension argument without developing deeper mathematical theory.
Examples
Uses three-score calculations, batched PyTorch tensors, image classification, and a query–key–value attention implementation.
What it assumes
Introduces probability-output constraints before formalizing softmax, while using tensors, batches, and network outputs in its code.
What you can do after
Apply it via PyTorch nn.Softmax or F.softmax to normalize scores along the intended dimension.
How it connects
Connects to single-label classification, negative log likelihood, cross-entropy loss, and dot-product self-attention.

Also explained in

Compare how 17 books explain softmax →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.