liveBook · Manning

Next-token prediction

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

A language-model training objective in which the model predicts the next token or word from preceding context.

Next-token prediction trains a language model to infer the next word or token from preceding context. The task uses language’s sequential structure to learn context, structure, and relationships without manually collected labels. section 1.1 section 1.6

GPT applies the objective autoregressively: a decoder-only transformer processes text left to right, predicts one next token, and can use each generated output as context for the following prediction. Pretraining on this simple objective supports later text completion, classification, spelling correction, translation, and instruction fine-tuning. section 1.6 section 1.8

A practical training pipeline constructs aligned context and target tensors:

  • Tokenize the source text into token IDs. section 2.6
  • Create an input sequence x and a target sequence y by shifting the same token stream one position: y contains the next token for every position in x. section 2.6
  • Extract fixed-length windows with a configurable stride, convert them to tensors, and batch them with a PyTorch DataLoader. section 2.6

Shifted context and target

With context_size = 4, x = [290, 4920, 2241, 287] and y = [4920, 2241, 287, 257]. The progressively growing contexts therefore produce [290] → 4920, [290, 4920] → 2241, and so on. A data loader returns these aligned tensors in batches.

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

How deep it goes
Explains self-labeling, decoder-only generation, shifted tensors, sliding windows, batching, and masking without deriving optimization mathematics.
Examples
Demonstrates shifted token IDs, decoded word contexts, sliding windows, PyTorch tensors, batches, and stride-controlled overlap.
What it assumes
Assumes basic neural-network and text-processing familiarity while introducing self-supervision, token IDs, shifted targets, and batching.
What you can do after
Build it from scratch by generating tokenized input-target pairs and batched PyTorch data loaders for GPT training.
How it connects
Connects self-supervised learning, tokenization, transformers, decoder modules, autoregressive models, attention, and 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 Build a Large Language Model (From Scratch). The section links point to the parts of the book it draws on.