liveBook · Manning

Chapter 4: Classification and regression

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

The chapter introduces classification and regression tasks and defines the vocabulary used to describe their inputs, outputs, and targets.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces classification and regression tasks and defines the vocabulary used to describe their inputs, outputs, and targets.

Key terms: sample, input, prediction, output, target, prediction error, classes, label, binary classification, categorical classification

4.1 Classifying movie reviews: A binary classification example

Introduces binary classification through the task of labeling movie reviews positive or negative from their text.

Key terms: two-class classification, binary classification, positive, negative

4.1.1 The IMDb dataset

This section introduces the IMDb movie-review dataset, its integer-encoded reviews and labels, and the reason for limiting its vocabulary.

Key terms: IMDb dataset, training, testing, negative, positive, preprocessed, word indices, train_data, test_data, train_labels

4.1.2 Preparing the data

The section prepares IMDb review sequences and labels as tensors that can be fed into a neural network.

Key terms: integer sequences, tensors, multi-hot encode, Embedding layer, labels, NumPy arrays, ints, floats

4.1.3 Building your model

This section builds and configures a Dense-layer model for predicting whether a movie review is positive.

Key terms: Dense layers, intermediate layers, units, scalar prediction, weight matrix W, representation space, relu, activation function, non-linearity, hypothesis space

4.1.4 Validating your approach

The section uses a validation set to monitor an IMDb classifier, identify overfitting, choose a training duration, and preserve the test set for final evaluation.

Key terms: validation set, test set, training data, validation_data, validation_split, model.fit(), History object, history, loss, training loss

4.1.5 Using a trained model to generate predictions on new data

This section shows how to use a trained model to predict the likelihood that a review is positive and interpret its confidence.

Key terms: predict method, likelihood, positive, confident

4.1.6 Further experiments

The section proposes controlled experiments to test whether the IMDb classifier’s architecture and training choices are reasonable.

Key terms: representation layers, units, validation and test accuracy, mean_squared_error, binary_crossentropy, tanh

4.1.7 Wrapping up

The section summarizes the IMDb example’s practical lessons about data preparation, model design, training choices, and monitoring overfitting.

Key terms: preprocessing, tensors, binary vectors, Dense layers, relu activations, sentiment classification, binary classification, Dense layer, sigmoid activation, binary_crossentropy

4.2 Classifying newswires: A multiclass classification example

The section frames Reuters topic classification as a single-label, multiclass problem and distinguishes it from multilabel classification.

Key terms: multiclass classification, single-label, multiclass classification, multilabel, multiclass classification

4.2.1 The Reuters dataset

Introduces the Reuters newswire dataset and explains how its multiclass labels and text examples are represented in Keras.

Key terms: Reuters dataset, topics, topic index, training set, num_words=10000, word indices, training examples, test examples

4.2.2 Preparing the data

This section reuses the earlier input-vectorization approach and introduces integer and one-hot representations for multiclass labels.

Key terms: labels, one-hot encoding, categorical encoding, label index

4.2.3 Building your model

This section explains how the Reuters model’s layer sizes, output, loss, and metrics suit a 46-class classification task.

Key terms: Dense layers, intermediate layers, information bottleneck, 64 units, Dense layer of size 46, softmax activation, probability distribution, output[i], categorical_crossentropy, loss function

4.2.4 Validating your approach

The section uses validation curves to choose a training duration, then compares test accuracy with an empirical random baseline.

Key terms: validation set, loss and accuracy curves, overfit, epochs, test set, accuracy, random baseline, random classifier, 46 classes

4.2.5 Generating predictions on new data

This section shows how to interpret a trained newswire classifier’s predictions on new samples.

Key terms: predict method, class probability distribution, predicted class

4.2.6 A different way to handle the labels and the loss

This section explains how to choose the loss function when multiclass labels are kept as integers rather than categorically encoded.

Key terms: integer tensors, categorical encoding, categorical_crossentropy, sparse_categorical_crossentropy

4.2.7 The importance of having sufficiently large intermediate layers

This section shows how an undersized intermediate layer creates an information bottleneck in a 46-class classifier.

Key terms: information bottleneck, intermediate layers, validation accuracy

4.2.8 Further experiments

This section suggests changing the width and number of intermediate layers to build intuition about model configuration choices.

Key terms: intermediate layers, units, softmax classification layer

4.2.9 Wrapping up

Summarizes how to configure a single-label multiclass classifier’s output, loss, label encoding, and intermediate layer sizes.

Key terms: Dense layer, softmax activation, probability distribution, single-label, multiclass classification, categorical crossentropy, categorical encoding, one-hot encoding, categorical_crossentropy, sparse_categorical_crossentropy, information bottlenecks

4.3 Predicting house prices: A regression example

This section introduces regression as predicting continuous values rather than discrete class labels and cautions that logistic regression is a classification algorithm.

Key terms: classification, regression, discrete label, continuous value, logistic regression, classification algorithm

4.3.1 The California Housing Price dataset

Introduces a small California housing dataset for predicting district-level median home prices from eight numerical features.

Key terms: block group, district, numerical features, median value, small version, training samples, test samples

4.3.2 Preparing the data

This section prepares house-price data for training by normalizing input features with training-set statistics and scaling target prices to a manageable range.

Key terms: feature-wise normalization, mean, standard deviation, training data, test data, normalizing the test data, targets, weights, learning rate, predictions

4.3.3 Building your model

This section explains the model size, output layer, loss, and metric chosen for predicting a single continuous house price.

Key terms: intermediate layers, units, overfitting, scalar regression, linear layer, activation function, sigmoid, mean_squared_error, mean squared error, loss function

4.3.4 Validating your approach using K-fold validation

This section uses K-fold validation to evaluate a house-price model with limited data, choose a training duration, and assess a final model.

Key terms: validation set, validation split, high variance, K-fold cross-validation, partitions, folds, validation score, per-epoch validation score log, mean absolute error (MAE), overfitting

4.3.5 Generating predictions on new data

This section explains how to interpret predictions from the house-price regression model and contrasts them with classification predictions.

Key terms: predict(), scalar regression model, hundreds of thousands of dollars

4.3.6 Wrapping up

Summarizes the loss, metrics, preprocessing, validation, and model-size choices illustrated by the scalar regression example.

Key terms: scalar regression, mean squared error (MSE), MAE, accuracy, features, scaled independently, preprocessing, K-fold validation, intermediate layers, overfitting

4.4 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 3: Introduction to TensorFlow, PyTorch, JAX, and Keras Chapter 5: Fundamentals of machine learning →

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.