Review — Scaled dot-product attention
Back to the lesson · Learning path plan
What to remember
- For the query at position i, take a dot product with the key at each position j. Divide these scores by the square root of the key dimension.
- After applying any mask, apply softmax over the keys for that query. The weights in the row sum to 1.
- Weight the corresponding value vectors and sum them. For scalar values 2 and 6, weights (0.75, 0.25) give an output of 3.
- With comparable component variances, dot products tend to grow in magnitude as the dimension grows. Scaling helps avoid softmax saturation.
Recall from memory
For one query, the two raw scores are \(\ln 9\) and \(0\). The keys have dimension \(d_k=4\) and the scalar values are \(2\) and \(6\), respectively. With no mask, compute the scaled scores, the weights and the output.
Show the answer
Dividing by \(\sqrt{4}=2\) gives scaled scores \(\ln 3\) and \(0\). Softmax gives weights \(\frac{3}{4}\) and \(\frac{1}{4}\). The output is \(\frac{3}{4}\times 2+\frac{1}{4}\times 6=3\).