liveBook · Manning

Softmax

As explained in Build a Text-to-Image Generator (from Scratch)

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

Softmax converts a vector of raw scores into nonnegative values that sum to one. In attention, these values become attention weights, expressing how strongly each token attends to every token. section 2.1.1

Scaled dot-product attention applies softmax in a short sequence:

  • Compute similarities by taking dot products between query and key vectors. section 2.1.1
  • Divide the scores by the square root of the key-vector dimension to stabilize training before applying softmax. section 2.1.1
  • Use the resulting attention weights to calculate a weighted combination of the value vectors. section 3.3.3

Softmax in self-attention

For the four-token sentence “Wie geht es dir,” the input embedding has shape 4 × 256. Softmax turns each token’s attention scores into weights whose total is 100%, allowing the resulting weighted values to produce contextually enriched token representations. section 2.1.1

Figure 2.3 A diagram of the self-attention mechanism

Visualizes how softmax fits between query-key scoring and value aggregation in self-attention. section 2.1.1

Listing 3.4 Calculating self-attention in each attention head
class AttentionHead(nn.Module):
    def __init__(self, hidden_size, attention_head_size, bias=True):
        super().__init__()
        self.hidden_size = hidden_size
        self.attention_head_size = attention_head_size
        self.query = nn.Linear(hidden_size, attention_head_size, bias=bias)
        self.key = nn.Linear(hidden_size, attention_head_size, bias=bias)
        self.value = nn.Linear(hidden_size, attention_head_size, bias=bias)   
    def forward(self, x):
        query = self.query(x)
        key = self.key(x)
        value = self.value(x)  #1
        attention_scores = torch.matmul(query, key.transpose(-1, -2))
        attention_scores = attention_scores / math.sqrt(\
                self.attention_head_size)    
        attention_probs = nn.functional.softmax(attention_scores,
              dim=-1)  #2
        attention_output = torch.matmul(attention_probs,   #3
            value)   #3
        return (attention_output, attention_probs) 

Shows a PyTorch attention head applying softmax to scaled attention scores. section 3.3.3

How Build a Text-to-Image Generator (from Scratch) covers it

How deep it goes
Explains scoring, scaling, normalization, and value aggregation, while deferring detailed multi-head implementation internals.
Examples
Uses the sentence “Wie geht es dir,” a four-token embedding, and a PyTorch attention-head implementation.
What it assumes
Assumes query, key, and value vectors plus dot products; explains softmax’s role in attention.
What you can do after
Apply it via scaled dot-product attention to normalize scores and aggregate value vectors.
How it connects
Ties softmax to attention mechanisms, query-key similarity, value vectors, and multi-head 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 Build a Text-to-Image Generator (from Scratch). The section links point to the parts of the book it draws on.