Optimization finds values that minimize or maximize an objective function. In neural-network training, it adjusts model weights to reduce a loss function on the training data. The optimizer determines how those updates are made. section 1.6 section 5.1
Training a digit classifier
This R/Keras example uses Adam to minimize classification loss on MNIST images: library(keras3) .[.[train_images, train_labels], .[test_images, test_labels]] <- dataset_mnist() train_images <- array_reshape(train_images, c(60000, 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) section 2.1 section 2.5
For each mini-batch, the model makes predictions and the loss compares them with the targets. Backpropagation computes the gradient of the loss with respect to the weights; the optimizer uses that feedback to update them. Adam specifies the update rules in the example. Five epochs mean five passes through the training data. section 2.5 section 1.6
The loss is the quantity training minimizes directly; metrics such as accuracy are monitored but not directly optimized. Keras also accepts optimizer objects, which allow settings such as the learning rate to be configured. section 3.6.4
model |> compile( optimizer = optimizer_rmsprop(learning_rate = 1e-4), loss = my_custom_loss, metrics = c(my_custom_metric_1, my_custom_metric_2) )
This configuration sets a learning rate on an RMSprop optimizer object. section 3.6.4
Better training performance does not necessarily mean better performance on unseen data. If the model fits the training data too closely, it can overfit. Monitor training and validation loss and relevant metrics; validation performance beginning to degrade is a sign of overfitting. section 5.1 section 6.2.4





























