Beginner1h16m

Attention in Transformers: Concepts and Code in PyTorch

Instructor: Josh Starmer

StatQuest logo

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
  • StatQuestStatQuest
  • Learn more aboutMembership PRO Plan

What you'll learn

  • Learn how the attention mechanism in LLMs helps convert base token embeddings into rich context-aware embeddings.

  • 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

PRO

This 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

Concepts in Attention in Transformers: Concepts and Code in PyTorch and the courses that connect to themAttention in Transformers: Concepts and Code in PyTorchAttention in Transformers…Attention MechanismSelf-attentionMasked self-attentionWord embeddingsPositional encodingContext-aware embeddingsEncoder-only transformersDecoder-only transformersBuild and Train an LLM with JAXBuild and Train an LLM wi…Build Long-Context AI Apps with JambaBuild Long-Context AI App…Concepts in Attention in Transformers: Concepts and Code in PyTorch and the courses that connect to themAttention in Transformers: Concepts and Code in PyTorchAttention in Tr…Attention MechanismAttention Mecha…Self-attentionMasked self-attentionMasked self-att…Word embeddingsPositional encodingPositional enco…Context-aware embeddingsContext-aware e…Encoder-only transformersEncoder-only tr…Decoder-only transformersDecoder-only tr…

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. 1.Introduction — Attention overview and course structure
  2. 2.Self-Attention vs Masked Self-Attention — Profound differences in capabilities
  3. 3.Word Embeddings — Neural network training to create meaningful representations
  4. 4.Positional Encoding — Adding word order information
  5. 5.Self-Attention Mechanism — Queries, keys, values, similarity computation
  6. 6.Multi-Head Attention — Multiple attention heads learning different aspects
  7. 7.Masked Self-Attention Implementation — Causal masking for generation
  8. 8.Encoder-Only Transformers — BERT-style models for embeddings
  9. 9.Decoder-Only Transformers — GPT-style models for generation
  10. 10.Full Transformer Architecture — Encoder-decoder models
  11. 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
Unlock certificates

Elevate your learning experience with Pro

Upgrade to Pro and gain unlimited accomplishments on your resume

Learn More

Instructor

Josh Starmer

Josh Starmer

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!