LESSON 3.6 · Code · 150 min

GPT: autoregressive objectives and weight tying

During training, GPT processes a token sequence in parallel under a causal mask and uses the same sequence shifted by one position as labels, computing next-token cross-entropy at

DIRECT ANSWER · VERIFIED SOURCES ·

Why do both GPT training and generation reduce to next-token prediction?

During training, GPT processes a token sequence in parallel under a causal mask and uses the same sequence shifted by one position as labels, computing next-token cross-entropy at every position. During generation there are no future labels, so the model takes logits at the final position, selects or samples one token, appends it to the context, and repeats. Both estimate the same conditional distribution but execute it differently.

Lecture alignment

Lecture alignment: in Karpathy's build-GPT walkthrough, trace the one-token x/y shift, triangular mask, logits reshape, cross-entropy, and token-by-token generate loop.

Key takeaways

  • The causal mask prevents position t from seeing later labels and leaking training information.
  • Training supervises many sequence positions in parallel; generation feeds each new token back autoregressively.
  • Temperature and top-k change how a token is selected from logits, not the next-token learning objective.

Boundary and caveat

Training receives ground-truth prefixes while generation increasingly consumes its own outputs, so their input distributions are not identical. Tying input and output weights is common but not required, and context length remains bounded by the model configuration.

Primary sources

Learning objectives

  1. Explain what problem “GPT: autoregressive objectives and weight tying” solves without hiding behind terminology.
  2. Trace the variables and causal links across causal language modeling, embedding tying, logits.
  3. Complete “Build miniGPT from scratch” and judge the result with evidence rather than intuition.

Core concepts

causal language modeling

causal language modeling is part of the lesson’s causal model. State its inputs, outputs, invariants, and failure mode; then verify it with a hand-check or a minimal experiment before moving to an optimized implementation.

embedding tying

embedding tying is part of the lesson’s causal model. State its inputs, outputs, invariants, and failure mode; then verify it with a hand-check or a minimal experiment.

logits

logits is part of the lesson’s causal model. State its inputs, outputs, invariants, and failure mode; then verify it with a hand-check or a minimal experiment.

and generation

and generation is part of the lesson’s causal model. State its inputs, outputs, invariants, and failure mode; then verify it with a hand-check or a minimal experiment.

Build and verify

Build miniGPT from scratch

  • Predict: write the expected output, trend, or failure before running code.
  • Build: implement only the minimum components needed to answer the question.
  • Verify: compare with a baseline or trusted implementation; save seeds, parameters, and raw outputs.
  • Transfer: change one shape, dataset, scale, or workload condition and explain whether the conclusion still holds.

Open the complete interactive lesson