3.5.8 · D1Sequence Models

Foundations — Encoder-decoder architecture

2,764 words13 min readBack to topic

This page assumes nothing. Before you can read the parent note, you need to be fluent in a handful of symbols and pictures. We build each one from scratch, in an order where every item leans on the one before it.


0. What is a "sequence"? — the picture behind everything

Before any math, look at what we are even feeding the machine.

Figure — Encoder-decoder architecture
Figure 1 — Two rows of boxes. The cyan row (top) is the input sequence "I love AI", labelled . The amber row (bottom) is the output "J'aime l'IA", labelled . What to observe: three boxes go in, two come out — the white arrow and caption "3 in → 2 out" show that the two rows have different lengths. That mismatch is the whole reason this architecture exists.

A sequence is just an ordered list of things. "I love AI" is a sequence of three words. Order matters: "AI love I" is a different sequence even though it uses the same words.

WHY the topic needs this: the whole reason encoder-decoders exist is that the input list and the output list can have different lengths (). Translation turns a 3-word English sequence into a 2-word French one. No fixed-size machine could do that directly — hence two machines.


1. Subscripts and time steps:

The parent note writes . Let's earn every piece.

  • = "an input token."
  • The little number below, the subscript, says which position in the line. is the first token, the second.
  • with a letter means "the token at some general position " — a placeholder so we can write one rule that works for every position.
  • is the last input token, because is the total count.

2. Embedding: — turning a word into numbers

A neural network cannot add up the letters "l-o-v-e." It only does arithmetic on numbers. So each token must first become a list of numbers, called a vector.

Figure — Encoder-decoder architecture
Figure 2 — Word to number-column. On the left, the cyan word "love"; the amber arrow labelled emb( ) feeds it into the white column of four numbers on the right. What to observe: the word does not stay a word — it becomes a fixed list of numbers (here ; in real code ). This is the only form the network's arithmetic can touch, which is why every token must pass through this arrow first.

WHY the topic needs this: when the parent writes "", the bold letter and the are saying " is a vector of 512 real numbers." You need the vector idea to even read that line.


3. The hidden state and the recurrent function

This is the heart of the encoder. The parent writes:

Read it as a sentence: "the new memory () is made by a function that looks at the current token () and the old memory ()."

  • = hidden state = the machine's running memory / notepad.
  • = the memory one step ago (subtract 1 from the seat number → the previous seat).
  • = the fixed rule that mixes "what I just read" with "what I remembered." The same is reused at every step (that is what "recurrent" means — the loop reuses one rule).

Figure — Encoder-decoder architecture
Figure 3 — The recurrence unrolled. Read it left to right. The amber boxes at the bottom are the tokens "I", "love", "AI"; each feeds upward (white arrow) into a cyan memory circle . The horizontal white arrows carry the old memory forward into the next circle — that is the "same rule reused each step" (noted top-left). What to observe: the last circle is pulled out (amber arrow) and renamed , the context pill. Nothing new is invented for ; it is literally the final memory.

WHY the topic needs this: the context vector is nothing but this final snowball, . The whole "squeeze the sentence into one pill" idea is this recursion run to the end.

The actual can be a plain RNN cell, an LSTM, or a GRU — the parent note lets you swap in any of these. They differ only in how carefully they decide what to keep and what to forget.


4. tanh: the squasher inside

Example 3 of the parent uses:

Two new pieces here.

and — a weight matrix multiplies a vector to reshape/rescale it; a bias is a vector added on. Together is the network's adjustable "knobs" (the parameters it learns). Different subscripts (, , ) are just different knob-sets for different jobs.

— the hyperbolic tangent. It is a function that takes any number and gently squashes it into the range .

Figure — Encoder-decoder architecture
Figure 4 — The tanh curve. The horizontal axis is the raw input (any number); the vertical axis is the output. What to observe: the cyan S-curve is nearly a straight line through the origin (small signals pass through almost unchanged) but flattens toward the two amber dashed lines at and (huge signals get clamped). No input, however large, escapes the band — that is exactly why the memory cannot blow up.


5. From scores to probabilities: exp and softmax

The decoder ends with:

Let's earn each symbol.

  • = logits = raw, unbounded scores the network outputs, one per possible word in the vocabulary (the full list of words the model knows). is the score for word .
  • means , the exponential. Why exponentiate? Because turns any number (even negative) into a positive one, and it stretches gaps: a slightly higher score becomes a much higher weight. This lets the model express confident preferences.
  • = "add up over every word in the vocabulary." The big is just a compact "add all of these."
  • Dividing each by that total sum forces all the values to add up to exactly .

in words: "the probability of the next output token , given () all the tokens generated before it (, meaning ) and the input ."


6. Products, logs, and the loss

The training objective chains all those per-step probabilities together.

The product : the expression means "multiply these probabilities for every step from to ." The star superscript marks the ground-truth correct token — the answer key we are trying to match (it is not an exponent). We multiply because the probability of the whole correct sentence is the chance of getting step 1 right and step 2 right and so on.

Why the log? Multiplying many numbers each less than gives a tiny number the computer can't store safely (underflow). The logarithm turns a product into a sum:

Sums are stable and easy to differentiate. That single identity is why the parent's loss suddenly switches from to .

The minus sign and : training minimizes a cost, but we want to maximize likelihood — so we flip the sign (maximize = minimize ) and divide by the number of examples to get an average.

Here (theta) is the bundle of all the 's and 's — every knob in both networks. Training = nudging to make this number small. The nudging itself is done by backpropagation through time.


The decoder feeds its own last output back in as the next input — this is autoregressive ("self" + "regress on past outputs"). At each step it faces a choice of which word to emit. Two strategies appear in the parent:

  • Greedy: always take the single highest-probability word. Fast, but can paint itself into a corner.
  • Beam search: keep the best few partial sentences alive at once. See beam search for the full mechanism.

Prerequisite map

Sequence and tokens

Subscripts x_t and time steps

Embedding word to vector

Hidden state h_t and recurrent f

tanh squasher inside f

Logits z and softmax probability

Product then log then loss L

Autoregressive decoding and search

Encoder squeezes to context c

Decoder unrolls output y

Encoder-decoder architecture


Equipment checklist

Cover the answers and test yourself; if any line is fuzzy, reread its section.

What does a subscript like tell you?
The position (seat number) of a token in the sequence, not a power.
What is the difference between and ?
is an input token at time ; is the encoder's memory (hidden state) at time ; "enc" is a name tag, not an exponent.
What does an embedding do and why is it needed?
Turns a word into a vector of numbers so the network can do arithmetic on it.
Read aloud.
"The context vector is a list of 512 real numbers."
In , what are the three roles?
New memory , made by rule from the current token and the previous memory .
How is the recurrence started at ?
You must supply an initial state — either all zeros ("blank notepad") or a learned parameter — because there is no token .
Why is there a inside the cell?
To squash every value into so the memory can't blow up; it is smooth and bounded.
What does softmax guarantee about its outputs?
They are all in and sum to exactly — a valid probability distribution.
Why exponentiate the logits before normalizing?
makes every score positive and sharpens gaps so confident scores dominate.
Why does the loss use instead of ?
turns a fragile product of tiny numbers into a stable sum, avoiding underflow and easing differentiation.
What does the star superscript mean, and how does differ?
The star flags a ground-truth (correct) token; the parenthesized instead numbers which training example it belongs to.
What does the minus sign in accomplish?
It converts "maximize likelihood" into "minimize cost."
What does stand for?
All learnable parameters — every weight matrix and bias in encoder and decoder.
What makes decoding "autoregressive"?
The decoder feeds its own previous output back in as the next input.