Classification, probabilities, and decision costs
Connect logits, cross-entropy, and thresholds without confusing a confidence score with a decision.
On this page
The question for today
What you’ll understand
- Convert logits into binary or multiclass probabilities.
- Explain why cross-entropy penalizes confident errors.
- Choose a threshold using explicit error costs.
A numerical prediction and a decision are separate objects. A service may estimate a 20% chance of failure and still trigger an intervention if missing a failure is very expensive. Classification models usually learn scores that are transformed into probabilities, then a policy turns those probabilities into actions. Keeping these stages separate makes training, evaluation, and deployment easier to reason about.
From an unrestricted score to a probability
A binary logistic model produces a real-valued logit . The sigmoid maps it between zero and one. A zero logit gives probability one-half; positive logits favor class one. The logit is the log odds, so adding a fixed amount multiplies the odds rather than adding a fixed probability. This allows a linear score to represent a nonlinear probability curve without allowing impossible negative probabilities or values above one.
For mutually exclusive classes, softmax converts a vector of logits into a categorical distribution. Subtracting the same constant from every logit leaves probabilities unchanged; numerical implementations exploit this by subtracting the largest logit before exponentiation. Multilabel tasks are different: an image may contain both a bicycle and a person, so separate sigmoid outputs are often appropriate. The label semantics, rather than the number of output neurons, determine the correct probability model.
A loss rewards probability quality
For a binary label, the Bernoulli likelihood assigns probability p to a positive outcome and one minus p to a negative outcome. Negative log likelihood therefore penalizes assigning very little probability to what actually happened. A correct label guessed at 0.51 and one predicted at 0.99 have the same thresholded accuracy but different losses. Conversely, a confident error is much more costly under log loss than a hesitant one. This makes the training objective sensitive to information discarded by accuracy.
Under the stated binary model, differentiating cross-entropy with respect to the logit gives probability minus label. Use a numerically stable logits-based loss rather than manually taking logarithms of rounded probabilities. For example, PyTorch's BCEWithLogitsLoss combines the sigmoid and binary loss; its input should be logits, not probabilities already passed through sigmoid. Class weighting or resampling changes the effective training objective and can alter probability interpretation, so evaluate and calibrate on the intended population.
Thresholds encode consequences
A threshold of one-half is justified only under particular assumptions, such as equally costly false positives and false negatives with calibrated deployment probabilities. Let a false alarm cost one unit and a missed positive cost nine. Acting positive has expected cost one minus p; acting negative has expected cost nine p. Comparing them gives a threshold of 0.1. The probability model has not changed, but the rational action policy has.
Evaluate a threshold using counts of true positives, false positives, and missed positives. Precision asks how many predicted positives are correct; recall asks how many actual positives were found. A rare class can produce high accuracy with zero useful recall. Choose operating points on validation data, document capacity constraints, and then evaluate once on the held-out test. Costs, prevalence, and intervention effectiveness can change, so a threshold is an operational assumption to revisit rather than an eternal model property.
Work it through
One prediction, two different policies
A calibrated failure model outputs p=0.2. A false intervention costs 1 unit and a missed failure costs 9 units. Assume correct decisions cost zero.
If you intervene, a false alarm happens with probability 0.8, giving expected cost 0.8×1=0.8.
If you do not intervene, a miss happens with probability 0.2, giving expected cost 0.2×9=1.8.
Choose intervention because 0.8 is smaller than 1.8. Solving 1−p<9p gives p>0.1, so a default threshold of 0.5 would implement the wrong costs.
The optimal action here is positive despite the negative class being more probable. This calculation assumes calibrated probabilities, the declared costs, and no extra intervention effects.
The living notebook
Explore the model
Interactive lab · network
From a score to a probability
Change input x and weight w in the single sigmoid computation sigma(wx−0.5). Inspect the resulting logit and probability, then compare action costs separately. The lab does not fit a classifier, estimate calibration, or select a threshold.
- Product · wx
- 1.2
- Logit · wx − 0.5
- 0.7
- Sigmoid output
- 0.6682
One inspectable sigmoid unit, σ(wx − 0.5). Playback traces the computation graph. There is no training, attention mechanism or hidden deep network. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forA score becomes a probability through a specified transformation; consequences still determine the action.
Pause & reason
Check your understanding
Your turn
Try it yourself
A positive example receives probability 0.8 from model A and 0.2 from model B. Compute each binary log loss using natural logarithms and explain the ordering.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
For a positive label, only the negative log of p remains.
Compare with the solution
Model A has loss −ln(0.8), approximately 0.223. Model B has loss −ln(0.2), approximately 1.609. The outcome was positive, so assigning it more probability is rewarded. One observed outcome does not establish which model is calibrated; that requires examining many predictions and outcomes.
Keep the idea
Take it with you
- Choose the probability model from the label semantics.
- Cross-entropy uses confidence information that accuracy discards.
- Thresholds belong to an explicit decision policy.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.