Read query, key and value roles
Type the indices of each value in their original order. · 183 words
In a transformer, attention lets each token gather information from other tokens. A query is a numeric vector saying what information the current token wants. A key is a vector describing what a candidate token offers for matching that request. A value is the information that candidate can pass on. These are not necessarily different words. They are learned views of token representations. Separate linear projections, called projection matrices, create the three vectors. Vectors in one attention calculation have the same length, which lets the model compare them and combine them.
Imagine a museum kiosk receiving the request 'red umbrella'. The request can be represented by a query emphasizing both color and object. A candidate caption, 'red umbrella in the lobby', has a key that emphasizes those matching topics. Its value carries useful details, such as 'aisle 2, stand 4'. A different caption receives another key and value. Comparing the request query with the keys gives a strong match more weight in the response. The query means what is sought, the key means what can match, and the value means what is transferred.
Check your recall
In the museum example, which vector carries the actual location details, and what are the other two vectors used for?
Check your answer
The value carries the location details because it contains the information passed to the response. The query describes what the visitor wants, while the key describes how well a candidate caption matches that request.
Sources and credits
Original KeyTouch lesson. Background reading · Vaswani et al. · Attention Is All You Need.