TRANSFORMER INTERNALS · LESSON 135 OF 278

Queries, Keys, and Values

Understand Q, K, and V projections as the core data flow inside self-attention.

What you will learn

Understand Q, K, and V projections as the core data flow inside self-attention.

In simple words

Each token is projected into query, key, and value vectors. Query-key similarity produces attention scores; those scores weight the value vectors that are combined into the output representation.

Visual explanation: Queries, Keys, and Values

Trace how Q + K becomes Weighted V through attention weights.

WATCH THE IDEA

Queries, Keys, and Values

Playing
Q + KWeighted VAttention weights
  1. 1Observe: Q + K
  2. 2Transformation: Attention weights
  3. 3Outcome: Weighted V

Observe: Q + K. Trace how Q + K becomes Weighted V through attention weights.

The diagram highlights Q + K, Attention weights, Weighted V. Use the labels to trace the main relationship from left to right.

Queries, keys, and values inside attentionToken representations split into query, key, and value paths; relevance weights determine which values are combined.TOKENSQUERYKEYVALUEATTENTIONOUTPUT
Queries, keys, and values inside attentionToken representations split into query, key, and value paths; relevance weights determine which values are combined.

Real-world example: Operational AI decision

A team applies queries, keys, and values to a real business workflow.

What the team decides: Queries ask what matters, keys describe what is available, and values carry the information to combine.

Risk to avoid: A strong technical concept still needs measurement, safeguards, and ownership in production.

Technical explanation

Each token is projected into query, key, and value vectors. Query-key similarity produces attention scores; those scores weight the value vectors that are combined into the output representation.

Python and implementation examples

Use these examples to connect the concept to working code. Package-based examples assume the named library is installed in your own environment.

Single-head scaled dot-product attention

python
import math
import torch

Q = torch.randn(4, 8)
K = torch.randn(4, 8)
V = torch.randn(4, 8)

scores = Q @ K.T / math.sqrt(K.shape[-1])
weights = torch.softmax(scores, dim=-1)
output = weights @ V

print(output.shape)  # torch.Size([4, 8])

This is the core attention equation without projection layers, masks, batching, or multiple heads.

Common misconception

This concept is not a guarantee of a correct outcome by itself. Its value depends on suitable evidence, careful implementation, and evaluation against the real task.

QUICK QUIZ

In self-attention, what are queries and keys mainly used for?

Practical application

Queries ask what matters, keys describe what is available, and values carry the information to combine.

Key takeaway

Queries ask what matters, keys describe what is available, and values carry the information to combine.

Next concept

Continue with Scaled Dot-Product Attention.

Engineering view

Queries ask what matters, keys describe what is available, and values carry the information to combine.

Internals view

Understand Q, K, and V projections as the core data flow inside self-attention. Each token is projected into query, key, and value vectors. Query-key similarity produces attention scores; those scores weight the value vectors that are combined into the output representation.

Architecture view

Consider the Q + K → Attention weights → Weighted V flow, and make the system's inputs, checks, and outcomes explicit.

Was this lesson helpful?

Lesson discussion

Comments are reviewed before publication. Keep discussion focused on learning and do not post private information.

Loading discussion…

Your quiz answer and current lesson are saved on this device. Sign in to explore account-backed learning features.

Content version 1 · Last reviewed 2026-10-16