Classification Loss Functions
Logistic loss evaluates one prediction on one labeled example. Empirical Risk Minimization then combines those individual evaluations across the training set.
Try it: Loss Functions of the Margin
How the 0-1, hinge, logistic and squared losses each score a classifier's margin z = y(⟨w,x⟩ + b), and why hinge (and base-2 logistic) are convex surrogates that upper-bound the 0-1 loss.
How it works
- For each point compute ⟨w,x⟩ + b and the margin z = y(⟨w,x⟩ + b): positive means correctly classified.
- 0-1 loss: 1 if z ≤ 0, else 0.
- Hinge loss: ; logistic loss: ; squared loss: , which equals because .
- Average each loss over the points to get the empirical risks.
- Hinge, squared and base-2 logistic are ≥ the 0-1 loss everywhere; natural-log logistic dips below it near z = 0.
Default run (9 steps): Classifier w = (0.5, 0.866), b = 0. For each point the margin z = y(⟨w,x⟩ + b) is positive when it is on its correct side; every loss is a function of z. … Averages (empirical risks): 0-1 0.286, hinge 0.338, logistic 0.333, squared 1.887. Hinge and squared loss are ≥ the 0-1 loss at every point; natural-log logistic is below it at 2 points (ln 2 ≈ 0.69 < 1 at z = 0) — use base 2 for a true upper bound.
Simplified: Linear classifier in 2-D with at most 10 points; the boundary case z = 0 counts as a mistake.
Loading the simulation…
Try it: Logistic Regression
How logistic regression turns the linear score ⟨w,x⟩ + b into a probability , how gradient descent on the average logistic loss moves the boundary, and how the decision threshold decides which points count as training errors.
How it works
- The linear stage computes a real score ⟨w,x⟩ + b for every point; the sigmoid stage maps it into [0, 1] as , read as the probability that the label is +1.
- Each labelled point (y ∈ {±1}) costs log(1 + exp(−y(⟨w,x⟩ + b))): small when y·score is large and positive, large when the score contradicts the label.
- ERM averages these losses into ; gradient descent starts at (every , every loss ) and repeats .
- Or place the boundary yourself: the line through handles A and B, with steepness ‖w‖, and read off every probability and loss.
- Finally the threshold t: predict +1 when , i.e. when ; count the training errors.
Default run (57 steps): Start at w = 0, b = 0: every score is 0, so every h_w(x) = σ(0) = 0.5 and every logistic loss is ln 2 ≈ 0.693. Gradient descent now steps w ← w − η∇LS(w) with η = 0.5. … Trained: w = (0.34, 0.478), b = -0.521, average logistic loss 0.51. Threshold: predict +1 when h_w(x) ≥ 0.5 ⇔ ⟨w,x⟩ + b ≥ ln(0.5/0.5) = 0. That gives 2 training errors (p_6, p_12).
Simplified: Two features on a half-unit grid, at most 14 points, full-batch gradient descent with a fixed step size and no regularisation. On separable data the weights keep growing, so training simply stops after the chosen number of iterations (or when the gradient is below 1e-9). Some intermediate iterations are grouped into one step when there are more than 60.
Loading the simulation…
Why a Prediction Needs a Loss
A logistic regression model produces h_w(x), a value in the interval [0, 1]. The model uses this value in relation to the two possible labels, y ∈ {−1, 1}. A prediction is not equally useful in every situation: it may support the actual label or work against it. Logistic loss turns that relationship into a numerical measure of how bad the prediction is for its true label.
Logistic loss evaluates one prediction on one labeled example. Empirical Risk Minimization then combines those individual evaluations across the training set.
Reading the Signed Score
The logistic loss is defined by log(1 + exp(−y〈w, x〉)). The important quantity inside this expression is the signed product y〈w, x〉. It evaluates the model's score relative to the actual label, rather than inspecting 〈w, x〉 by itself.
The sign of y〈w, x〉 tells us whether the model's score agrees with the label. A positive value indicates that the score supports the true label. A negative value indicates disagreement with the label and leads to higher loss. As the signed quantity becomes less favorable and eventually negative, the logistic loss increases.
The diagram shows direction rather than particular numerical loss values. Its central lesson is that the loss evaluates the score relative to the label. The same raw score 〈w, x〉 can therefore be interpreted differently when the actual label changes, because the product y〈w, x〉 changes with y.
From Support to Contradiction
Comparing two labeled examples
Consider two examples evaluated by the same logistic-regression model. In the first, the signed quantity y〈w, x〉 is strongly positive. In the second, it is strongly negative. Which example receives the larger logistic loss?
- 1First example
A strongly positive y〈w, x〉 means that the model's score supports the true label. The corresponding logistic loss is lower than it would be for a less favorable signed quantity.
- 2Second example
A strongly negative y〈w, x〉 means that the score disagrees with the true label. Negative values lead to higher logistic loss.
- 3Comparison
The second example receives the larger loss because its signed quantity is less favorable and negative.
Logistic loss is lower when the signed score supports the true label and higher when the signed score contradicts it.
Averaging Loss with ERM
The loss for one example gives a local view of model quality. Empirical Risk Minimization, or ERM, provides the global view: it minimizes the average logistic loss over all training examples. The training process therefore evaluates each labeled example, combines those loss values through their average, and seeks model parameters that minimize that average.
This pipeline connects two levels of reasoning. First, logistic loss asks how bad one prediction is for its true label. Then ERM aggregates those individual judgments so that the model is evaluated over the entire training set rather than on only one example.
Common Interpretation Mistakes
Common mistake Treating a positive raw score 〈w, x〉 as automatically favorable.
What it looks likeLooking only at 〈w, x〉 and ignoring y.
Why it failsLogistic loss evaluates the score relative to the actual label through y〈w, x〉.
Do this insteadDetermine the sign of y〈w, x〉 before interpreting the prediction.
Common mistake Assuming that disagreement with the label should produce a smaller loss.
What it looks likeInterpreting a negative y〈w, x〉 as a successful prediction.
Why it failsNegative values indicate disagreement with the label and lead to higher loss.
Do this insteadAssociate negative signed quantities with stronger penalty.
Common mistake Confusing one example's loss with the ERM objective.
What it looks likeUsing the logistic loss from a single training example as the complete training evaluation.
Why it failsERM minimizes the average logistic loss over all training examples.
Do this insteadSeparate the local loss for one example from the average loss across the training set.
Check Your Reasoning
Two predictions have signed quantities y〈w, x〉 with opposite signs. Prediction A has a positive signed quantity, while Prediction B has a negative signed quantity. Without calculating numerical values, identify which prediction receives the higher logistic loss and explain why.
Hints
- Start by interpreting the sign of each signed quantity.
- Recall how logistic loss responds when the score disagrees with the true label.
Explain the difference between these two statements: logistic loss evaluates one labeled example, and ERM minimizes average logistic loss.
Hints
- Identify the local object being evaluated in the first statement.
- Identify what is aggregated in the second statement.
Key Takeaways
- 1Logistic loss measures how bad a logistic-regression prediction is for its true label.
- 2Its defining expression is log(1 + exp(−y〈w, x〉)).
- 3The sign of y〈w, x〉 matters: negative values indicate disagreement with the label and lead to higher loss.
- 4Predictions that support the true label receive lower loss than predictions that contradict it, in the directional sense described by the source.
- 5Empirical Risk Minimization minimizes the average logistic loss over the training set.
At a glance
- Logistic loss evaluates the quality of a prediction relative to its true label.
- The signed product y〈w, x〉 is the key quantity because it incorporates both the model score and the actual label.
- A less favorable or negative signed quantity produces higher logistic loss.
- ERM uses the average of the individual logistic losses to evaluate and optimize performance across the training set.