liveBook · Manning

GPT-2

As explained in Natural Language Processing in Action, Second Edition

GPT-2 is an autoregressive transformer language model developed by OpenAI and trained to generate text from preceding context.

GPT-2 is a causal transformer language model for generating text from a prompt. It predicts a next token from the preceding tokens, then uses generated tokens as context for subsequent predictions. This makes it useful for experimenting with text generation, but a fluent continuation is not necessarily correct or useful. section 10.2 section 10.2.1 section 10.3.11

A practical generation workflow:

  • Load matching pretrained model and tokenizer weights with Hugging Face: GPT2LMHeadModel.from_pretrained('gpt2') and GPT2Tokenizer.from_pretrained('gpt2'). Encode the prompt with that tokenizer and decode the generated token IDs to read the result. section 10.2.1
  • Inspect predictions before generating a passage: for the prompt “NLP is a”, take the last position’s logits, apply softmax, and rank candidate tokens. In the shown run, the top decoded candidates are “ new” and “ non”. section 10.2.1
  • Change the generation strategy when results are repetitive. One demonstrated setting uses nucleus sampling with do_sample=True, top_p=0.92, and max_length=50; it changes the continuation, not its guarantee of accuracy. section 10.2.1

From an awkward continuation to a domain-tuned model

With the prompt “NLP is”, pretrained GPT-2 produces an incorrect description of NLP and repeats a database phrase. To adapt its generation patterns, filter a corpus to natural-language lines, tokenize and pad each line as a training example, split examples into training and evaluation sets, and fine-tune a fresh pretrained GPT-2 with a language-modeling data collator and Hugging Face Trainer. Compare continuations from the original and fine-tuned models rather than assuming fine-tuning makes every answer reliable. section 10.2.1 section 10.2.2

GPT-2 supplies generation rather than retrieval. For question answering grounded in supplied documents, pair semantic search with a generative model and instruct that model not to invent unsupported answers. The demonstrated retrieval-augmented pipeline uses FLAN-T5, not GPT-2; GPT-2 is cited there as an example of a causal generator. section 10.3.11

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Works through model loading, tokenization, logits, sampling, and a Trainer-based fine-tuning workflow; does not build the transformer from scratch.
Examples
Uses “NLP is” and “NLP is a” prompts, ranked next-token candidates, and before-and-after continuations from corpus fine-tuning.
What it assumes
Assumes basic Python and PyTorch use while showing how to load matching pretrained weights and tokenize prompts.
What you can do after
Apply it via Hugging Face Transformers to generate text, inspect token predictions, adjust sampling, and fine-tune on a sentence corpus.
How it connects
Connects causal language modeling to tokenization, softmax, nucleus sampling, fine-tuning, and retrieval-augmented question answering.

Also explained in

Compare how 6 books explain GPT-2 →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.