2.2.10 · D1Linear & Logistic Regression

Foundations — Log-loss - binary cross-entropy

1,909 words9 min readBack to topic

Everything below builds those tools from scratch, in the exact order the parent note uses them. No symbol appears before its plain-words meaning and its picture.


1. A label — the ground truth

The curly braces just list the only allowed values. There is no "0.5 truth" — reality already happened, so the answer is one or the other.

Why the topic needs it: log-loss compares a guess to this fixed 0-or-1 answer. Without a crisp truth there is nothing to score against.


2. A probability — the model's guess

The little hat on (read "y-hat") is a universal convention meaning "estimated version of ". It is not the truth; it is the model's opinion.

Round brackets = open interval (endpoints excluded); square brackets = closed interval (endpoints included). This tiny distinction matters later: can crawl close to 0 or 1 but is never allowed to touch them, because touching breaks the logarithm (§4).

Figure — Log-loss  -  binary cross-entropy

Picture as a slider from 0 to 1. If the slider sits at 0.9 the model is saying "I'm 90% sure the answer is 1." Look at the red marker: it can slide right up against the walls but the walls themselves are forbidden.

Why the topic needs it: the whole loss is a function of this one number. See Sigmoid function and Logistic Regression for where comes from.


3. The set and the "only one term survives" trick

Because is exactly 0 or 1, any expression multiplied by or by acts like a switch:

This is why the parent's Bernoulli formula collapses to a single case each time — the exponents 0 and 1 turn factors on and off (, ).

Why the topic needs it: it lets one clean formula cover both label cases without an if statement.


4. The logarithm — turning "unlikely" into "costly"

This is the star tool, so we build it slowly.

In machine learning "" with no base written means the natural log , base . The choice of base only rescales the answer (natural log gives units called nats), so it never changes which model wins.

Why not just use the probability directly? Why introduce a new tool at all? Because we need a scoring rule with two specific shapes, and only delivers both:

  1. As the correct-answer probability approaches 1, the cost should fall to 0. Indeed .
  2. As that probability approaches 0, the cost should explode to , punishing confident mistakes brutally. Indeed as .

No polynomial does this — you need the log's blow-up near zero.

Figure — Log-loss  -  binary cross-entropy

Look at the red curve of : flat and near zero on the right (you gave the right answer high probability → cheap), and rocketing up on the left (you gave it almost no chance → ruinously expensive). Trace the marked points: vs .

Why the topic needs it: is the log-loss. Everything else is bookkeeping.


5. The sigmoid — where is born

The model first computes a plain number (a weighted sum, could be any value from to ). We must squash it into a valid probability in .

Figure — Log-loss  -  binary cross-entropy

The red S-curve is the sigmoid. Notice it lives entirely inside the band — that's why its output is a legitimate . The dashed line marks , the point of maximum indecision.

One fact the parent uses without proof — the slope of the sigmoid: Read as "the derivative — how fast rises as increases." Its value is largest at (steepest in the middle) and near zero far out on either side (flat tails). This exact expression is the thing that magically cancels inside the gradient in the parent note.

Why the topic needs it: links the model's raw score to the probability the loss consumes. See Sigmoid function.


6. Sum , product , and the average

The parent scores a whole dataset of examples, so we need shorthand for "combine all of them."

  • Product appears when we combine independent probabilities — independence means joint chance = multiply.
  • Sum appears after we take a log, because turns products into sums (easier, and no tiny-number underflow).
  • The factor simply averages, so the loss doesn't grow just because you have more data — it stays comparable across dataset sizes.

Why the topic needs it: it's the machinery that lifts the one-example loss to the whole-batch loss .


7. Two loss symbols: and

Why the topic needs it: the parent derives first (clean, single-case), then averages into (what gradient descent actually minimizes — see Gradient Descent).


Sanity check with the parent's numbers


Prerequisite map

True label y in 0 or 1

Switch trick from 0 or 1

Raw score z any real number

Sigmoid squashes z into 0 to 1

Predicted probability y-hat

Logarithm cost blows up near 0

Sum and product over N points

Average with one over N

Single example loss ell

Batch loss J the log-loss


Equipment checklist

What does mean and why is it never 0.5?
is the fixed true label, only 0 or 1; reality already happened so there is no fractional truth.
What is and what interval does it live in?
The model's predicted probability that the label is 1; , open interval (endpoints forbidden).
Why do round brackets matter for ?
Endpoints excluded so never receives 0 or 1 and never produces /NaN.
What question does answer?
"To what power must I raise to get ?" — it undoes exponentiation.
Why use as the cost instead of the probability itself?
It gives 0 cost for a correct-and-confident guess and cost for a confident wrong guess.
Can be negative for ?
No; of a number below 1 is negative, and the leading minus flips it positive, so cost .
Write the sigmoid and its output range.
, output strictly in ; .
State the sigmoid slope identity.
.
What is the "switch trick" of and ?
Multiplying by keeps a term when , by keeps it when ; exactly one survives.
Difference between and , and when each appears?
adds (appears after taking logs); multiplies (appears for joint probability of independent points).
Why divide the batch loss by ?
To average, so the loss is comparable regardless of dataset size.
Difference between and ?
is one example's loss; is the average over the batch.