Attention

A self-attention layer lets every token in a sentence look at the others and take a blend of what it finds. As a formula it fits on one line, O = softmax(QKᵀ/√d)·V, but the line reads inside out. Here it is in the order it runs, for the four tokens the cat sat down. Each token is two numbers, so every matrix stays small enough to read.

W_Q = \begin{pmatrix} \tangle{q11} & \tangle{q12} \\ \tangle{q21} & \tangle{q22} \end{pmatrix} \quad W_K = \begin{pmatrix} \tangle{k11} & \tangle{k12} \\ \tangle{k21} & \tangle{k22} \end{pmatrix} \quad W_V = \begin{pmatrix} \tangle{v11} & \tangle{v12} \\ \tangle{v21} & \tangle{v22} \end{pmatrix}

Step 1 of 7:

Mask off

In the plane

The same layer, drawn as points. Pick whose output to follow: the query of sat.

The query and the keys. Keys on the shaded side score above zero.
The values and the output, pulled toward each value by its weight.

Things to try

Things to notice

How it's built

Open the source panels above for the full markup, script and styles.