Case study 02

GPTs

A decoder-only GPT written from scratch in PyTorch, about 10M parameters.

Overview

A from-scratch implementation of a GPT-style language model in roughly 200 lines of PyTorch. No Hugging Face, no shortcuts: tokenisation, attention, training loop and sampling are all written by hand.

It is trained on the tiny Shakespeare dataset and generates new text in the same style, character by character.

What it does

How it works

  1. 01Encode the corpus at character level and split into train and validation sets.
  2. 02Sample random 256-token blocks as training batches.
  3. 03Run them through embeddings, 6 transformer blocks and a language-model head.
  4. 04Optimise cross-entropy loss with AdamW.
  5. 05Sample from the trained model one token at a time.

Stack

More projects

Work with me