Positional Encoding Explained: How AI Knows Word Order | LLM Course #17
"Morning chai" and "chai morning" use the same words, but embeddings and attention alone cannot tell them apart. In Lecture 17 of the LLM Masterclass: why Transformers need positional encoding, learned position tables (GPT-2), the sinusoidal positional encoding from Attention Is All You Need (2017), adding positions to token vectors, rotary position embedding (RoPE, 2021) as turning arrows, and both in NumPy for ChotuGPT. 💻 Code for this lecture (branch lecture-17): https://github.com/programmingline/chotugpt/tree/lecture-17 Lecture 17 of 32 — LLM Masterclass · Module 3: Text to numbers ⚠️ ChotuGPT is a simplified teaching model. It uses 4 dimensions (the original Transformer used 512); the rotation angles in the RoPE animation are illustrative. In this lecture: • Why word order is lost: same embedding everywhere, and attention treats tokens like a bag • Learned position embeddings: one vector per position (GPT-2: 1,024 positions) • Sinusoidal encoding: PE(pos, 2i) = sin(pos / 10000^(2i/d)), PE(pos, 2i+1) = cos(…) • Waves at different speeds, like the hands of a clock; wavelengths from 2π to 10000·2π • Adding position vectors to token vectors: chai at position 0 ≠ chai at position 1 • RoPE (Su et al., 2021): turn queries and keys by their position, so scores depend on distance • NumPy code: positional_encoding() and rope() in chotugpt/embedding.py Chapters: 00:00 Lecture 16 quiz answer 00:11 Positional encoding 00:23 Why word order is lost 00:48 Word order is meaning 00:55 A learned position table (GPT-2) 01:08 Sine and cosine waves (2017) 01:45 Adding position to token vectors 02:12 RoPE: rotary position embedding (2021) 02:48 Positions in NumPy 03:20 Quiz 03:41 Recap & next lecture ✅ Lecture 16 quiz answer: A, a lookup table of learned vectors. Each token ID simply pulls out its row. ❓ Quiz: Why does a Transformer need positional encoding? A) Attention alone ignores word order B) To save memory C) To translate languages. Comment your answer. I reveal it in Lecture 18! 👍 Like, subscribe and comment your quiz answer. ⏮ Previous: Lecture 16 — Embeddings: meaning as geometry ⏭ Next: Lecture 18 — Self-attention, part 1: queries, keys and values Sources: A. Vaswani et al., "Attention Is All You Need", NeurIPS 2017 (section 3.5, positional encoding). J. Su, Y. Lu, S. Pan, A. Murtadha, B. Wen, Y. Liu, "RoFormer: Enhanced Transformer with Rotary Position Embedding", arXiv:2104.09864, 2021. A. Radford et al., "Language Models are Unsupervised Multitask Learners", OpenAI 2019 (GPT-2). H. Touvron et al., "LLaMA: Open and Efficient Foundation Language Models", 2023. #LLM #Transformer #PositionalEncoding…more
"Morning chai" and "chai morning" use the same words, but embeddings and attention alone cannot tell them apart. In Lecture 17 of the LLM Masterclass: why Transformers need positional encoding, learned position tables (GPT-2), the sinusoidal positional encoding from Attention Is All You Need (2017), adding positions to token vectors, rotary position embedding (RoPE, 2021) as turning arrows, and both in NumPy for ChotuGPT. 💻 Code for this lecture (branch lecture-17): https://github.com/programmingline/chotugpt/tree/lecture-17 Lecture 17 of 32 — LLM Masterclass · Module 3: Text to numbers ⚠️ ChotuGPT is a simplified teaching model. It uses 4 dimensions (the original Transformer used 512); the rotation angles in the RoPE animation are illustrative. In this lecture: • Why word order is lost: same embedding everywhere, and attention treats tokens like a bag • Learned position embeddings: one vector per position (GPT-2: 1,024 positions) • Sinusoidal encoding: PE(pos, 2i) = sin(pos / 10000^(2i/d)), PE(pos, 2i+1) = cos(…) • Waves at different speeds, like the hands of a clock; wavelengths from 2π to 10000·2π • Adding position vectors to token vectors: chai at position 0 ≠ chai at position 1 • RoPE (Su et al., 2021): turn queries and keys by their position, so scores depend on distance • NumPy code: positional_encoding() and rope() in chotugpt/embedding.py Chapters: 00:00 Lecture 16 quiz answer 00:11 Positional encoding 00:23 Why word order is lost 00:48 Word order is meaning 00:55 A learned position table (GPT-2) 01:08 Sine and cosine waves (2017) 01:45 Adding position to token vectors 02:12 RoPE: rotary position embedding (2021) 02:48 Positions in NumPy 03:20 Quiz 03:41 Recap & next lecture ✅ Lecture 16 quiz answer: A, a lookup table of learned vectors. Each token ID simply pulls out its row. ❓ Quiz: Why does a Transformer need positional encoding? A) Attention alone ignores word order B) To save memory C) To translate languages. Comment your answer. I reveal it in Lecture 18! 👍 Like, subscribe and comment your quiz answer. ⏮ Previous: Lecture 16 — Embeddings: meaning as geometry ⏭ Next: Lecture 18 — Self-attention, part 1: queries, keys and values Sources: A. Vaswani et al., "Attention Is All You Need", NeurIPS 2017 (section 3.5, positional encoding). J. Su, Y. Lu, S. Pan, A. Murtadha, B. Wen, Y. Liu, "RoFormer: Enhanced Transformer with Rotary Position Embedding", arXiv:2104.09864, 2021. A. Radford et al., "Language Models are Unsupervised Multitask Learners", OpenAI 2019 (GPT-2). H. Touvron et al., "LLaMA: Open and Efficient Foundation Language Models", 2023. #LLM #Transformer #PositionalEncoding