🔮 Transformer Attention Visualizer

Interactive Self-Attention Mechanism Demo

Input Sentence

Self-Attention Heatmap

Darker = stronger attention. Each row shows what a token attends to.

Attention Heads

Example Sentences

Visualization

Understanding Transformer Attention

Transformers revolutionized NLP with the self-attention mechanism. Instead of processing sequences sequentially (like RNNs), transformers process all positions in parallel while learning which positions to pay attention to.

Self-Attention Mechanism

For each token in sequence:

  • Queries (Q): "What am I looking for?"
  • Keys (K): "What do I contain?"
  • Values (V): "What should I output?"

Attention computed as:

  • Attention(Q, K, V) = softmax(QK^T / √d_k) × V
  • QK^T: Compatibility scores between all token pairs
  • √d_k: Scaling factor (prevents large values)
  • Softmax: Convert to probabilities
  • × V: Weighted sum of values

Multi-Head Attention

Instead of single attention, use multiple heads in parallel:

  • Each head learns different patterns
  • Head 1 might focus on syntax
  • Head 2 might focus on semantics
  • Heads concatenated and projected
  • Typical: 8-16 heads

Why Transformers Work

  • Parallel Processing: All positions processed simultaneously
  • Long-Range Dependencies: Any token can attend to any other
  • No Vanishing Gradients: Direct connections between all positions
  • Flexible: Works for various sequence lengths

Transformer Architecture

  • Encoder:
    • Self-attention layer
    • Feed-forward layer
    • Layer normalization + residual connections
    • Stacked 6-12 layers
  • Decoder:
    • Masked self-attention (autoregressive)
    • Cross-attention to encoder
    • Feed-forward layer
    • Layer normalization + residual
  • Positional Encoding:
    • Injects position information
    • Sine/cosine functions
    • Learned embeddings (BERT)

Famous Transformer Models

  • BERT (2018): Bidirectional encoder, masked language modeling
  • GPT Series: Autoregressive decoder, massive scale
  • T5: Text-to-text framework
  • Vision Transformer (ViT): Transformers for images
  • DALL-E: Text-to-image generation

Applications

  • Machine translation (original application)
  • Text generation (GPT)
  • Question answering
  • Summarization
  • Image classification (ViT)
  • Object detection (DETR)
  • Protein structure prediction (AlphaFold)

Experiment with the Visualizer

Use the interactive tool above to:

  • Enter sentences and see attention patterns
  • Switch between different attention heads
  • See which words attend to which
  • Understand attention weight distributions
  • Compare simple vs complex sentences

Transformers are the foundation of modern AI - GPT, BERT, and countless other breakthrough models. Understanding attention is key to understanding modern NLP!