Cross Entropy Loss
   HOME

TheInfoList



OR:

In
information theory Information theory is the mathematical study of the quantification (science), quantification, Data storage, storage, and telecommunications, communication of information. The field was established and formalized by Claude Shannon in the 1940s, ...
, the cross-entropy between two
probability distribution In probability theory and statistics, a probability distribution is a Function (mathematics), function that gives the probabilities of occurrence of possible events for an Experiment (probability theory), experiment. It is a mathematical descri ...
s p and q, over the same underlying set of events, measures the average number of
bit The bit is the most basic unit of information in computing and digital communication. The name is a portmanteau of binary digit. The bit represents a logical state with one of two possible values. These values are most commonly represented as ...
s needed to identify an event drawn from the set when the coding scheme used for the set is optimized for an estimated probability distribution q, rather than the true distribution p.


Definition

The cross-entropy of the distribution q relative to a distribution p over a given set is defined as follows: H(p, q) = -\operatorname_p
log q Log most often refers to: * Trunk (botany), the stem and main wooden axis of a tree, called logs when cut ** Logging, cutting down trees for logs ** Firewood, logs used for fuel ** Lumber or timber, converted from wood logs * Logarithm, in mathem ...
where \operatorname_p cdot/math> is the
expected value In probability theory, the expected value (also called expectation, expectancy, expectation operator, mathematical expectation, mean, expectation value, or first Moment (mathematics), moment) is a generalization of the weighted average. Informa ...
operator with respect to the distribution p. The definition may be formulated using the
Kullback–Leibler divergence In mathematical statistics, the Kullback–Leibler (KL) divergence (also called relative entropy and I-divergence), denoted D_\text(P \parallel Q), is a type of statistical distance: a measure of how much a model probability distribution is diff ...
D_(p \parallel q), divergence of p from q (also known as the ''relative entropy'' of p with respect to q). H(p, q) = H(p) + D_(p \parallel q), where H(p) is the
entropy Entropy is a scientific concept, most commonly associated with states of disorder, randomness, or uncertainty. The term and the concept are used in diverse fields, from classical thermodynamics, where it was first recognized, to the micros ...
of p. For
discrete Discrete may refer to: *Discrete particle or quantum in physics, for example in quantum theory * Discrete device, an electronic component with just one circuit element, either passive or active, other than an integrated circuit * Discrete group, ...
probability distributions p and q with the same
support Support may refer to: Arts, entertainment, and media * Supporting character * Support (art), a solid surface upon which a painting is executed Business and finance * Support (technical analysis) * Child support * Customer support * Income Su ...
\mathcal, this means The situation for
continuous Continuity or continuous may refer to: Mathematics * Continuity (mathematics), the opposing concept to discreteness; common examples include ** Continuous probability distribution or random variable in probability and statistics ** Continuous ...
distributions is analogous. We have to assume that p and q are
absolutely continuous In calculus and real analysis, absolute continuity is a smoothness property of functions that is stronger than continuity and uniform continuity. The notion of absolute continuity allows one to obtain generalizations of the relationship betwe ...
with respect to some reference measure r (usually r is a
Lebesgue measure In measure theory, a branch of mathematics, the Lebesgue measure, named after French mathematician Henri Lebesgue, is the standard way of assigning a measure to subsets of higher dimensional Euclidean '-spaces. For lower dimensions or , it c ...
on a
Borel Borel may refer to: People * Antoine Borel (1840–1915), a Swiss-born American businessman * Armand Borel (1923–2003), a Swiss mathematician * Borel (author), 18th-century French playwright * Borel (1906–1967), pseudonym of the French actor ...
σ-algebra In mathematical analysis and in probability theory, a σ-algebra ("sigma algebra") is part of the formalism for defining sets that can be measured. In calculus and analysis, for example, σ-algebras are used to define the concept of sets with a ...
). Let P and Q be probability density functions of p and q with respect to r. Then -\int_\mathcal P(x)\, \log Q(x)\, \mathrmx = \operatorname_p \log Q and therefore NB: The notation H(p,q) is also used for a different concept, the joint entropy of p and q.


Motivation

