Vectors and matrices

Q,K,VQ, K, V Q, K, V (query, key, value)

In attention: queries Q, keys K and values V, each a matrix with one row per token.

In ML papers

In attention, each token is projected three ways. Its query (QQ row) says what it is looking for, its key (KK row) what it offers to be matched on, and its value (VV row) what it passes on if matched. Scores are query-key dot products, QK⊤QK^\top; after a softmax they weight the values. They come from the input by learned matrices: Q=XWQQ = XW^Q, K=XWKK = XW^K, V=XWVV = XW^V. Here the superscripts are labels, not powers.

Other meanings, by context

How to type it

LaTeXShows
Q, K, VQ,K,VQ, K, V
\mathbf{Q}, \mathbf{K}, \mathbf{V}Q,K,V\mathbf{Q}, \mathbf{K}, \mathbf{V}

A worked example

q=(1,0), k=(1,1):q⋅k2≈0.71\mathbf{q} = (1, 0),\ \mathbf{k} = (1, 1): \quad \frac{\mathbf{q} \cdot \mathbf{k}}{\sqrt{2}} \approx 0.71

One query-key score in a 2-dimensional head.

Equations that use it

Related

x⊤y\mathbf{x}^\top \mathbf{y}Transposesoftmax⁡(z)i=ezi∑jezj\operatorname{softmax}(\mathbf{z})_i = \frac{e^{z_i}}{\sum_j e^{z_j}}Softmaxdk\sqrt{d_k}Square rootW∈Rm×n\mathbf{W} \in \mathbb{R}^{m \times n}Bold uppercase (matrix)

Decode this example →