Lesson 7 of 15
Cross-Entropy
Cross-Entropy
Cross-entropy measures the average number of bits needed to encode events from distribution using a code optimized for distribution :
Relationship to KL Divergence
This decomposition shows:
- — the irreducible entropy of (minimum bits needed)
- — extra bits wasted by using the wrong model
Since , we have always.
In Machine Learning
Cross-entropy is the standard loss function for classification. With true labels (one-hot vectors) and model predictions (softmax outputs):
When is one-hot (true class probability = 1, all others = 0), this simplifies to just .
import math
def cross_entropy(p, q):
epsilon = 1e-15
return -sum(p[i] * math.log2(q[i] + epsilon)
for i in range(len(p)) if p[i] > 0)
p = [0.8, 0.2]
q = [0.6, 0.4]
print(round(cross_entropy(p, q), 4)) # 0.854
Your Task
Implement:
cross_entropy(p, q)— forcross_entropy_loss(y_true, y_pred)— same function, ML naming convention
Python runtime loading...
Loading...
Click "Run" to execute your code.