{"slug": "tiny-causal-attention-lm-from-scratch-in-numpy-manual-backprop-on-cpu", "title": "Tiny causal-attention LM from scratch in NumPy: manual backprop on CPU", "summary": "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.", "body_md": "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.\n\nThe 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.\n\nThe 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.\n\nRunnable notebook (Apache 2.0): [Tiny GPT-Style LM from Scratch (NumPy) | Kaggle](https://www.kaggle.com/code/jaspindersingh83/tiny-gpt-style-lm-from-scratch-numpy)\n\nI would welcome feedback on the manual backward pass and on the best next teaching step: positional embeddings, a validation split, or a larger corpus.", "url": "https://wpnews.pro/news/tiny-causal-attention-lm-from-scratch-in-numpy-manual-backprop-on-cpu", "canonical_source": "https://discuss.huggingface.co/t/tiny-causal-attention-lm-from-scratch-in-numpy-manual-backprop-on-cpu/193206#post_1", "published_at": "2026-12-06 04:44:03+00:00", "updated_at": "2026-12-06 04:45:46.909433+00:00", "lang": "en", "topics": ["artificial-intelligence", "machine-learning", "large-language-models", "ai-research", "neural-networks"], "entities": ["NumPy", "Kaggle", "Apache 2.0", "Jaspinder Singh"], "also_reported_by": [], "takeaway": "A single-head causal-attention LM with hand-derived gradients through attention and the embedding lookup trains from random init in about 21 seconds on Kaggle CPU, dropping batch loss from 3.367 to 3.177 over 150 SGD steps on a 157-character corpus.", "recommendation": {"advice": "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.", "playbook": "training-data", "playbook_url": "https://wpnews.pro/playbooks/training-data"}, "alternates": {"html": "https://wpnews.pro/news/tiny-causal-attention-lm-from-scratch-in-numpy-manual-backprop-on-cpu", "markdown": "https://wpnews.pro/news/tiny-causal-attention-lm-from-scratch-in-numpy-manual-backprop-on-cpu.md", "text": "https://wpnews.pro/news/tiny-causal-attention-lm-from-scratch-in-numpy-manual-backprop-on-cpu.txt", "jsonld": "https://wpnews.pro/news/tiny-causal-attention-lm-from-scratch-in-numpy-manual-backprop-on-cpu.jsonld"}}