Transformer Architecture Explained: Self-Attention, Feed-Forward, and Residuals
Introduced by Vaswani et al. in 2017, the Transformer architecture replaced recurrent and convolutional layers with attention mechanisms, unlocking massive training parallelization across modern GPU clusters.
The Core Mathematical Formula: Scaled Dot-Product Attention
Attention computes a weighted combination of value vectors based on the similarity between query and key vectors:
Attention(Q, K, V) = softmax( (Q * K^T) / sqrt(d_k) ) * V
Anatomy of a Transformer Layer
| Sub-layer | Mathematical Role | Intuitive Function |
|---|---|---|
| Multi-Head Attention | Parallel projection into \(h\) distinct Q, K, V subspaces. | Tokens route contextual information to one another. |
| Residual Stream (\(x + \text{SubLayer}(x)\)) | Identity skip connection preserving gradient fidelity. | A communication bus that layers read from and write into. |
| Feed-Forward Network (MLP) | Two linear projections with GELU or SwiGLU activation. | Acts as key-value associative memory for world knowledge. |
Explore the Mechanics Live on MEJ7
Rather than staring at static diagrams, open the Mechanistic Interpretability track on mej7.com to test:
- The interactive Value Mixing lab (\( \text{THE CAT SAT} \)).
- The live Residual Stream visualization showing Layer 1 and Layer 2 subspaces.
- The 3D Embedding Ring showcasing semantic clustering.
Experiment with this concept in the interactive lab →
Academic References & Foundational Reading
- Vaswani, A., et al. (2017): Attention Is All You Need. NeurIPS 2017.
- Elhage, N., et al. (2021): A Mathematical Framework for Transformer Circuits. Anthropic.