DEEP LEARNING LESSON 13 TRANSFORMERS

Query, Key, and Value

Query, Key, and Value are the three main components used by the attention mechanism to determine which information is relevant and how that information should be combined.

1. What Are Query, Key, and Value?

In attention, we use three representations:

Q = Query
K = Key
V = Value

The simplest way to remember them is:

Query
"What am I looking for?"

Key
"What information do I represent?"

Value
"What information should I provide?"

The Query is compared with the Keys to determine which information is relevant. The corresponding Values are then combined using the resulting attention weights.

2. Simple Search Example

Imagine you are searching a database for information.

You ask:

"Which animal can fly?"

This is your Query.

The database might contain:

Key 1 → Dog
Key 2 → Cat
Key 3 → Bird
Key 4 → Fish

The system compares your query with these keys.

Query
"What animal can fly?"

        ↓

Dog    → low relevance
Cat    → low relevance
Bird   → high relevance
Fish   → low relevance

The corresponding information is stored in the Values.

Bird
 ↓
Value
"Birds have wings and can fly."

This is only an analogy, but it gives you the correct intuition for Q, K, and V.

3. How This Works in Attention

Now replace the database example with neural network vectors.

Query
   ↓
Compare with Keys
   ↓
Attention Scores
   ↓
Softmax
   ↓
Attention Weights
   ↓
Weighted Values
   ↓
Output

So:

Q → decides what information to look for

K → helps decide whether information is relevant

V → contains the information that will be used

4. Example With a Sentence

Consider:

The cat drank milk because it was thirsty.

Suppose the model is processing:

"it"

The Query for "it" represents what information is useful for understanding this token.

"it"
 ↓
Query

The model compares this Query against Keys representing the other tokens.

Query: "it"

        compare with

Key("The")
Key("cat")
Key("drank")
Key("milk")
Key("because")
Key("it")
Key("was")
Key("thirsty")

Some relationships may receive higher scores than others.

"it"
 ↓
cat       → potentially relevant
milk      → potentially relevant
thirsty   → potentially relevant
the       → less relevant
because   → less relevant

The model then uses the corresponding Values to construct a context-aware representation.

5. What Is a Query?

A Query represents what the current token is looking for from the other tokens.

Query
   ↓
"What information is useful to me?"

For example, when processing the word "bank":

I deposited money in the bank.

bank
 ↓
Query
 ↓
"What context helps me understand
what bank means?"

The Query is therefore used to search for relevant information.

6. What Is a Key?

A Key represents the type of information associated with a token that can be compared with a Query.

Query
   ↓
"What am I looking for?"

        compare with

Key
   ↓
"What kind of information do I represent?"

The Query-Key comparison produces an attention score.

Query × Key
      ↓
Attention Score

A higher score means the Query and Key are more strongly related according to the learned representations.

7. What Is a Value?

The Value contains the information that will actually be combined to produce the attention output.

Query
   ↓
Compare with Keys
   ↓
Determine importance
   ↓
Use Values
   ↓
Output

This distinction is important:

Key
 ↓
Used for matching


Value
 ↓
Used for information

The Key helps determine how relevant something is.

The Value provides the information that gets combined.

8. Complete Example

Suppose we have:

Query = [1, 0]

Key 1 = [1, 0]
Key 2 = [0, 1]
Key 3 = [1, 1]

Calculate the Query-Key similarities.

Query · Key 1

[1, 0] · [1, 0]

= 1
Query · Key 2

[1, 0] · [0, 1]

= 0
Query · Key 3

[1, 0] · [1, 1]

= 1

Therefore:

Scores = [1, 0, 1]

Convert them into attention weights using softmax:

Weights ≈ [0.42, 0.16, 0.42]

Now suppose the Values are:

Value 1 = [10, 0]
Value 2 = [0, 20]
Value 3 = [30, 30]

The output is approximately:

0.42 × [10, 0]
+
0.16 × [0, 20]
+
0.42 × [30, 30]

Calculate each part:

[4.2, 0]
+
[0, 3.2]
+
[12.6, 12.6]

Final output:

[16.8, 15.8]

The important point is not the exact numbers. The important point is the process:

Query
 ↓
Compare with Keys
 ↓
Scores
 ↓
Weights
 ↓
Combine Values
 ↓
Output

9. Query, Key, and Value With Python

import tensorflow as tf


query = tf.constant([
    [1.0, 0.0]
])


keys = tf.constant([
    [1.0, 0.0],
    [0.0, 1.0],
    [1.0, 1.0]
])


values = tf.constant([
    [10.0, 0.0],
    [0.0, 20.0],
    [30.0, 30.0]
])


# Compare Query with Keys
scores = tf.matmul(
    query,
    keys,
    transpose_b=True
)


# Convert scores into attention weights
weights = tf.nn.softmax(
    scores,
    axis=-1
)


