Post

Transformers from Scratch - Part 6: Training and Applications

Transformers from Scratch - Part 6: Training and Applications

Welcome to the final part of our Transformers series! In parts 1 through 5, we’ve built the complete architecture. Now let’s understand how to train it and where it’s used.

Training vs Inference

The Transformer behaves differently during training and inference. Understanding this distinction is crucial.

Training: Teacher Forcing

During training, we use teacher forcing—a technique that accelerates learning.

How It Works

Instead of feeding the model’s own predictions, we feed the ground truth (correct target sequence):

1
2
3
4
5
6
7
8
Input: "Hello world"
Target: "Bonjour le monde"

Decoder receives: <START> Bonjour le monde
                  (Ground truth, shifted right)

Predicts:        Bonjour le monde <END>
                 (Target sequence)

Key Point: Even if the model incorrectly predicts “Salut” instead of “Bonjour”, the next step still receives the correct “Bonjour” from the ground truth.

Why Teacher Forcing?

Without teacher forcing (slow convergence):

1
2
3
4
5
Step 1: Predict "Salut" (wrong)
Step 2: Use "Salut" → Predict "la" (wrong)
Step 3: Use "la" → Predict "terre" (wrong)
...
Errors compound! Takes forever to learn.

With teacher forcing (fast convergence):

1
2
3
4
5
Step 1: Predict "Salut" (wrong) ← But we see this is wrong
Step 2: Use "Bonjour" (correct ground truth) → Learn from correct context
Step 3: Use "le" (correct ground truth) → Learn from correct context
...
Learns faster! Each position learns independently.

The Masking Magic

Remember masked attention? It ensures that even though we feed the entire target sequence:

  • Position 1 can only see position 0
  • Position 2 can only see positions 0-1
  • Position 3 can only see positions 0-2

So it still learns autoregressive generation!

Parallel Training

Huge advantage: All target positions computed simultaneously!

1
2
3
4
5
6
7
Traditional RNN approach:
  Step 1: Predict word 1 → Step 2: Predict word 2 → ...
  (Sequential, slow)

Transformer with teacher forcing:
  All positions predicted at once!
  (Parallel, fast)

Result: Training is 100x faster than RNNs!

The Loss Function

We use cross-entropy loss between predictions and ground truth:

\[\mathcal{L} = -\frac{1}{N}\sum_{i=1}^{N} \log P(y_i | y_{<i}, x)\]

Where:

  • $y_i$: Ground truth token at position $i$
  • $y_{<i}$: All previous ground truth tokens
  • $x$: Input sequence
  • $N$: Output sequence length

Loss Computation Example

Target: “Bonjour le monde”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Position 1:
  Predicted probabilities: [Bonjour: 0.7, Salut: 0.2, ...]
  Ground truth: "Bonjour"
  Loss: -log(0.7) = 0.36

Position 2:
  Predicted probabilities: [le: 0.8, la: 0.15, ...]
  Ground truth: "le"
  Loss: -log(0.8) = 0.22

Position 3:
  Predicted probabilities: [monde: 0.9, terre: 0.05, ...]
  Ground truth: "monde"
  Loss: -log(0.9) = 0.11

Total Loss: (0.36 + 0.22 + 0.11) / 3 = 0.23

Lower loss = better predictions!

Inference: Autoregressive Generation

During inference (actual use), we don’t have ground truth. We must use our own predictions.

The Process

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Step 1: Start
  Input: <START>
  Output: "Bonjour" (predicted)

Step 2: Use Previous Prediction
  Input: <START> Bonjour
  Output: "le" (predicted)

Step 3: Continue
  Input: <START> Bonjour le
  Output: "monde" (predicted)

Step 4: End
  Input: <START> Bonjour le monde
  Output: <END> (predicted)

Final Output: "Bonjour le monde"

Key Difference: Each step depends on previous predictions, not ground truth!

Inference Strategies

1. Greedy Decoding (Simple & Fast)

1
2
3
4
5
6
7
8
9
def greedy_decode(model, input):
    output = [START_TOKEN]
    for _ in range(max_length):
        probs = model(input, output)
        next_token = argmax(probs)  # Pick highest
        output.append(next_token)
        if next_token == END_TOKEN:
            break
    return output

2. Beam Search (Better Quality)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def beam_search(model, input, beam_width=5):
    # Keep top-5 candidate sequences
    beams = [(START_TOKEN, 0.0)]  # (sequence, score)
    
    for _ in range(max_length):
        candidates = []
        for seq, score in beams:
            probs = model(input, seq)
            # Expand each beam
            top_k = get_top_k(probs, beam_width)
            for token, prob in top_k:
                new_seq = seq + [token]
                new_score = score + log(prob)
                candidates.append((new_seq, new_score))
        
        # Keep best beams
        beams = get_top_k(candidates, beam_width)
    
    return beams[0]  # Best sequence

