The Big Question
Attention solved a major sequence-modeling problem: every token could look directly at every other token. The next question is architectural.
Can we build an entire neural network around attention?
The answer is the transformer. A transformer is not just attention. It combines self-attention with positional information, feed-forward networks, residual connections, layer normalization, and many repeated blocks.
The Remaining Problem
Attention lets tokens communicate directly, but attention alone does not know word order. The sentences below contain the same words but mean very different things:
The burger ate Jessie.
A transformer needs both direct token-to-token communication and information about position.
Core Intuition
Think of a transformer layer as a meeting. Every token enters with its own meaning. Through self-attention, each token listens to the other tokens it finds relevant. Then a small neural network processes the updated meaning of each token.
After one layer, every token is more contextual. After many layers, each token has absorbed increasingly rich information from the whole sequence.
The Transformer Block
| Token embeddings | Convert tokens into vectors |
| Positional information | Tell attention where tokens appear |
| Multi-head self-attention | Mix information across tokens |
| Residual connection | Add the original signal back |
| Layer normalization | Stabilize representation scale |
| Feed-forward network | Transform each token independently |
| Stacked layers | Repeat the block to build richer representations |
Attention mixes information between tokens. The feed-forward network transforms each token representation after that mixing. Residual connections and normalization keep the stack trainable.
Interactive Demo
One transformer block
Token representations
| query | Jessie | ordered | a | burger |
|---|---|---|---|---|
| Jessie | 0.42 | 0.28 | 0.08 | 0.22 |
| ordered | 0.35 | 0.08 | 0.06 | 0.51 |
| a | 0.08 | 0.12 | 0.12 | 0.68 |
| burger | 0.18 | 0.46 | 0.06 | 0.30 |
Self-attention lets each token retrieve relevant context from every other allowed token.
Attention versus transformer
Attention is the token-mixing operation. A transformer block wraps attention with positional information, feed-forward networks, residual paths, and normalization.
Causal masking
Decoder-only language models must not look at future tokens during training. Turn on the mask to block attention to positions on the right.
Order matters
Without positional information, self-attention knows which tokens are present but not where they appear. That is why position is added before attention.
Toggle positional information and causal masking. Click the block components to see how attention becomes a full transformer layer.
Mathematics And Architecture
Token Embeddings
Text first becomes tokens, and each token becomes a vector:
A sequence of tokens becomes a matrix:
Positional Encoding
Self-attention needs position information. The simplest view is that each token embedding receives a position vector:
Common choices include fixed sinusoidal positional encodings, learned positional embeddings, and rotary positional embeddings in many modern language models.
Self-Attention
Each token representation is projected into queries, keys, and values:
The attention operation is:
This produces contextual token representations. For example, ordered can attend to Jessie and burger so its representation includes who did the ordering and what was ordered.
Multi-Head Attention
Language contains many relationships at once: subject to verb, adjective to noun, pronoun to noun, cause to effect, and instruction to answer. Multi-head attention runs several attention mechanisms in parallel so different heads can learn different relationships.
Each head has its own query, key, and value projections. The head outputs are concatenated and projected back to the model dimension.
Feed-Forward Network
After attention mixes information across tokens, a small neural network transforms each token independently:
This distinction matters: attention mixes across tokens, while the feed-forward network processes each token's updated representation.
Residual Connections
Deep networks are easier to train when layers learn corrections:
The shortcut lets information and gradients flow through the network. If a layer is not useful yet, the model can keep the original representation and add only a small change.
Layer Normalization
As representations move through many layers, their scale can become unstable. Layer normalization stabilizes each token representation by normalizing across its feature dimension.
The intuition is simple: keep activations in a reasonable range so optimization does not spiral out of control.
Encoder, Decoder, And Causal Masking
| Type | Example | Used for |
|---|---|---|
| Encoder-only | BERT-style models | classification, semantic search, embeddings |
| Decoder-only | GPT-style models | next-token generation |
| Encoder-decoder | Original translation transformer | sequence-to-sequence tasks |
Decoder-only transformers use causal masking. When predicting the next token, a token can attend only to itself and earlier tokens, not future tokens.
Why Transformers Beat RNNs
- They process all tokens in parallel during training.
- Any token can directly attend to any other token.
- Stacked attention layers learn rich contextual representations.
Computational Cost
Full self-attention compares every token with every other token. For sequence length , the attention matrix has entries:
This quadratic scaling is why long context windows are expensive.
Implementation
This is a tiny decoder-style transformer block. It includes self-attention, a causal mask, residual connections, layer normalization, and a feed-forward network.
import torch
from torch import nn
class TransformerBlock(nn.Module):
def __init__(self, d_model, num_heads, d_ff):
super().__init__()
self.attn = nn.MultiheadAttention(
embed_dim=d_model,
num_heads=num_heads,
batch_first=True,
)
self.norm1 = nn.LayerNorm(d_model)
self.norm2 = nn.LayerNorm(d_model)
self.ffn = nn.Sequential(
nn.Linear(d_model, d_ff),
nn.GELU(),
nn.Linear(d_ff, d_model),
)
def forward(self, x):
seq_len = x.size(1)
mask = torch.triu(
torch.ones(seq_len, seq_len, device=x.device),
diagonal=1,
).bool()
attn_out, attn_weights = self.attn(
x,
x,
x,
attn_mask=mask,
need_weights=True,
)
x = self.norm1(x + attn_out)
ffn_out = self.ffn(x)
x = self.norm2(x + ffn_out)
return x, attn_weights
block = TransformerBlock(d_model=64, num_heads=4, d_ff=256)
tokens = torch.randn(8, 12, 64)
output, weights = block(tokens)
print(output.shape) # (8, 12, 64)
print(weights.shape) # attention matrixA full language model stacks many of these blocks, adds token and positional embeddings before them, and adds an output layer that predicts the next token.
Interview Discussion
What problem did transformers solve compared with RNNs?
They removed sequential recurrence, allowing tokens to communicate directly through attention and be processed in parallel during training.
Why does self-attention need positional information?
Attention alone knows which tokens are present but not their order, so position information is needed to distinguish sentences with the same words in different orders.
What does multi-head attention do?
It runs several attention mechanisms in parallel so different heads can learn different relationships.
What does the feed-forward network do inside a transformer block?
It transforms each token representation independently after attention has mixed information across tokens.
Why are residual connections important?
They create shortcut paths for information and gradients, making deep transformer stacks easier to train.
What is causal masking?
It blocks a token from attending to future tokens, preserving the next-token prediction setup.
Why does attention become expensive for long sequences?
Every token compares with every other token, so the attention matrix grows quadratically with sequence length.
Active Recall
1. Explain the difference between attention and a transformer.
2. Why does a transformer need positional information?
3. What does attention do inside a transformer block?
4. What does the feed-forward network do after attention?
5. Why are residual connections and layer normalization useful?
6. What is the difference between encoder-only and decoder-only transformers?
7. Why are GPT-style models decoder-only?
8. Why is attention cost O(n squared)?
Common Mistakes
- Thinking transformer means attention only.
- Forgetting that attention alone has no order information.
- Confusing encoder-only, decoder-only, and encoder-decoder models.
- Treating causal masking as optional for next-token prediction.
- Assuming feed-forward layers mix information across tokens.
- Ignoring the quadratic cost of full self-attention.
Connection To Transformer as a Language Model
This completes the Deep Learning arc: from one artificial neuron to the transformer. The next section asks how a transformer becomes a language model through next-token prediction, tokenization, pretraining, scaling, alignment, and decoding.