In this article
Before you start
This page is one step of the machine learning algorithms learning path, and it builds on probability and statistics.
Why interviewers ask this
A wrong loss function trains a model that looks fine in a notebook and misbehaves in production, so this question tests whether a candidate knows what the loss actually optimizes. It is short, but it hides a chain: which loss, why that one, what the model outputs, and what happens when one class is rare. Candidates report it in machine learning rounds.
A weak answer says "cross-entropy" and stops. A strong answer names binary cross-entropy, says it is computed on a predicted probability, and explains why squared error is a worse fit. A Staff-level answer adds the decisions around the loss: class weights for rare positives, a separately chosen threshold, regularization and calibration.
Choosing a binary classification loss from first principles
- Log loss
- Log loss is a loss function for two-class problems that scores a predicted probability by the negative natural logarithm of the probability given to the true class, averaged over the examples. Confident wrong predictions are penalized heavily, and a probability of one on the right class costs nothing. It is the same quantity as binary cross-entropy.
In plain words
Picture a weather forecaster who must state a chance of rain, not just yes or no. After the day ends, you score her by how much chance she gave to what actually happened. Saying ninety percent and being right earns a small penalty. Saying ninety percent and being wrong earns a big one.
Log loss is that scoring rule for a model. The analogy breaks in one place: a model is trained by repeating the scoring thousands of times and adjusting its weights, while a forecaster only receives the score.
In one precise sentence, log loss is the average negative logarithm of the probability the model gave to the true class.
Step 1: Score the probability given to the true class
Log loss charges the negative logarithm of the probability given to the true class. A logistic regression model first computes a raw score. The logit is that raw score, the log of the ratio of the probabilities of the two outcomes, and the sigmoid function turns it into a probability between 0 and 1 (Google Machine Learning Crash Course, sigmoid, accessed 2026-10-26). In the course's spam example an output of 0.932 means a 93.2% probability that the email is spam.
If the email is spam, the loss is minus the natural log of 0.932, which is 0.0704. At a probability of 0.5 the loss is 0.6931, and at 0.1 it is 2.3026.1 For a not-spam email the same rule uses one minus the probability. Over N examples:
Here y is the label, 0 or 1, and p is the predicted probability of class 1. The base of the logarithm only rescales the loss by a constant; this page uses the natural logarithm. The Google course says logistic regression models use log loss instead of squared loss (accessed 2026-10-26).
Step 2: A confident wrong answer costs far more under log loss than under squared error
Squared error is the square of the gap between the label and the predicted probability; it is a poor training loss through a sigmoid, as the gradient below shows. Take a spam email, label 1, and vary the probability the model gave to spam.
| Probability given to the true class | Log loss | Squared error |
|---|---|---|
| 0.932 | 0.0704 | 0.0046 |
| 0.5 | 0.6931 | 0.25 |
| 0.1 | 2.3026 | 0.81 |
| 0.01 | 4.6052 | 0.9801 |
Squared error never exceeds one, while log loss keeps growing as the probability falls toward zero.
Text description of the diagram
- At a probability of 0.01 on the true class, log loss is 4.6052 and squared error is 0.9801.
- Squared error never exceeds one, while log loss keeps growing as the probability falls toward zero.
The gradient is where it matters. With a sigmoid output, the log loss gradient is the probability minus the label, measured against the logit. By the chain rule, squared error multiplies that by 2p(1 − p). At a probability of 0.01 on the true class, the log loss gradient is −0.99 and the squared-error gradient is −0.0196, so a squared-error model barely learns from its worst mistake.1 Gradient descent follows these gradients to update the weights. For logistic regression, log loss is also convex in the weights, so gradient descent has a single global minimum; squared error through a sigmoid is not convex, so training can stall on flat regions.
Step 3: Train on probabilities, then apply the threshold
The threshold turns a probability into a class after training. Log loss never sees the class, so changing the threshold changes accuracy but not the loss.
Take four emails with labels 1, 0, 1, 0 and predicted probabilities 0.9, 0.2, 0.6 and 0.4 (illustrative). Their losses are 0.1054, 0.2231, 0.5108 and 0.5108, and the mean is 0.3375. At a threshold of 0.5 all four are classified correctly. At 0.7 the third email, with probability 0.6, flips to the wrong class and accuracy falls to 0.75, yet the mean loss stays 0.3375.1
So choose the threshold separately, from the costs of false alarms and misses. For well-calibrated probabilities, the cost-minimizing threshold is the cost of a false alarm divided by the sum of the costs of a false alarm and a miss. The receiver operating characteristic (ROC) curve draws that trade; see computing an ROC curve in scikit-learn. You now have the whole answer: a sigmoid probability, log loss on it, and a threshold afterwards.
Step-by-step description
- Log loss scores the probability given to the true class.
- The gradient is the probability minus the label.
- A threshold turns the probability into a class.
- Log loss charges the negative logarithm of the probability given to the true class.
- With a sigmoid output, the log loss gradient is the probability minus the label.
- The threshold turns a probability into a class after training.
Knowing it is half the job. Saying it is the other half.
Interview Clozo is an AI interview copilot for real interview rounds: it hears the question and puts a strong answer on your screen, and you say it in your own words. See the features or how it works.
Start Free Trial →The Staff-level answer
If you had a binary classification problem, which loss function would you use?
I'd use binary cross-entropy, also called log loss, on a model that outputs a probability through a sigmoid. It is the negative log likelihood of the labels under a Bernoulli model, so minimizing it is maximum likelihood, and it punishes confident wrong predictions heavily. Squared error is a worse fit because its gradient through the sigmoid shrinks exactly when the model is confidently wrong; log loss is also convex for logistic regression, while squared error through a sigmoid is not. I'd train on the probability and choose the decision threshold separately, from the cost of false alarms and misses. If positives are rare, I'd add class weights or resample, then recheck calibration. And I'd regularize.
What makes it Staff-level: it ties the loss to the probability view, gives the reason for rejecting the alternative, and names the rare-class and threshold decisions without being asked.
The rubric
| Dimension | Weak answer | Strong answer | Staff answer |
|---|---|---|---|
| Correctness | Says "cross-entropy" and stops | Binary cross-entropy on a sigmoid probability | Adds the likelihood view: minimizing log loss is maximum likelihood |
| Depth | Cannot say why not squared error | Squared error learns slowly from confident mistakes | Shows the gradient: probability minus label against a damped one, and convex vs non-convex |
| Trade-offs | Ignores a rare positive class | Mentions class weights or resampling | Notes that rebalancing can bias probabilities, so rechecks calibration |
| Communication | Lists terms | Gives the answer, the reason, then the trade-off | Says it in under a minute, with one concrete number |
| Follow-up readiness | Surprised by a threshold question | Separates the loss from the threshold | Chooses the threshold from the costs of each error |
Three variants of this question
Which loss would you use if only 0.1% of the examples are positive?
Keep log loss and treat the imbalance with class weights or resampling, then pick the threshold separately. The Google course notes that fraudulent purchases might make up less than 0.1% of the examples, and a model that always says "not fraud" then scores at least 99.9% accuracy with zero recall.1 For extreme imbalance, focal loss (Lin et al., 2017) is a current option that down-weights easy examples so training focuses on hard ones. The same course lists downsampling and upweighting the majority class as one remedy and warns that upweighting the minority class may increase prediction bias.
Would you use hinge loss or squared error instead of log loss?
Use log loss when you need probabilities. Hinge loss, the loss of a support vector machine, trains a margin and outputs an uncalibrated score whose sign is the class, not a probability, and squared error, which Step 2 showed learning poorly from confident mistakes, is a worse fit for probabilities. Choose hinge only when a class is all you need.
Is log loss also the metric I should report?
Report it as one metric, not the only one. Log loss judges the quality of the probabilities, while precision, recall and the ROC curve judge the classes at a chosen threshold, so a model can have a good log loss and still miss most rare positives at a poor threshold.
Common mistakes
- "I'll use mean squared error because it works for regression." (wrong) Through a sigmoid, squared error learns slowly from confident mistakes. (right) "Log loss, because its gradient stays large when the model is confidently wrong."
- "The loss compares the predicted class to the label." (wrong) It compares the predicted probability to the label; the class comes from a threshold afterwards. (right) "The loss uses the probability, and I choose the threshold separately."
- "Cross-entropy and log loss are different losses." (wrong) For two classes they are the same quantity. (right) "Binary cross-entropy, also called log loss."
- "99.9% accuracy means the fraud model works." (wrong) At a prevalence of 0.1%, always saying "not fraud" scores that.1 (right) "I check recall and precision at a chosen threshold, not accuracy alone."
Follow-up questions
What is the difference between log loss and cross-entropy?
For two classes there is none: log loss is binary cross-entropy. Cross-entropy loss generalizes it to many classes as the negative of the sum, over the classes, of the label times the log of the predicted probability.
Why does logistic regression need regularization?
The Google course says regularization, such as L2 regularization or early stopping, is crucial in logistic regression to prevent overfitting, and ties this to the model's asymptotic nature (accessed 2026-10-26). In plain terms, the sigmoid can only approach 0 and 1, so when the classes are linearly separable the loss keeps rewarding larger weights; see L1 vs L2 regularization.
What happens to the loss if the model gives probability 0.001 to the true class?
The loss is 6.9078, which is 2.3026 more than at a probability of 0.01, because every tenfold drop adds the natural log of ten.1 At exactly zero the loss would be infinite. The practical fix is to compute the loss from the logits with a log-sum-exp (binary cross-entropy (BCE) "with logits") instead of clipping probabilities.
How do you choose the decision threshold?
Start from the costs: if a miss is worse than a false alarm, lower the threshold, and the reverse. Validate the choice on held-out data, and recheck it if the class mix in production drifts from training, as covered in distribution shift.
Why is minimizing log loss the same as maximum likelihood?
Model each label as a coin flip with probability p of being 1. The likelihood of the data is the product of p for each positive and 1 − p for each negative, and the negative log of that product, averaged, is log loss. Minimizing one therefore maximizes the other.
- Use binary cross-entropy, also called log loss, on a sigmoid probability.
- The loss scores the probability given to the true class, not the thresholded class.
- Squared error learns slowly from confident mistakes because its gradient shrinks through the sigmoid.
- Rare positives call for class weights or resampling, plus a threshold chosen from error costs.
- Regularize, and report precision and recall beside log loss.
Sources
- Logistic regression: Loss and regularization (Machine Learning Crash Course) — Google for Developers, 26 Oct 2026
- Logistic regression: Calculating a probability with the sigmoid function (Machine Learning Crash Course) — Google for Developers, 26 Oct 2026
- Datasets: Class-imbalanced datasets (Machine Learning Crash Course) — Google for Developers, 26 Oct 2026
- Focal Loss for Dense Object Detection (arXiv 1708.02002) — arXiv (Lin et al.), 7 Aug 2017