3. Sampling (Diverse Outputs)

1
2
3
4
5
6
7
8
9
10
def sample_decode(model, input, temperature=1.0):
    output = [START_TOKEN]
    for _ in range(max_length):
        probs = model(input, output)
        probs = probs / temperature  # Control randomness
        next_token = random_sample(probs)
        output.append(next_token)
        if next_token == END_TOKEN:
            break
    return output

Complete Architecture Summary

Let’s visualize the complete flow one more time:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
INPUT TEXT: "Hello world"
    ↓
TOKENIZATION: [152, 856]
    ↓
━━━━━━━━━━━━━━━━━━ ENCODER ━━━━━━━━━━━━━━━━━━
Input Embedding (152→[...], 856→[...])
    +
Positional Encoding
    ↓
Encoder Layer 1:
  ├─ Multi-Head Self-Attention
  ├─ Add & Norm
  ├─ Feed-Forward Network
  └─ Add & Norm
    ↓
Encoder Layers 2-6 (same structure)
    ↓
Encoder Output: (2, 512)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

    ↓ (fed to decoder)

━━━━━━━━━━━━━━━━━━ DECODER ━━━━━━━━━━━━━━━━━━
Output Embedding (<START> Bonjour le...)
    +
Positional Encoding
    ↓
Decoder Layer 1:
  ├─ Masked Multi-Head Self-Attention
  ├─ Add & Norm
  ├─ Cross-Attention (with encoder output)
  ├─ Add & Norm
  ├─ Feed-Forward Network
  └─ Add & Norm
    ↓
Decoder Layers 2-6 (same structure)
    ↓
Decoder Output: (seq_len, 512)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

    ↓
Linear Layer: (seq_len, 512) → (seq_len, 30000)
    ↓
Softmax: Convert to probabilities
    ↓
OUTPUT: "Bonjour le monde"

Key Innovations

The Transformer introduced several groundbreaking concepts:

  1. Self-Attention: Direct connections between all positions
    • Path length: O(1) vs O(n) for RNNs
    • Captures long-range dependencies
  2. Multi-Head Attention: Multiple parallel attention mechanisms
    • Different heads learn different relationships
    • Richer representations
  3. Positional Encoding: Injects sequence order information
    • Sinusoidal functions
    • No learned parameters needed
  4. Residual Connections: Enables training of deep networks
    • Gradients flow easily
    • Prevents degradation
  5. Layer Normalization: Stabilizes training
    • Faster convergence
    • Better gradient flow
  6. Parallelization: Unlike RNNs, can process all tokens simultaneously
    • Much faster training
    • Better GPU utilization

Advantages Over RNNs/LSTMs

AspectRNN/LSTMTransformer
Training SpeedSlow (sequential)Fast (parallel)
Long DependenciesDifficult (vanishing gradients)Easy (direct connections)
Path LengthO(n) between distant tokensO(1) between any tokens
ParallelizationLimited (sequential)Excellent (all tokens at once)
MemoryFixed hidden stateAttention to all positions
InterpretabilityBlack boxAttention weights visualizable
ScalabilityLimitedScales to billions of parameters

Model Size Comparison

Original Transformer (2017):

  • Parameters: ~65M
  • Encoder layers: 6
  • Decoder layers: 6
  • Attention heads: 8
  • Model dimension: 512

Modern Large Models (2024):

  • GPT-4: ~1.7T parameters
  • BERT-Large: 340M parameters
  • T5-11B: 11B parameters

The architecture scales beautifully!

Real-World Applications

Transformers have revolutionized AI across many domains:

1. Machine Translation

Task: Translate text between languages

Examples:

  • Google Translate
  • DeepL
  • Microsoft Translator

Why Transformers Excel:

  • Captures long-range dependencies (“it” referring to distant nouns)
  • Handles different word orders (English vs Japanese)
  • Cross-attention aligns source and target

2. Text Generation

Task: Generate coherent, contextual text

Examples:

  • GPT-3, GPT-4 (ChatGPT)
  • Claude
  • Gemini

Capabilities:

  • Story writing
  • Code generation
  • Email composition
  • Creative content

3. Text Summarization

Task: Condense long documents into summaries

Types:

  • Extractive (select key sentences)
  • Abstractive (generate new text)

Applications:

  • News summarization
  • Research paper abstracts
  • Meeting notes

4. Question Answering

Task: Answer questions based on context

Examples:

  • BERT for SQuAD
  • ChatGPT
  • Copilot

Types:

  • Extractive QA (find answer in text)
  • Generative QA (generate answer)

5. Sentiment Analysis

Task: Determine sentiment (positive/negative/neutral)

Applications:

  • Social media monitoring
  • Customer review analysis
  • Brand monitoring

Advantage: Transformers understand context and sarcasm better!

6. Named Entity Recognition

Task: Identify entities (people, places, organizations)

Example:

1
2
3
4
Input: "Apple CEO Tim Cook announced..."
Output:
  - Apple: ORGANIZATION
  - Tim Cook: PERSON

7. Code Generation

Task: Generate or complete code

Examples:

  • GitHub Copilot
  • Code Llama
  • GPT-4 for coding

Capabilities:

  • Function generation
  • Bug fixing
  • Documentation
  • Code translation

8. Image Understanding

Task: Understand and generate images

Examples:

  • DALL-E (text-to-image)
  • Stable Diffusion
  • Vision Transformers (ViT)

How: Treat image patches as tokens!

9. Multimodal Applications

Task: Process multiple modalities (text, image, audio)

Examples:

  • GPT-4 Vision (text + images)
  • Whisper (speech recognition)
  • CLIP (text-image alignment)

10. Scientific Applications

Fields:

  • Drug discovery (AlphaFold)
  • Protein folding prediction
  • Material science
  • Climate modeling

The Impact

Before Transformers (Pre-2017)

1
2
3
4
5
- RNNs/LSTMs dominant
- Slow training
- Limited context
- Sequential processing
- Moderate performance

After Transformers (2017-Present)

1
2
3
4
5
6
- Transformers dominant
- Fast training
- Unlimited context (with modifications)
- Parallel processing
- State-of-the-art performance
- Foundation models possible

Key Milestones

2017: “Attention Is All You Need” paper

  • Introduced Transformer architecture
  • Machine translation breakthrough

2018: BERT released

  • Bidirectional pre-training
  • Revolutionized NLP

2019: GPT-2 released

  • Showed scaling potential
  • 1.5B parameters

2020: GPT-3 released

  • 175B parameters
  • Few-shot learning capability

2022: ChatGPT released

  • Brought AI to mainstream
  • Conversational AI

2023-2024: GPT-4, Gemini, Claude

  • Multimodal capabilities
  • Enhanced reasoning
  • Longer context windows

Conclusion: Why Transformers Matter

The Revolution

Transformers didn’t just improve performance—they fundamentally changed how we approach sequence processing:

  1. Parallelization: Made training at scale possible
  2. Attention: Learned where to focus, not just what
  3. Scalability: Bigger models → better performance (scaling laws)
  4. Transferability: Pre-train once, fine-tune for many tasks
  5. Versatility: From text to images to proteins

The Future

Transformers continue to evolve:

Efficiency Improvements:

  • Sparse attention (reduce O(n²) complexity)
  • Linear transformers
  • Flash Attention

Architecture Variations:

  • Encoder-only (BERT)
  • Decoder-only (GPT)
  • Encoder-decoder (T5)

Emerging Applications:

  • Video understanding
  • 3D generation
  • Robotics control
  • Scientific discovery

What We’ve Learned

Throughout this 6-part series, we’ve covered:

Part 1: RNN limitations → Need for attention Part 2: Architecture overview, embeddings, positional encoding Part 3: Multi-head attention mechanism in depth Part 4: Layer normalization and feed-forward networks Part 5: Decoder, cross-attention, output generation Part 6: Training, inference, and real-world applications

Final Thoughts

The Transformer architecture is elegant in its simplicity yet powerful in its capabilities. By replacing recurrence with attention, it unlocked:

  • Parallel processing
  • Better long-range modeling
  • Scalability to billions of parameters
  • Foundation for modern AI

Understanding Transformers deeply isn’t just about knowing one architecture—it’s about understanding the foundation of modern AI, from ChatGPT to DALL-E to AlphaFold.

The attention mechanism truly is “all you need.”


Series Complete! 🎉

Full Series Navigation:

Further Reading

Papers:

  • “Attention Is All You Need” (Vaswani et al., 2017)
  • “BERT: Pre-training of Deep Bidirectional Transformers” (Devlin et al., 2018)
  • “Language Models are Few-Shot Learners” (Brown et al., 2020)

Resources:

  • The Illustrated Transformer by Jay Alammar
  • The Annotated Transformer
  • Stanford CS224N: NLP with Deep Learning

Thank you for following this series! 🚀

This post is licensed under CC BY 4.0 by the author.