liveBook · Manning

Optimization

As explained in Deep Learning with R, Third Edition

Optimization is the task of finding input or parameter values that minimize or maximize an objective function.

Optimization adjusts a model’s weights to improve its performance on training data. A loss function compares predictions with targets; an optimizer uses that feedback to change the weights in a direction that lowers the loss. Repeating this process across batches turns initially random weights into a trained network. section 1.6 section 5.1

Train a digit classifier

In R with Keras, choose the optimizer when configuring the model, then call fit() to train it: library(keras3) .[.[train_images, train_labels], .[test_images, test_labels]] <- dataset_mnist() train_images <- array_reshape(train_images, c(60000, 28 * 28)) / 255 test_images <- array_reshape(test_images, c(10000, 28 * 28)) / 255 model <- keras_model_sequential() |> layer_dense(512, activation = "relu") |> layer_dense(10, activation = "softmax") model |> compile( optimizer = "adam", loss = "sparse_categorical_crossentropy", metrics = "accuracy" ) fit(model, train_images, train_labels, epochs = 5, batch_size = 128) evaluate(model, test_images, test_labels) Here, the loss supplies the signal to minimize, Adam specifies how weights are updated, and accuracy is monitored rather than directly optimized. Each epoch passes through the training data. section 2.1 section 2.5 section 3.6.4

To configure optimization, keep three choices separate:

  • Choose a loss function suited to the task: it defines what training minimizes. section 3.6.4
  • Choose an optimizer to govern the updates. Keras accepts a shortcut such as "adam" or an optimizer object; an object can set options such as learning_rate. section 2.5 section 3.6.4
  • Choose metrics to monitor performance. Unlike the loss, they are not directly optimized. section 3.6.4

Lower training loss is not the whole goal. Continued fitting can improve training performance while performance on unseen data worsens—overfitting. Monitor training and validation results to spot that divergence. When checking whether a model has enough capacity, adding layers, enlarging layers, or training longer can deliberately expose the point where validation performance begins to degrade. section 5.1 section 6.2.4

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains loss-driven weight updates and mini-batch training, then configures built-in optimizers; leaves custom training loops for later.
Examples
Trains a Dense MNIST digit classifier with Adam, sparse categorical crossentropy, five epochs, and held-out test evaluation.
What it assumes
Introduces weights, loss, and the training loop from first principles while demonstrating them through R and Keras code.
What you can do after
Apply it via Keras compile() and fit() to select an optimizer, train a model, and monitor its results.
How it connects
Connects optimization to loss functions, backpropagation, stochastic gradient descent, metrics, generalization, and overfitting.

Also explained in

Compare how 30 books explain optimization →

Related concepts

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