Udemy
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
LLM From Scratch in Python: Build a GPT with PyTorch
New
1 students

LLM From Scratch in Python: Build a GPT with PyTorch

Build an LLM from scratch: a tiny GPT in PyTorch with a BPE tokenizer, self-attention, transformer blocks and sampling
Created byAdi Tejus
Last updated 11/2026
English

What you'll learn

  • Build a character tokenizer and a byte-level BPE tokenizer from scratch and measure their trade-offs
  • Write causal self-attention and multi-head attention in PyTorch and verify them against the built-in function
  • Assemble a GPT from embeddings, transformer blocks, residual connections and layer norm, and test each part
  • Train a GPT on a laptop CPU with AdamW, warmup and cosine decay, gradient clipping and honest evaluation
  • Control generation with greedy decoding, temperature, top-k and top-p, and measure the effect on real text
  • Estimate parameters, training compute and memory for any GPT-style model, and speed up generation with a KV cache

Coding Exercises

This course includes our updated coding exercises so you can practice your skills as you learn.

See a demo
Image of coding exercise example

Course content

8 sections • 8 lectures • 1h 52m total length
  • Build a GPT from scratch in Python: a tiny model, trained on a laptop CPU6:53

    Overview. Build a GPT from scratch in Python: a tiny model, trained on a laptop CPU. The finished project running first, then the plan for the course, what you need, and the course code (attached here as a download).

Requirements

  • Basic Python (functions, lists, loops, classes) and a little familiarity with matrices
  • A computer with Python 3.11 or newer and about 1 GB of no-cost disk (CPU-only PyTorch); no graphics card needed
  • Google Colab works too, if you prefer not to install anything

Description

This course contains the use of artificial intelligence.

You build a small GPT from an empty file, step by step, in Python and PyTorch, and train it on an ordinary laptop CPU in minutes. No graphics card, no API key and no paid service: the training text is three public-domain novels by Jane Austen (about two million characters).

You write a character tokenizer and a byte-level BPE tokenizer from scratch and measure what the vocabulary size buys and costs (including why the same question can cost almost six times the tokens in Greek as in English); write causal self-attention in six lines and prove it matches PyTorch's built-in function; build multi-head attention, the MLP, residual connections and layer norm, and measure what happens when each one is removed; write the training loop with AdamW, warmup and cosine decay, gradient clipping and honest evaluation; sample with greedy decoding, temperature, top-k and top-p, measured on real outputs; and finish with parameter counting, compute and memory estimates, a small scaling experiment and a KV cache.

The models are tiny on purpose, and the course is honest about what they can and can't do: the character model (820,224 parameters, about 19 minutes of training) writes text in Austen's style with real names and mostly real words, and sentences that do not mean anything. Every number shown is from a real run (31 October and 8 November 2026); the code, logs, tests, practicals and cheat sheets are included. Narration is AI-voiced; the code, runs and results are real.

The course is 2 h 14 min of video in 8 sections. Section 1 is the overview and setup; the other sections are hands-on, and every program is run for real on screen. Each hands-on section comes with a one-page cheat sheet (PDF), a short quiz, a practical with starter and solution files and an assignment with a model answer; 5 coding exercises let you write the code yourself.

Who this course is for:

  • Developers and AI engineers who use language models and want to know exactly how they work inside
  • Students preparing for AI or machine learning engineering interviews
  • Anyone who finished a beginner Python course and wants a hands-on, measured introduction to transformers