liveBook · Manning

Training

As explained in Deep Learning with Python, Third Edition

The phase during which a model’s parameters are optimized from data before deployment for prediction.

Training is how a model acquires useful weights from data instead of relying on rules written by a programmer. In supervised learning, examples pair inputs with expected outputs. A loss function measures the gap between predictions and targets; that feedback guides changes to the weights. Once trained, the model can use those weights to make predictions. section 1.3 section 2.4

When MNIST training stalls

Suppose a digit classifier reaches only 20%–40% training and validation accuracy. With an RMSprop learning rate of 1.0, its updates are too large to reach a good fit. Lowering the rate to 1e-2 lets the same model train. If loss is stuck, try changing the learning rate first; a larger batch size can also make gradients less noisy. section 5.3.1

A training loop repeats a small set of operations:

  • Draw a batch of inputs and targets, then run a forward pass to obtain predictions. section 2.4
  • Compute a loss that measures the mismatch between predictions and targets. section 2.4
  • Compute gradients and let the optimizer move weights toward lower loss. The learning rate controls the size of the move. section 2.6
  • Repeat across batches; one pass through the training data is an epoch. section 2.5.3
def fit(model, images, labels, epochs, batch_size=128):
    for epoch_counter in range(epochs):
        print(f"Epoch {epoch_counter}")
        batch_generator = BatchGenerator(images, labels)
        for batch_counter in range(batch_generator.num_batches):
            images_batch, labels_batch = batch_generator.next()
            loss = one_training_step(model, images_batch, labels_batch)
            if batch_counter % 100 == 0:
                print(f"loss at batch {batch_counter}: {loss:.2f}")

The `fit` function makes the outer structure concrete: repeat epochs, visit each batch, run one training step, and report loss. section 2.5.3

For routine supervised training, configure a Keras model with an optimizer, loss, and monitoring metrics, then call `fit()`. Metrics such as accuracy are reported but are not directly minimized. If the learning algorithm needs more control, write a custom loop; or override `keras.Model.train_step()` to keep `fit()` features such as callbacks. Low training loss alone is not the goal: check validation performance to guard against overfitting. section 3.6.4 section 7.4.4 section 5.0

How Deep Learning with Python, Third Edition covers it

How deep it goes
Works through batches, forward passes, losses, gradient updates, epochs, and custom loops, with practical guidance for stalled fitting.
Examples
Uses an MNIST learning-rate comparison, a hand-written epoch loop, and Keras `fit()` customization.
What it assumes
Introduces training from inputs, targets, and feedback while assuming familiarity with basic Keras models for advanced loop customization.
What you can do after
Build it from scratch as a training loop, or apply it via Keras `compile()` and `fit()` and diagnose a stalled run.
How it connects
Connects loss functions and optimizers to gradient descent, backpropagation, validation, and overfitting.

Also explained in

Compare how 24 books explain training →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.