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





























