@ProfTomYeh: Transformer by hand ~ 6 steps walkthrough below Studying the Transformer architecture is like opening up the engine hoo…
Summary
A step-by-step walkthrough explaining the core components of the Transformer architecture by manually calculating attention weighting and feed-forward networks to illustrate how Transformers function.
View Cached Full Text
Cached at: 08/23/26, 09:35 AM
Transformer by hand ~ 6 steps walkthrough below
Studying the Transformer architecture is like opening up the engine hood of your car. So many unknown parts: embeddings, positional encoding, attention weighting, self-attention, cross-attention, multi-head attention, layer norm, skip connections, softmax, linear, Nx, shifted right, query, key, value, masking.
But which of those actually make the car run?
I think at the core, there are two most essential components: Attention weighting and the feed-forward network.
Everything else is an enhancement to make it run faster and longer, which is how we got from a car to a truck, and to the word “large” in large language model.
So I drew and calculated those two parts entirely by hand.
Goal: push five features through one transformer block, filling in every cell yourself.
- Given
Five positions of input features, arriving from the previous block.
- Attention matrix
Let us feed all five features to a query-key module (QK) and read back an attention weight matrix, A. The details of that module are a post of their own.
- Attention weighting
We multiply the input features by A to get the attention weighted features, Z. Still five positions. The effect is to combine features across positions, horizontally: X1 becomes X1 + X2, X2 becomes X2 + X3, and so on.
- First layer
Let us feed all five weighted features into the first layer of the FFN. Multiply by the weights and biases. This time the combining happens across feature dimensions, vertically, and each feature grows from 3 numbers to 4.
Note that every position goes through the same weight matrix. That is what “position-wise” means.
- ReLU
We cross out the negatives. They become zeros.
- Second layer
Let us bring it back down: 4 dimensions to 3. The output feeds the next block, which has a completely separate set of parameters, and the whole thing runs again.
You have just calculated a transformer block by hand.
The takeaway: the two parts are doing two different jobs, and neither one alone is enough. Attention mixes across positions, so a feature can see its neighbours. The FFN mixes across feature dimensions, so each position can think about itself. Horizontal, then vertical. Then that pattern repeats N times, each block with its own separate set of weights. That is the Nx from the list up top, and that is what makes the transformer run.
Save this post!
#AIbyHand #Transformers #DeepLearning
Similar Articles
@ProfTomYeh: Switch Transformer by hand ~ 13 steps walkthrough below The Switch Transformer, by Fedus, Zoph, and Shazeer in 2022, is…
A 13-step visual walkthrough explaining how the Switch Transformer works, covering sparse mixture-of-experts routing and why models like GPT-4, Claude, DeepSeek-V3, and Kimi use this architecture to stay efficient.
@ProfTomYeh: Backpropagation by hand ~ 11 steps walkthrough below Backpropagation is the algorithm that actually trains a neural net…
A walkthrough of backpropagation by hand through a 3-layer network using matrix multiplication, showing all 11 steps from gradients to weight updates.
@_rohit_tiwari_: https://x.com/_rohit_tiwari_/status/2063982924714901858
This article provides a visual guide to the Transformer architecture in Large Language Models, covering self-attention, causal self-attention, masked multi-head attention, and the output layer with step-by-step explanations and examples.
Transformer Math Explorer [P]
This interactive tool visualizes the mathematical underpinnings of transformer models through dataflow graphs, covering architectures from GPT-2 to Qwen 3.6 and various attention mechanisms.
@techwith_ram: A Derivation Of The Transformer Architecture by Brandon Sandhu The paper develops an intuitive, mathematical understand…
This paper by Brandon Sandhu provides a mathematically rigorous yet accessible derivation of the Transformer architecture, covering tokenization, embeddings, attention mechanisms, and other core components, with prerequisites in linear algebra, calculus, probability, and information theory.