IntermediateEnglish28 lectures · 3h 24m

Attention and Transformers from Scratch

From RNN Seq2Seq via Bahdanau and Luong attention to a full Transformer, built and trained in PyTorch

Watch 3 lectures free

01

What changes after this course

  • ●Instead of reading a new architecture paper as a wall of diagrams, you map each block to code
  • ●Instead of guessing what a Transformer looks at, you plot its attention maps and check
  • ●Instead of explaining attention from a diagram, you explain it in a code review from code you wrote and trained

02

Why it matters now

Attention is the idea behind every modern language model, and libraries hide it behind one import. When a model misbehaves or a new paper changes one block, it is hard to tell what changed from the import alone. This course starts from the problem attention was invented to solve and ends with a Transformer you assembled, trained and used to translate.

03

What you leave with

  • ✓Notebooks for the Seq2Seq bottleneck, attention and Transformer assembly
  • ✓A bonus notebook that trains Bahdanau, Luong and scaled dot-product attention on one corpus, with alignment heatmaps side by side
  • ✓Your own trained Transformer and its cross-attention maps
  • ✓8 lecture scripts in Markdown

04

Skills you'll build

  • →Show why a fixed context vector breaks long sentences, with a measurement you ran yourself
  • →Implement Bahdanau, Luong and multi-head attention in PyTorch
  • →Read “Attention Is All You Need” and point to the code for every component

Inside the course

Screens from the lectures. Every plot and number comes from the notebooks you download, run in Jupyter.

Notebook output: a Seq2Seq model with hidden size 128, whole-sequence accuracy falling from 96.9% at length 4 to 0.4% at length 16

notebooks/seq2seq_bottleneck_EN.ipynb

Longer input, and the fixed vector runs out

An RNN Seq2Seq model whose context vector is 128 wide, tested on longer and longer inputs. Whole-sequence accuracy is 96.9% at length 4, 50.4% at length 10 and 0.4% at length 16. You measure the bottleneck before you fix it.

Lecture 3.2 · Training setup and the first run

Lower-triangular look-ahead mask and a causal attention heatmap with nothing above the diagonal

notebooks/transformer_assembly_EN.ipynb

The look-ahead mask, printed and plotted

A 6 by 6 lower-triangular mask, then softmax over random scores with it applied. Row 0 can only see position 0; row 5 sees everything up to 5. Nothing above the diagonal gets any weight.

Lecture 7.2 · Masking

Timing table: 8 heads, loop 21.93 ms vs batched 15.54 ms; 64 heads, loop 81.93 ms vs batched 42.84 ms

notebooks/transformer_assembly_EN.ipynb

What a Python loop over heads costs

Multi-head attention written as a loop over heads, then as one batched matmul, timed on a CPU. With 8 heads the batched version is 1.41x faster; with 64 heads, 1.91x. The notebook says plainly that these timings are an illustration for this machine.

Lecture 7.4 · Multi head attention and what the loop costs

The assembled Transformer translating 'the book is on the table' to 'le livre est sur la table', with its encoder-decoder attention heatmap

notebooks/transformer_assembly_EN.ipynb

Your Transformer translates, and shows where it looked

The Transformer you assemble from parts translates "the book is on the table" to "le livre est sur la table". The cross-attention map, averaged over heads, shows "livre" reading "book" and "est" reading "is".

Lecture 7.7 · Translating cross attention and what we built

What you'll learn

  • ✓Explain the fixed-context-vector problem of RNN Seq2Seq models and measure it yourself
  • ✓Derive additive (Bahdanau) and multiplicative (Luong) attention and implement both in PyTorch
  • ✓Read 'Attention Is All You Need' and map each component to code
  • ✓Implement positional encoding, masking, multi-head attention, Add & Norm and feed-forward blocks
  • ✓Assemble, train and use a complete Transformer for translation and inspect its cross-attention

About this course

Attention is the idea behind every modern language model, and it was invented to solve one concrete problem: a sequence-to-sequence model squeezes a whole sentence into one fixed vector, and long sentences fall apart. This course starts from that problem and builds all the way up to a working Transformer.

We begin with what language models can do and the roadmap of an NLP engineer. Then we implement an RNN Seq2Seq model in PyTorch and measure its limits: the fixed context vector, what a wider hidden state does and does not fix. We derive the attention mechanism in depth — the core idea, the alignment weights, Bahdanau's additive attention — and implement Bahdanau and Luong attention with alignment visualisations. Next we read "Attention Is All You Need" section by section: self-attention, multi-head attention, positional encoding, feed-forward blocks, encoder and decoder. Finally we assemble a Transformer from parts — positional encoding, masking, multi-head attention, Add & Norm, encoder and decoder layers — train it, translate with it, and inspect cross-attention.

Theory sessions use slides; implementation sessions show the notebook on screen while the narration explains every cell. Small models that train in minutes, full understanding of what each line does. Resources also include a bonus notebook that trains Bahdanau, Luong and scaled dot-product attention on the same corpus, so you can compare the three scoring rules and their alignment heatmaps side by side.

Curriculum8 sections · 28 lectures · 3h 24m

Section 2. Limits of RNN Seq2Seq

  • 🔒Opening questions and the Seq2Seq idea5:54
  • 🔒How a Seq2Seq model is implemented8:30
  • 🔒The problem with a fixed context vector7:30
  • 🔒The arrival of attention7:48

Section 3. Building a Seq2Seq Model

  • 🔒Data encoder and decoder in PyTorch9:18
  • 🔒Training setup and the first run7:12
  • 🔒Does a wider hidden state help Sweeps5:18

Section 4. The Attention Mechanism in Depth

  • 🔒The core idea of attention6:12
  • 🔒Bahdanau attention in detail9:18
  • 🔒Implementing Bahdanau attention5:48

Section 5. Implementing Attention

  • 🔒Bahdanau attention additive scoring training and alignment12:54
  • 🔒Luong attention multiplicative scoring9:24

Section 6. Transformer Architecture

  • 🔒Self attention8:24
  • 🔒Multi head attention and positional encoding9:54
  • 🔒Reading Attention Is All You Need9:48
  • 🔒Feed forward encoder decoder and assembly5:54

Section 7. Assembling the Transformer

  • 🔒Positional encoding8:30
  • 🔒Masking5:12
  • 🔒From one head to many5:12
  • 🔒Multi head attention and what the loop costs6:30
  • 🔒Add and Norm feed forward encoder and decoder layers8:30
  • 🔒Final assembly and training7:24
  • 🔒Translating cross attention and what we built8:00

Section 8. Wrap-up

  • 🔒What we learned6:06
  • 🔒Where to go next5:42

Requirements

  • · A computer with Python 3.10+ (a free Google Colab account is enough for most sessions)
  • · Comfort reading Python code; you do not need to be an expert
  • · Basic PyTorch and an idea of what an RNN does

Who this is for

  • · Developers who use Transformers and want to understand the mechanism
  • · Students starting NLP or preparing for deep-learning interviews
  • · Engineers who want to build a Transformer from parts rather than import one

Read alongside the course

The voice-over in this course is synthesized with a text-to-speech model from scripts written and reviewed by the instructor, and the on-screen material (notebooks, code, slides) is the instructor's own work.

Attention and Transformers from Scratch | SOTAAZ Blog