Loss Functions and Tasks

2 min

Objective: align target, output layer and loss function.

The loss provides the training signal. The metric describes quality according to the need. A loss must be differentiable almost everywhere; a business metric need not be.

TaskOutputTargetPyTorch loss
Regression(N,1) or (N,)floatMSELoss, L1Loss, HuberLoss
Binary1 logit0/1 floatBCEWithLogitsLoss
K classesK logitsinteger indexCrossEntropyLoss
MultilabelK logitsK values 0/1BCEWithLogitsLoss

Cross-entropy

For the true class y, the multiclass loss equals -log p_y. A confident and wrong prediction is heavily penalized. CrossEntropyLoss combines log_softmax and the negative log-likelihood; do not feed it probabilities already normalized.

Reduction and imbalance

The loss of a batch is generally an average. With rare classes, you can weight the classes, but weighting replaces neither an appropriate validation protocol nor metrics such as recall, precision and PR-AUC.

python
import torch
from torch import nn

weights = torch.tensor([1.0, 4.0])
criterion = nn.CrossEntropyLoss(weight=weights)

Label smoothing

Label smoothing replaces a fully certain target with a slightly softened distribution. It can reduce over-confidence, but changes calibration and must be validated, not applied ritually.

Quick check

Why is accuracy not used directly as a loss? Should softmax be applied before CrossEntropyLoss?

Answers

Accuracy is discontinuous and provides almost no gradient. No, the loss expects logits.

Mastery activity — Loss, target and metric

For real-estate pricing, binary fraud, one species among ten and independent tags, specify the logit shape, target encoding, training loss and business metric. Add a case where MAE would be preferred over MSE. Success: the loss and the metric play explicitly distinct roles.

From task to loss function

Reading: the loss makes learning differentiable, while the metric judges the value of the model. They can therefore be different. The main trap is choosing a loss just because its name resembles the metric, without checking the target format, expected logits and business cost of errors.