FixTheRobot Explore Robotics / LLM / LM3 / LM3-L1

This lab works on phones, but writing code is much easier on a laptop.

LLM · Embeddings and the Mathematics of Attention · LM3-L1 · Intermediate

Compute Attention Step by Step

Attention is a handful of matrix operations: dot products, a softmax and a weighted sum. Written out for a few tokens, each step is small enough to inspect by eye.

You will see what goes wrong without the scale factor, compute the weights robustly, and mix the values with them.

Your task

    Simulation
    solution.pyLoading Python…
    Ctrl+Enter runs
    What Python can I use?

    Plain Python with math (and slamkit for small matrix helpers: matmul, transpose, solve, inverse, identity). Images, point clouds and detections arrive as lists inside the dictionary D; an image is a list of rows, each row a list of pixels. There is no NumPy or OpenCV: you write the algorithm yourself, which is the point of the lab. Print with print().

    Hints

    What this lab teaches

    Stage 1 shows why attention divides the scores by √d. Stage 2 computes the scaled scores and a numerically safe row-wise softmax. Stage 3 turns weights into outputs by mixing the value rows. The vectors are small random numbers, not embeddings learned from text; the lab teaches the arithmetic of attention, not what a trained model attends to. Your code is graded on hidden matrices.

    Theory for this lab

    Embeddings and the Mathematics of Attention labs

    1. LM3-L1: Compute Attention Step by Step: this lab.
    2. LM3-L2: Implement Causal Masking
    3. LM3-L3: Implement Causal Multi-Head Attention in PyTorch

    Educational simplification: matrices of a few rows and columns with random entries, a single attention head, plain Python lists instead of tensors, and no learned projection matrices. Real models use the same arithmetic on tensors with batch, head and sequence dimensions (Lesson LM3-T6). All data in this lab are invented.

    Get the next lab

    Get notified when new robotics labs are released. One email per new lab, nothing else.