liveBook · Manning

Chapter 7: A deep dive on Keras

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

The introduction previews advanced Keras workflows for applications that go beyond Sequential models and the default fit() loop.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction previews advanced Keras workflows for applications that go beyond Sequential models and the default fit() loop.

7.1 A spectrum of workflows

Explains how Keras supports a range of increasingly flexible workflows without requiring users to abandon familiar tools as their needs grow.

Key terms: progressive disclosure of complexity, spectrum of workflows, usability, flexibility, fit(), shared APIs, Layer, Model, workflows

7.2 Different ways to build Keras models

Introduces Keras’s three model-building APIs by comparing their simplicity, flexibility, and tradeoffs.

Key terms: Sequential model, Functional API, model subclassing

7.2.1 The Sequential model

This section shows how to build a Sequential model incrementally and how input shape determines when its weights and summary become available.

Key terms: Sequential model, add() method, weights, input shape, built, build(), Input class, summary(), output shape

7.2.2 The Functional API

This section explains how the Keras Functional API builds models as graphs of symbolic tensors, supports multiple inputs and outputs, and exposes layer connections for visualization and feature reuse.

Key terms: Sequential model, Functional API, graph, nonlinear topology, Input, symbolic tensor, shape, dtype, Model constructor, fit()

7.2.3 Subclassing the Model class

This section shows how to build a Keras model by subclassing Model and explains the flexibility and limitations of that approach.

Key terms: Model subclassing, __init__(), call(), forward pass, weights, Layer subclass, Model subclass, fit(), evaluate(), predict()

7.2.4 Mixing and matching different components

Keras model-building patterns can be combined rather than used as mutually exclusive choices.

Key terms: Sequential model, Functional API, Model subclassing, spectrum of workflows

7.2.5 Remember: Use the right tool for the job

Explains how to choose among Keras model-building workflows, favoring the Functional API when a model can be expressed as a graph of layers.

Key terms: Functional API, model subclassing, directed acyclic graph of layers, layer connectivity, Functional models, subclassed layers

7.3 Using built-in training and evaluation loops

Introduces Keras’s range of model-training workflows and two ways to customize its built-in workflow.

Key terms: progressive disclosure of complexity, spectrum of workflows, fit(), compile(), evaluate(), predict(), custom metrics, callbacks

7.3.1 Writing your own metrics

This section explains when to write a custom Keras metric and how its state, value, and reset behavior are defined.

Key terms: metrics, keras.metrics, training data, test data, keras.metrics.Metric, Keras variables, update_state(), result(), reset_state(), backpropagation

7.3.2 Using callbacks

This section explains how Keras callbacks let a training run respond to its progress, focusing on early stopping and model checkpointing.

Key terms: callback, fit(), keras.callbacks, EarlyStopping, validation loss, target metric, epochs, ModelCheckpoint, model checkpointing, best model

7.3.3 Writing your own callbacks

This section shows how to create a custom Keras callback for an action during training that built-in callbacks do not cover.

Key terms: keras.callbacks.Callback, custom callback, on_epoch_*, on_batch_*, logs, epoch index, batch index

7.3.4 Monitoring and visualization with TensorBoard

This section explains why detailed feedback speeds model development and how to monitor Keras training with TensorBoard.

Key terms: experiments, loop of progress, final loss, TensorBoard, training and evaluation metrics, model architecture, histograms, embeddings, keras.callbacks.TensorBoard, fit()

7.4 Writing your own training and evaluation loops

This section explains when Keras’s built-in fit() workflow may be insufficient and recalls the basic steps of a custom training loop.

Key terms: fit() workflow, supervised learning, targets, custom training logic, forward pass, loss, gradients, model’s weights, batches

7.4.1 Training vs. inference

This section explains two details of a Keras training loop: setting the model to training mode for the forward pass and requesting gradients only for trainable weights.

Key terms: training Boolean argument, forward pass, Dropout layer, inference, model.trainable_weights, model.weights, trainable weights, non-trainable weights, backpropagation

7.4.2 Writing custom training step functions

This section shows how to write a supervised-learning training step with Keras on TensorFlow, PyTorch, and JAX, emphasizing how each backend obtains gradients and updates state.

Key terms: GradientTape, forward pass, backend, computation graph, loss.backward(), weight.value.grad, model.zero_grad(), stateless_call(), compute_loss_and_updates(), jax.value_and_grad

7.4.3 Low-level usage of metrics

This subsection shows how to track and reset Keras metrics in a low-level training loop, with a caveat for JAX stateless functions.

Key terms: update_state(), result(), keras.metrics.Mean, reset_state(), JAX, stateless function, stateless metrics API

7.4.4 Using fit() with a custom training loop

This section shows how to run a custom training step through Keras fit() instead of writing an entire training loop.

Key terms: fit(), custom training step function, training loop, keras.Model, train_step(), compile(), metric names, loss, JAX, per-batch loss

7.4.5 Handling metrics in a custom train_step()

This section identifies how a custom train_step() accesses the loss and metrics configured with compile().

Key terms: compile(), self.compute_loss, regularization losses, self.metrics

7.5 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 6: The universal workflow of machine learning Chapter 8: Image classification →

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.