liveBook · Manning

Next-token prediction

As explained in Deep Learning with Python, Third Edition

A language-model training objective that teaches the model to predict the next token in a sequence from previous tokens.

Next-token prediction trains a language model to continue a sequence by selecting the most probable token after the tokens already provided. This makes a pretrained model behave like an autocomplete system rather than a naturally conversational agent. section 16.3.1

Prompting changes the likely continuation

A prompt such as “How can I make brownies?” can continue as a forum-style exchange, while “The following brownie recipe is easy to make in just a few steps. You can start by” makes recipe instructions more likely. The model is extending the text pattern, not interpreting the prompt conversationally. section 16.3.1

The practical consequences are straightforward:

  • Expect plausible continuation, not automatically useful answers; usefulness depends strongly on the prompt’s surrounding context. section 16.3.1
  • Treat each label as the next token in the sequence, shifted one position relative to the input token IDs. section 16.3.2
  • During instruction fine-tuning, compute loss for response tokens while excluding the fixed prompt and padded positions. section 16.3.2

How Deep Learning with Python, Third Edition covers it

How deep it goes
Explains the objective through generation examples and shifted labels, then stops short of deriving the training algorithm.
Examples
Uses Gemma continuations for advice, brownie recipes, and factual prompts, plus token-ID batches showing shifted next-token labels.
What you can do after
Evaluate generated text as probable continuation and inspect or prepare next-token targets during Keras-based fine-tuning.
How it connects
Connects next-token prediction with causal language modeling, text generation, tokenization, padding masks, and instruction fine-tuning.

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 Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.