liveBook · Manning

Objective function

As explained in Deep Learning with Python, Third Edition

A function that compares predictions with targets and returns a scalar indicating how wrong the model is for a given task.

How does a model know what to improve? An objective function—also called a loss or cost function—compares its predictions with the targets and returns a loss value. Training uses that value as feedback: an optimizer adjusts the model’s weights to reduce it. section 1.6

Choosing a loss for handwritten digits

A digit classifier predicts one of ten classes. With integer labels, configure Keras with `loss="sparse_categorical_crossentropy"`; `fit()` then processes mini-batches and updates weights to reduce that loss. If the labels instead use categorical encoding, choose `categorical_crossentropy`. The two losses have the same mathematics but expect different label formats. section 2.5 section 4.2.6

Three practical checks help when configuring training:

  • Match the loss to the task. Binary crossentropy suits two-class classification; categorical crossentropy suits many-class classification. Common tasks usually have built-in choices. section 3.6.5
  • Keep loss separate from metrics. The optimizer minimizes the loss; metrics such as accuracy report performance during training and validation but are not optimized directly. section 3.6.4
  • Check whether minimizing the chosen quantity really serves the goal. A model may find shortcuts that lower a poorly chosen objective without producing the intended result. section 3.6.5

A task can need more than one loss. For object detection, classification uses sparse categorical crossentropy, while a custom bounding-box loss handles location and confidence. The box loss counts placement error only where a labeled box exists and scales components so empty cells do not dominate. Keras sums the output losses during training. section 12.2.4

Listing 12.12 Training the YOLO model
model.compile(
    optimizer=keras.optimizers.Adam(2e-4),
    loss={"box": box_loss, "class": "sparse_categorical_crossentropy"},
)
model.fit(
    train_dataset,
    validation_data=val_dataset,
    epochs=4,
)

The `compile()` call assigns a custom loss to the box output and a built-in loss to the class output, then `fit()` trains both together. section 12.2.4

How Deep Learning with Python, Third Edition covers it

How deep it goes
Defines the training role, works through Keras configuration, and develops a custom object-detection loss without a dense mathematical derivation.
Examples
Uses handwritten-digit classification, integer-versus-categorical labels, and separate YOLO box and class losses.
What it assumes
Introduces the loss concept from predictions and targets, then assumes familiarity with labels, layers, and Keras training calls for worked configurations.
What you can do after
Apply it via Keras `compile()` by selecting a task-appropriate loss or assigning losses to model outputs.
How it connects
Connects predictions and targets to optimizers, backpropagation, metrics, classification, and object detection.

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 Python, Third Edition. The section links point to the parts of the book it draws on.