Home

05 Notebook: NumPy for Transformers

notebook attention numpy lab lecture-5

Goal

Build a working scaled dot-product attention from scratch in pure NumPy. The notebook is also a NumPy crash course — if you have never typed np.zeros before, the first six subsections walk you in from the start. By the end you have a 12-line attention function plus a multi-head wrapper plus a feed-forward network.

Open the Notebook

Open in JupyterLite — runs directly in your browser, no installation required.

The notebook uses only NumPy and matplotlib, both pre-installed in JupyterLite. Your progress is saved in your browser; export the notebook if you want a permanent copy.

What You Will Do

  1. NumPy primer — array creation (np.zeros, np.eye, np.arange, np.diag), slicing 1-D and 2-D, the difference between A @ B and A * B
  2. Implement dot_product and my_matmul — two stub-and-test pairs that build the atomic operations attention runs on
  3. Embedding lookup with array indexing — turn token IDs into vectors via E[token_ids]
  4. Broadcasting and the axis= argument — a per-row LayerNorm in three lines
  5. Implement layernorm as a function — wrap LN for reuse
  6. Implement split_heads and merge_heads — the multi-head reshape/transpose dance
  7. Implement causal_mask — foreshadows the next notebook
  8. Capstone — reproduce the lecture’s [12%, 40%, 16%, 11%, 20%] cat-sat-on-it weights, then run multi-head attention on the dragon-drinks toy
  9. Implement my_gelu before adding an FFN to your block

Deliverables (AI Diary entry — notebook §12)

Reference

© 2026 Dominik Müller   •  Powered by Soopr   •  Theme  Moonwalk