# Combine the Values
output = tf.matmul(
    weights,
    values
)


print("Scores:")
print(scores.numpy())

print("\nAttention weights:")
print(weights.numpy())

print("\nOutput:")
print(output.numpy())

10. Understand the Python Code

Step 1 — Create Query

query = tf.constant([
    [1.0, 0.0]
])

We create a Query vector.

[1, 0]

Think of it as the current information we want to use to find relevant information.

Step 2 — Create Keys

keys = tf.constant([
    [1.0, 0.0],
    [0.0, 1.0],
    [1.0, 1.0]
])

We have three Keys.

Key 1 = [1, 0]
Key 2 = [0, 1]
Key 3 = [1, 1]

The Query will be compared with each of them.

Step 3 — Create Values

values = tf.constant([
    [10.0, 0.0],
    [0.0, 20.0],
    [30.0, 30.0]
])

These contain the information that will eventually be combined.

Step 4 — Calculate Scores

scores = tf.matmul(
    query,
    keys,
    transpose_b=True
)

This compares the Query against every Key.

Query
   │
   ├── compare → Key 1
   ├── compare → Key 2
   └── compare → Key 3
             ↓
       Attention scores

Step 5 — Calculate Weights

weights = tf.nn.softmax(
    scores,
    axis=-1
)

Softmax converts the scores into normalized weights.

Scores
[1, 0, 1]

        ↓

Softmax

        ↓

Weights
[0.42, 0.16, 0.42]

Step 6 — Combine Values

output = tf.matmul(
    weights,
    values
)

The weights determine how strongly each Value contributes to the final output.

Value 1 × 0.42
+
Value 2 × 0.16
+
Value 3 × 0.42
        ↓
Final output

11. How a Real Transformer Creates Q, K, and V

The previous example manually created Q, K, and V. Real Transformers normally learn them from the input.

Input X
   │
   ├──→ X × WQ → Query
   │
   ├──→ X × WK → Key
   │
   └──→ X × WV → Value

Here:

X
 ↓
Input representation

WQ
 ↓
Learned Query weights

WK
 ↓
Learned Key weights

WV
 ↓
Learned Value weights

These weight matrices are trainable parameters.

During training, the neural network learns useful transformations for creating Queries, Keys, and Values.

12. Why Do We Need Q, K, and V?

You might ask:

"Why not just use one vector?"

Because attention has two different jobs:

1. Decide what information is relevant.

2. Retrieve and combine that information.

Query and Key help with the first job.

Query + Key
      ↓
Relevance

Value handles the second job.

Attention weights + Values
      ↓
Information
      ↓
Output

This separation gives the model flexibility to learn useful relationships.

13. Easy Analogy: Library

Imagine you enter a library and ask:

"I want books about Python."

Your request is the:

Query
"What am I looking for?"

Each book has information describing what it is about. Those descriptions act like:

Keys
"What kind of information does this book represent?"

The actual content of the selected books acts like:

Values
"What information should I retrieve?"

The system compares your Query with the Keys and then retrieves information from the relevant Values.

Query
   ↓
Compare with Keys
   ↓
Find relevant books
   ↓
Retrieve Values
   ↓
Useful information

14. Q, K, and V in Self-Attention

In self-attention, all three come from the same input sequence.

Input sequence
      │
      ├────→ Q
      │
      ├────→ K
      │
      └────→ V

This does NOT mean Q, K, and V are identical.

They are different learned transformations of the same input.

Same input
    │
    ├──→ Transformation 1 → Q
    ├──→ Transformation 2 → K
    └──→ Transformation 3 → V

15. Complete QKV Flow

Input
  ↓
Create Q, K, V
  ↓
Query × Keys
  ↓
Attention scores
  ↓
Softmax
  ↓
Attention weights
  ↓
Weights × Values
  ↓
Attention output

This is the heart of the attention mechanism.

16. The Most Important Distinction

QUERY
"What am I looking for?"

       ↓

KEY
"How relevant am I to that request?"

       ↓

VALUE
"Here is the information I provide."

Remember:

Query + Key
     ↓
Determine importance

Attention weights + Value
     ↓
Produce information

17. Final Summary

Query
 ↓
What information am I looking for?

Key
 ↓
How relevant is this information?

Value
 ↓
What information should I provide?

        ↓

Query × Keys
        ↓
Attention scores
        ↓
Softmax
        ↓
Attention weights
        ↓
Weighted Values
        ↓
Attention output

If you remember only one thing from this lesson, remember:

Q = Search

K = Match

V = Information
QUICK CHECK

Check Your Understanding

1. What does Query represent?
What the current computation is looking for.

2. What does Key represent?
Information used to determine how relevant a token is to the Query.

3. What does Value represent?
The information that is actually combined to create the attention output.

4. What happens when Query is compared with Key?
An attention score is produced.

5. What happens after the scores?
Softmax converts them into attention weights, which are then used to combine the Values.