liveBook · Manning

GPT model

As explained in Learn Generative AI with PyTorch

A family of generative pretrained Transformer language models referenced here as the host family that Codex is integrated into.

A GPT model is a decoder-only Transformer that turns token sequences into next-token predictions, enabling text or music generation. It can be built from scratch for a focused dataset and task. section 12.1 section 14.3

The model embeds tokens and their positions, processes them through stacked decoder blocks, and produces vocabulary-sized logits. Each block combines causal self-attention with a feed-forward network, using layer normalization and residual connections. section 12.1.1 section 12.3.3

A practical GPT training workflow includes:

  • Choose sequence length, embedding dimension, decoder-layer count, attention heads, vocabulary size, and dropout rates. section 12.1 section 14.3
  • Tokenize raw text, map tokens to integer indexes, divide the sequence into fixed-length inputs, and shift them to create next-token targets. section 12.1 section 12.1.2
  • Train on varied text that matches the desired style, then generate by repeatedly selecting a next token from the prompt-conditioned predictions. section 12.1.2 section 12.1

A compact GPT configuration

A small implementation uses three decoder layers, 256-dimensional embeddings, four attention heads, a maximum sequence length of 128, and a 10,600-token vocabulary. Word and positional embeddings are added, the result passes through the decoder stack, and a linear head returns 10,600 logits. This configuration contains 5.12 million parameters, making training feasible without supercomputing facilities. section 12.1.1 section 12.3.3

Table 12.1 A comparison of our GPT with different versions of GPT-2 models (view table figure)

GPT-2S

GPT-2M

GPT-2L

GPT-2XL

Our GPT

Embedding dimension

768

1,024

1,280

1,600

256

Number of decoder layers

12

24

36

48

3

Number of heads

12

16

20

25

4

Sequence length

1,024

1,024

1,024

1,024

128

Vocabulary size

50,257

50,257

50,257

50,257

10,600

Number of parameters

124 million

350 million

774 million

1,558 million

5.12 million

Compare embedding dimensions, decoder layers, attention heads, sequence lengths, vocabulary sizes, and parameter counts with GPT-2 variants. section 12.1.1

How Learn Generative AI with PyTorch covers it

How deep it goes
Explains configuration, token preparation, decoder internals, training flow, and generation through a small working architecture.
Examples
Demonstrates Hemingway-style generation from three novels and adapts the architecture for music generation.
What you can do after
Build it from scratch, train it on tokenized domain text, and generate continuations from prompts.
How it connects
Connects GPT with Transformer architecture, GPT-2, causal self-attention, positional encoding, tokenization, embeddings, and softmax.

Also explained in

Compare how 18 books explain GPT model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.