Chammarychammary

Let's build GPT: from scratch, in code, spelled out.

Andrej Karpathy · 1:56:20 · 3 years ago

Building a functional GPT-style language model involves constructing a neural network that uses self-attention to allow tokens to communicate with one another, iteratively processing text to predict upcoming characters.

  • Data preparation — Text is converted into integer sequences for processing and split into training and validation groups to evaluate how well the model generalizes .

  • Self-attention mechanism — Each token creates "query" and "key" vectors; the dot product of these determines how much information the model aggregates from other tokens in the sequence .

  • Causal masking — A lower-triangular matrix prevents tokens from accessing future data, which ensures the model remains autoregressive during training .

  • Deep network stability — The architecture incorporates two essential features to keep the model trainable as depth increases:

    • Residual connections — These provide a path for gradients to flow unimpeded, preventing signal degradation .
    • Layer normalization — This maintains a consistent distribution of features across tokens .
  • Multi-head parallelization — Multiple attention channels running simultaneously allow the model to learn different types of token relationships at once .

  • Training stages — The base model is trained to complete documents, but behaving as an assistant requires separate fine-tuning steps, such as using reinforcement learning .

  • How does the "scaled" version of attention improve model stability?

  • What is the difference between an encoder block and a decoder block?