AboutWhat I doProjectsLabContact
  • PYTORCH
  • TRANSFORMERS
  • NLP

Transformer from scratch

Transformer from scratch

A GPT-style language model implemented from scratch in PyTorch: multi-head attention, causal masking and residual blocks written by hand, plus a byte-pair tokenizer and an ablation study. Code at github.com/delcenjo/transformer-from-scratch.

A GPT-style language model written from scratch in PyTorch: multi-head attention, causal masking, residual blocks and positional embeddings by hand, with basic operations instead of torch.nn.Transformer. The byte-pair tokenizer is handwritten too. The goal was to be able to look inside when something breaks.

  • Val. perplexity6.89
  • No pos. embeddings8.04
  • Tokenizerhandwritten BPE

Writing it to understand it

Reading the attention paper is not the same as implementing the head reshape and getting the dimensions wrong three times. Every piece is written with basic operations and covered by tests, so the mechanics are visible and safe to poke at.

Ablations: breaking things on purpose

The character baseline reaches a validation cross-entropy of 1.93 (perplexity 6.89). Removing the positional embeddings pushes it to 8.04 — without positions, attention cannot tell token order apart. And the surprise: a single attention head scored 6.76, slightly better than several, at this model size.

What I keep

Now, when a transformer does not converge or a tokenizer splits a word in the wrong place, I have a mental model of which piece to look at. No course gave me that intuition: it came from implementing every tensor and watching it fail.

Interactive notebook

Runs real Python in your browser via Pyodide (WebAssembly), no server involved: the first load takes a few seconds to start the interpreter, and after that every cell can be re-run.

Real ablations and the trained model generating text in numpy

A runnable Jupyter notebook: real Python running in your browser via Pyodide, no server involved.

Shall we connect?

Let's talk: feedback, collaboration or an opportunity.

You can write to me about a project, a technical question, to give me feedback, or about an internship or a first junior role. I always reply.

Write to me

Next projects:

Built with

PythonPyTorchscikit-learnpandasNumPyJupyterHugging FaceLangChainFastAPIPydanticDockerGitHub ActionsPrometheusGrafanaMLflowPostgreSQLGitGitHubLinuxStreamlitSQLiteAnthropicBashPyPIOllamaPythonPyTorchscikit-learnpandasNumPyJupyterHugging FaceLangChainFastAPIPydanticDockerGitHub ActionsPrometheusGrafanaMLflowPostgreSQLGitGitHubLinuxStreamlitSQLiteAnthropicBashPyPIOllama
hola@jmwebsoluciones.com