liveBook · Manning

Chapter 15: Language models and the Transformer

What you will learn in chapter 15 of Deep Learning with Python, Third Edition

This introduction previews text generation, English-to-Spanish translation, and the Transformer as the chapter’s topics.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction previews text generation, English-to-Spanish translation, and the Transformer as the chapter’s topics.

15.1 The language model

The section explains how text generation becomes a tractable prediction problem by representing text as token sequences and predicting one next token at a time.

Key terms: integer sequence, tokens, detokenize, vocabulary, output integer sequences, classifier output, language model, p(token|past tokens), probability distribution, next token

15.1.1 Training a Shakespeare language model

The section prepares Shakespeare text as character-level next-character training data, builds a small recurrent language model, and reports its training result.

Key terms: equal-length chunks, feature and label sequences, next character, TextVectorization, character-level vocabulary, split, standardization, RNN, recurrent state, Embedding

15.1.2 Generating Shakespeare

This section turns a trained next-character Shakespeare model into an autoregressive generator by adapting its GRU state handling, priming it with a prompt, and feeding predictions back one character at a time.

Key terms: GRU cell’s state, RNN state, autoregressive model, prompt, prime, internal state of the GRU, most likely next character, detokenize, Bidirectional(GRU(...)), training accuracy

15.2 Sequence-to-sequence learning

This section introduces sequence-to-sequence modeling and explains how an encoder and decoder work during training and inference.

Key terms: sequence-to-sequence modeling, seq2seq, source sequence, target sequence, encoder, decoder, inference, encoded source sequence, seed token, stop token

15.2.1 English-to-Spanish translation

This section prepares English–Spanish sentence pairs as tokenized, padded training data for a translation model.

Key terms: text_pairs, training, validation, test sets, TextVectorization, [start], [end], ¿, punctuation, tf.data pipeline

15.2.2 Sequence-to-sequence learning with RNNs

This section builds an RNN encoder–decoder translation model, explains how it generates translations, and identifies its evaluation and sequence-length limitations.

Key terms: RNN, source sequence, target sequence, encoder RNN, decoder RNN, initial state, Bidirectional layer, encoder, decoder, GRU

15.3 The Transformer architecture

This section introduces attention as a contextual alternative to passing sequence information through an RNN’s state and explains the Einsum notation used in Transformer code.

Key terms: RNN, internal state, sequence, attention, Transformer architecture, recurrent layers, Einsum notation, Einstein summation notation, axes

15.3.1 Dot-product attention

This section builds dot-product attention from relevance scores and weighted source vectors, then explains learned query-key-value projections, score scaling, and multiple attention heads.

Key terms: attention scores, source sequence, target token, weighted sum, softmax, dot-product, matrix, target sequence, Dense layers, query

15.3.2 Transformer encoder block

The section explains how a Transformer encoder uses self-attention and a feedforward network to build contextual token representations, with normalization, padding masks, and stackable blocks.

Key terms: self-attention, token, attention score, representation, MultiHeadAttention, feedforward network, dense layers, activation function, LayerNormalization, BatchNormalization

15.3.3 Transformer decoder block

This section explains how a Transformer decoder combines target-sequence self-attention with attention to encoder output, and why its self-attention needs a causal mask.

Key terms: decoder block, self-attention, cross-attention, MultiHeadAttention, encoder output sequence, source_mask, attention_mask, padding, causal attention mask, lower-triangular

15.3.4 Sequence-to-sequence learning with a Transformer

This section builds a Transformer translation model, then uses its disappointing result to show why attention alone needs positional information to handle word order.

Key terms: GRU layers, TransformerEncoder, TransformerDecoder, attention, word order, positional information, positional embedding

15.3.5 Embedding positional information

This section adds learned positional embeddings to a translation Transformer, reports the resulting gains, and explains why attention trains faster than a GRU on accelerators.

Key terms: positional embedding, word embedding, position vector, positional information, next word, GRU, attention, looped state passing, GPU, TPU

15.4 Classification with a pretrained Transformer

The section explains why Transformers’ training speed and ability to benefit from more data helped drive a shift toward large pretrained text models.

Key terms: Transformer, RNNs, GPU, TPU, data hungry, validation performance, scaling up, large pretrained models

15.4.1 Pretraining a Transformer encoder

The section explains how BERT pretrains a bidirectional Transformer encoder on masked text and why its contextual representations can be adapted to NLP tasks.

Key terms: BERT, bidirectional, encoder, masked language modeling, [MASK], causal language model, unsupervised, pretrained word embeddings, context, fine-tuning

15.4.2 Loading a pretrained Transformer

This section explains how to load pretrained RoBERTa assets with KerasHub and why its tokenizer and backbone are suited to downstream tasks.

Key terms: matching tokenizer, matching model architecture, pretrained weights, KerasHub, from_preset(), subword tokenizer, character-level tokenizer, word-level tokenizer, vocabulary, Embedding layer

15.4.3 Preprocessing IMDb movie reviews

This section prepares IMDb reviews for RoBERTa fine-tuning by matching its pretrained token format and discusses why pretraining data sources matter.

Key terms: RoBERTa tokenizer, StartEndPacker, <s>, </s>, <pad>, pretraining data, data sources, tokens, biases

15.4.4 Fine-tuning a pretrained Transformer

The section adapts a pretrained RoBERTa backbone for IMDb review classification, explains how to turn its token sequence into one prediction, and reports the accuracy and cost tradeoff.

Key terms: backbone, first token’s representation, attention, pooling, classification head, Dense projection, nonlinearity, fine-tune, IMDb, 93%

15.5 What makes the Transformer effective?

This section explains Transformer effectiveness through attention-refined embedding spaces, their capacity for complex vector programs, and the possibilities and limits of interpolation.

Key terms: Word2Vec, embedding space, word arithmetic, attention, semantically continuous, semantically interpolative, representation power, autoregressive optimization objective, vector programs, continuous

15.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 14: Text classification Chapter 16: Text generation →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.