TensorTau PathToAGI Sequences Language and Transformers Level 09

Level 09 · Sequences Language and Transformers · Chapter 141

Transformers: Building With Attention

🧪 Interactive Lab

The matching notebook is the complete hands-on laboratory for this lesson. It contains the runnable code, experiments, visualizations, and challenges.

🧭 Where this lesson fits

Previous lesson: Blog 16 — Attention: What Should I Look At?.

Today: Blog 17 — Transformers: Building With Attention.

Next lesson: Blog 18 — How Does a Language Model Learn to Predict Text?.

Student rule: if you cannot explain why this lesson follows the previous one, stop and reread the final takeaway of the previous blog. The equations below should feel like a continuation, not a new language.

1. From tokens to representations

Suppose the input is

The cat sat

The text is converted into tokens, then token embeddings.

But embeddings alone do not tell the model where each token occurs.

We therefore need positional information.


2. Position matters

These sequences contain the same words:

cat chased dog
dog chased cat

but their meanings differ.

A Transformer therefore needs information about token position.

One family of methods uses sinusoidal positional encodings such as

PE(pos,2i)=sin⁡(pos100002i/d)PE(pos,2i)=\sin\left(\frac{pos}{10000^{2i/d}}\right) PE(pos,2i+1)=cos⁡(pos100002i/d)PE(pos,2i+1)=\cos\left(\frac{pos}{10000^{2i/d}}\right)

Modern models may instead use learned or relative/rotary positional methods. The central requirement is the same: the model needs access to positional information.


3. One Transformer block

A simplified Transformer block can be pictured as:

flowchart TD
    X[Token representations] --> A[Multi-head self-attention]
    A --> R1[Residual connection + normalization]
    R1 --> F[Feed-forward network]
    F --> R2[Residual connection + normalization]
    R2 --> O[Output representations]

Different Transformer variants use slightly different ordering and normalization conventions, but these are the major ideas.


4. Multi-head attention

One attention mechanism may learn one type of relationship.

Instead of using one head, Transformers use multiple heads.

For head ii:

headi=Attention(QWiQ,KWiK,VWiV)head_i=Attention(QW_i^Q,KW_i^K,VW_i^V)

Then concatenate the heads:

MultiHead(Q,K,V)=Concat(head1,…,headh)WOMultiHead(Q,K,V)=Concat(head_1,\ldots,head_h)W^O

Different heads can learn different interaction patterns.

Do not assume that every head has a clean human-readable role; learned representations can be distributed and redundant.


5. Feed-forward network

After attention, each token representation passes through a position-wise feed-forward network.

A simplified form is

FFN(x)=W2σ(W1x+b1)+b2FFN(x)=W_2\sigma(W_1x+b_1)+b_2

Attention mixes information between positions.

The feed-forward network transforms each position’s representation independently.

This division is a useful mental model.


6. Residual connections

Suppose a block computes F(x)F(x).

A residual connection produces

y=x+F(x)y=x+F(x)

Instead of forcing a layer to learn an entirely new representation, the network can learn a useful modification of the existing one.

Residual connections also help optimization in deep networks.


7. Why Transformers train efficiently

RNNs naturally process tokens sequentially.

Self-attention can process many sequence positions in parallel during training.

For a sequence length nn, the attention score matrix has shape

(n,n)(n,n)

because every query can compare with every key.

That gives attention a major computational cost as sequence length grows, roughly quadratic in nn for standard full attention.

So Transformers trade sequential recurrence for highly parallel computation and a different scaling challenge.


8. Encoder and decoder families

The original Transformer architecture had an encoder and decoder.

Later models often use different subsets:

  • encoder-only models for representation tasks;
  • decoder-only models for autoregressive generation;
  • encoder-decoder models for sequence-to-sequence tasks.

This is why “Transformer” describes an architecture family rather than one single model design.


9. A small PyTorch module

import torch
import torch.nn as nn

layer = nn.TransformerEncoderLayer(
    d_model=64,
    nhead=4,
    batch_first=True
)

x = torch.randn(8, 20, 64)
y = layer(x)

print(y.shape)

The shape remains

8 × 20 × 64

The layer transforms the representations while preserving batch size, sequence length and model dimension in this example.


10. The big picture

Text
 ↓
Tokens
 ↓
Embeddings + position information
 ↓
Transformer blocks
 ├─ Self-attention
 ├─ Residual + normalization
 ├─ Feed-forward network
 └─ Residual + normalization
 ↓
Contextual representations
 ↓
Prediction head

This architecture can be stacked many times.


Think Like a Scientist 🧠

Explain the difference between:

Embedding\text{Embedding}

and

Contextual representation\text{Contextual representation}

Then ask:

Why would the representation of “bank” need to change depending on the sentence?

If you can answer that, you understand one of the central motivations behind attention.


What you should remember

A Transformer combines attention, nonlinear transformations, residual connections, normalization and positional information into a highly scalable sequence architecture.

Attention answers “what should interact with what?”

The surrounding architecture makes those interactions trainable and reusable at scale.

Now we can finally ask the question that powers modern language models:

How does a model learn to predict text?


📚 Go Deeper — Understand the Architecture, Not Just the Name

3Blue1Brown is one of the best visual companions for understanding the mathematical structure behind neural networks and attention.

ZacharyLLM is particularly relevant here for connecting the Transformer architecture to modern LLMs.

Frame Zero is useful for first-principles explanations of modern ML systems.

Visual Kernel can provide another visual/technical perspective on model internals.

Welch Labs is valuable for its implementation-first philosophy and supporting code; its AI material explicitly combines exercises, graphics and code.

Use MrJensenMath10 when the underlying algebra, functions or matrix operations need reinforcement.

The architectural insight

A Transformer is not “just attention.”

It is a repeated block of cooperating ideas:

mix information→preserve information→transform information→repeat\boxed{ \text{mix information} \rightarrow \text{preserve information} \rightarrow \text{transform information} \rightarrow \text{repeat} }

That repeated structure is what turns a single attention mechanism into a deep architecture.

Six ways to work the chapter, climbing from explaining it in your own words to investigating it like a researcher. Reading is not enough — this is where the chapter lands.

Exercises should progress from understanding to research behaviour.

Level 1 — Explain it

Explain the idea in plain language without equations.

Level 2 — Calculate it

Solve a tiny numerical example by hand.

Level 3 — Predict it

Predict code output, model behaviour, tensor shapes, or graph movement before running anything.

Level 4 — Break it

Deliberately change an assumption or parameter and explain what fails.

Level 5 — Build it

Implement the core mechanism from scratch before using a high-level library.

Level 6 — Investigate it

Form a hypothesis, design an experiment, compare alternatives, and explain the evidence.