⚡ A GPT-style Language Model from Scratch

Emma describes the structure of a GPT-style LLM, embeddings, Transformer blocks with causal attention and an LM head, and its training objective, predicting the next token. This notebook builds a tiny one with PyTorch and trains it on synthetic discharge sheets in which the diagnosis at the top decides the drug and the warning sign several sentences later.

The interactive notebook includes:

  • A corpus where the context matters.
  • From text to training examples: input and target shifted by one token.
  • Causal self-attention, Pre-LayerNorm Transformer blocks and an LM head with weight tying.
  • Training by next-token prediction with the cross-entropy.
  • The model against a trigram model on 500 new sheets.
  • The attention weights: the head that finds the diagnosis.
  • Generating discharge sheets by sampling, and the incoherent ones.

Want to modify the code and experiment on your own?
Open the notebook in Google Colab

Bibliography