CourseTransformers: From Attention to Trained, Served and Interpreted Models · Module 2: Self-Attention Mechanics · part 13 of 111
Part 13 · Module 2: Self-Attention Mechanics

Lesson 1: Scaled dot-product attention, derived and built by hand

18 min read·13 Oct 2026
Module 2 · Lesson 1 · About 50 minutes · Needs: Module 1, Lessons 1 and 2 (tensors, dot products, softmax, the copy race)

The situation

Sol 404 at Aurora Station. In Module 1 your transformer beat the GRU, but its attention came from PyTorch's ready-made nn.TransformerEncoderLayer. Kofi Mensah will not sign off a part nobody in the lab has opened. "Write attention yourself," he says, "and show me it gives the same numbers."

This lesson starts tinyformer/attention.py. If you met attention in the ai-engineering course (Module 6, Lesson 1), skim sections 1 and 3; the checks, shapes and head survey are new.

The rest of this course is yours to keep

This course is bought on its own, once, and stays readable afterwards, including the parts added to it later.