Udemy
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
Build an LLM from Scratch to Reasoning: Code GPT in PyTorch
New
Rating: 4.0 out of 5(2 ratings)
8 students

Build an LLM from Scratch to Reasoning: Code GPT in PyTorch

Draw it, compute it by hand, code it live: GPT-2, fine-tuning, LoRA and a reasoning model trained with GRPO
Created byThanh Vũ
Last updated 11/2026
English
English

What you'll learn

  • Turn raw text into token IDs, training batches and embeddings, and predict every tensor shape before running the code
  • Compute attention weights by hand and match them with PyTorch, then build causal multi-head attention
  • Build every part of a GPT model and count its parameters exactly
  • Pretrain a small GPT, read its loss and perplexity, and control its text with temperature and top-k
  • Load OpenAI's GPT-2 weights into a model you wrote yourself
  • Fine-tune GPT-2 into a text classifier and measure its accuracy on unseen data
  • Fine-tune GPT-2 medium to follow instructions on a free cloud GPU, and grade its answers with a larger model
  • Add LoRA layers to a frozen model, count the trainable numbers by hand, and fine-tune with a fraction of the cost
  • Measure a small reasoning model on MATH-500 and raise its accuracy with step-by-step prompts, sampling, voting and self-refinement
  • Compute the GRPO loss by hand, then train a model to reason with reinforcement learning and distillation on a free GPU
  • Explain modern ideas such as the KV cache, mixture of experts and the differences between GPT-2 and Llama

Course content

9 sections • 84 lectures • 10h 17m total length
  • Theory 1.1 Predict the next word4:01
  • Theory 1.2 From raw text to an assistant: the course map6:23
  • What an LLM does

Requirements

  • Basic Python: functions, loops, lists and classes
  • High-school math. Matrix multiplication and logarithms are explained when we need them.
  • No PyTorch experience needed: every line is typed and explained as we go
  • A computer that can run Jupyter notebooks, and a free Google account for the GPU chapters

Description

This course contains the use of artificial intelligence.

A chat model can feel like magic: you type a question, and a fluent answer appears. But a language model is not a black box. It is a stack of small steps, and every one of them can be drawn on paper, checked with a calculator and written in a few lines of PyTorch.

Many engineers learn these steps the slow way, piecing them together from research papers and half-finished code. This course gives you the steps in order, one at a time, so each new idea rests on a piece you have already built and tested. When you have written every part of a GPT yourself, a loss that won't drop or a shape that doesn't fit stops being a mystery: you know where to look.

Every chapter follows the same three steps:

  1. Draw it. Short hand-drawn lessons explain one idea at a time, with small examples you can follow.
  2. Compute it by hand. Key numbers (softmax, attention scores, the loss, parameter counts, file sizes) are worked out with real values.
  3. Code it live. We type the code in a real notebook, run every cell, and check each output against the hand calculation.

What you'll build, chapter by chapter:

  • What an LLM does: next-word prediction, and the map from raw text to an assistant.
  • Text to numbers: tokens, byte pair encoding, sliding windows, token and position embeddings.
  • Attention: attention for one word by hand, then query, key and value, the causal mask, dropout and multi-head attention.
  • The GPT model: layer normalization, GELU, shortcut connections and the transformer block. Assemble GPT-2 and count its 124 million parameters.
  • Pretraining: cross-entropy loss and perplexity, a full training loop, temperature and top-k, and OpenAI's real GPT-2 weights loaded into your own model.
  • Fine-tuning for classification: turn GPT-2 into a spam classifier that reaches about 96% accuracy on messages it has never seen.
  • Fine-tuning to follow instructions: train GPT-2 medium (355 million parameters) on 1,100 instructions, then let a larger model grade its answers from 0 to 100.
  • LoRA: freeze the model and train only about 2% of the numbers, and still reach about 97% accuracy on the spam task.
  • Reasoning models: load Qwen3-0.6B, write a grader and score it on MATH-500, make it think step by step, sample and vote, and let it fix its own answers. Then compute GRPO by hand, run reinforcement learning and distillation on a free GPU, and read the training logs to see what makes a run stable or collapse.
  • Extras: the KV cache, long-context attention, mixture of experts, how GPT-2 becomes Llama, today's model family, and preference tuning.

Small models, the same ideas. The models you train here are small enough for an ordinary computer, but they are built from the same parts as today's large models: the same tokenizer ideas, the same attention, the same training loop. What you learn carries over.

What's included: 84 lectures, a quiz at the end of every chapter, and the notebooks from every live lesson, attached to the matching section. Chapters 1 to 8 also end with the exercises from the open-source LLMs-from-scratch repository, solved live.

Hardware: chapters 1 to 6 run on a normal laptop CPU. Chapters 7 and 8 train a larger model, so we switch to a free GPU in Google Colab, and the lessons show every step of that setup. Chapter 9 runs entirely in Colab on a free T4 GPU.

Narration uses an AI-synthesized voice. Code cells follow the open-source LLMs-from-scratch and reasoning-from-scratch repositories (Apache-2.0); the explanations, drawings and examples are our own.

Who this course is for:

  • Developers who use LLMs at work and want to know what happens inside
  • Machine learning learners who know some PyTorch and want to build a real model end to end
  • Students who want to understand transformers by computing them, not just reading about them
  • Researchers and engineers who want a working, readable GPT they can change and experiment with
  • Anyone curious how reasoning models learn to think step by step, and what GRPO actually computes