Learn how the attention mechanism in LLMs helps convert base token embeddings into rich context-aware embeddings.
Attention in Transformers: Concepts and Code in PyTorch
Instructor: Josh Starmer
Earn an accomplishment with PRO · Updated Oct 7, 2025

- Beginner
- 1h16m
- 11 Video Lessons
- 3 Code Examples
- 1 Graded Assignment PRO
- Earn an accomplishment with PRO
- Updated Oct 7, 2025
- Instructor: Josh Starmer
StatQuest- Learn more aboutMembership PRO Plan
What you'll learn
Understand the Query, Key, and Value matrices, what they are for, how to produce them, and how to use them in attention.
Learn the difference between self-attention, masked self-attention, and cross-attention, and how multi-head attention scales the algorithm.
Course recap
PROThis course provides a deep conceptual and practical understanding of the attention mechanism in transformers, the architecture underlying modern LLMs. It covers word embeddings, positional encoding, self-attention vs masked self-attention, and their implications for encoder-only vs decoder-only transformer architectures.
Concept map
Key concepts
- Attention MechanismMechanism that helps establish relationships among words by calculating similarities and using them to make associations
- Self-attentionLooks at words before and after the word of interest; creates context-aware embeddings; used in encoder-only transformers
- Masked self-attentionIgnores words after the word of interest; enables generation; used in decoder-only transformers (like ChatGPT)
- Word embeddingsLearned numerical representations where similar words used in similar contexts get similar vectors
- Positional encodingAdding word order information to embeddings since transformers process all tokens simultaneously
- Context-aware embeddingsEmbeddings that capture sentence/document meaning, not just individual word meaning; produced by self-attention + positional encoding
- Encoder-only transformersCreate context-aware embeddings for classification and clustering tasks
- Decoder-only transformersTrained for text generation using masked self-attention
Lesson highlights
- 1.Introduction — Attention overview and course structure
- 2.Self-Attention vs Masked Self-Attention — Profound differences in capabilities
- 3.Word Embeddings — Neural network training to create meaningful representations
- 4.Positional Encoding — Adding word order information
- 5.Self-Attention Mechanism — Queries, keys, values, similarity computation
- 6.Multi-Head Attention — Multiple attention heads learning different aspects
- 7.Masked Self-Attention Implementation — Causal masking for generation
- 8.Encoder-Only Transformers — BERT-style models for embeddings
- 9.Decoder-Only Transformers — GPT-style models for generation
- 10.Full Transformer Architecture — Encoder-decoder models
- 11.PyTorch Implementation — Complete attention implementation
About this course
This course clearly explains the ideas behind the attention mechanism. It walks through the algorithm itself and how to code it in Pytorch. Attention in Transformers: Concepts and Code in PyTorch, was built in collaboration with StatQuest, and taught by its Founder and CEO, Josh Starmer.
The attention mechanism was a breakthrough that led to transformers, the architecture powering large language models like ChatGPT. Transformers, introduced in the 2017 paper “Attention is All You Need” by Ashish Vaswani and others, revolutionized AI with their scalable design.
Learn how this foundational architecture works to improve your intuition about building reliable, functional, and scalable AI applications.
What you’ll do:
- Understand the evolution of the attention mechanism, a key breakthrough that led to transformers.
- Learn the relationships between word embeddings, positional embeddings, and attention.
- Learn about the Query, Key, and Value matrices, how to produce them, and how to use them in attention.
- Go through the math required to calculate self-attention and masked self-attention to learn how and why the equation works the way it does.
- Understand the difference between self-attention and masked self-attention, and how one is used in the encoder to build context-aware embeddings and the other is used in the decoder for generative outputs.
- Learn the details of the encoder-decoder architecture, cross-attention, and multi-head attention, and how they are incorporated into a transformer.
- Use PyTorch to code a class that implements self-attention, masked self-attention, and multi-head attention.
Who should join?
Anyone who has basic Python knowledge and wants to learn how the attention mechanism in LLMs like ChatGPT works.
Course Outline
11 Lessons・3 Code Examples- IntroductionVideo・6m
- The Main Ideas Behind Transformers and AttentionVideo・4m
- The Matrix Math for Calculating Self-AttentionVideo・11m
- Coding Self-Attention in PyTorchVideo with Code Example・8m
- Self-Attention vs Masked Self-AttentionVideo・14m
- The Matrix Math for Calculating Masked Self-AttentionVideo・3m
- Coding Masked Self-Attention in PyTorchVideo with Code Example・5m
- Encoder-Decoder AttentionVideo・4m
- Multi-Head AttentionVideo・2m
- Coding Encoder-Decoder Attention and Multi-Head Attention in PyTorchVideo with Code Example・4m
- ConclusionVideo・1m
- Quiz
Graded・Quiz
・10m


Elevate your learning experience with Pro
Upgrade to Pro and gain unlimited accomplishments on your resume
Instructor
Course access is free for a limited time during the DeepLearning.AI learning platform beta!
Want to learn more about Generative AI?
Keep learning with updates on curated AI news, courses, and events, as well as Andrew’s thoughts from DeepLearning.AI!

