liveBook · Manning

Pretraining

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

The initial training phase in which a language model learns general patterns from a large text corpus before task-specific adaptation.

Pretraining is the initial stage of LLM development: a model learns broad language patterns from a large, diverse corpus by predicting the next token. The resulting foundation model can then be fine-tuned for narrower tasks. section 1.3 section 1.8

GPT-like pretraining connects data preparation, a decoder-based transformer, attention, token prediction, and weight optimization. Large datasets expose the model to syntax, semantics, context, and general knowledge across many topics and languages. section 1.5 section 1.8

A practical pretraining workflow includes:

  • Prepare tokenized input and target batches from unlabeled text. section 1.8 section 5.0
  • Iterate over batches and epochs, reset gradients, calculate cross-entropy loss, backpropagate, and update weights with AdamW. section 5.2 section 5.6
  • Evaluate training and validation losses, generate text samples, and monitor divergence as a signal of overfitting. section 5.2
  • Save, resume, or load openly available weights when full-scale training is impractical. section 5.0 section 5.6

Small GPT-like training run

A PyTorch run initializes a GPTModel, moves it to a device, configures AdamW, and calls train_model_simple for ten epochs. With the prompt “Every effort moves you,” generated text changes from repeated punctuation and words to coherent, though sometimes memorized, passages. Training loss continues falling while validation loss stagnates, indicating overfitting.

Figure 1.3 Pretraining an LLM involves next-word prediction on large text datasets. A pretrained LLM can then be fine-tuned using a smaller labeled dataset.
figure

Shows the two-stage flow from next-word prediction on large text datasets to fine-tuning on a smaller labeled dataset. section 1.3

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

How deep it goes
Descends from definition to a PyTorch training loop, loss evaluation, sampling, and weight management, while deferring advanced optimization techniques.
Examples
Uses GPT-3’s corpus, a small educational dataset, PyTorch training code, loss plots, and generated continuations as concrete examples.
What it assumes
Assumes basic PyTorch and neural-network familiarity while teaching the training workflow, weight parameters, evaluation, and text monitoring.
What you can do after
Builds and evaluates a small GPT-like model, or loads pretrained weights for downstream fine-tuning.
How it connects
Connects next-token prediction with tokenization, transformer architecture, attention mechanism, fine-tuning, cross entropy loss, and AdamW optimizer.

Also explained in

Compare how 14 books explain pretraining →

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.