liveBook · Manning

Softmax

As explained in Transformers in Action

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

Softmax turns a set of scores into nonnegative weights that sum to one. In attention, those weights determine how much each input contributes to an output. In text generation, softmax probabilities determine how likely tokens are to be sampled. section 2.2.3 section 4.1.5

For scaled dot-product attention, first compare a query with the keys to produce scores. Apply softmax across those scores, then use the resulting weights to combine the values. In the implementation below, `axis=1` applies softmax across each row of query–key scores; multiplying by `V` produces the attention output. section 2.2.3

Listing 2.3 Simplified scaled dot-attention implementation
embed_1 = np.array([0, 1, 0]) #1
embed_2 = np.array([1, 0, 1])
embed_3 = np.array([0, 1, 1])
embed_4 = np.array([1, 1, 0])

embeddings = np.array([embed_1, embed_2, embed_3, embed_4]) #2

Wq = rand(3, 3)  #3
Wk = rand(3, 3) 
Wv = rand(3, 3)

Q = embeddings.dot(Wq)  #4
K = embeddings.dot(Wk)
V = embeddings.dot(Wv)

attention_scores = softmax(Q.dot(K.T) / sqrt(K.shape[1]), axis=1) #5

attention_output = attention_scores.dot(V) #6

The `attention_scores` line shows softmax applied after scaling the query–key dot products; the next line uses those weights to combine values. section 2.2.3

Temperature changes the distribution *before* softmax is applied to token logits. A temperature of 1 leaves the probabilities unchanged. Higher temperatures make them more uniform and sampling more varied; lower temperatures concentrate probability on the likeliest tokens, approaching greedy decoding as temperature nears zero. section 4.1.5

Listing 4.5 Temperature sampling implementation
temperature_outputs = model.generate(
    input_ids=input_ids,
    max_new_tokens=128,
    do_sample=True,
    temperature=0.7,
    num_return_sequences=3,
    pad_token_id=tokenizer.eos_token_id
)

This `model.generate()` call enables sampling and sets `temperature=0.7`, showing how to control the token distribution without implementing softmax directly. section 4.1.5

How Transformers in Action covers it

How deep it goes
Works through softmax’s role in attention and temperature sampling with code, but does not implement the softmax function itself.
Examples
Shows row-wise softmax in a NumPy attention calculation and `temperature=0.7` in a Transformers generation call.
What it assumes
Assumes familiarity with vectors and code while introducing attention weights through dot products and a weighted sum.
What you can do after
Apply it via Transformers `model.generate()` to adjust sampling temperature, and interpret softmax weights in an attention calculation.
How it connects
Links softmax to scaled dot-product attention, queries, keys, values, logits, temperature sampling, and greedy decoding.

Also explained in

Compare how 17 books explain softmax →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Transformers in Action. The section links point to the parts of the book it draws on.