Lensa ML
Lensa ML

The Transformer Architecture

Step 1 of 6

Why Replace Recurrence?

The problem that motivated the paper

Sequence length4
RNNTransformer1234path = 3 hops1234path = 1 hop (any pair)Complexity Comparison (Paper Table 1)Sequential opsO(4)O(1)Max path lengthO(4)O(1)Comp/layerO(n·d²)O(n²·d)
SEQ LEN
4
RNN STEPS
4
ATTN STEPS
1
PATH LEN
3 vs 1

Short sequences are fine for RNNs. But the Transformer paper showed that self-attention connects every token directly in O(1) hops — no information bottleneck, and fully parallelizable across GPUs.

Step 1 of 6: Why Replace Recurrence?

The problem that motivated the paper

Sequence length4
RNNTransformer1234path = 3 hops1234path = 1 hop (any pair)Complexity Comparison (Paper Table 1)Sequential opsO(4)O(1)Max path lengthO(4)O(1)Comp/layerO(n·d²)O(n²·d)
SEQ LEN
4
RNN STEPS
4
ATTN STEPS
1
PATH LEN
3 vs 1

Short sequences are fine for RNNs. But the Transformer paper showed that self-attention connects every token directly in O(1) hops — no information bottleneck, and fully parallelizable across GPUs.

Step 2 of 6: The Architecture

The encoder-decoder stack

ENCODERDECODERInputsOutputs (shifted right)Input EmbeddingOutput Embedding+Positional Encoding+Positional EncodingMulti-Head AttentionAdd & NormFeed ForwardAdd & NormMasked Multi-Head AttnAdd & NormMulti-Head AttentionAdd & NormFeed ForwardAdd & NormLinearSoftmaxOutput Probabilities
COMPONENT
All
ENCODER
N=6
DECODER
N=6
D_MODEL
512

The Transformer (Vaswani et al., 2017) is an encoder-decoder architecture built entirely from attention — no recurrence, no convolution. The encoder reads the full input bidirectionally; the decoder generates output tokens autoregressively with causal masking. Click a component to explore.

Step 3 of 6: Scaled Dot-Product Attention

How tokens decide what to focus on

Attention(Q,K,V) = softmax(QKᵀ / √dₖ) · VHow much should each token pay attention to every other?QueryKeyThecatsatdownThecatsatdown0.230.450.120.200.260.260.250.240.360.430.100.100.260.400.140.21"The" attends to:The23%cat45%sat12%down20%
QUERY
The
ATTENDS TO
cat
WEIGHT
45%
√D_K
8.0

"The" pays 45% attention to "cat". Each token creates a Query vector ("what am I looking for?") and a Key vector ("what do I contain?"). The dot product Q·K measures relevance, divided by √d_k to prevent extreme values, then softmax turns scores into weights that sum to 1.

Step 4 of 6: Multi-Head Attention

Why multiple heads see more than one

Same input, different attention patternsShowing 2 of 8 heads — each learns different relationshipsHead 1dₖ = 64The24%cat20%sat28%down28%focuses on "sat" (28%)Head 2dₖ = 64The23%cat25%sat24%down27%focuses on "down" (27%)Concat(Head 1, Head 2) → Output Projectiond_model=512 → 8 heads × d_k=64 → concat → d_model=512Same total cost as one big head, but 8 different attention patterns
QUERY
The
HEAD 1
sat
HEAD 2
down
SHOWN
2 of 8

When "The" queries, Head 1 focuses on "sat" while Head 2 focuses on "down". Each head projects Q, K, V into a different subspace (d_k=64), learning to detect different relationships — one might capture syntax, another semantics.

Step 5 of 6: Position Without Recurrence

How the model knows word order

Why does position matter?Without position info, "cat sat" = "sat cat" to the modelThe0cat1sat2on3the4warm5soft6mat7Position 0 encoding (unique fingerprint)Even dims: sin(pos / 10000^(2i/d)) Odd dims: cos(pos / 10000^(2i/d))pos=0: different frequencies per dimension create a unique pattern0.00sin01.00cos00.00sin11.00cos10.00sin21.00cos20.00sin31.00cos3How similar is each position to position 0?The (0)1.00 (self)cat (1)0.88sat (2)0.64on (3)0.49the (4)0.57warm (5)0.79soft (6)0.95mat (7)0.88
POSITION
0
WORD
The
NEAREST
cat
FARTHEST
mat

Position 0 ("The"): each bar above is computed by plugging pos=0 into sin and cos at different frequencies. Low-frequency dimensions (sin0, cos0) change slowly across positions; high-frequency ones (sin3, cos3) change rapidly. Together they form a unique "address" that's added to the word embedding — without it, "cat sat" and "sat cat" would look identical.

Step 6 of 6: The Full Pipeline

End-to-end prediction and paper results

Temperature1.0
English → French TranslationSource (English)ThecatsatdownTarget (French)Lechats'estassisEmbed + PositionEncoder × 6understands inputEmbed + PositionDecoder × 6cross-attentionLinear + Softmaxnext French word?Next word predictionTemperature = 1.0 — balanced<pad>NaN%theNaN%aNaN%catNaN%dogNaN%Paper: 6 layers · 512 dims · 8 heads · 28.4 BLEU (EN→DE)Trained in 3.5 days on 8 GPUs — set new state-of-the-art
TEMP
1.0
PREDICTION
<pad>
CONFIDENCE
NaN%
RANDOMNESS
Normal

Temperature 1.0 is the balanced setting. The encoder processes the full English sentence, the decoder generates French token by token. At each step, the decoder uses cross-attention to "read" the encoder's output — aligning "chat" with "cat", "assis" with "sat". This is the architecture that achieved state-of-the-art translation in 2017.