liveBook · Manning

Decoding strategy

As explained in Transformers in Action

Decoding is the process of generating an output sequence conditioned on an input sequence.

A decoding strategy structures how a model generates an output sequence from an input sequence. It navigates a large space of possible token sequences by determining how the next token is selected, affecting coherence, creativity, and diversity. section 4.1 section 4.3

Common strategies differ in search breadth, determinism, and output diversity:

  • Greedy search selects the most probable token at every step, making it efficient but unable to reconsider earlier choices. section 4.1.1
  • Beam search maintains multiple candidate sequences, often improving fluency while tending toward similar outputs. section 4.3
  • Top-k and nucleus sampling restrict stochastic selection to high-probability tokens, promoting diversity. section 4.3
  • Temperature sampling sharpens or flattens the probability distribution to control randomness; combining it with top-p balances coherence and novelty. section 4.3

Greedy generation with model.generate

Tokenize a prompted completion request, load a causal language model, and call model.generate with do_sample=False and max_new_tokens=128. This produces a deterministic continuation by selecting the most probable next token at each step. section 4.1.1

How Transformers in Action covers it

How deep it goes
Explains the decoding-versus-sampling distinction, compares five strategies, and demonstrates greedy generation without detailing model internals.
Examples
Uses a maze analogy, a conditional-probability formulation, and a Transformers greedy-generation listing.
What it assumes
Assumes familiarity with input sequences, output sequences, tokens, conditional probabilities, and transformer-based text generation.
What you can do after
Apply it via Transformers model.generate by selecting deterministic or stochastic generation settings for a desired coherence–diversity trade-off.
How it connects
Connects decoding with sampling, greedy search, beam search, top-k sampling, nucleus sampling, temperature sampling, and prompting techniques.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Transformers in Action. The section links point to the parts of the book it draws on.