Seq2seq and Beam Search

Greedy decoding takes the best next token and can regret it. Beam search keeps several options alive and picks at the end.

Overview

The architecture

Encoder-decoder handles the case where input and output are both sequences and their lengths differ — translation, summarisation, speech to text.

The encoder reads the input and makes a form. The decoder emits output tokens one at a time, each conditioned on the form and on all it has emitted so far.

The first version compressed the whole input into a single fixed vector, which became the bottleneck that motivated focus: let the decoder look back at each input spot rather than at one summary. That is the change the field turned on, and the transformer is its conclusion.

Autoregressive decoding isn't exact to that architecture. Any model that emits one token at a time faces the same question: at each step you've a chance over the vocab, and you've to choose.

Seq2seq and Beam Search

This module needs JavaScript: the numbers are computed in the page rather than recorded.

Worth knowing

An encoder reads the input into a representation; a decoder emits tokens one at a time, conditioned on what it has already emitted.
Greedy decoding takes the highest-probability token at each step. It cannot reconsider.
Beam search keeps the k best partial sequences and expands all of them, choosing only at the end.
Sequence probabilities are products, so short sequences win. Length normalisation is not optional.

Seq2seq and Beam Search

How a model that emits one token at a time chooses a whole sequence. Why the obvious method isn't the best one.

Greedy decoding, and its regret

Take the most probable token each time.

Fast, simple, and it optimises the wrong thing. You want the most probable *sequence*, and the highest-chance first token can lead into a region where all that follows is bad.

The chart shows the cumulative log chance of the beam's paths. With the width at 1 you get greedy decoding, and the readout checks it against the best sequence a wider beam found. It's often worse, and the reason is always the same: it committed early to a thing that looked good and didn't pay off.

Keep the k best partial sequences. At each step, expand all of them, score each continuation, keep the best k total, and continue. At the end, take the best complete sequence.

Drag the width from 1 upward and watch the best score improve.

It's still a heuristic. It does not find the globally best sequence — that would need searching an exponentially large tree — and a path discarded early because it looked weak can never come back, even if it led a place great. Beam search only reduces how often that happens.

The length problem

Sequence chance is a product of per-token chances, so each extra token times by a thing less than 1. Longer sequences have lower chance, always, and unnormalised beam search so has a systematic bias toward stopping early.

The standard fix cuts the log chance by the length, at times raised to a tunable power. It's a correction with no principled derivation, it's needed, and each production decoder has one.

Wider is not always better

Raise the width past about 5 and translation quality often *falls*. This is called the beam search curse, and it's well documented.

The explanation is that a wider beam finds sequences the model truly considers more probable, and the model's notion of probable isn't the same as good. Very high-chance text is bland: it's the safe, plain continuation. A wider search is a more faithful search of a flawed goal.

That is also why open-ended generation abandons beam search fully. Sampling methods — temperature, top-k, nucleus — on purpose don't take the most probable path, because for creative text the most probable path is the boring one. Beam search lives where there's a right answer: translation, speech spotting, constrained generation.

Where greedy loses, on a tree you can check

The claim that greedy decoding optimises the wrong thing is a two-step example away. Suppose the first token is A with chance 0.6 or B with 0.4, and the best continuation after each is:

first tokenP(first)best P(second)sequence probability
A0.60.50.30
B0.40.90.36

Greedy takes A, because 0.6 beats 0.4, and lands on a sequence of chance 0.30. Beam search with width 2 keeps both A and B alive, discovers that B leads to a 0.9 continuation, and returns the 0.36 sequence — the more probable *sequence*, which the greedy first choice ruled out at step one. That is the entire arg for beam search: the highest-chance first token can open into a region where all after it's worse.

It's still a heuristic, not a global search, and it has its own trap. Sequence chance is a product of per-token terms. So Each extra token times by a thing below 1 and longer sequences score lower automatically — which is why unnormalised beam search truncates, and why each production decoder cuts the log-chance by length. Widen the beam too far and quality can even fall, because a more faithful search of the model's chance finds the blandest, safest text. That is the beam-search curse, and it's why open-ended generation samples instead.

The deeper reason open-ended generation abandons beam search is that the model's notion of probable isn't the same as good. A more faithful search of the chance surface settles on the safe, plain continuation — the blandest text is often the most probable. So past a beam width of about five, translation and summarisation quality can fall rather than rise. Beam search earns its place only where there's a truly correct answer to find: translation, speech spotting, constrained decoding. For creative text, temperature, top-k and nucleus sampling on purpose avoid the most probable path, because that path is just the one worth leaving.

And the cost isn't free: a beam of width k times both work and memory by k, which for a large model is often the binding constraint long before quality is, so the width is chosen against a hardware budget as much as against the translation it buys.

Where it goes wrong with Seq2seq and Beam Search

No length norming. All comes out truncated.

A very wide beam on open-ended text. Blander, not better.

Using beam search for creative generation. Use sampling.

Losing the cost. Width k times both work and memory by k, and for a large model that is the binding constraint.

Questions people ask about Seq2seq and Beam Search

What is a seq2seq model? An encoder reads the input sequence into a representation and a decoder generates the output sequence one token at a time.

Why is greedy decoding not enough? Picking the best token at each step can lock you into a poor path. A slightly worse first word may lead to a much better whole sentence.

How does beam search work? It keeps the k best partial sequences at each step and extends all of them. At the end it returns the best complete one.

Why does beam search favour short outputs? Each token multiplies in a probability below one, so longer sequences score lower. Length normalisation or a length penalty corrects this.

Is a larger beam always better? No. Past a modest width, quality can drop, and very wide beams tend to produce bland, repetitive text.

When do I sample instead? For creative generation where diversity matters. Use top-k or nucleus sampling rather than a beam, which suits tasks with a single right answer such as translation.

Greedy decoding, and the sentence it cannot find

At each step a decoder picks a token. Taking the most likely one each time isn't the same as finding the most likely sentence -- here is a case where it demonstrably isn't.

example_01.pyNumPy
Output

The next block exhaustively enumerates every sequence and shows which one is actually the most probable.

example_02.pyNumPy
Output

The next block runs beam search and shows how widening the beam finds the better sequence.

example_03.pyNumPy
Output

The next block shows why beam search needs length normalisation.

example_04.pyNumPy
Output

Check yourself

0 of 3

Answer without scrolling back up.

  1. Why can greedy decoding produce a worse sequence than beam search?

  2. Why does beam search need length normalisation?

  3. Why does a very wide beam often make translation worse?

Cheat sheet

Seq2seq and Beam Search

Feed a sequence into an encoder, decode one token at a time, and compare greedy search with beam search to see how wider beams change output.

DEEP LEARNING · vizlearn.in/deep_learning/seq2seq_and_beam_search.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.