Log loss is cross entropy, and its floor is the entropy
Average the negative log-likelihood over the rows, and you get the log loss. scikit-learn's documentation calls it logistic loss or cross-entropy loss, the negative log-likelihood of a logistic model. In general, for a true distribution p over some outcomes and a model's q, the cross entropy is
H(p,q)=−∑ipilogqi
Predict
Is H(p, q) the same number as H(q, p)?
The Deep Learning book notes that any loss made of a negative log-likelihood is a cross entropy between the data's distribution and the model's. With q = p, it's Shannon's entropy, H(p), one of the quantities he said play a central role in information theory as measures of information, choice and uncertainty.
Pick an answer above to see the rest
Predict
Predict the shop's actual return rate, 12.3%, for every order. What's the log loss?
def log_loss(outcomes, q): """The average negative log-likelihood of 0/1 outcomes when every prediction is q.""" return -np.mean(outcomes * np.log(q) + (1 - outcomes) * np.log(1 - q))rate = k / nfor q in (0.5, 0.3, rate, 0.05): print(f"predict {q:.1%} for every order: log loss {log_loss(returned, q):.3f}")print(f"entropy of a {rate:.1%} rate: {-(rate * np.log(rate) + (1 - rate) * np.log(1 - rate)):.3f}")
predict 50.0% for every order: log loss 0.693predict 30.0% for every order: log loss 0.461predict 12.3% for every order: log loss 0.374predict 5.0% for every order: log loss 0.415entropy of a 12.3% rate: 0.374
Guessing too high or too low both cost more. That's the minimum error in the OpenAI question: against data from p, no q scores below H(p), and only q = p reaches it. Shannon showed the entropy vanishes only when the outcome is certain, so any real return rate leaves a floor above 0. The gap above the floor is the KL divergence, the next lesson's subject:
H(p,q)=H(p)+DKL(p∥q)
MacKay states Gibbs' inequality: the KL divergence is never negative, and it's 0 only if the two distributions are equal. So, in the Deep Learning book's words, minimizing the cross entropy with respect to q is the same as minimizing the KL divergence. Move both sliders, and the curve's lowest point stays at q = p, on the floor.
Cross entropy and its floorInteractive
cross entropy, nats per order ↑
the model's prediction q →
cross entropy at each qthe floor: the entropy of pyour q, and its gap above the floor
Cross entropy
0.458
Entropy, the floor
0.367
Gap
0.091
Predicting 30% when the true rate is 12% costs 0.458 nats per order. The floor is the entropy, 0.367, and only q = p reaches it. The gap, 0.091, is the KL divergence, the next lesson's subject.
In the editor, write cross_entropy(p, q). It prints a 12% rate scored against 50%.
Pick an answer above to see the rest
Instructions
1
Write cross_entropy(p, q): −Σ p log q with natural logs, where a term with p = 0 counts as 0.