cross-entropy
machine learning
data science
loss function
probability theory

What is cross-entropy?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Cross-entropy is a measure from the field of information theory, often used to evaluate the performance of machine learning models, particularly for classification tasks. It quantifies the difference between two probability distributions: the true distribution (usually referred to as the target distribution) and the predicted distribution.

Understanding Cross-Entropy

In essence, cross-entropy calculates the average number of bits needed to encode (or predict) data taken from a true distribution PP using a code that has been optimized for an approximate distribution QQ. The lower the cross-entropy, the closer the predictions are to the actual distributions.

Mathematical Definition

For discrete variables, the cross-entropy H(P,Q)H(P, Q) between two probability distributions PP and QQ over the same underlying set is defined as:

H(P, Q) = - \sum_{x} P(x) \log Q(x)$$where: * $P(x)$ is the true probability of event $x$. * $Q(x)$ is the predicted probability of event $x$. ### Cross-Entropy in Machine Learning In the context of machine learning, particularly classification problems, cross-entropy is widely used as a loss function. The cross-entropy loss function measures the performance of a classification model whose output is a probability value between 0 and 1. The loss increases as the predicted probability diverges from the actual label. #### Binary Classification For a binary classification task, the cross-entropy loss can be expressed as: $$L(y, \hat{y}) = - \left[y \log(\hat{y}) + (1 - y) \log(1 - \hat{y})\right]$$where: * $y$ is the actual binary label (0 or 1). * $\hat{y}$ is the predicted probability of the label being 1. #### Multiclass Classification For a multiclass classification problem with a one-hot encoded true distribution, this extends to: $$L(y, \hat{y}) = - \sum_{c=1}^{C} y_c \log(\hat{y}_c)$$where: * $C$ is the number of classes. * $y_c$ is the binary indicator (0 or 1) if class label $c$ is the correct classification for observation; otherwise 0. * $\hat{y}_c$ is the probability that observation is of class $c$. ### Advantages of Using Cross-Entropy * **Sensitivity to Error:** Cross-entropy loss is more sensitive to changes in the model predictions than other loss functions, such as Mean Squared Error (MSE), particularly for large differences between predicted probabilities and true distributions. * **Probabilistic Interpretation:** It works well when outputs are in the form of probabilities, allowing a more intuitive probabilistic interpretation of the outcome. ### Example Consider a binary classification problem where a model outputs a probability $\hat{y}$, and the true label $y$ is 1. Possible outcomes of cross-entropy loss could be: | $\hat{y}$ | $y$ | Cross-Entropy Loss | | --------------------- | ----- | ------------------ | | 0.9 | 1 | 0.105 | | 0.7 | 1 | 0.357 | | 0.3 | 1 | 1.203 | | 0.1 | 1 | 2.303 | The closer $\hat{y}$ is to $y$, the lower the loss, indicating better performance of the model. ### Related Concepts * **Kullback-Leibler Divergence (KL Divergence):** Cross-entropy is related to KL Divergence, which quantifies the amount of additional information required to represent $P$ using $Q$. The relationship is given by: $$ H(P, Q) = H(P) + D_{KL}(P \| Q) where H(P)H(P) is the entropy of PP.

  • Entropy: Entropy H(P)H(P) measures the amount of uncertainty inherent in the distribution PP. Cross-entropy can be viewed as the entropy of the true distribution plus the additional cost due to the approximation with QQ.

Implementations in Python

In Python, common libraries like Scikit-Learn, Keras, and TensorFlow offer built-in functions to compute cross-entropy loss, making it very accessible for machine learning practitioners. For instance, in TensorFlow, the function might be used as:

python
1import tensorflow as tf
2
3# Define true labels and predictions
4y_true = [[0, 1], [0, 0], [1, 0]]
5y_pred = [[0.05, 0.95], [0.1, 0.9], [0.9, 0.1]]
6
7# Calculate the categorical crossentropy
8loss = tf.keras.losses.categorical_crossentropy(y_true, y_pred)

Through the utilization of cross-entropy, models can be accurately calibrated with respect to the likelihood of outcomes, allowing for efficient learning and precise predictions, playing a critical role in advancing artificial intelligence and data science applications.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.