All projects

02Machine learning

Transformer vs. LSTM

Two small language models built from scratch in PyTorch and trained on Shakespeare. Send one prompt to both and compare what each architecture writes.

  • PyTorch
  • Custom BPE
  • FastAPI
  • Next.js
Connecting to the model server…

One prompt, two architectures

Both models trained on Shakespeare with identical settings.

Transformer

Attention

Send a prompt to see how the Transformer continues it.

LSTM

Recurrent

Send a prompt to see how the LSTM continues it.

Project Overview

This project builds a Transformer and an LSTM language model completely from scratch in PyTorch, trains both on Shakespeare's complete works with identical hyperparameters, and puts them side-by-side so you can send a prompt and watch the two architectures diverge. Type a prompt in the box above and press Send.

Transformer

Causal (GPT-style) decoder with multi-head self-attention; it attends to all prior tokens in one parallel pass.

RNN / LSTM

Stacked LSTM cells that compress sequence history into a hidden state, updated token-by-token.

Custom BPE Tokeniser

Built from scratch on the Shakespeare corpus, with no external tokenisation library, vocabulary tuned to Early Modern English.

Architecture Comparison

Both models are trained with identical hyperparameters (same learning rate, batch size, context length, embedding dimension, and number of layers) on the same data split. The only variable is the architecture, making the comparison as controlled as a laboratory experiment.

Transformer Decoder

  • ▸Multi-head self-attention: each token attends to every prior token simultaneously: long-range dependencies are free.
  • ▸Causal masking: future positions are masked so the model can only condition on the past during training.
  • ▸Parallelisable: the entire sequence is processed in one forward pass, making training fast on GPU.
  • ▸Tends to be more coherent at longer range, can reproduce Shakespearean structure more faithfully.

RNN / LSTM

  • ▸Recurrent hidden state: history is compressed into a fixed-size vector updated one token at a time.
  • ▸LSTM gating: input, forget, and output gates mitigate the vanishing gradient problem of plain RNNs.
  • ▸Sequential: tokens must be processed in order; training is slower and GPU utilisation is lower.
  • ▸Often more repetitive at longer range, style differences become apparent after a few tokens.

About This Project

Technologies Used

  • PyTorch: Deep learning framework for implementing, training, and checkpointing both architectures from scratch
  • Transformer architecture: Attention-based decoder with multi-head self-attention and feed-forward sublayers
  • RNN / LSTM: Recurrent network with Long Short-Term Memory cells for sequential language modelling
  • Custom BPE tokeniser: Byte-pair encoding built from scratch on the training corpus, with no external tokenisation library
  • FastAPI: Python backend serving both models via a single inference endpoint, with a lightweight health check for cold-start detection
  • Next.js & React: Frontend framework powering the dual-panel chat interface and side-by-side comparison interface
  • Docker & Railway: Containerised deployment with Docker Compose locally and Railway for production hosting

Key Features

  • Side-by-side panels (violet for Transformer, blue for LSTM); every prompt gets both responses at once
  • Both models trained on identical data with identical hyperparameters for a genuinely controlled comparison
  • Custom tokeniser built from the corpus, with no pre-trained vocabulary
  • Automatic wake-up with live progress while the Railway host cold-starts
  • Auto-resizing textarea with Shift+Enter multi-line support
  • Independent scroll tracking per model panel so outputs can be read at different paces