Query, key, and normalized weights
In self-attention, a token representation supplies a query that is compared with each token's key; the resulting scores are normalized into weights and used to combine value vectors. The demo omits learned vectors and uses a hand-designed distance rule: for selected index i and key index j, its score is exp(−0.9|i−j|), plus 0.4 when i=j. Dividing by the sum makes the row total 100%. For query “learn,” neighboring words receive more weight than distant words; selecting another query recomputes the row.
A word embedding is a dense learned vector; an RNN carries a state token by token, whereas self-attention directly mixes information across positions. This five-token picture illustrates only a single query row, not a trained transformer.
Complexity and scope
Let L be the number of tokens. This demo computes and displays one row of L weights in O(L) time and O(L) space (including the visible cells); its fixed sentence has L=5. For all queries, forming the full dot-product score map costs O(L²dₖ) arithmetic when each key has dimension dₖ and materializing it takes O(L²) score storage. A transformer layer performs additional projection and feed-forward work; this visualization is not a runtime model and does not measure those costs.