liveBook · Manning

Scaled-dot product attention

As explained in Transformers in Action

Scaled dot-product attention computes query-key dot products, scales them by the square root of the key dimension, applies softmax, and uses the results to weight values.

Scaled dot-product attention produces a weighted representation of a sequence. Each input is projected into a query, key, and value. Query–key dot products measure how strongly positions relate; softmax turns the scores into weights, which are used to sum the values. section 2.2.3

Before softmax, divide the dot products by the square root of the key dimension. Scaling controls score variance and helps stabilize training. section 2.2.3

One output from five inputs

To form the third output, compare the third input with all five inputs, normalize the five scores with softmax, then take their weighted sum. 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

A NumPy listing shows the projections, scaled score calculation, softmax, and final multiplication by values. section 2.2.3

How Transformers in Action covers it

How deep it goes
Works through the calculation and a simplified implementation, including the reason for scaling.
Examples
Uses a five-input output calculation and a NumPy implementation with projected embeddings.
What it assumes
Assumes familiarity with vectors and matrix multiplication while introducing the roles of queries, keys, and values.
What you can do after
Build it from scratch using matrix projections, scaled dot products, softmax, and value weighting.
How it connects
Links self-attention to queries, keys, values, softmax, and multihead attention.

Also explained in

Compare how 4 books explain scaled-dot product attention →

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.