liveBook · Manning

Gradient descent

As explained in Deep Learning with Python, Third Edition

A parameter update step computed from gradients to reduce model error.

Gradient descent updates a model’s weights using gradients to reduce its loss. Instead of testing changes to every weight separately, it computes how the loss varies with the weights and updates them together. section 2.4

A single coefficient

Suppose a coefficient is 0.3 and the batch loss is 0.5. Raising it to 0.35 increases the loss to 0.6; lowering it to 0.25 reduces the loss to 0.4. Moving downward looks promising. But testing both directions for every coefficient would require many expensive forward passes; a gradient supplies a more efficient direction for updating all the coefficients. section 2.4

The gradient describes how loss changes as the weights move in different directions. Because the model’s operations are differentiable, they can be combined into a differentiable function from weights to batch loss. A training mini-batch supplies the data for one gradient-descent update. section 2.4 section 4.0

Listing 5.7 Training an MNIST model with an incorrectly high learning rate
(train_images, train_labels), _ = mnist.load_data()
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype("float32") / 255

model = keras.Sequential(
    [
        layers.Dense(512, activation="relu"),
        layers.Dense(10, activation="softmax"),
    ]
)
model.compile(
    optimizer=keras.optimizers.RMSprop(learning_rate=1.0),
    loss="sparse_categorical_crossentropy",
    metrics=["accuracy"],
)
model.fit(
    train_images, train_labels, epochs=10, batch_size=128, validation_split=0.2
)

This MNIST training run uses RMSprop with a learning rate of 1.0. Training and validation accuracy stall around 20%–40%. section 5.3.1

Listing 5.8 The same model with a more appropriate learning rate
model = keras.Sequential(
    [
        layers.Dense(512, activation="relu"),
        layers.Dense(10, activation="softmax"),
    ]
)
model.compile(
    optimizer=keras.optimizers.RMSprop(learning_rate=1e-2),
    loss="sparse_categorical_crossentropy",
    metrics=["accuracy"],
)
model.fit(
    train_images, train_labels, epochs=10, batch_size=128, validation_split=0.2
)

The same model can train when the learning rate is lowered to 1e-2. section 5.3.1

If training stalls, check the update configuration:

  • Try lowering or increasing the learning rate. A rate that is too high can overshoot a good fit; one that is too low can make training appear stalled. section 5.3.1
  • Try increasing the batch size. More samples yield more informative, less noisy gradients. section 5.3.1
  • Consider the optimizer and weight initialization too. These choices interact, but tuning learning rate and batch size while holding the others constant is often sufficient. section 5.3.1

How Deep Learning with Python, Third Edition covers it

How deep it goes
Develops an intuitive gradient explanation and practical tuning guidance without heavy mathematical notation.
Examples
Uses a coefficient-and-loss comparison and MNIST training runs with contrasting learning rates.
What it assumes
Builds mathematical intuition without requiring a mathematics background or heavy notation.
What you can do after
Diagnose/debug stalled training by adjusting learning rate or batch size.
How it connects
Connects differentiability, gradients, loss functions, mini-batches, and weight updates.

Also explained in

Compare how 18 books explain gradient descent →

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.