liveBook · Manning

Greedy algorithm

As explained in Build a Large Language Model (From Scratch)

A decoding strategy that always selects the highest-probability next token at each generation step.

Greedy decoding generates text one token at a time, selecting the vocabulary token with the highest probability at every step. Because softmax preserves logit order, argmax can select directly from logits, although the probability conversion clarifies the pipeline. section 4.7 section 5.6

Worked generation

Starting with “Hello, I am,” the loop appends one predicted token per iteration, producing six additional token IDs. The IDs can then be decoded into text; an untrained model may produce gibberish. section 4.7

How Build a Large Language Model (From Scratch) covers it

How deep it goes
Explains the iterative PyTorch loop, tensor positions, context truncation, softmax, argmax, and concatenation.
Examples
Demonstrates “Hello, I am” generation through encoded IDs, six iterations, decoded output, and an untrained model’s gibberish.
What it assumes
Assumes a GPT model and tokenizer, while demonstrating context encoding, evaluation mode, and token-ID decoding.
What you can do after
Apply it via PyTorch to generate and decode a fixed number of tokens from an encoded context.
How it connects
Connects tokenization, logits, softmax, probability sampling, temperature scaling, and GPT text generation.

Also explained in

Compare how 12 books explain greedy algorithm →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Large Language Model (From Scratch). The section links point to the parts of the book it draws on.