liveBook · Manning

Objective function

As explained in Deep Learning with R, Third Edition

A loss function is a function that assigns a numerical measure of discrepancy between a model's prediction and the true target.

An objective function, also called a loss or cost function, measures how far a model’s prediction is from its target. Its numerical result is the feedback signal for learning: the optimizer adjusts the model’s weights to reduce the loss. section 1.6

Train a digit classifier

For a model that predicts a probability for each digit, compile it with `model |> compile(optimizer = "adam", loss = "sparse_categorical_crossentropy", metrics = "accuracy")`, then train with `fit(model, train_images, train_labels, epochs = 5, batch_size = 128)`. The loss guides weight updates; accuracy reports the fraction of correctly classified images. section 2.1 section 2.5

Choose the loss for the job, not merely for a low number. Binary cross-entropy suits two-class classification; categorical cross-entropy suits multiclass classification; mean squared error suits regression. A poorly matched objective can reward shortcuts rather than the behavior you intended. section 3.6.5 section 6.2.3

Table 6.1 Which loss function, last-layer activation, and metrics to use for different tasks
Task
Last-layer activation
Loss function
Metrics
Binary classification
Sigmoid
Binary cross-entropy
Binary accuracy, ROC AUC
Multiclass, single-label classification
Softmax
Categorical cross-entropy
Categorical accuracy, top- k categorical accuracy, ROC AUC
Multiclass, multilabel classification
Sigmoid
Binary cross-entropy
Binary accuracy, ROC AUC
Regression
None
Mean squared error
Mean absolute error

Compare common tasks alongside their last-layer activations, losses, and monitoring metrics. section 6.2.3

Loss and metrics have different jobs. Training minimizes a differentiable loss computable on a mini-batch; a metric such as accuracy or ROC AUC measures a result you care about but need not be directly optimized. For a classifier, cross-entropy can serve as a proxy when ROC AUC cannot be optimized directly. Watch training and validation loss together: worsening validation performance while training continues is a sign of overfitting. section 3.6.4 section 6.2.3 section 6.2.4

When a built-in loss is not enough

An object detector can need a custom bounding-box loss. Its confidence target can use intersection over union for cells containing an object and zero for empty cells. Scaling placement errors up and empty-cell confidence errors down keeps the latter from overwhelming training. section 12.2.4

How Deep Learning with R, Third Edition covers it

How deep it goes
Develops the definition through Keras configuration, task-selection rules, and a coded custom detection loss without a formal derivation.
Examples
Uses MNIST digit classification and a YOLO bounding-box loss as concrete cases.
What it assumes
Introduces loss from predictions and targets without requiring prior knowledge of backpropagation; supervised examples use labeled inputs.
What you can do after
Apply it via Keras `compile()` to choose a task-matched loss, or build a custom loss for bounding boxes.
How it connects
Links objective functions to optimizers, backpropagation, classification metrics, and gradient descent.

Also explained in

Compare how 30 books explain objective function →

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.