3.5.14 · D1Sequence Models

Foundations — Beam search decoding

2,284 words10 min readBack to topic

This page assumes you have seen none of the notation in the parent note. We build every symbol from scratch, in an order where each one leans only on the ones before it. If a picture defines an idea better than words, we draw it.


1. What is a "sequence"? The symbol

A sequence is just an ordered list of things — here, a list of words (or word-pieces) that the model produces one at a time, left to right.

The picture: think of a row of boxes filled left to right. Box number holds token .

Figure — Beam search decoding

Why the topic needs it. Beam search is a way of choosing which sequence to output. Before we can score or search over sequences, we need a name for one — that name is , and its parts are the .


2. The input we translate from:

The model does not invent text from nothing. It reads an input — the English sentence to translate, the image to caption, the article to summarize — and we call that whole input (bold again, because it is also a list).

This machinery — read , write one token at a time — is exactly the sequence-to-sequence model. The reading half compresses into an internal summary (often written , the encoder output), and the attention mechanism lets the writing half look back at the useful parts of at each step. For this page you only need one fact from all that: the model can, at any step, hand us a probability for every possible next token.


3. Probability:

A probability is a number between and that measures how much the model believes something. = "no way", = "certain", = "fairly likely".

So is "the probability that the model produces sentence , given it read input ". That is the exact number beam search is trying to make large.

The picture: a probability is a slice of a full pie of size . All possible next tokens split that pie; a bigger slice = a more likely token.

Figure — Beam search decoding

Why the topic needs it. Beam search's whole job is to hunt for the with the biggest . Without probabilities there is nothing to compare candidates by.


4. Building a sentence's probability: the product

The model does not score a whole sentence in one shot. It writes token by token, and at each step it gives the probability of the next token given everything so far.

So the per-step number is = "probability of the token in slot , given the words already written and the input".

To get the probability of the whole sentence, we multiply these step-probabilities together:

Why multiply, and not add? Because each step is a separate choice that must all happen. The probability of "this word AND then that word AND then that word" is the product of the individual probabilities. Think of flipping a coin heads three times: , not .

The picture: a chain of links. Each link is one step-probability; the strength of the whole chain is all links multiplied.


5. Picking the biggest:

We want the best sentence. Two related symbols:

Beam search's dream target is where the little star just marks "the optimal one". In words: out of all possible sentences, hand me the one with the highest probability.

Why we can't just compute it directly. If the vocabulary has tokens and sentences are length , there are possible sentences — astronomically many. We cannot check them all, so we need a clever partial search. That search is beam search.


6. The vocabulary size and the branching problem

At every step the model could pick any of tokens. So from one half-sentence there are ways to continue; from each of those, more; the tree of possibilities branches by at every level and explodes as .

Figure — Beam search decoding

Why the topic needs . Beam search's cost is governed by (it must consider all next tokens for each kept candidate). Understanding that the space is is why we prune instead of search everything.


7. The special markers <START> and <END>

Two tokens are not real words — they are punctuation for the machine:

  • <START> — a fixed marker placed before position 1 so the model always has "something to condition on" at the first step. It is given, never predicted, and so carries no probability of its own.
  • <END> — the token the model emits when it thinks the sentence is finished. Once a candidate produces <END>, that candidate is complete and stops growing.

Why this matters for counting length. When we later divide by the length , we count only the generated tokens — the ones the model actually predicted. The <START> marker is not counted: For example the finished list [<START>, Bonjour, <END>] has generated tokens.


8. Logarithms: turning products into sums

Multiplying many small probabilities (each below 1) gives a tiny number that a computer can round down to zero — this is called underflow. The logarithm rescues us.

Because probabilities are between and , their logs are negative (or zero). A probability of gives ; smaller probabilities give more-negative logs. So "less likely" = "more negative score". Keep that sign in mind — it explains why every score in the parent note is a negative number, and why closer to zero is better.

Applying fact 1 to our chain-rule product turns it into a friendly sum:

This sum-of-logs is the score beam search actually adds up step by step.


9. Length normalization and the exponent

A longer sentence has more negative terms in its sum, so raw score punishes length — a good long sentence loses to a mediocre short one. The fix is to divide by length, softened by an exponent (Greek "alpha"):

  • : divide by full length → the plain average log-prob per token.
  • : , so no normalization at all (raw sum).
  • : a gentle in-between used in practice — some, but not full, forgiveness for length.

This is a decoding-time knob, cousin to the temperature knob you meet in sampling — both reshape how we choose from the model's probabilities without changing the model itself. The quality of the final is then judged against reference translations with a metric like the BLEU score.


10. Beam width

Beam search's rhythm: keep candidates → expand each into continuations ( in total) → keep only the best → repeat. The "beam" is that surviving handful, like a flashlight beam of width sweeping through the giant tree of sentences.


Prerequisite map

probability P given input

chain rule product over steps

vocabulary size V

branching tree V to the T

logarithm turns product to sum

log-probability score

Beam search decoding

length normalization with alpha

beam width k keeps top candidates

START and END markers

argmax the best sequence

Read it bottom-up: probabilities and the vocabulary feed the branching tree; logs turn the probability product into an addable score; normalization and the width tame that score; all of it flows into beam search.


Equipment checklist

Cover the right side and answer aloud. If any stumps you, reread its section.

What does mean?
The whole output sentence as an ordered list; each is the single token at position ; is the length.
What does the bar in mean?
"Probability of given that is known."
What is ?
All tokens chosen before position , i.e. .
Why do we multiply step-probabilities to score a whole sentence?
Each step is a separate choice that must all happen; probability of "this AND that AND that" is the product.
What does tell you to do?
Multiply the listed terms together for up to (multiplication's version of ).
Difference between and ?
is the best value; is the input that achieves it — here, which sentence .
Why switch from probability to -probability?
Products of tiny probabilities underflow to zero; logs turn the product into a stable sum, and preserves the ranking.
What sign do log-probabilities have, and which is better?
Negative (or zero); closer to zero = higher probability = better.
Why divide the score by ?
Raw sums penalize longer sentences; dividing by length (softened by ) removes the unfair length bias.
What does do?
, so no normalization — the raw log-prob sum.
What is the beam width , and what is ?
How many partial sentences we keep each step; is greedy decoding.
Why is the full search , and why can't we do it?
Each of positions branches into choices → sentences, astronomically many — hence we prune with beams.
Why isn't <START> counted in the length ?
It is a fixed marker, not a predicted token, so it carries no probability; .