cd /news/artificial-intelligence/tiny-causal-attention-lm-from-scratc… · home › topics › artificial-intelligence › article
[ARTICLE · art-182715] src=discuss.huggingface.co ↗ pub= topic=artificial-intelligence verified=true sentiment=· neutral

Tiny causal-attention LM from scratch in NumPy: manual backprop on CPU

A NumPy-only, character-level causal-attention language model trained from random initialization on CPU cut batch loss from 3.367 to 3.177 over 150 SGD steps in a full run of about 21 seconds on Kaggle CPU, according to the notebook's author, who published the code under Apache 2.0. The notebook implements character tokenization, shifted next-token targets, a single-head causal attention mask, cross-entropy, manual gradients through attention and the embedding lookup, and autoregressive sampling. The author cautions the 157-character corpus makes the result in-sample training only, not a generalization benchmark, and that the model omits positional embeddings and feed-forward blocks.

recommendation

Build the smallest inspectable causal-attention LM in NumPy with manual backprop to learn the mechanics, but treat its in-sample loss as a teaching signal, not a benchmark, and add a validation split before drawing conclusions.

more in playbook: Fine-tuning and data
read1 min views6 publishedDec 6, 2026

I put together a runnable, character-level next-token language model to make the mechanics of causal attention inspectable. It is built with NumPy and trained from random initialization, with no pretrained weights, API, GPU, or external dataset.

The notebook covers character tokenization, shifted next-token targets, a single-head causal attention mask, cross-entropy, manual gradients through attention and the embedding lookup, SGD, and autoregressive sampling. On Kaggle CPU, the complete run takes about 21 seconds.

The initial batch loss is 3.367 and the final batch loss after 150 steps is 3.177. That is an in-sample training result, not a generalization benchmark. The corpus is just 157 characters, generated text is rough, and this minimal model omits positional embeddings and feed-forward blocks. It is an educational attention LM, not ChatGPT or a practical chatbot.

Runnable notebook (Apache 2.0): Tiny GPT-Style LM from Scratch (NumPy) | Kaggle I would welcome feedback on the manual backward pass and on the best next teaching step: positional embeddings, a validation split, or a larger corpus.

── more in #artificial-intelligence 4 stories · sorted by recency
── more on @numpy 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/tiny-causal-attentio…] indexed:0 read:1min 2026-12-06 · —