← govind p lal
AI and ML Bootcamp · Lecture 8 · lecture notes

Training Your First Model: A Mini LLM on Tiny Shakespeare

In short — This lecture puts earlier concepts together and trains a small 3-million-parameter language model in Colab. It covers data prep, next-token targets, dropout, the assembled model, the training loop, and generation with temperature. The model learns to write text that looks like Shakespeare.

Setup and data

builds on Lecture 2: Week 1 - AI and ML | Fast-tracking the course of AI | 18/01/2026, Lecture 4: Week 3 - AI and ML | Transformers- Part 1 | 31/01/2026, Lecture 6: Week 5 - AI&ML | Introduction to Tensors and PyTorch | 14/02/2026

Goal: train a small model on Tiny Shakespeare so it writes text with a Shakespeare-like structure (speaker, lines, reply).

Vocabulary - all tokens the model has access to. Each token gets an ID, and the ID looks up its embedding.

!

Assignment: implement Byte Pair Encoding (BPE) yourself instead of character tokens.

Train/validation split

Keep part of the data unseen during training. It tells us whether the model does well on new data.

Batches and next-token targets

builds on Lecture 2: Week 1 - AI and ML | Fast-tracking the course of AI | 18/01/2026, Lecture 7: Week 6 - AI&ML | What Changed Since 2017 — and Let's Build It | 21/02/2026

Autoregressive models learn by next-token prediction. So the target is the input shifted by one.

Example

X = chars at i..i+n, Y = chars at i+1..i+n+1. For input 'T' the target is 'H'; for 'H' it is 'E'.

RMSNorm in code

adds to earlier · Code-level details: eps=1e-6, learnable weight initialised to ones, worked example. · builds on Lecture 7: Week 6 - AI&ML | What Changed Since 2017 — and Let's Build It | 21/02/2026

Dropout

The Problem: with many parameters the model may memorise the data instead of generalising (overfitting).

The Solution: Dropout - randomly set a fraction of values in a layer to zero.

RoPE in code

adds to earlier · How RoPE is coded: frequencies, even/odd pairs, rotation formula, why only Q and K. · builds on Lecture 7: Week 6 - AI&ML | What Changed Since 2017 — and Let's Build It | 21/02/2026
!

RoPE is not applied to V. Value holds the token's actual information, which should not change with position. Only Q and K decide similarity.

GQA in code

adds to earlier · Implementation: repeat_kv with expand, 8 query heads / 2 KV heads, mask via triu. · builds on Lecture 7: Week 6 - AI&ML | What Changed Since 2017 — and Let's Build It | 21/02/2026, Lecture 5: Week 4 - AI&ML | Transformers: Part 2 | 07/02/2026, Lecture 6: Week 5 - AI&ML | Introduction to Tensors and PyTorch | 14/02/2026

SwiGLU in code

adds to earlier · Code structure with w_gate, w_up, w_down and F.silu. · builds on Lecture 7: Week 6 - AI&ML | What Changed Since 2017 — and Let's Build It | 21/02/2026
def forward(self, x):
    gate = F.silu(self.w_gate(x))
    up = self.w_up(x)
    return self.dropout(self.w_down(gate * up))

F -> torch.nn.functional, the library holding relu, silu and similar operations.

Transformer block and full LM

builds on Lecture 7: Week 6 - AI&ML | What Changed Since 2017 — and Let's Build It | 21/02/2026, Lecture 7: Week 6 - AI&ML | What Changed Since 2017 — and Let's Build It | 21/02/2026, Lecture 6: Week 5 - AI&ML | Introduction to Tensors and PyTorch | 14/02/2026
Input xRMSNormGQA + RoPEx + attentionRMSNormSwiGLU FFNx + FFN
Block: normalise before each sublayer, add the input back after.

Residual connection - add the previous input back so information is not lost and gradients do not shrink.

Full LM: token embeddings -> N transformer blocks -> final RMSNorm -> linear head -> logits.

SettingValue
Vocab size65
Model dim256
Layers4
Query heads8
KV heads2
FFN dim680
Sequence length256
Parametersabout 3 million

.to(device) moves the model to the GPU.

Training run

adds to earlier · Hyperparameters, torch.no_grad for evaluation, reading train vs validation loss curves. · builds on Lecture 3: Week 2 - AI and ML | Neural Networks from Scratch | 24/01/2026, Lecture 6: Week 5 - AI&ML | Introduction to Tensors and PyTorch | 14/02/2026, Lecture 6: Week 5 - AI&ML | Introduction to Tensors and PyTorch | 14/02/2026

Hyperparameters - settings humans choose (steps, batch size, learning rate, eval interval). The model does not learn them.

!

If validation loss falls then rises while train loss keeps falling, the model overfits. Fix: remove parameters or layers, add dropout.

Generation and temperature

adds to earlier · Temperature mechanism, effects at 0.5/0.8/1.2, typical coding values. · builds on Lecture 2: Week 1 - AI and ML | Fast-tracking the course of AI | 18/01/2026

Generate: encode prompt -> logits -> divide by temperature -> softmax -> sample next token -> append -> repeat.

From base model to chat and reasoning models

builds on Lecture 2: Week 1 - AI and ML | Fast-tracking the course of AI | 18/01/2026

Glossary

Vocabulary
All tokens the model can use, each with an ID.
Dropout
Randomly zeroing a fraction of values during training to reduce overfitting.
Hyperparameter
A setting chosen by humans, such as learning rate or batch size.
Temperature
Divisor on logits that controls randomness when sampling.
Residual connection
Adding a layer's input back to its output.
torch.no_grad
Context that stops gradient tracking.
MPS
Apple GPU backend in PyTorch, used instead of CUDA on Macbooks.