A transformer is the same block stacked many times: attention adds context, a normalising step keeps numbers steady, and a small network works on each word. Follow "bank" through one block.
AI · beginner · 10 min · 10 steps · free
What you'll learn
Name the parts of a transformer block
Explain residual connections: adding, not replacing
Follow one word's vector through a block
transformer_block.py
import math
# One transformer block: attention mixes in context, then a small network works on the result.
dims = ["nature", "money"]
x = [0.5, 0.5] # "bank" coming in: it could mean either
context = [0.8, 0.2] # what attention mixed in from "river" (see the attention lesson)
# 1. Add the attention result to the input. Adding (a "residual") keeps the original meaning too.
x = [x[0] + context[0], x[1] + context[1]]
# 2. Normalise, so the numbers stay a steady size however many blocks are stacked.
size = math.sqrt(x[0] ** 2 + x[1] ** 2)
x = [round(v / size, 2) for v in x]
# 3. Feed-forward: a tiny neural network (with ReLU), added on as well.
hidden = [max(0, 1.0 * x[0] - 0.5 * x[1]), max(0, -0.5 * x[0] + 1.0 * x[1])]
x = [round(x[0] + hidden[0], 2), round(x[1] + hidden[1], 2)]
print("bank leaving the block:", x)
Step by step
This brings in Python's built-in math module, for functions like math.exp and math.sqrt.
Two meaning dimensions, to keep things small. Real models use thousands.
"bank" arrives at the block with an even mix: it could mean a river bank or a money bank.
What attention found in the sentence: mostly "nature", from "river".
Step 1: add the attention result to the input. bank is now [1.3, 0.7], leaning towards nature.
Step 2: measure the vector's length…
…and divide by it, so the vector has length 1: [0.88, 0.47]. Without this, numbers could grow huge after many blocks.
Step 3: a tiny neural network with ReLU (negative results become 0). It boosts nature by 0.645, money by 0.03.
Added on: [1.52, 0.5]. "bank" now clearly means the river kind.
One block: attention (look at other words), normalise, feed-forward (think about this word), each added on. A large language model is this block repeated 30 to 100 times, then a final layer that scores every possible next token.
Common mistakes
Thinking each layer replaces the word's meaning: residuals keep adding to it
Forgetting that the feed-forward network works on each word separately
Best practices
Picture a transformer as a stack: the same kind of block, with different learned weights in each
Key takeaways
A transformer block = attention + feed-forward network, each with a residual connection.
Residual connections add each step's result instead of replacing the input.
Normalisation keeps the numbers in a steady range.
Large models stack many identical-looking blocks with different learned weights.
Check yourself
x = [1, 0] and a step outputs [0.2, 0.3]. With a residual connection, what comes out?
Answer: [1.2, 0.3]. The step's output is added to the input.
Which part of the block lets a word use information from other words?
Answer: Attention. Attention mixes in other words; the feed-forward network works on each word on its own.