Transformer learning · Numerical walkthrough

How self-attention creates a context-aware vector

A clear, step-by-step numerical example: start with the embedding for cat, calculate attention, and see how the vector changes when it gathers information from its sentence.

6 stepsWorked exampleIllustrative numbers
00 The idea

One token. A whole sentence of context.

Let's use a tiny numerical example to see exactly how self-attention takes a token's starting vector and creates a context-aware vector.

We'll use the sentence:

“The cat eats fish.”

Our focus will be the token cat.

The central idea is that cat starts with its own embedding, but self-attention lets it gather information from The, eats, and fish. It then combines that information into a new representation.

The key question: How does the starting vector [0, 2] become a new vector that reflects the surrounding words?
01 Input representation

Start with the embedding vectors

For simplicity, imagine each token has a vector with just two numbers. These numbers are illustrative, not actual embeddings from a trained model.

The
[1, 0]
cat
[0, 2]
eats
[1, 1]
fish
[2, 0]

Let's call the embedding of cat:

xcat = [0, 2]

This is the starting representation. It doesn't yet incorporate information gathered from the other tokens in this sentence.

02 Build Q, K, V

Create queries, keys, and values

Self-attention transforms each input vector into three vectors:

  • Query (Q): What information is this token looking for?
  • Key (K): What information can this token match against?
  • Value (V): What information does this token contribute if selected?

These are calculated using learned weight matrices:

Q = XWQ    K = XWK    V = XWV

To keep the arithmetic easy, let's assume all three weight matrices are identity matrices:

WQ = WK = WV = I

That means, in this toy example, each query, key, and value equals its original embedding.

For cat:

Qcat = Kcat = Vcat = [0, 2]
Keep in mind: The identity matrices are just a simplifying assumption. Real Transformers learn these matrices during training.
03 Calculate the scores

Compare cat with every token

The query for cat is:

Qcat = [0, 2]

To decide which tokens are relevant, self-attention computes the dot product between this query and each token's key.

TokenKey (K)Dot product
The[1, 0]0
cat[0, 2]4
eats[1, 1]2
fish[2, 0]0

For example, the score for eats is:

[0, 2] · [1, 1] = 0(1) + 2(1) = 2

A larger score means a stronger match between the query and key in this example.

In a real Transformer, these scores reflect learned relationships, not necessarily human-defined semantic relevance.

04 Normalize the scores

Turn the scores into attention weights

Before applying softmax, the standard attention formula scales the scores by the square root of the key dimension, √2.

The scaled scores become approximately:

[0, 2.828, 1.414, 0]

Softmax converts them into positive weights that sum to 1:

αcat ≈ [0.043, 0.735, 0.178, 0.043]

How much attention does cat give each token?

The
4.3%
cat
73.5%
eats
17.8%
fish
4.3%

Illustrative weights from our chosen vectors and identity matrices.

Notice that cat gives the most weight to itself, followed by eats.

Important: Attention weights are not fixed meanings or necessarily probabilities of semantic relevance. They are weights used to combine information.
05 Mix the information

Combine the value vectors

This is the crucial step. Self-attention uses the weights to calculate a weighted sum of the value vectors.

Recall our values:

VThe = [1, 0]
Vcat = [0, 2]
Veats = [1, 1]
Vfish = [2, 0]

The new attention output for cat is:

zcat = 0.043[1, 0]
        + 0.735[0, 2]
        + 0.178[1, 1]
        + 0.043[2, 0]

Calculate the first coordinate:

0.043 + 0 + 0.178 + 0.086 = 0.307

Calculate the second coordinate:

0 + 1.470 + 0.178 + 0 = 1.648

Therefore, approximately:

zcat ≈ [0.307, 1.648]
Before attention
[0, 2]
Starting embedding of cat
Attention output
[0.307, 1.648]
Weighted mixture of value vectors

The numbers changed because the new vector combines information from multiple tokens, according to the attention weights.

06 Continue through the layer

Is this the final contextual representation?

Not quite. We've calculated the self-attention output, which is one part of a Transformer layer.

In a typical Transformer block, the model then:

  1. Combines the attention output with the original input through a residual connection.
  2. Applies normalization.
  3. Processes the result through a feed-forward neural network, usually with another residual connection and normalization.

The resulting vector becomes input to the next layer. Repeating these operations can produce increasingly contextual representations.

↗ The big picture

From a starting vector to a contextualized vector

1
Starting embedding of cat

[0, 2]

2
Calculate Q, K and V

Create learned representations for matching and information sharing.

3
Calculate attention weights

Determine how strongly each token contributes.

4
Weighted sum of values

zcat ≈ [0.307, 1.648]

5
Further Transformer processing

Residual connections, normalization and feed-forward layers.

The key insight: Self-attention doesn't look up a predefined meaning for cat. It uses learned projections to decide how to combine information from the available tokens.

One subtlety: the starting embedding is already learned. Self-attention doesn't create meaning from nothing; it transforms and contextualizes an existing representation.

A useful next step: explore why Q, K and V are different in real Transformers, or how residual connections preserve the original embedding.