Loss Functions

Deep Learning interview cross-entropy MSE objective

What is a loss function and how is it chosen?

The loss function is the single number training minimizes — it measures how wrong the model's predictions are. The choice encodes what “wrong” means for your task, so it's driven by the problem type and how you want errors penalized.

Follow-ups
  • “Loss vs metric?” — The loss must be differentiable to train on; the metric (accuracy, F1) is what you actually care about and may not be differentiable.

MSE vs MAE vs Huber for regression?

They differ in how harshly they punish large errors — i.e. how sensitive they are to outliers.

MSE squares errors

Smooth, penalizes big misses hard — sensitive to outliers. Optimizes the mean.

MAE absolute errors

Robust to outliers; optimizes the median. Gradient is constant, harder near zero.

Huber best of both

Quadratic for small errors, linear for large — smooth yet outlier-tolerant.

What loss do you use for classification?

Cross-entropy (log loss). It measures the distance between the predicted probability distribution and the true labels, heavily penalizing confident wrong predictions.

Binary
  • Binary cross-entropy on a sigmoid output.
  • −[y·log p + (1−y)·log(1−p)]
Multiclass
  • Categorical cross-entropy on a softmax output.
  • Pairs naturally with backprop — the gradient simplifies to (p − y).

Why not use MSE for classification?

Two reasons. MSE on top of a sigmoid gives a non-convex, flat-gradient loss — when the model is confidently wrong, the gradient is tiny, so it learns slowly. And MSE doesn't match the probabilistic objective the way cross-entropy does.

Follow-ups
  • “What does cross-entropy fix?” — Its gradient is large exactly when the prediction is confidently wrong, so the model corrects fast.

How do you handle class imbalance in the loss?

When one class dominates, plain cross-entropy is swamped by the majority class. Reweight the loss so the minority class matters.

Class weights weighted CE

Weight each class inversely to its frequency.

Focal loss focus on hard

Down-weights easy, well-classified examples — popular for detection.

Resampling data-level

Oversample minority / undersample majority as an alternative.