Skip to content

Attention Mechanisms: Why the Math Looks Like This

Why this matters

The first bridge lesson gave the plain idea:

attention = compare words, turn comparisons into weights, mix information

The full lesson then starts using vector words:

  • dot product
  • same direction
  • softmax
  • sum to 1
  • weighted sum
  • scaled dot-product attention

This page explains why those pieces are there.

You do not need calculus for this. The goal is to make the math-shaped parts feel motivated instead of random.

Mental model

Attention needs a controlled way to answer two questions:

1. Which tokens match what I am looking for?
2. After I know that, how much should I borrow from each token?

The math exists because the model is working with vectors, not English words.

So the transformer needs numeric versions of these ideas:

Human idea Attention mechanism
Does this token match what I need? dot product score
Which matches matter most? softmax weights
How do I combine the useful information? weighted sum of values

The full lesson's attention formula is just a compact version of that table.

Core ideas

  • A dot product is a simple way to score whether two vectors point in a similar direction.
  • "Same direction" means the number patterns line up.
  • Raw scores are not yet good borrowing amounts.
  • Softmax turns raw scores into attention weights.
  • Attention weights sum to 1 so they behave like one full unit of attention split across tokens.
  • Queries and keys decide the weights.
  • Values provide the information that gets mixed.
  • Scaling keeps dot product scores from becoming too large before softmax.

Walkthrough

Start with the question attention is answering

Imagine the model is updating the token it in this sentence:

The cat sat on the mat because it was tired.

The model wants to know:

When I update "it", which earlier tokens should influence it?

A human might say:

"cat" matters a lot
"because" matters a little
"mat" probably matters less

The model needs to do that with numbers.

Vectors are number patterns

A vector is just a list of numbers.

For teaching, use tiny two-number vectors:

query for "it"     = [2, 1]
key for "cat"      = [3, 1]
key for "mat"      = [0, 4]
key for "because"  = [1, 1]

Do not treat these as real model numbers. They are small fake numbers chosen to make the idea visible.

Read the query for it as:

what "it" is looking for

Read each key as:

how another token can be matched

What "same direction" means

In vector language, "same direction" means the number patterns line up.

These point in the same direction:

[3, 2]
[6, 4]

The second vector is basically a bigger version of the first. Both numbers increased in the same pattern.

These point in the opposite direction:

[3, 2]
[-3, -2]

The signs are flipped.

A tiny arrow picture:

[3, 2]    -> northeast
[6, 4]    -> northeast, but longer
[-3, -2]  -> southwest

When the full lesson says two vectors point in a similar direction, you can read it as:

their number patterns are similar

Why use a dot product?

A dot product gives a single score for how much two vectors line up.

For two-number vectors:

[a, b] dot [c, d] = a*c + b*d

Use the query for it:

query for "it" = [2, 1]

Compare it with the keys:

key for "cat"     = [3, 1]
score             = 2*3 + 1*1 = 7

key for "mat"     = [0, 4]
score             = 2*0 + 1*4 = 4

key for "because" = [1, 1]
score             = 2*1 + 1*1 = 3

So the raw scores are:

cat      7
mat      4
because  3

The model has not borrowed anything yet. It has only measured match strength.

Higher dot product means:

this key matches this query more strongly

Raw scores are not good borrowing amounts

The scores tell us that cat is the best match, but they are not yet clean attention weights.

Raw scores can be:

  • too large
  • negative
  • hard to compare across different rows
  • not adding up to a meaningful total

For borrowing, we want something like percentages:

cat gets most of the attention
mat gets a little
because gets a little
total attention = 100%

That is why attention normalizes the scores.

Why should the weights sum to 1?

Weights that sum to 1 behave like a recipe.

For example:

context for "it" =
70% information from "cat"
20% information from "mat"
10% information from "because"

Those weights add to 100%.

That means the model is splitting one full unit of attention across tokens.

In decimal form:

70% = 0.70
20% = 0.20
10% = 0.10

0.70 + 0.20 + 0.10 = 1.00

So when the full lesson says attention weights sum to 1, it means:

the model has a controlled mixture, not arbitrary score sizes

Why not just divide by the total?

It is tempting to do this:

score / total score

Sometimes that works in simple examples. But attention scores can be negative.

Example:

cat       7
mat       4
bad match -2

If you divide by the total:

total = 7 + 4 + (-2) = 9

cat       =  7/9
mat       =  4/9
bad match = -2/9

Now one "weight" is negative.

That is awkward for the beginner mental model:

borrow negative 22% from this token

Softmax avoids that problem.

What softmax does

Softmax is a score-to-weight converter.

It does three useful things:

1. makes every weight positive
2. makes all weights add up to 1
3. makes bigger scores stand out more

For our raw scores:

cat      7
mat      4
because  3

