Welcome to this interactive tutorial on transformer neural networks! This guide will take you from the basics to training your own transformer model for text generation.
- What Are Transformers?
- Key Concepts
- Architecture Deep Dive
- Hands-On: Your First Transformer
- Understanding the Training Process
- Advanced Experiments
- Troubleshooting
Transformers are a revolutionary neural network architecture introduced in the 2017 paper "Attention is All You Need". They've become the foundation for modern language models like GPT, BERT, and T5.
- Parallel Processing: Unlike RNNs, transformers process all tokens simultaneously
- Long-Range Dependencies: Can effectively model relationships between distant words
- Transfer Learning: Pre-trained transformers can be fine-tuned for specific tasks
- Scalability: Performance improves predictably with model size
Tokens are the basic units of text that the model processes. They can be:
- Characters: 'h', 'e', 'l', 'l', 'o'
- Subwords: 'hel', 'lo'
- Words: 'hello'
Embeddings convert tokens into numerical vectors that capture semantic meaning.
# Example: "Hello world" → [101, 2310, 2088] → [[0.2, -0.5, ...], [0.1, 0.8, ...], ...]The heart of transformers is the attention mechanism, which allows the model to focus on relevant parts of the input when processing each token.
Self-Attention Formula:
Attention(Q, K, V) = softmax(QK^T / √d_k)V
Where:
- Q (Query): What information am I looking for?
- K (Key): What information do I have?
- V (Value): The actual information content
Instead of one attention mechanism, transformers use multiple "heads" that can focus on different types of relationships:
- Head 1: Grammar relationships
- Head 2: Semantic meaning
- Head 3: Position/order
- etc.
Since transformers process all tokens in parallel, they need a way to understand word order. Position encodings add information about each token's position in the sequence.
A transformer consists of stacked layers, each containing:
Our implementation makes several modern design choices that differ from the original 2017 paper:
-
Decoder-Only Architecture: We implement GPT-style autoregressive transformers rather than encoder-decoder
- Benefit: Simpler to understand and implement
- Trade-off: No bidirectional context (can't look ahead)
-
Learned Positional Embeddings: Instead of sinusoidal encoding, we learn position embeddings
- Benefit: Can adapt to specific tasks and datasets
- Trade-off: Won't generalize to sequences longer than training
-
Pre-Normalization: Layer norm before (not after) each sub-layer
- Benefit: More stable training, especially for beginners
- Trade-off: Slightly different from original paper
-
AdamW Optimizer: Modern optimizer with decoupled weight decay
- Benefit: Better generalization than original Adam
- Trade-off: One more hyperparameter to tune
These choices reflect current best practices and make training more forgiving for learners.
- Computes attention between all token pairs
- Multiple heads capture different relationships
- Includes residual connection and layer normalization
- Two linear transformations with ReLU activation
- Processes each position independently
- Also includes residual connection and normalization
- Stabilizes training by normalizing activations
- Can be applied before (pre-norm) or after (post-norm) each sub-layer
Let's train a character-level transformer on Shakespeare text!
- Open your browser to
http://localhost:8050 - You'll see the main training interface
For your first model, use the "Tiny Shakespeare" recipe:
- Embedding Dimension: 128
- Attention Heads: 4
- Layers: 4
- Sequence Length: 256
- Batch Size: 64
- Learning Rate: 3e-4
- Embedding Dimension: Size of token representations (higher = more expressive)
- Number of Heads: Parallel attention mechanisms (must divide embedding dimension)
- Number of Layers: Depth of the network (more = better but slower)
- Dropout: Regularization to prevent overfitting (0.1 = drop 10% of connections)
- Sequence Length: How many characters the model sees at once
- Batch Size: Number of sequences processed together
- Learning Rate: How fast the model learns (too high = unstable, too low = slow)
- Weight Decay: Another regularization technique
- Click "Initialize Model" - This creates your transformer
- Click "Start Training" - Watch the metrics!
- Loss: Should decrease over time (lower is better)
- Perplexity: Measures prediction quality (lower is better)
- Learning Rate: Follows the schedule (warmup → training → fine-tuning)
Once loss drops below 2.0:
- Enter a prompt like "To be or not to be"
- Set temperature (0.8 is a good default)
- Click "Generate" and see your model's creativity!
We use cross-entropy loss, which measures how wrong the model's predictions are. For each position, the model predicts the next character, and we compare this to the actual next character.
Our training uses three phases:
- Warmup (10% of steps): Gradually increase learning rate
- Main Training (80% of steps): Constant high learning rate
- Fine-tuning (10% of steps): Lower learning rate for refinement
- Loss < 3.0: Model is learning basic patterns
- Loss < 2.0: Generating readable text
- Loss < 1.5: High-quality generation
- Loss < 1.0: Potential overfitting
Try these variations:
- Deeper Models: Increase layers to 6 or 8
- Wider Models: Increase embedding dimension to 256 or 512
- More Heads: Try 8 or 16 attention heads
- Learning Rate: Try 1e-3 (faster) or 1e-4 (more stable)
- Batch Size: Larger batches (128) for stability, smaller (32) for speed
- Sequence Length: Longer sequences (512) capture more context
Switch between:
- Character-level: Simple, great for learning
- BPE (GPT-2): More efficient, better for real text
Explore our research on Predictive Coding Networks:
- Switch to the "PCN Experiments" tab
- Compare standard vs PCN-enhanced transformers
- Investigate the "label leakage" phenomenon
- Check learning rate: Too high causes instability, too low prevents learning
- Verify data: Ensure dataset loaded correctly
- Reduce model size: Start smaller, then scale up
- Reduce batch size: Halve it until training works
- Reduce sequence length: Try 128 instead of 256
- Use smaller model: Fewer layers or smaller embeddings
- Train longer: Model needs more time
- Check temperature: Too high (>1.5) causes randomness
- Verify tokenizer: Ensure encode/decode works correctly
- Check GPU availability in the status panel
- Ensure PyTorch is installed with CUDA support
- Try restarting the application
- Experiment with Different Datasets: Upload your own text files
- Try LoRA Fine-tuning: Efficient adaptation to new tasks
- Explore Attention Patterns: Visualize what the model learns
- Read the Research: Check our PCN experiments for cutting-edge ideas
- Original Transformer Paper
- The Illustrated Transformer
- Attention Visualization Guide
- PCN Research Summary
Fast Learning (See results quickly):
- Tiny model (4 layers, 128 dim)
- High learning rate (3e-3)
- Character tokenization
Best Quality (Production-like):
- Large model (8 layers, 512 dim)
- Moderate learning rate (3e-4)
- BPE tokenization
- Train for 10,000+ steps
Research/Experimentation:
- Try PCN variants
- Compare attention mechanisms
- Experiment with hybrid architectures
Remember: The best way to understand transformers is to experiment! Start with the tiny model, watch the metrics, and gradually increase complexity as you build intuition.
Happy training! 🚀