liveBook · Manning

Softmax

As explained in Grokking AI Algorithms, Second Edition

The softmax function converts a vector of real-valued scores into nonnegative values that sum to one.

Softmax turns a list of real-valued scores into nonnegative values that add up to 1. These outputs can be read as a distribution of attention or prediction: each value shows a score’s share of the whole. section 11.20

In attention, softmax creates a pie chart of token relevance. Before applying it, scores are scaled because dot products from larger dimensions can become too large and make softmax behave poorly. section 11.20

The calculation follows a short sequence:

  • Scale the scores according to their dimensionality. section 11.20
  • Exponentiate each scaled score, making every result positive and magnifying differences. section 11.20
  • Divide each exponential by the sum of all exponentials, producing values between 0 and 1 whose total is 1. section 11.20

From attention scores to weights

Suppose token scores become 0.1555, 0.0221, and 1.0000 after exponentiation, while the total is 2.46. Softmax assigns weights of about 0.0632, 0.0090, and 0.4064. The largest score receives the largest share, but every token retains a normalized value.

The same idea converts language-model logits—raw scores for vocabulary tokens—into probabilities. A model can then compare possible next tokens, although meaningful predictions require substantial training rather than a single small batch. section 11.29

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Works through scaling, exponentiation, normalization, and numeric tables, but does not develop deeper theory.
Examples
Uses token attention tables and a 35-token vocabulary probability table to demonstrate normalization and prediction.
What it assumes
Assumes basic scores and token vectors, while introducing scaling, exponentials, logits, and normalization through examples.
What you can do after
Evaluate attention relevance and token probabilities by following the softmax calculation from scores to normalized values.
How it connects
Connects softmax to attention weights, self-attention, logits, probabilities, vocabulary, and next-token prediction.

Also explained in

Compare how 17 books explain softmax →

Related concepts

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