3.5.9 · D1Sequence Models

Foundations — The attention mechanism intuition

2,442 words11 min readBack to topic

This page assumes you know nothing. Every letter, arrow, and squiggle that appears in the parent note is unpacked below, in an order where each idea only uses ideas already built.


0. The picture we keep returning to

Everything in attention lives on top of one image: an input sentence turned into a row of boxes, and an output being written one box at a time while looking back at the input row.

Figure — The attention mechanism intuition

Keep that picture in your head. Every symbol below is a label on some part of it.


1. A sequence, and its length

  • The input sequence is the sentence we read (e.g. "Je suis étudiant").
  • The output sequence is what we produce (e.g. "I am a student").

We need names for how many items each has:

Why the topic needs this: attention computes one score for every input position at every output step. So the total amount of work is "output steps input positions" . You cannot even count the work without these two symbols.

The subscript will always mean "which input position" ( runs ), and the subscript will always mean "which output step" ( runs ). Two different letters because they count two different things.


2. A vector — a box that holds many numbers

Each word can't be stored as one number; a word carries many features (is it a noun? is it plural? is it about animals?). So we store it as a list of numbers.

Figure — The attention mechanism intuition

Why the topic needs this: the parent note writes and . That notation is read aloud as " is a vector of length ." and are just how many numbers are in an encoder box vs a decoder box — they can differ, which is exactly why a "projection" step will later be needed. See RNN and LSTM fundamentals for where these vectors come from.


3. Hidden states: and

A network reads words one at a time and, after each word, holds a summary of "everything I've understood so far." That running summary is a vector called a hidden state.

Why the topic needs this: attention asks "for output step , which input words matter?" To answer it needs (a) a description of what the writer is currently doing — that's — and (b) a description of each input word — those are the . Both are vectors, and the whole mechanism is a way to compare them. Building these is the encoder's job; see Encoder-decoder architecture.


4. A score is a single number:

We want to turn "how well does input word match what the writer needs at step ?" into one number. A big number = strong match.

Think of a whole grid of these numbers — one per (output step, input word) pair. That grid is exactly the heatmap the parent note draws.

Why a scalar and not a vector? Because next we want to rank and mix the input words, and you can only sort by a single number. A vector has no natural "bigger."


5. The tools that build the score

The score formula uses three tools. Let's earn each.

5a. A matrix, and what "" means

has numbers; has numbers. To compare them we must first drag both into a common-size space of length . The device that reshapes a length- vector into a length- vector is a matrix.

Figure — The attention mechanism intuition

So and are both length- vectors. Now they are the same size and can be added (add matching slots). That is why two different matrices and appear: one resizes the writer's state, the other resizes an input word.

5b. — the gentle squashing curve

After adding, we pass the result through applied to each number.

Figure — The attention mechanism intuition

Why this tool and not just adding? A plain sum can only express straight-line (linear) relationships. Real language relevance is bendy: "matches only if it's a plural noun near the verb." introduces a bend (nonlinearity), letting the model carve out such curved regions. It also keeps numbers bounded so nothing blows up.

5c. — collapsing a vector to one number (dot product)

After we still have a vector of length , but we promised would be a single number. The tool that turns a vector into one number is the dot product with a learned vector .

Why this tool? We need exactly one scalar to rank input words. The dot product is the simplest, learnable way to compress a length- vector into one number, and is learned so the model decides which combination of features signals "relevant."

Putting 5a–5c together, in order:

  1. resize both vectors to length (, ),
  2. add them,
  3. bend with ,
  4. collapse to one number with . That whole pipeline is .

6. and softmax — turning scores into fair shares

The scores can be any numbers (negative, huge, tiny). We want instead weights that are all positive and add up to exactly , so they read like "percent of attention."

Why and not just "divide by the total"? Because raw scores can be negative — dividing negatives by a total gives negative "weights," which is nonsense for a share of attention. first lifts everything to be positive, then division makes fair shares. It also sharpens gaps: a slightly bigger score becomes a distinctly bigger share.


7. The context vector — a custom-mixed summary

Now we have weights. We use them to blend the input word-vectors.

Why the topic needs this: is the whole payoff. Instead of one frozen summary of the sentence for every output word (the old Seq2seq models approach), the writer gets a fresh, focused summary rebuilt for each step . That is attention in one line.


8. How the writer uses


Prerequisite map

sequence and its lengths Tx Ty

vectors in R^d

hidden states h_i and s_t

matrix reshaping Wa Ua

alignment score e_ti

tanh bending

dot product with v_a

exp then softmax gives alpha_ti

context vector c_t weighted sum

decoder update s_t

Attention mechanism

Each box only depends on boxes above it — that is the order this page built them in.


Worked micro-check (numbers from the parent's Example 1)

At output step the three scores are .

  • , , .
  • Sum .
  • , , .
  • Check they add to : . ✓

The biggest score ("suis") wins the biggest share — exactly the behaviour softmax promises. This same machinery, stacked and generalised, becomes Transformers and self-attention and powers modern Neural machine translation.


Equipment checklist

What does count?
The number of items (words) in the input sequence.
What does the notation say in plain words?
" is a vector — a list of real numbers."
Why do we align using instead of ?
Because can't be computed until we have , and is built from the writer's state — so we must use the previous one.
What single-number output does the dot product give, and why do we want a single number?
One scalar; we need a scalar so we can rank input words (only single numbers have a natural "bigger/smaller").
Why must scores pass through before dividing?
To make every value positive first, otherwise negative scores would produce meaningless negative "shares."
What two guarantees does softmax give the weights ?
Each is in , and together they sum to .
In , what happens if one weight is near and the rest near ?
becomes almost exactly that one — a near-hard lookup.
Why is (a bend) in the score formula rather than plain addition?
A plain sum is linear; adds nonlinearity so the model can express curved "relevant only if..." conditions.