mej7.comHow AI Works
Architecture · Arkitektura

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-layerMathematical RoleIntuitive Function
Multi-Head AttentionParallel 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:

Experiment with this concept in the interactive lab →

Academic References & Foundational Reading

  1. Vaswani, A., et al. (2017): Attention Is All You Need. NeurIPS 2017.
  2. Elhage, N., et al. (2021): A Mathematical Framework for Transformer Circuits. Anthropic.