In
information theory Information theory is the mathematical study of the quantification (science), quantification, Data storage, storage, and telecommunications, communication of information. The field was established and formalized by Claude Shannon in the 1940s, ...
, the Kraft–McMillan theorem establishes that any directly decodable coding scheme for coding a message to identify one value x_i out of a set of possibilities \ can be seen as representing an implicit probability distribution q(x_i) = \left(\frac\right)^ over \, where \ell_i is the length of the code for x_i in bits. Therefore, cross-entropy can be interpreted as the expected message-length per datum when a wrong distribution q is assumed while the data actually follows a distribution p. That is why the expectation is taken over the true probability distribution p and not q. Indeed the expected message-length under the true distribution p is \begin \operatorname_p
ell An ell (from Proto-Germanic *''alinō'', cognate with Latin ''ulna'') is a northwestern European unit of measurement, originally understood as a cubit (the combined length of the forearm and extended hand). The word literally means "arm", an ...
&= - \operatorname_p\left frac\right\\ ex&= - \operatorname_p\left log_2 \right\\ ex&= - \sum_ p(x_i)\, \log_2 q(x_i) \\ ex&= -\sum_x p(x)\, \log_2 q(x) = H(p, q). \end


Estimation

There are many situations where cross-entropy needs to be measured but the distribution of p is unknown. An example is
language model A language model is a model of the human brain's ability to produce natural language. Language models are useful for a variety of tasks, including speech recognition, machine translation,Andreas, Jacob, Andreas Vlachos, and Stephen Clark (2013)"S ...
ing, where a model is created based on a training set T, and then its cross-entropy is measured on a test set to assess how accurate the model is in predicting the test data. In this example, p is the true distribution of words in any corpus, and q is the distribution of words as predicted by the model. Since the true distribution is unknown, cross-entropy cannot be directly calculated. In these cases, an estimate of cross-entropy is calculated using the following formula: H(T,q) = -\sum_^N \frac \log_2 q(x_i) where N is the size of the test set, and q(x) is the probability of event x estimated from the training set. In other words, q(x_i) is the probability estimate of the model that the i-th word of the text is x_i. The sum is averaged over the N words of the test. This is a Monte Carlo estimate of the true cross-entropy, where the test set is treated as samples from p(x).


Relation to maximum likelihood

The cross entropy arises in classification problems when introducing a logarithm in the guise of the
log-likelihood A likelihood function (often simply called the likelihood) measures how well a statistical model explains observed data by calculating the probability of seeing that data under different parameter values of the model. It is constructed from the j ...
function. The section is concerned with the subject of estimation of the probability of different possible discrete outcomes. To this end, denote a parametrized family of distributions by q_, with \theta subject to the optimization effort. Consider a given finite sequence of N values x_i from a training set, obtained from
conditionally independent In probability theory, conditional independence describes situations wherein an observation is irrelevant or redundant when evaluating the certainty of a hypothesis. Conditional independence is usually formulated in terms of conditional probabi ...
sampling. The likelihood assigned to any considered parameter \theta of the model is then given by the product over all probabilities q_(X=x_i). Repeated occurrences are possible, leading to equal factors in the product. If the count of occurrences of the value equal to x_i (for some index i) is denoted by \#x_i, then the frequency of that value equals \#x_i/N. Denote the latter by p(X=x_i), as it may be understood as empirical approximation to the probability distribution underlying the scenario. Further denote by PP := ^ the
perplexity In information theory, perplexity is a measure of uncertainty in the value of a sample from a discrete probability distribution. The larger the perplexity, the less likely it is that an observer can guess the value which will be drawn from the ...
, which can be seen to equal \prod_ q_(X=x_i)^ by the calculation rules for the logarithm, and where the product is over the values without double counting. So \mathcal(\theta; ) = \prod_ q_(X=x_i) = \prod_ q_(X=x_i)^ = PP^ = ^ or \log \mathcal(\theta; ) = -N\cdot H(p, q_\theta). Since the logarithm is a monotonically increasing function, it does not affect extremization. So observe that the likelihood maximization amounts to minimization of the cross-entropy.


Cross-entropy minimization

