Skip to main content
Primary: Phase 5Technical TutorialAdvancedFree Learning Material
Harvard NLP

The Annotated Transformer

Line-by-line PyTorch implementation and mathematical walkthrough of the original 'Attention Is All You Need' Transformer paper.

Written by Sasha Rush and collaborators at Harvard NLP (updated to modern PyTorch), The Annotated Transformer presents the seminal 2017 Vaswani et al. paper as an executable literacy document. Every mathematical equation for scaled dot-product attention, multi-head projection, positional encoding, residual layer normalization, and label smoothing is immediately followed by clean PyTorch code.

Resource Tracking

Course status is tracked independently from roadmap topic checkboxes so you can mark external courses without altering your topic mastery percentage.

Suggested Learning Position

Phase 5 Line-by-Line Code & Paper Companion (Modules 5.1–5.3)

Language:English
Access & Link Verification

100% free open-source technical article and executable Jupyter notebook.

Status: Verified (2026-11-09)

What You Will Learn from This Resource

  • Translate scaled dot-product and multi-head attention equations directly into batched PyTorch tensor code.
  • Understand attention masking, positional encodings, and encoder-decoder vs. decoder-only stacks.
  • Trace tensor shapes `[B, H, T, d_k]` through every projection and residual connection.

Recommended Prerequisites

Review these foundations before starting so you get the most out of this material:

  • PyTorch `nn.Module` fluency and matrix multiplication broadcasting (Phases 1 & 3).

Topics & Keywords

Harvard NLPAnnotated TransformerSelf-AttentionMulti-Head AttentionPyTorch
Curriculum Integration

How This Resource Fits Into Your AI & LLM Roadmap

Open Interactive Roadmap

Phase-Specific Study Recommendations (1)

Phase 5 · Resource 5.3Practice & Reference

The Annotated Transformer

In Phase 5: Transformers From Scratch

Read alongside Modules 5.1–5.3 and the 'Attention Is All You Need' paper to see how every equation maps line-by-line into PyTorch tensor operations.

Continue Exploring
Browse Full Library
Phase 5Research Paper
Original Research Authors
AdvancedFree Learning Material

Attention Is All You Need

The landmark 2017 research paper by Vaswani et al. that introduced the Transformer architecture based entirely on self-attention mechanisms, replacing recurrence and convolutions.

Research PaperarXivAttention Is All You NeedTransformersSelf-Attention
Your Status:
Phases 3, 4, 5Recommended StartVideo Series
Andrej Karpathy
IntermediateFree Learning Material

Neural Networks: Zero to Hero

A code-first introduction to neural networks covering automatic differentiation, backpropagation, language models, and GPT implementations.

Used across:Phase 3 (3.1)Phase 4 (4.3)Phase 5 (5.1)
Andrej KarpathyMicrogradMakemoreBackpropagationAutograd
Your Status:
Phases 5, 6GitHub Repository
Andrej Karpathy
AdvancedFree Learning Material

nanoGPT

The simplest, fastest, and most readable open-source repository for training and fine-tuning medium-sized GPT language models (~300 lines of model code and ~300 lines of training loop).

Used across:Phase 5 (5.4)Phase 6 (6.3)
nanoGPTAndrej KarpathyGPT-2PyTorchPretraining
Your Status: