GPT: Decoder-Only Pre-Training and Generative Language Modeling

Learning Objectives: By the end of this lesson, you will be able to:

  • Explain the architecture and pre-training objectives of GPT models
  • Contrast GPT with BERT and understand the decoder-only, autoregressive approach
  • Use Hugging Face Transformers to generate text with a pre-trained GPT-2 model

1. Welcome and Introduction (5 minutes)

Icebreaker Activity

Have you ever used an AI to write a story, answer a question, or complete your sentence? How do you think it “knows” what comes next?

Lesson Overview

  • Duration: Approximately 2.5 hours including all exercises (or 60–90 minutes when selected activities are assigned)
  • Prerequisites: Understanding of Transformer blocks, embeddings, and sequence modeling
  • Materials Needed: Python (Jupyter Notebook or IDE), Hugging Face Transformers library, sample text data

2. Learning Objectives and Session Outline (5 minutes)

What We’ll Cover Today:

  1. The GPT architecture and its innovations
  2. Pre-training objective: autoregressive language modeling
  3. Decoder-only architecture and its implications
  4. Hands-on practice and exercises
  5. Common pitfalls and how to avoid them

Success Criteria:

By the end of this session, you should be able to explain GPT’s pre-training and generative paradigm and use a pre-trained GPT-2 model for text generation.

3. Foundation Knowledge (15-20 minutes)

Core Concepts

GPT Architecture

GPT (Generative Pre-trained Transformer) is a decoder-only Transformer model. It generates text by predicting the next token in a sequence, using only left-to-right context (autoregressive).

Pre-Training Objective

  • Autoregressive Language Modeling: The model is trained to predict the next token in a sequence, given all previous tokens.

Real-World Analogy: Imagine writing a story one token at a time, always basing your next token on everything you’ve written so far.

Why This Matters: GPT’s generative ability enables it to perform a wide range of tasks, from text completion to creative writing, with zero-shot, one-shot, and few-shot learning.

Key Terminology

  • Autoregressive: Predicts the next token based on previous tokens
  • Decoder-Only: Uses only the Transformer decoder stack
  • Zero-Shot/Few-Shot Learning: Performing tasks with little or no task-specific training

4. Detailed Explanation and Examples (25-30 minutes)

Understanding GPT’s Generative Paradigm

GPT is pre-trained on massive text corpora to predict the next token. Unlike BERT, it does not use bidirectional context or masking. During inference, it generates text one token at a time, feeding each new token back into the model.

Practical Applications

  • Text generation (stories, code, emails)
  • Dialogue systems and chatbots
  • Zero-shot and few-shot task completion

Common Misconceptions

  1. Misconception: GPT can “see” the whole sentence at once. Reality: GPT only uses left-to-right context; it does not look ahead.
  2. Misconception: GPT is always better than BERT for all tasks. Reality: GPT excels at generation, while BERT is better for understanding/classification.

5. Live Coding Demonstration (20-25 minutes)

Example 1: Loading and Using GPT-2 for Text Generation

from transformers import GPT2Tokenizer, GPT2LMHeadModel
import torch

tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = GPT2LMHeadModel.from_pretrained('gpt2')

prompt = "Once upon a time,"
inputs = tokenizer(prompt, return_tensors='pt')
outputs = model.generate(**inputs, max_new_tokens=30, num_return_sequences=1)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))

Explanation:

  • Loads a pre-trained GPT-2 model and tokenizer
  • Generates a continuation of the prompt

Example 2: Zero-Shot and Few-Shot Prompting

prompt = "Translate English to French: The cat is on the mat."
inputs = tokenizer(prompt, return_tensors='pt')
outputs = model.generate(**inputs, max_new_tokens=40)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))

Key Points:

  • GPT-2 can perform tasks with just a prompt (zero-shot)
  • Adding examples in the prompt enables few-shot learning

Example 3: Comparing GPT and BERT Architectures

Feature BERT GPT
Stack Encoder-only Decoder-only
Context Bidirectional Left-to-right
Pre-training MLM + NSP Autoregressive LM
Main Use Understanding Generation

Debugging Demonstration

Show common errors and how to fix them:

# Common mistake: using a BERT tokenizer/model with GPT-2
try:
    from transformers import BertTokenizer
    tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
    model = GPT2LMHeadModel.from_pretrained('gpt2')
    # This will cause tokenization issues
except Exception as e:
    print(e)

# Corrected version
from transformers import GPT2Tokenizer
# Use GPT2Tokenizer with GPT2LMHeadModel

6. Hands-On Practice (30-40 minutes)

Exercise 1: Basic Practice (10 minutes)

Objective: Generate text with GPT-2 from a prompt.

Instructions:

  1. Load the GPT-2 tokenizer and model
  2. Generate a continuation for the prompt: “Artificial intelligence is”
  3. Print the generated text

Starter Code:

from transformers import GPT2Tokenizer, GPT2LMHeadModel
# Your code here

Solution:

tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = GPT2LMHeadModel.from_pretrained('gpt2')
prompt = "Artificial intelligence is"
inputs = tokenizer(prompt, return_tensors='pt')
outputs = model.generate(**inputs, max_new_tokens=30)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))

Exercise 2: Intermediate Challenge (15 minutes)

Objective: Experiment with zero-shot and few-shot prompting.

Requirements:

  • Try a zero-shot task (e.g., translation, summarization)
  • Add a few examples to the prompt for few-shot learning
  • Compare the outputs

Hints:

  • Use clear instructions in the prompt
  • Try different tasks and observe the results

Exercise 3: Creative Application (15 minutes)

Objective: Use GPT-2 to generate creative content.

Challenge:

  • Write a prompt for a story, poem, or code snippet
  • Generate and refine the output

Extension Ideas:

  • Experiment with temperature and top-k sampling for more creative outputs
  • Try longer prompts or multi-turn dialogue

7. Common Pitfalls and Troubleshooting (10 minutes)

Frequent Mistakes

  1. Problem: Using the wrong tokenizer/model pair Cause: Tokenizer and model must match Solution: Always use GPT2Tokenizer with GPT2LMHeadModel

  2. Problem: Generating repetitive or nonsensical text Cause: Default generation settings may not be optimal Solution: Adjust parameters like temperature, top-k, and max_length

Debugging Tips

  • Check model and tokenizer compatibility
  • Experiment with generation parameters
  • Use small prompts for quick testing

8. Best Practices and Professional Tips (5 minutes)

Industry Standards

  • Use decoder-only models for generative tasks
  • Document prompt engineering strategies
  • Monitor for bias and inappropriate outputs

Next Steps in Your Learning Journey

  • Explore larger GPT models (GPT-3, GPT-4)
  • Try fine-tuning GPT-2 on your own data
  • Learn about encoder-decoder models (e.g., T5, BART)

Generation Controls

GPT models predict the next token, which may be a word, subword, byte, or punctuation mark. Prefer max_new_tokens when controlling generated continuation length because max_length counts prompt tokens too. temperature, top_k, and top_p affect generation only when sampling is enabled with do_sample=True; greedy decoding ignores those sampling controls.

9. Summary and Review (5 minutes)

Key Takeaways

  1. GPT is a decoder-only, autoregressive Transformer model for text generation
  2. Pre-training is done by predicting the next token in massive text corpora
  3. Prompt engineering enables zero-shot and few-shot learning

Quick Review Questions

  1. How does GPT differ from BERT in architecture and use?
  2. What is the main pre-training objective of GPT?
  3. How can you use GPT-2 for zero-shot or few-shot tasks?

For Instructors

Teaching Tips

  • Use diagrams to illustrate GPT’s architecture and generation process
  • Encourage students to experiment with different prompts and tasks
  • Discuss the strengths and limitations of generative models

Assessment Ideas

  • Quiz on GPT concepts and terminology
  • Coding assignment: generate text for different tasks
  • Peer review: students explain their prompt engineering strategies

Adaptation Notes

  • For beginners: focus on basic text generation
  • For advanced students: experiment with prompt engineering and fine-tuning
  • For online delivery: use Colab or Jupyter for interactive coding