Cross-entropy minimization is frequently used in optimization and rare-event probability estimation. When comparing a distribution q against a fixed reference distribution p, cross-entropy and
KL divergence KL, kL, kl, or kl. may refer to: Businesses and organizations * KLM, a Dutch airline (IATA airline designator KL) * Koninklijke Landmacht, the Royal Netherlands Army * Kvenna Listin ("Women's List"), a political party in Iceland * KL FM, a Ma ...
are identical up to an additive constant (since p is fixed): According to the
Gibbs' inequality 200px, Josiah Willard Gibbs In information theory, Gibbs' inequality is a statement about the information entropy of a discrete probability distribution. Several other bounds on the entropy of probability distributions are derived from Gibbs' ineq ...
, both take on their minimal values when p = q, which is 0 for KL divergence, and \mathrm(p) for cross-entropy. In the engineering literature, the principle of minimizing KL divergence (Kullback's " Principle of Minimum Discrimination Information") is often called the Principle of Minimum Cross-Entropy (MCE), or Minxent. However, as discussed in the article ''
Kullback–Leibler divergence In mathematical statistics, the Kullback–Leibler (KL) divergence (also called relative entropy and I-divergence), denoted D_\text(P \parallel Q), is a type of statistical distance: a measure of how much a model probability distribution is diff ...
'', sometimes the distribution q is the fixed prior reference distribution, and the distribution p is optimized to be as close to q as possible, subject to some constraint. In this case the two minimizations are ''not'' equivalent. This has led to some ambiguity in the literature, with some authors attempting to resolve the inconsistency by restating cross-entropy to be D_(p \parallel q), rather than H(p, q). In fact, cross-entropy is another name for
relative entropy Relative may refer to: General use *Kinship and family, the principle binding the most basic social units of society. If two people are connected by circumstances of birth, they are said to be ''relatives''. Philosophy *Relativism, the concept t ...
; see Cover and Thomas and Good. On the other hand, H(p, q) does not agree with the literature and can be misleading.


Cross-entropy loss function and logistic regression

Cross-entropy can be used to define a loss function in
machine learning Machine learning (ML) is a field of study in artificial intelligence concerned with the development and study of Computational statistics, statistical algorithms that can learn from data and generalise to unseen data, and thus perform Task ( ...
and
optimization Mathematical optimization (alternatively spelled ''optimisation'') or mathematical programming is the selection of a best element, with regard to some criteria, from some set of available alternatives. It is generally divided into two subfiel ...
. Mao, Mohri, and Zhong (2023) give an extensive analysis of the properties of the family of cross-entropy loss functions in machine learning, including theoretical learning guarantees and extensions to adversarial learning. The true probability p_i is the true label, and the given distribution q_i is the predicted value of the current model. This is also known as the log loss (or logarithmic loss or
logistic loss In machine learning and mathematical optimization, loss functions for classification are computationally feasible loss functions representing the price paid for inaccuracy of predictions in classification problems (problems of identifying which ...
); the terms "log loss" and "cross-entropy loss" are used interchangeably. More specifically, consider a binary regression model which can be used to classify observations into two possible classes (often simply labelled 0 and 1). The output of the model for a given observation, given a vector of input features x , can be interpreted as a probability, which serves as the basis for classifying the observation. In
logistic regression In statistics, a logistic model (or logit model) is a statistical model that models the logit, log-odds of an event as a linear function (calculus), linear combination of one or more independent variables. In regression analysis, logistic regres ...
, the probability is modeled using the
logistic function A logistic function or logistic curve is a common S-shaped curve ( sigmoid curve) with the equation f(x) = \frac where The logistic function has domain the real numbers, the limit as x \to -\infty is 0, and the limit as x \to +\infty is L. ...
g(z) = 1/(1+e^) where z is some function of the input vector x, commonly just a linear function. The probability of the output y=1 is given by q_ = \hat \equiv g(\mathbf\cdot\mathbf) = \frac 1 , where the vector of weights \mathbf is optimized through some appropriate algorithm such as
gradient descent Gradient descent is a method for unconstrained mathematical optimization. It is a first-order iterative algorithm for minimizing a differentiable multivariate function. The idea is to take repeated steps in the opposite direction of the gradi ...
. Similarly, the complementary probability of finding the output y=0 is simply given by q_ = 1-\hat. Having set up our notation, p\in\ and q\in\, we can use cross-entropy to get a measure of dissimilarity between p and q: \begin H(p,q) &= -\sum_i p_i \log q_i \\ ex&= -y\log\hat - (1-y) \log(1-\hat). \end Logistic regression typically optimizes the log loss for all the observations on which it is trained, which is the same as optimizing the average cross-entropy in the sample. Other loss functions that penalize errors differently can be also used for training, resulting in models with different final test accuracy. For example, suppose we have N samples with each sample indexed by n=1,\dots,N. The ''average'' of the loss function is then given by: \begin J(\mathbf) &= \frac \sum_^N H(p_n,q_n) \\ &= -\frac \sum_^N\ \left _n \log \hat y_n + (1 - y_n) \log (1 - \hat y_n)\right \end where \hat_n\equiv g(\mathbf\cdot\mathbf_n) = 1/(1+e^) , with g(z) the logistic function as before. (In this case, the binary label is often denoted by .) Remark: The gradient of the cross-entropy loss for logistic regression is the same as the gradient of the squared-error loss for
linear regression In statistics, linear regression is a statistical model, model that estimates the relationship between a Scalar (mathematics), scalar response (dependent variable) and one or more explanatory variables (regressor or independent variable). A mode ...
. That is, define X^\mathsf = \begin 1 & x_ & \dots & x_ \\ 1 & x_ & \cdots & x_ \\ \vdots & \vdots & & \vdots \\ 1 & x_ & \cdots & x_ \\ \end\in \mathbb^, \hat = \hat(x_,\dots,x_) = \frac, L(\boldsymbol) = -\sum_^N \left _i\log \hat_i+(1-y_i)\log(1-\hat_i)\right Then we have the result \fracL(\boldsymbol)=X^T(\hat-Y). The proof is as follows. For any \hat_i, we have \frac\ln\frac = \frac, \frac\ln \left(1-\frac\right)=\frac, \begin \fracL(\boldsymbol) &= -\sum_^\left frac-(1-y_i)\frac\right\\ &= - \sum_^ \left _i-\hat_i\right = \sum_^(\hat_i-y_i), \end \frac\ln \frac = \frac, \frac\ln\left -\frac\right= \frac, \fracL(\boldsymbol) = -\sum_^N x_(y_i-\hat_i) = \sum_^N x_(\hat_i-y_i). In a similar way, we eventually obtain the desired result.


Amended cross-entropy

It may be beneficial to train an ensemble of models that have diversity, such that when they are combined, their predictive accuracy is augmented. Assuming a simple ensemble of K classifiers is assembled via averaging the outputs, then the amended cross-entropy is given by e^k = H(p,q^k)-\frac\sum_H(q^j,q^k) where e^k is the cost function of the k^ classifier, q^k is the output probability of the k^ classifier, p is the true probability to be estimated, and \lambda is a parameter between 0 and 1 that defines the 'diversity' that we would like to establish among the ensemble. When \lambda=0 we want each classifier to do its best regardless of the ensemble and when \lambda=1 we would like the classifier to be as diverse as possible.


See also

* Cross-entropy method *
Logistic regression In statistics, a logistic model (or logit model) is a statistical model that models the logit, log-odds of an event as a linear function (calculus), linear combination of one or more independent variables. In regression analysis, logistic regres ...
*
Conditional entropy In information theory, the conditional entropy quantifies the amount of information needed to describe the outcome of a random variable Y given that the value of another random variable X is known. Here, information is measured in shannons, n ...
* Kullback–Leibler distance *
Maximum-likelihood estimation In statistics, maximum likelihood estimation (MLE) is a method of estimating the parameters of an assumed probability distribution, given some observed data. This is achieved by maximizing a likelihood function so that, under the assumed stati ...
*
Mutual information In probability theory and information theory, the mutual information (MI) of two random variables is a measure of the mutual Statistical dependence, dependence between the two variables. More specifically, it quantifies the "Information conten ...
*
Perplexity In information theory, perplexity is a measure of uncertainty in the value of a sample from a discrete probability distribution. The larger the perplexity, the less likely it is that an observer can guess the value which will be drawn from the ...


References


Further reading

* de Boer, Kroese, D.P., Mannor, S. and Rubinstein, R.Y. (2005)
A tutorial on the cross-entropy method
''Annals of Operations Research'' 134 (1), 19–67. {{DEFAULTSORT:Cross Entropy Entropy and information Loss functions