3.5.7 · D1Sequence Models

Foundations — Sequence-to-sequence models

3,528 words16 min readBack to topic

This page assumes you have seen nothing. We name every squiggle on the Sequence-to-sequence models page, draw the picture it stands for, and say why the topic cannot live without it. Read top to bottom — each item is a rung, and every rung stands on the one below it.


0. What a "sequence" even is

Before symbols, a mental image.

A sequence is just an ordered list of things where order matters. "I am happy" is a sequence of three words. Reverse it — "happy am I" — and it means something different (or nothing). That "order matters" is the whole reason we need special machinery: a normal calculator adds numbers where order doesn't change the answer, but language does.

Figure — Sequence-to-sequence models
Figure s01 — Three word-boxes "I / am / happy" in a row labelled , with arrows showing left-to-right order; below, the reversed list is flagged in pink as a different meaning. Read it to see that a sequence is an ordered list where shuffling changes meaning.


1. Subscripts and the time index

The parent writes . Let's earn every piece.

  • The letter means "an input token."
  • The little number below — the subscript — is a position label. = the first token, = the second. It is not multiplication and not a power; it is an address, like seat numbers in a row.
  • The parent also uses as a subscript (). Here stands for time step: we imagine reading the sentence left to right, one token per "tick" of a clock. is the first tick.
Question: In "Comment ça va?", what is ?
(three tokens).
Question: What does the un-subscripted mean in ?
the whole input sequence , not a single token.

2. Vectors, and bold letters like

A single number can't hold the meaning of a word — "happy" needs many shades (positive? emotional? adjective?). So we use a vector: an ordered list of numbers stacked together, like .

Figure — Sequence-to-sequence models
Figure s02 — A single vector drawn as four stacked yellow slots holding , each slot annotated "dimension 1..4", titled "a vector in ". Read it to see that a vector is just several numbers stacked, and the count of slots is its dimension.

  • Bold letters (like , and later ) signal "this is a vector, not a single number."
  • Each slot in the vector is a dimension. A vector with 4 slots "lives in 4-dimensional space."
Question: What does a vector in tell you?
it holds 512 real-number slots — it is 512-dimensional.

2b. Embeddings — turning a token index into a vector

We just said each token becomes an index (a single whole number like ). But §2 said meaning needs a vector, not one number. How do we bridge the two? With an embedding.

Picture a lookup table with one row per vocabulary word. Row 0 holds a vector for "I", row 1 for "am", and so on. Feeding token index ("happy") means "go fetch row 2" — out pops a vector.

Figure — Sequence-to-sequence models
Figure s05 — An embedding table drawn as rows indexed 0,1,2; a blue arrow shows index 2 selecting row 2 and pulling out the vector that becomes fed to the LSTM. Read it to see that the LSTM never eats a raw index — it eats the looked-up vector.

Question: What is ?
the embedding vector stored in row 1 of the table — the vector for token index 1.

3. The hidden state — a running memory

Now the star of the show. As the encoder reads token by token, it keeps a running summary of everything seen so far. That summary is a vector called the hidden state, written .

Picture a person reading a sentence aloud while jotting notes on a single sticky note. After each word they update the sticky note — old notes plus the new word give new notes. The sticky note is .

Figure — Sequence-to-sequence models
Figure s03 — Three memory-boxes chained by arrows labelled "carry memory", each fed from below by its token ; the last box is tagged "= context c". Read it to watch the sticky-note memory update at every step and become the context vector at the end.

  • = the blank sticky note before reading (all zeros).
  • reads: "the new note is made from the new token and the previous note ."
  • Every hidden state is itself a vector of the same size: . So all share the same slots — the sticky note never changes size, only its contents.
  • The superscript in vs just says which machine's note it is — encoder or decoder. It's a name tag, not math.

The names and are two specific recipes for this update-the-sticky-note step, designed so early words aren't forgotten too fast. You can treat them as black boxes here; the details live in LSTM networks and GRU networks.

Question: What is for the vector ?
— the same size as every other hidden state.

4. The context vector

After the encoder reads the last token, its final sticky note has "seen" the whole sentence. We give it a new name:

That's it — (for context, or "the thought") is simply the last hidden state, promoted to the role of "the compressed meaning of the entire input." It's the single vector that gets handed from the reader machine to the writer machine. Because it is a hidden state, too — same slots as every .


4b. The decoder recurrence — how the writer machine steps

The encoder's job is done: it handed over . Now the decoder — a second LSTM — writes the output one token at a time, keeping its own running memory .

