liveBook · Manning

Next-token prediction

As explained in Build a Text-to-Image Generator (from Scratch)

Next-token prediction is a sequence-generation technique that predicts the next discrete token from preceding tokens and conditioning information.

Next-token prediction generates a sequence one token at a time: the model predicts each next token from preceding tokens and any conditioning input. In a transformer, a causal mask prevents the decoder from using future tokens. section 2.2.2 section 4.1.2

Training and generation use the same next-token relationship in different ways:

  • During training, shift the token sequence to create inputs and targets. Compare the decoder’s predictions with the target tokens using cross-entropy loss, then update model parameters. section 2.2.2 section 4.1.2
  • During generation, begin with a start token, predict the next token, append it to the sequence, and repeat until an end token appears. section 2.4.2 section 4.1

Captioning an image

For the caption “a dog runs,” the input is “<start> a dog runs” and the target is “a dog runs <end>.” At inference, the decoder generates the caption one token at a time from image features and the tokens already produced. section 4.5 section 4.1.2

Figure 1.3 How the min-DALL-E model generates an image based on the prompt “panda with a top hat reading a book.” The top-left subplot shows the output when 32 image patches are generated. The second subplot in the top row shows the output when 64 patches are generated. The remaining images show the outputs for 96, 128, . . ., and 256 patches.
A collage of a panda reading a book

Description automatically generated

See how successive predictions build an image from patches, from the first 32 patches through the completed image. section 1.2

How Build a Text-to-Image Generator (from Scratch) covers it

How deep it goes
Explains shifted training targets, cross-entropy loss, causal masking, and autoregressive decoding.
Examples
Uses German-to-English translation, image captions such as “a dog runs,” and sequential image-patch generation.
What you can do after
Builds encoder–decoder transformers that train on shifted token targets and generate outputs one token at a time.
How it connects
Connects next-token prediction to transformers, causal attention, encoder–decoder architecture, and cross-entropy loss.

Also explained in

Compare how 9 books explain next-token prediction →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Text-to-Image Generator (from Scratch). The section links point to the parts of the book it draws on.