Transformer Deep-Dive Lab

Transformer Attention Engine

Real-time scaled dot-product attention, multi-head routing & tensor flow.

Softmax Temp ($\tau$ / $\sqrt{d_k}$) 1.00
8
Current Focus Query Token: "it" • Inspecting calculated dot products against all Key tokens.
Click any token below to route queries
Interactive Attention Map
Head 1: Syntactic Relations
Tokens in Context ($X \in \mathbb{R}^{n \times d}$)
Scaled Dot-Product Attention: $\text{Softmax}\left(\frac{Q K^T}{\sqrt{d_k}}\right)$ Weights $A \in [0, 1]$
Multi-Head Attention Layers (MHA)
Head 1 dim 64
Syntactic & modifier dependencies (adjectives & nouns).
Head 2 dim 64
Coreference & entity resolution ("it" → antecedent).
Head 3 dim 64
Local positional context (adjacent n-gram tokens).
Head 4 dim 64
Semantic verb-argument structure (actions & subjects).
Tensors & Mathematical Projections
$d_{model} = 64$
$$\text{Attention}(Q, K, V) = \text{Softmax}\left(\frac{Q K^T}{\sqrt{d_k}}\right) V$$
For query token "it": dot product with keys determines context aggregation from values $V$.
Attention Distribution for Selected Token ($A_{i,:}$)
Query ($q_i$) & Top Key ($k_j$) Dot Product Decomposition
Context Vector: $z_i = \sum_j A_{i,j} v_j$ (Value Weighted Sum)
Next Transformer Stage: Add & Norm $\rightarrow \text{LayerNorm}(x + \text{MHA}(x)) \rightarrow \text{FFN}(z) \rightarrow \text{LayerNorm}(z + \text{FFN}(z))$.
Step 1

Embedding & Positional

Tokens are converted to continuous vectors. Since attention is permutation-invariant, positional encodings ($PE_{(pos, 2i)} = \sin(pos/10000^{2i/d})$) inject word order.

Step 2

Q, K, V Projections

Each token projects into Queries (what it searches for), Keys (what it offers), and Values (content it passes forward) via learned weight matrices $W_Q, W_K, W_V$.

Step 3

Multi-Head Attention

Multiple attention heads run in parallel, enabling the model to simultaneously capture grammar, long-range pronouns, facts, and sentiment relations.

Step 4

Feed-Forward & Residuals

Residual connections prevent vanishing gradients, while 2-layer point-wise MLPs apply nonlinear transformations to enrich contextual representations.

Enjoy this tool? Build your own with Super