Two things must happen to start the writer:

  • Initialize its memory from the meaning vector. We set the decoder's very first hidden state to the context: This is literally "load the thought into the writer's head before it speaks."
  • Give it a starting word. It has no real previous word yet, so we feed a special "start" token as — the <SOS> token defined just below.

Then, at each output step , the decoder updates its memory from its previous memory and the previous output token:

Read it exactly like the encoder recurrence, but the "input token" is now the previous output word (looked up through the embedding table ), and the memory it builds on is the decoder's own .

Question: What is the decoder's starting memory ?
the context vector handed over by the encoder.
Question: What is fed as at the first output step?
the <SOS> start-of-sequence token.

5. Probability and the softmax

The decoder must choose the next word. It never says "the word is X" flatly — it gives a probability to every possible word.

To turn raw scores into probabilities we need a gadget that (a) makes everything positive and (b) makes the total add up to 1. That gadget is the softmax.

Figure — Sequence-to-sequence models
Figure s04 — A bar chart: pink bars are raw scores for four candidate tokens, blue bars are the softmax probabilities (summing to 1), with "Estoy" dominating. Read it to see softmax squash arbitrary scores into a clean probability distribution.

  • = the vocabulary, the full list of possible tokens. = how many there are.
  • and (the parent's ) are just learned numbers that reshape the hidden vector into one score per vocabulary word. is a matrix ("weights"), a vector ("bias") — think of them as a tunable lens.
Question: If softmax outputs , which token does greedy decoding pick?
Token 1 (probability 0.92), the largest.

6. Product , logarithm, and the loss

A whole sentence's probability is each word's probability multiplied together:

Multiplying many small probabilities gives a tiny number that computers round to zero. The fix: take the logarithm.

Applying it to the whole-sentence product. Take of both sides of the product above. By the rule " of a product = sum of the logs," applied times:

Question: Why minimize instead of maximizing ?
They're equivalent — maximizing = minimizing — but the log form turns a fragile product into a safe sum and is easy to differentiate.
Question: What single number does report?
how wrong the model's predicted probabilities are on one training example; smaller = better.

7. The special tokens <SOS> and <EOS> — and when to stop

We met <SOS> in §4b as the "go" signal. Its partner answers a different question: how does the decoder know when to stop writing? A sentence has no fixed length, so we need an explicit "I'm done" signal.

Question: What role does <SOS> play at decoding step 1?
It is the "previous token" that primes the decoder before any real word exists.
Question: When does autoregressive decoding stop?
when the model emits <EOS> (or hits the max-length safety cap).

8. — picking the winner

Greedy decoding uses:

Considering several candidate sentences at once instead of one greedy pick is beam search.


Prerequisite map

Sequences and tokens

Vectors and dimensions

Embedding lookup index to vector

Hidden state h_t running memory

RNN LSTM GRU update recipe

Encoder final state becomes context c

Decoder recurrence starts from c

Softmax gives probabilities

Product and log give loss L

Seq2Seq encoder decoder

Special tokens SOS EOS

Attention fixes the bottleneck

The arrows say "you must understand the tail before the head." Notice every path funnels into the seq2seq node, and it in turn points onward to attention — the very next topic. Backprop through both machines is backpropagation through time; the full modern successor is Transformers.


Equipment checklist

Reveal each line only after you can answer it in your own words.

A subscript like means
the token at position 2 — an address, not multiplication.
The un-subscripted means
the whole input sequence .
versus
input length versus output length; they may differ.
A vector is
an ordered list of numbers; each slot is a dimension.
means
a vector of real numbers.
An embedding is
the looked-up vector for token index , from the learned table .
Why embed instead of feeding the index
an index is a meaningless label; the embedding gives the network a rich, learnable vector.
The hidden state is
the running memory summarizing all tokens seen up to time ; it lives in .
Every shares
the same size — the sticky note never changes size.
says
the new memory is built from the new token plus the old memory.
The context vector is
the encoder's final hidden state — the compressed meaning handed to the decoder.
The decoder start is
the context vector .
The decoder step says
build new decoder memory from the previous output word and previous decoder memory.
reads
probability of the next word given the words so far and the meaning vector.
Softmax does
converts raw scores into positive probabilities that sum to 1, via and normalization.
versus
multiplies a list; adds a list.
turns a product into
a sum, because .
, the cross-entropy loss, is
— small when the true words get high probability.
<SOS> and <EOS> are
start and end signals added to the vocabulary.
Decoding stops when
the model emits <EOS> or hits the max-length cap.
returns
the word achieving the highest probability, not the probability value.