liveBook · Manning

Chapter 14: Text classification

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

This chapter introduction previews text preprocessing and simple text classification as foundations for later chapters.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduction previews text preprocessing and simple text classification as foundations for later chapters.

14.1 A brief history of natural language processing

The section traces NLP from handcrafted language rules to statistical models and Transformers, while distinguishing successful text pattern recognition from human-like understanding.

Key terms: natural language, machine language, formal rules, handcrafted rules, context, machine learning, corpus of data, natural language processing, NLP, pattern recognition

14.2 Preparing text data

This section explains how to turn raw text into numeric token sequences through standardization, splitting, and vocabulary-based indexing.

Key terms: standardization, splitting, indexing, tokenization, tokenizer, tokens, regular expressions, character tokens, word tokens, vocabulary

14.2.1 Character and word tokenization

This section builds simple character- and word-level tokenizers and explains the tradeoff between vocabulary size and encoded sequence length.

Key terms: character-level tokenizer, word-level tokenizer, standardization, splitting step, vocabulary, most common tokens, parameters, [UNK] token, sequence, compression

14.2.2 Subword tokenization

This section explains how subword tokenization uses byte-pair encoding to balance short token sequences with coverage of rare words, and how to choose among tokenization approaches.

Key terms: subword tokenization, WordTokenizer, CharTokenizer, vocabulary, byte-pair encoding, merge, symbol pair, subword vocabulary, merge rules, SubWordTokenizer

14.3 Sets vs. sequences

This section explains why word order complicates text representation and contrasts bag-of-words and sequence-model approaches to handling it.

Key terms: categorical features, feature space, category vectors, token vectors, word order, canonical order, meaning, ambiguity, bag-of-words models, recurrent models

14.3.1 Loading the IMDb classification dataset

This section inspects the IMDb review files, sets aside validation data, and loads training, validation, and test datasets for text classification.

Key terms: train, test, pos/, neg/, unsup/, inspect, text files, modeling, validation set, text_dataset_from_directory

14.4 Set models

This section introduces bag-of-words text classification by discarding token order, learning weights for word presence, and encoding reviews as multi-hot vectors.

Key terms: set, bag-of-words model, token IDs, multi-hot encoding, fixed-sized vector, vocabulary

14.4.1 Training a bag-of-words model

The section builds and evaluates a word-level bag-of-words classifier for IMDb reviews, then explains how to keep text preprocessing efficient during training.

Key terms: TextVectorization, adapt(), vocabulary, multi-hot encoding, bag-of-words, linear model, bias term, validation performance, test set, EarlyStopping

14.4.2 Training a bigram model

The section adds bigrams to a set-based IMDb sentiment classifier, shows the accuracy gain, and explains why longer n-grams become impractical.

Key terms: bigram, unigram, n-gram, bag-of-words, TextVectorization, vocabulary, adapt(), multi-hot encode, unigrams, bigrams

14.5 Sequence models

This section motivates sequence models for text classification and explains how to prepare variable-length reviews as fixed-length batches of token IDs.

Key terms: sequence models, raw word sequence, positional dependencies, token IDs, [PAD], truncation, padding, (batch_size, sequence_length)

14.5.1 Training a recurrent model

This section builds a bidirectional LSTM text classifier using one-hot token vectors, then explains why its large inputs make it slower and less accurate than the earlier set-based models.

Key terms: token ID, one-hot, vocabulary, multi-hot encoding, bidirectional LSTM, token sequence, classification, one-hot encoding, set-based models, word embeddings

14.5.2 Understanding word embeddings

This section explains why word embeddings represent words as dense, learned vectors whose geometry can reflect meaning, unlike one-hot vectors.

Key terms: one-hot encoding, orthogonal, feature engineering, word embeddings, dense vectors, sparse vectors, embedding space, semantic distance, geometric transformations, gender vectors

14.5.3 Using a word embedding

This section explains how Keras learns task-specific word embeddings, uses padding masks with an LSTM, and why the resulting efficiency gains do not necessarily improve accuracy.

Key terms: word-embedding space, semantic relationships, Embedding layer, input_dim, output_dim, backpropagation, embedding_size, mask_zero=True, [PAD], mask

14.5.4 Pretraining a word embedding

The section explains why to pretrain word embeddings, then builds a Continuous Bag of Words task from IMDb reviews to learn embeddings for an LSTM classifier.

Key terms: pretraining, unsupervised task, word embedding, Continuous Bag of Words (CBOW), sliding window, unordered context, TextVectorization, Embedding layer, GlobalAveragePooling1D, embedding space

14.5.5 Using the pretrained embedding for classification

The section applies a pretrained CBOW word embedding to an LSTM classifier, evaluates the result, and explains when a simpler set-based classifier may be preferable.

Key terms: CBOW embedding layer, LSTM embedding layer, embedding weights, initializer, LSTM, set-based models, unigram model, bigram model, ordered data, data-constrained

14.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 13: Timeseries forecasting Chapter 15: Language models and the Transformer →

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.