Lex AIMeridianby Lex AI

Build and Train Your Own GPT-2 Model

This course walks through every component of GPT-2, embeddings, causal attention, residuals, layer norm, feed-forward blocks, and assembles them into a decoder-only transformer trained end-to-end.

Prerequisites

  • Deep Neural Networks, especially backprop, softmax, and cross-entropy
  • Comfortable Python and PyTorch (tensors, nn.Module, DataLoader, autograd)
  • Recommended (not required): Attention Is All You Need for the original 2017 paper context. This course stands alone, every transformer concept used here is taught from scratch in code.
Course mapHover any lesson to see why it matters

Prerequisites

Attention Is All You Need

All transformer components: embeddings, attention, layer norm, FFN, encoder/decoder stacks

Deep Sequence Modelling — RNN

Sequence generation, next-token prediction, loss over time steps

Lessons

01Intermediate

Problem with RNNs and LSTMs

RNNs cannot scale to billion-token contexts

02Beginner

Token embeddings

Vocabulary IDs → learned d-model-dimensional vectors

03Intermediate

Positional embeddings

Add sinusoidal position codes so attention knows token order

04Intermediate

Attention & multi-head attention

12 parallel heads, each 64-dimensional, learning different patterns

05Intermediate

Causal masking

Upper-triangular mask forces left-to-right generation

06Intermediate

Residual connections

Add layer input to output — gradients flow through 12 stacked blocks

07Intermediate

Layer normalization

Pre-norm per token stabilises activations through 12 layers

08Intermediate

Feed-forward neural networks

Expand 4× to 3072, apply GELU, contract back to 768

09Intermediate

Generation of next tokens

Sequence → logits → sample: greedy, top-k, top-p, temperature

10Advanced

Decoder-only transformer

Stack 12 masked-attention + FFN blocks — no encoder needed

Unlocks

Fine-tuning GPT-2 on custom data

Adapt pretrained weights to your domain or task with SFT

RLHF & alignment

Reward modelling and PPO to align generation with human preferences

Inference optimisation & deployment

KV-cache, quantisation, and serving LLMs at scale

Finished this lesson?

Read through the lesson first (0/20s · scrolled 0% of 70%).