Module · 5 lessons

Positional Encoding & Embeddings

Attention has a blind spot: it can't tell word order. Learn why self-attention is permutation-invariant, build token embeddings from scratch, and add position information with both sinusoidal and learned positional encodings — assembling the input layer of a GPT in NumPy.

At a glance

Level
Advanced
Lessons
5 lessons
Time to complete
1 week
Cost
Free forever · no sign-up

Welcome to Positional Encoding & Embeddings, the fourth module — where you fix a blind spot you’ve already glimpsed. Back in Module 1, identical characters produced identical attention rows; that wasn’t a bug, it’s a fundamental property. Self-attention is permutation-invariant: it sees a sequence as an unordered set of vectors and has no idea which token came first. A language model that can’t tell “the boat” from “boat the” is useless, so we have to inject position deliberately.

You’ll start by demonstrating that invariance in NumPy, then build the two halves of a transformer’s input. First, token embeddings — a learned lookup table that turns character ids into vectors, with its forward pass and the scatter-add backward pass that trains it. Then position: the original sinusoidal encoding with its elegant sin/cos construction, and the learned positional embeddings that GPT actually uses, compared side by side. The guided project combines them into the complete input layer — embed the tokens, add their positions — that every later module feeds forward from.

Start with Lesson 1, where shuffling the input leaves attention completely unmoved.

Lessons in this module

Achievement

Complete all 5 lessons to finish the Positional Encoding & Embeddings module.

Start module
Sponsor

Keep DATATWEETS free. Help fund practical data, AI, and engineering lessons for learners worldwide.

Buy Me a Coffee at ko-fi.com