Back to projects

Machine learning

Language models from scratch

Building GPT-style transformers from their individual components, then adapting them for instructions and spam classification.

  • PyTorch
  • Transformers
  • Tiktoken

Overview

Understanding what happens inside a language model

Language models turn a sequence of words into a prediction about what comes next. This project explores that process by building the main components directly, rather than treating a transformer as a black box.

I worked from attention and embeddings up to a GPT-style model, then explored two practical adaptations: following instructions and classifying SMS messages as spam or ordinary messages.

The project connects the mathematics of language modelling with the software needed to prepare text, train a model, save its state, and generate output. It is an educational implementation of established architectures.

Technical detail

From text to next-token predictions

Text is split into tokens using the GPT-2 tokenizer. Token embeddings represent their content, while positional embeddings represent their order. A stack of transformer blocks processes those representations before a final projection produces vocabulary logits.

  1. Text and tokenisation
  2. Token + positional embeddings
  3. Causal transformer blocks
  4. Vocabulary logits
  5. Next-token generation

The PyTorch implementation separates multi-head attention, feed-forward layers, normalisation, and residual connections into individual modules. Causal masking prevents a position from looking at later tokens during next-token prediction.

Adapting the same foundation

Instruction fine-tuning formats instruction, input, and response examples into training sequences. The spam-classification workflow replaces the vocabulary output with a two-class head and selectively trains later layers, reusing the pretrained language representations.

The current training configuration uses 256-token windows, 12 transformer layers, 12 attention heads, and an embedding width of 768. The shorter context reduces attention memory requirements at the cost of how much preceding text the model can use.

What the training curves show

Recorded language-model training and validation lossTraining loss declines from 3.985 to 3.187 and validation loss from 4.857 to 4.294 over five logged epochs. Exact values are in the table below.5.04.03.0Logged epoch12345TrainingValidation
Training and validation loss across a five-epoch sequence. Validation tracks a separate text split, helping show how learning extends beyond the training examples. Explore the training history.
Recorded loss values
EpochTraining lossValidation loss
13.9854.857
23.5744.421
33.3394.476
43.2374.403
53.1874.294

Across this five-epoch training sequence, validation loss falls from 4.857 to 4.294, while training loss falls from 3.985 to 3.187. The gap between them makes generalisation an important part of interpreting the result.

These losses describe one training sequence, not a comparison with commercial models. The current workflow uses a sequential text split; document-level holdouts and fixed generation evaluations would provide a stronger picture of performance on unfamiliar material.