Self-Attention in Transformers: Queries, Keys, and Values

Please read my earlier posts on RNNs, the transformer revolution, and the transformer architecture before diving into this post.

This post zooms in on the mechanism that makes transformers so powerful: self-attention. It lets every token in a sequence look at every other token and decide how much information to borrow from each one.

Suppose you read this sentence: “The animal did not cross the road because it was too tired.”

To understand what “it” means, your brain looks back at “the animal.” Self-attention performs a similar job: for each word, it examines the other words and creates a new, context-aware representation.

Unlike an RNN, which processes tokens one after another, self-attention can relate tokens across the entire sequence in parallel. This parallelism is one reason transformers train efficiently on modern hardware.

Queries, keys, and values

Self-attention gives every token three learned representations:

ComponentIntuitionRole
Query“What am I looking for?”Used by the current token to search for relevant context.
Key“What do I advertise?”Used by every token to describe what information it can offer.
Value“What information do I provide?”The content that gets mixed into the output.

Think of a search engine. Your search phrase is the query. Each webpage has a title or metadata that acts as its key. The actual page content is the value. The engine compares your query with each key, ranks the results, and returns a blend of the most relevant values.

In self-attention, all three come from the same input sequence. Each token embedding is passed through separate trainable transformations to create its query, key, and value representations. These transformations let the model learn different ways to represent a token depending on whether it is acting as a searcher, an index, or a source of information.

At a high level, self-attention follows four steps:

  1. Compare a query with every key.
    For each token, the model compares its query with the keys of all tokens in the sequence. A strong match means that the other token is likely relevant.
  2. Score the relevance.
    The comparison produces a relevance score for every pair of tokens. For example, when processing the word “it,” the model may find that “animal” has a much stronger relevance score than “road.”
  3. Convert scores into attention weights.
    The scores are normalized so that, for each token, all attention weights add up to 1. These weights represent how much attention the current token should pay to every other token. The model learns these weights during training.
  4. Combine the values.
    Each token’s final representation is a weighted blend of the value vectors from the sequence. If “animal” receives a high attention weight, its value contributes more strongly to the updated representation of “it.”

Self-attention is the transformer’s core building block because it:

  • Captures long-range dependencies without repeatedly passing information through time steps.
  • Processes tokens in parallel, unlike sequential RNNs.
  • Produces context-aware representations for every token.
  • Provides the foundation for multi-head attention, where several attention patterns run side by side.

In short, queries decide what to look for, keys determine relevance, and values supply the information. The attention-weighted combination of values is what turns a static word embedding into a representation that understands its context.

While modern models use many efficiency-focused variants of attention, the query-key-value idea remains the foundation for understanding how transformers process sequences.

Leave a Reply

Your email address will not be published. Required fields are marked *