Neural Network Mechanics • "Attention Is All You Need"

How Do Transformers Work?

Transformers process natural language in parallel by learning which words relate to each other. Type any sentence or pick a scenario below to compute scaled dot-product self-attention matrices (Q, K, V), switch attention heads, and inspect token contextualization in real time.

Click a Query Token (Qi) to inspect incoming attention weights: Query: "bank"
Attention Bipartite Graph (Source Q → Target K)

Attention Formula: Softmax(Q × KT / √dk)

A[i, j] = exp(q_i · k_j / √d_k) / Σ exp(...)

Query vector compares alignment with each Key vector. Softmax normalizes row sum to exactly 1.0 (100%).

Top Attended Targets for Selected Token

Target Token (Key) Raw Dot Prod Weight (α)
Calculated: 6 × 6 attention matrix across 4 heads. Normalization valid (Σ = 1.0).

The Core Innovations of the Transformer

Step 1

No Recurrence (Parallelization)

Older RNNs (Recurrent Neural Networks) processed words sequentially: step 1, then step 2, etc. Transformers compute every word simultaneously across time using Positional Encodings added to token embeddings, unlocking massive GPU training throughput.

Step 2

Query, Key, and Value Projections

Each word embedding is projected via linear learned weight matrices into three vectors: Query (Q) asks "What am I looking for?", Key (K) responds "What information do I have?", and Value (V) carries the actual content passed forward.

Step 3

Multi-Head Attention

A single attention distribution can only focus on one kind of relationship at a time. By splitting projections into multiple independent heads (e.g. 8 or 96 in GPT-4), the model simultaneously tracks syntax (verbs to objects), pronouns (coreference), and semantic disambiguation.

Step 4

Residuals & Feed-Forward (MLP)

After attention aggregates contextual values, a Residual Connection (x + Sublayer(x)) preserves earlier information and prevents vanishing gradients. A two-layer feed-forward network then stores factual memories and refines the contextualized tokens.

Why is the dot-product divided by √dk?

As the key dimension d_k grows large, dot products grow proportionally in magnitude. Very large inputs into the Softmax function push it into regions with extremely small gradients (the vanishing gradient problem). Dividing by √dk stabilizes variance to 1.0.

How does the Transformer handle disambiguation (e.g., "bank")?

In isolation, "bank" has multiple senses (financial institution vs. river embankment). When self-attention calculates high affinity between "bank" and "river", the contextualized vector for "bank" incorporates the value vector of "river", shifting its representation toward the geological sense before passing to subsequent layers.

What is causal masking in decoder-only models (like GPT)?

In generative models, future tokens are masked out with -∞ before the softmax step. This ensures position i can only attend to positions ≤ i, preventing the model from cheating during autoregressive next-token prediction.

Enjoy this tool? Build your own with Super