Skip to content

Transformers — Tokens, Attention, Positional Codes, and How They Relate to MLPs/CNNs/GNNs | MIT 6.7960 Lecture 8

Aug 30, 20261 min
TL;DRA Transformer is not an architecture from nowhere: tokens discretize data, attention does soft aggregation, positional codes restore order. Seen next to MLPs/CNNs/GNNs, all of them are special cases of 'weighted aggregation over neighbors'.

🌏 中文版

Video status: Videos included. Source details

Source version: based on MIT 6.7960 Fall 2024 OCW. Videos, slides, and assignments are public at MIT OCW. This lecture is taught by Phillip Isola; the required reading is the Transformers note (examples are visual, but the architecture applies to any data).


The three core ideas

Lecture 8 breaks the Transformer into three independent but interlocking ideas:

  1. Token: slice the input into a set of discrete units (subwords for text, patches for images, time steps for sequences). Tokenization lets one architecture handle arbitrarily structured data.
  2. Attention: each token "looks at" every other token and aggregates their information weighted by relevance. This is a soft, data-driven message aggregation — not the hard-coded local receptive field of a CNN.
  3. Positional code: because attention itself is order-agnostic (shuffle the tokens and the attention output just permutes), you must explicitly inject "position" into the representation.

Together these yield an architecture that handles variable-length, arbitrarily structured input and models global dependencies.

The math and intuition of attention

Scaled dot-product attention at its core:

Attention(Q, K, V) = softmax(Q Kᵀ / √d_k) V
  • Q (query), K (key), V (value) are all linear projections of the input tokens.
  • Q Kᵀ measures how well "what this token is looking for" matches "what that token offers" — a similarity score.
  • softmax turns scores into a probability (normalized weights).
  • Those weights do a weighted average over V, producing each token's output representation.

Multi-head attention simply projects Q/K/V into several subspaces, runs attention in each, then concatenates — letting the model attend to different kinds of dependency (syntax, semantics, distance, …) at once.

A minimal PyTorch implementation:

import torch
import torch.nn.functional as F

def scaled_dot_product_attention(q, k, v):
    # q, k, v: (batch, seq_len, d_k)
    d_k = q.size(-1)
    scores = (q @ k.transpose(-2, -1)) / (d_k ** 0.5)
    weights = F.softmax(scores, dim=-1)
    return weights @ v, weights

Positional encoding: restoring order

Pure attention is permutation equivariant — change the input order and the output merely reorders; the model itself has no notion of before/after. Three common fixes:

  • Sinusoidal (Vaswani original): position vectors from sine/cosine at varying frequencies, added to token embeddings. Benefit: extrapolates to sequences longer than seen in training.
  • Learned positional embedding: simply learn a position lookup table. Simple, but does not naturally extrapolate.
  • RoPE (Rotary Position Embedding): encodes position into a rotation so that relative position shows up directly in the inner product — one of the主流 choices in today's LLMs.

The choice is fundamentally a trade-off between giving up extrapolation for expressive flexibility.

Why it is family with MLP / CNN / GNN

The most illuminating point of this lecture: put the Transformer next to the architectures you already studied, and they are all special cases of "weighted aggregation over neighbors (message passing)" — differing only in who counts as a neighbor and how weights are computed:

  • MLP: each neuron sees every neuron in its layer (fully connected = everyone is a neighbor); weights are fixed parameters.
  • CNN: each pixel sees only its local 3×3 neighbors; weights are spatially shared convolution kernels.
  • GNN: each node sees only graph-connected nodes; weights are decided by edges.
  • Transformer: each token sees all tokens (global neighbors); weights are computed dynamically from the data itself (QK similarity).

So the Transformer does not "overturn" earlier architectures — it expands the neighbor set to global and turns static weights into dynamic, data-dependent ones. That also explains why it shines at long-range dependency (e.g., a pronoun at the end of a sentence referring to a noun at the start).

Why this matters in practice

  • Do not mythologize the Transformer: it is an architecture with very weak inductive bias (global attention assumes almost no structure), so its data efficiency is usually worse than CNNs (vision) or GNNs (graphs), yet its ceiling is highest when data is abundant.
  • Attention is not free: the O(n²) sequence-length complexity means long sequences are VRAM-hungry. In practice mitigate with FlashAttention, sparse attention, or chunking long sequences.
  • Positional encoding is a hidden landmine: if your task is order-sensitive (most are) and you forget positional codes, performance collapses.

Course video sources

Rechecked against the live MIT OCW Fall 2024 gallery on 2026-10-10: the lecture number matches and the video is public and embeddable. Lecture numbering and topics follow OCW.

YouTube:MIT 6.7960 Fall 2024 — Lec 08. Architectures: Transformers

Original videos: MIT 6.7960 Fall 2024 — Lec 08. Architectures: Transformers

Course and recording entries:

Checked: 2026-10-10.

Content check: verified against the video transcript (2026-10-10): read the Lec 08 transcript (Phillip Isola, 1:14:35): the video covers tokens, attention (query/key/softmax), positional codes, and compares Transformers side by side with MLPs, CNNs and GNNs, matching the article; nothing needed correcting.

Update Log

  • 2026-10-10: Added explicit video status and checked recording sources and access notes.
  • 2026-10-10: Rechecked video status. Live-checked the official course page: lecture numbers and recording links match and the videos are public, so the status is now “Videos included.”
  • 2026-10-10: Checked the video content against its transcript. The video topic matches the article; no correction needed.

References