Softmax turns them into weights like this:

cat      about 94%
mat      about 5%
because  about 1%

The exact percentages are less important than the job softmax performs:

raw match scores -> clean attention weights

Softmax is not just normal division. It exaggerates stronger matches, while still making the result positive and sum to 1.

Values are what actually get mixed

Queries and keys decide the attention weights.

Values provide the information that gets mixed.

For the same example:

query from "it" dot key from "cat"      -> score for cat
query from "it" dot key from "mat"      -> score for mat
query from "it" dot key from "because"  -> score for because

scores through softmax -> attention weights

attention weights mix the values

Plain version:

queries and keys answer: how much should I look there?
values answer: what information do I get from there?

So if the weights are:

cat      94%
mat       5%
because   1%

Then the new representation for it mostly borrows from the value of cat.

The weighted sum

A weighted sum is just the recipe being applied.

Imagine the value vectors are:

value from "cat"      = [10, 2]
value from "mat"      = [1, 8]
value from "because"  = [2, 2]

And the attention weights are:

cat      0.94
mat      0.05
because  0.01

The context vector is:

0.94 * value from "cat"
+ 0.05 * value from "mat"
+ 0.01 * value from "because"

You do not need to calculate the final numbers here.

The point is:

the new "it" vector is mostly cat information,
with a small amount of mat and because information mixed in

That is attention's "borrowing" step.

Why scale the scores?

The full lesson also divides the scores by the square root of the key/query size.

This is called scaling.

The beginner intuition:

dot products get larger when vectors have more dimensions

A two-number vector has two chances to add to the score.

A sixty-four-number vector has sixty-four chances to add to the score.

Large scores can make softmax too confident too quickly:

small score gap -> softer weights
huge score gap  -> one token gets almost everything

Scaling cools the scores down before softmax.

So when you see:

divide by square root of the key/query size

read it as:

keep scores in a reasonable range before softmax

How this maps to the full lesson code

After the plain example, the code shape is much less mysterious:

scores = queries @ keys.T
weights = torch.softmax(scores, dim=-1)
context = weights @ values

Read it as:

queries @ keys.T        -> compare queries with keys
softmax(scores)         -> turn raw scores into weights
weights @ values        -> mix value information

Scaled dot-product attention adds one cooling step:

scores = queries @ keys.T
scores = scores / key_size**0.5
weights = torch.softmax(scores, dim=-1)
context = weights @ values

Read the whole flow as:

compare -> cool down -> convert to weights -> mix

Term Decoder

Term Friendly meaning
vector list of numbers representing a token
direction the pattern of numbers in a vector
same direction similar number pattern
dot product score for how much two vectors line up
attention score raw match strength before normalization
softmax converter from scores to positive weights that sum to 1
attention weight how much to borrow from a token
weighted sum recipe-style mixture using weights
query what the current token is looking for
key what another token can be matched by
value information another token can contribute
scaling cooling down scores before softmax

Common traps

Dot product is not magic

In this lesson, read dot product as a similarity score. It gives a bigger number when the query and key number patterns line up more strongly.

Summing to 1 is not decoration

Weights sum to 1 so the model has a controlled mixture. It is splitting one full unit of attention across tokens.

Softmax is not simple division

Simple division can behave badly with negative scores. Softmax makes weights positive, makes them sum to 1, and emphasizes stronger matches.

Values do not decide the weights

Queries and keys decide the scores and weights. Values are the information mixed after the weights are known.

Same direction does not require a literal picture

The arrow picture is only a teaching aid. For model vectors, same direction mostly means the number patterns are similar.

Check yourself

If two vectors point in a similar direction, should their dot product be high or low?

High. A higher dot product means the vectors line up more strongly.

Why should attention weights add up to 1?

So they behave like a controlled recipe: one full unit of attention split across tokens.

Which pair decides the score: query-key, query-value, or key-value?

Query-key. The query from the current token is compared with keys from tokens it might attend to.

What does softmax do to raw attention scores?

It turns them into positive weights that sum to 1, while giving stronger matches more influence.

What gets mixed to create the context vector?

The value vectors, using the attention weights.

Why are scores scaled before softmax?

To keep dot product scores from becoming too large when vectors have many dimensions.

Now Read the Full Lesson

The full attention lesson uses the same flow with real tensors:

query-key dot products -> scores
scale scores           -> keep them controlled
softmax                -> attention weights
weights times values   -> context vectors

When you see the compact formula or PyTorch code, translate it back to:

compare -> cool down -> convert to weights -> mix

Next: Attention Mechanisms

Source anchors

  • Supports the dot-product attention, softmax, scaling, query/key/value, and weighted-sum sections in 13-attention-mechanisms.md.
  • Uses simplified teaching numbers rather than notebook-faithful tensor values.