fbpx
Wikipedia

Cross-entropy

In information theory, the cross-entropy between two probability distributions and over the same underlying set of events measures the average number of bits needed to identify an event drawn from the set if a coding scheme used for the set is optimized for an estimated probability distribution , rather than the true distribution .

Definition edit

The cross-entropy of the distribution   relative to a distribution   over a given set is defined as follows:

 ,

where   is the expected value operator with respect to the distribution  .

The definition may be formulated using the Kullback–Leibler divergence  , divergence of   from   (also known as the relative entropy of   with respect to  ).

 

where   is the entropy of  .

For discrete probability distributions   and   with the same support  , this means

 .

 

 

 

 

(Eq.1)

The situation for continuous distributions is analogous. We have to assume that   and   are absolutely continuous with respect to some reference measure   (usually   is a Lebesgue measure on a Borel σ-algebra). Let   and   be probability density functions of   and   with respect to  . Then

 

and therefore

 .

 

 

 

 

(Eq.2)

NB: The notation   is also used for a different concept, the joint entropy of   and  .

Motivation edit

In information theory, the Kraft–McMillan theorem establishes that any directly decodable coding scheme for coding a message to identify one value   out of a set of possibilities   can be seen as representing an implicit probability distribution   over  , where   is the length of the code for   in bits. Therefore, cross-entropy can be interpreted as the expected message-length per datum when a wrong distribution   is assumed while the data actually follows a distribution  . That is why the expectation is taken over the true probability distribution   and not  . Indeed the expected message-length under the true distribution   is

 

Estimation edit

There are many situations where cross-entropy needs to be measured but the distribution of   is unknown. An example is language modeling, where a model is created based on a training set  , 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,   is the true distribution of words in any corpus, and   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:

 

where   is the size of the test set, and   is the probability of event   estimated from the training set. In other words,   is the probability estimate of the model that the i-th word of the text is  . The sum is averaged over the   words of the test. This is a Monte Carlo estimate of the true cross-entropy, where the test set is treated as samples from  [citation needed].

Relation to maximum likelihood edit

The cross entropy arises in classification problems when introducing a logarithm in the guise of the log-likelihood 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  , with   subject to the optimization effort. Consider a given finite sequence of   values   from a training set, obtained from conditionally independent sampling. The likelihood assigned to any considered parameter   of the model is then given by the product over all probabilities  . Repeated occurrences are possible, leading to equal factors in the product. If the count of occurrences of the value equal to   (for some index  ) is denoted by  , then the frequency of that value equals  . Denote the latter by  , as it may be understood as empirical approximation to the probability distribution underlying the scenario. Further denote by   the perplexity, which can be seen to equal   by the calculation rules for the logarithm, and where the product is over the values without double counting. So

 

or

 

Since the logarithm is monotone function increasing function, it does not affect extremization. So observe that the likelihood maximization amounts to minimization of the cross-entropy.

Cross-entropy minimization edit

Cross-entropy minimization is frequently used in optimization and rare-event probability estimation. When comparing a distribution   against a fixed reference distribution  , cross-entropy and KL divergence are identical up to an additive constant (since   is fixed): According to the Gibbs' inequality, both take on their minimal values when  , which is   for KL divergence, and   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, sometimes the distribution   is the fixed prior reference distribution, and the distribution   is optimized to be as close to   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  , rather than  . In fact, cross-entropy is another name for relative entropy; see Cover and Thomas[1] and Good.[2] On the other hand,   does not agree with the literature and can be misleading.

Cross-entropy loss function and logistic regression edit

Cross-entropy can be used to define a loss function in machine learning and optimization. The true probability   is the true label, and the given distribution   is the predicted value of the current model. This is also known as the log loss (or logarithmic loss[3] or logistic loss);[4] the terms "log loss" and "cross-entropy loss" are used interchangeably.[5]

More specifically, consider a binary regression model which can be used to classify observations into two possible classes (often simply labelled   and  ). The output of the model for a given observation, given a vector of input features  , can be interpreted as a probability, which serves as the basis for classifying the observation. In logistic regression, the probability is modeled using the logistic function   where   is some function of the input vector  , commonly just a linear function. The probability of the output   is given by

 

where the vector of weights   is optimized through some appropriate algorithm such as gradient descent. Similarly, the complementary probability of finding the output   is simply given by

 

Having set up our notation,   and  , we can use cross-entropy to get a measure of dissimilarity between   and  :

 
 
Plot shows different loss functions that can be used to train a binary classifier. Only the case where the target output is 1 is shown. It is observed that the loss is zero when the target is equal to the output and increases as the output becomes increasingly incorrect.

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.[6] For example, suppose we have   samples with each sample indexed by  . The average of the loss function is then given by:

 

where  , with   the logistic function as before.

The logistic loss is sometimes called cross-entropy loss. It is also known as log loss.[duplication?] (In this case, the binary label is often denoted by {−1,+1}.[7])

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. That is, define

 
 
 

Then we have the result

 

The proof is as follows. For any  , we have

 
 
 
 
 
 

In a similar way, we eventually obtain the desired result.

See also edit

References edit

  1. ^ Thomas M. Cover, Joy A. Thomas, Elements of Information Theory, 2nd Edition, Wiley, p. 80
  2. ^ I. J. Good, Maximum entropy for hypothesis formulation, especially for multidimensional contingency tables, Ann. of Math. Statistics, 1963
  3. ^ The Mathematics of Information Coding, Extraction and Distribution, by George Cybenko, Dianne P. O'Leary, Jorma Rissanen, 1999, p. 82
  4. ^ Probability for Machine Learning: Discover How To Harness Uncertainty With Python, Jason Brownlee, 2019, p. 220: "Logistic loss refers to the loss function commonly used to optimize a logistic regression model. It may also be referred to as logarithmic loss (which is confusing) or simply log loss."
  5. ^ sklearn.metrics.log_loss
  6. ^ Noel, Mathew; Banerjee, Arindam; D, Geraldine Bessie Amali; Muthiah-Nakarajan, Venkataraman (March 17, 2023). "Alternate loss functions for classification and robust regression can improve the accuracy of artificial neural networks". arXiv:2303.09935. {{cite journal}}: Cite journal requires |journal= (help)
  7. ^ Murphy, Kevin (2012). Machine Learning: A Probabilistic Perspective. MIT. ISBN 978-0262018029.

Further reading edit

  • 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.

cross, entropy, information, theory, cross, entropy, between, probability, distributions, displaystyle, displaystyle, over, same, underlying, events, measures, average, number, bits, needed, identify, event, drawn, from, coding, scheme, used, optimized, estima. In information theory the cross entropy between two probability distributions p displaystyle p and q displaystyle q over the same underlying set of events measures the average number of bits needed to identify an event drawn from the set if a coding scheme used for the set is optimized for an estimated probability distribution q displaystyle q rather than the true distribution p displaystyle p Contents 1 Definition 2 Motivation 3 Estimation 4 Relation to maximum likelihood 5 Cross entropy minimization 6 Cross entropy loss function and logistic regression 7 See also 8 References 9 Further readingDefinition editThe cross entropy of the distribution q displaystyle q nbsp relative to a distribution p displaystyle p nbsp over a given set is defined as follows H p q E p log q displaystyle H p q operatorname E p log q nbsp where E p displaystyle E p cdot nbsp is the expected value operator with respect to the distribution p displaystyle p nbsp The definition may be formulated using the Kullback Leibler divergence D K L p q displaystyle D mathrm KL p parallel q nbsp divergence of p displaystyle p nbsp from q displaystyle q nbsp also known as the relative entropy of p displaystyle p nbsp with respect to q displaystyle q nbsp H p q H p D K L p q displaystyle H p q H p D mathrm KL p parallel q nbsp where H p displaystyle H p nbsp is the entropy of p displaystyle p nbsp For discrete probability distributions p displaystyle p nbsp and q displaystyle q nbsp with the same support X displaystyle mathcal X nbsp this means H p q x X p x log q x displaystyle H p q sum x in mathcal X p x log q x nbsp Eq 1 The situation for continuous distributions is analogous We have to assume that p displaystyle p nbsp and q displaystyle q nbsp are absolutely continuous with respect to some reference measure r displaystyle r nbsp usually r displaystyle r nbsp is a Lebesgue measure on a Borel s algebra Let P displaystyle P nbsp and Q displaystyle Q nbsp be probability density functions of p displaystyle p nbsp and q displaystyle q nbsp with respect to r displaystyle r nbsp Then X P x log Q x d x E p log Q displaystyle int mathcal X P x log Q x mathrm d x operatorname E p log Q nbsp and therefore H p q X P x log Q x d x displaystyle H p q int mathcal X P x log Q x mathrm d x nbsp Eq 2 NB The notation H p q displaystyle H p q nbsp is also used for a different concept the joint entropy of p displaystyle p nbsp and q displaystyle q nbsp Motivation editIn information theory the Kraft McMillan theorem establishes that any directly decodable coding scheme for coding a message to identify one value x i displaystyle x i nbsp out of a set of possibilities x 1 x n displaystyle x 1 ldots x n nbsp can be seen as representing an implicit probability distribution q x i 1 2 ℓ i displaystyle q x i left frac 1 2 right ell i nbsp over x 1 x n displaystyle x 1 ldots x n nbsp where ℓ i displaystyle ell i nbsp is the length of the code for x i displaystyle x i nbsp in bits Therefore cross entropy can be interpreted as the expected message length per datum when a wrong distribution q displaystyle q nbsp is assumed while the data actually follows a distribution p displaystyle p nbsp That is why the expectation is taken over the true probability distribution p displaystyle p nbsp and not q displaystyle q nbsp Indeed the expected message length under the true distribution p displaystyle p nbsp is E p ℓ E p ln q x ln 2 E p log 2 q x x i p x i log 2 q x i x p x log 2 q x H p q displaystyle operatorname E p ell operatorname E p left frac ln q x ln 2 right operatorname E p left log 2 q x right sum x i p x i log 2 q x i sum x p x log 2 q x H p q nbsp Estimation editThere are many situations where cross entropy needs to be measured but the distribution of p displaystyle p nbsp is unknown An example is language modeling where a model is created based on a training set T displaystyle T nbsp 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 displaystyle p nbsp is the true distribution of words in any corpus and q displaystyle q nbsp 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 i 1 N 1 N log 2 q x i displaystyle H T q sum i 1 N frac 1 N log 2 q x i nbsp where N displaystyle N nbsp is the size of the test set and q x displaystyle q x nbsp is the probability of event x displaystyle x nbsp estimated from the training set In other words q x i displaystyle q x i nbsp is the probability estimate of the model that the i th word of the text is x i displaystyle x i nbsp The sum is averaged over the N displaystyle N nbsp 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 displaystyle p x nbsp citation needed Relation to maximum likelihood editThe cross entropy arises in classification problems when introducing a logarithm in the guise of the log likelihood 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 8 displaystyle q theta nbsp with 8 displaystyle theta nbsp subject to the optimization effort Consider a given finite sequence of N displaystyle N nbsp values x i displaystyle x i nbsp from a training set obtained from conditionally independent sampling The likelihood assigned to any considered parameter 8 displaystyle theta nbsp of the model is then given by the product over all probabilities q 8 X x i displaystyle q theta X x i nbsp Repeated occurrences are possible leading to equal factors in the product If the count of occurrences of the value equal to x i displaystyle x i nbsp for some index i displaystyle i nbsp is denoted by x i displaystyle x i nbsp then the frequency of that value equals x i N displaystyle x i N nbsp Denote the latter by p X x i displaystyle p X x i nbsp as it may be understood as empirical approximation to the probability distribution underlying the scenario Further denote by P P e H p q 8 displaystyle PP mathrm e H p q theta nbsp the perplexity which can be seen to equal x i q 8 X x i p X x i displaystyle textstyle prod x i q theta X x i p X x i nbsp by the calculation rules for the logarithm and where the product is over the values without double counting So L 8 x i q 8 X x i x i q 8 X x i x i P P N e N H p q 8 displaystyle mathcal L theta mathbf x prod i q theta X x i prod x i q theta X x i x i PP N mathrm e N cdot H p q theta nbsp or log L 8 x N H p q 8 displaystyle log mathcal L theta mathbf x N cdot H p q theta nbsp Since the logarithm is monotone function increasing function it does not affect extremization So observe that the likelihood maximization amounts to minimization of the cross entropy Cross entropy minimization editMain article Cross entropy method Cross entropy minimization is frequently used in optimization and rare event probability estimation When comparing a distribution q displaystyle q nbsp against a fixed reference distribution p displaystyle p nbsp cross entropy and KL divergence are identical up to an additive constant since p displaystyle p nbsp is fixed According to the Gibbs inequality both take on their minimal values when p q displaystyle p q nbsp which is 0 displaystyle 0 nbsp for KL divergence and H p displaystyle mathrm H p nbsp 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 sometimes the distribution q displaystyle q nbsp is the fixed prior reference distribution and the distribution p displaystyle p nbsp is optimized to be as close to q displaystyle q nbsp 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 K L p q displaystyle D mathrm KL p parallel q nbsp rather than H p q displaystyle H p q nbsp In fact cross entropy is another name for relative entropy see Cover and Thomas 1 and Good 2 On the other hand H p q displaystyle H p q nbsp does not agree with the literature and can be misleading Cross entropy loss function and logistic regression editCross entropy can be used to define a loss function in machine learning and optimization The true probability p i displaystyle p i nbsp is the true label and the given distribution q i displaystyle q i nbsp is the predicted value of the current model This is also known as the log loss or logarithmic loss 3 or logistic loss 4 the terms log loss and cross entropy loss are used interchangeably 5 More specifically consider a binary regression model which can be used to classify observations into two possible classes often simply labelled 0 displaystyle 0 nbsp and 1 displaystyle 1 nbsp The output of the model for a given observation given a vector of input features x displaystyle x nbsp can be interpreted as a probability which serves as the basis for classifying the observation In logistic regression the probability is modeled using the logistic function g z 1 1 e z displaystyle g z 1 1 e z nbsp where z displaystyle z nbsp is some function of the input vector x displaystyle x nbsp commonly just a linear function The probability of the output y 1 displaystyle y 1 nbsp is given by q y 1 y g w x 1 1 e w x displaystyle q y 1 hat y equiv g mathbf w cdot mathbf x frac 1 1 e mathbf w cdot mathbf x nbsp where the vector of weights w displaystyle mathbf w nbsp is optimized through some appropriate algorithm such as gradient descent Similarly the complementary probability of finding the output y 0 displaystyle y 0 nbsp is simply given by q y 0 1 y displaystyle q y 0 1 hat y nbsp Having set up our notation p y 1 y displaystyle p in y 1 y nbsp and q y 1 y displaystyle q in hat y 1 hat y nbsp we can use cross entropy to get a measure of dissimilarity between p displaystyle p nbsp and q displaystyle q nbsp H p q i p i log q i y log y 1 y log 1 y displaystyle H p q sum i p i log q i y log hat y 1 y log 1 hat y nbsp nbsp Plot shows different loss functions that can be used to train a binary classifier Only the case where the target output is 1 is shown It is observed that the loss is zero when the target is equal to the output and increases as the output becomes increasingly incorrect 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 6 For example suppose we have N displaystyle N nbsp samples with each sample indexed by n 1 N displaystyle n 1 dots N nbsp The average of the loss function is then given by J w 1 N n 1 N H p n q n 1 N n 1 N y n log y n 1 y n log 1 y n displaystyle begin aligned J mathbf w amp frac 1 N sum n 1 N H p n q n frac 1 N sum n 1 N bigg y n log hat y n 1 y n log 1 hat y n bigg end aligned nbsp where y n g w x n 1 1 e w x n displaystyle hat y n equiv g mathbf w cdot mathbf x n 1 1 e mathbf w cdot mathbf x n nbsp with g z displaystyle g z nbsp the logistic function as before The logistic loss is sometimes called cross entropy loss It is also known as log loss duplication In this case the binary label is often denoted by 1 1 7 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 That is define X T 1 x 11 x 1 p 1 x 21 x 2 p 1 x n 1 x n p R n p 1 displaystyle X T begin pmatrix 1 amp x 11 amp dots amp x 1p 1 amp x 21 amp cdots amp x 2p vdots amp vdots amp amp vdots 1 amp x n1 amp cdots amp x np end pmatrix in mathbb R n times p 1 nbsp y i f x i 1 x i p 1 1 exp b 0 b 1 x i 1 b p x i p displaystyle hat y i hat f x i1 dots x ip frac 1 1 exp beta 0 beta 1 x i1 dots beta p x ip nbsp L b i 1 N y i log y i 1 y i log 1 y i displaystyle L overrightarrow beta sum i 1 N y i log hat y i 1 y i log 1 hat y i nbsp Then we have the result b L b X T Y Y displaystyle frac partial partial overrightarrow beta L overrightarrow beta X T hat Y Y nbsp The proof is as follows For any y i displaystyle hat y i nbsp we have b 0 ln 1 1 e b 0 k 0 e b 0 k 0 1 e b 0 k 0 displaystyle frac partial partial beta 0 ln frac 1 1 e beta 0 k 0 frac e beta 0 k 0 1 e beta 0 k 0 nbsp b 0 ln 1 1 1 e b 0 k 0 1 1 e b 0 k 0 displaystyle frac partial partial beta 0 ln left 1 frac 1 1 e beta 0 k 0 right frac 1 1 e beta 0 k 0 nbsp b 0 L b i 1 N y i e b 0 k 0 1 e b 0 k 0 1 y i 1 1 e b 0 k 0 i 1 N y i y i i 1 N y i y i displaystyle begin aligned frac partial partial beta 0 L overrightarrow beta amp sum i 1 N left frac y i cdot e beta 0 k 0 1 e beta 0 k 0 1 y i frac 1 1 e beta 0 k 0 right amp sum i 1 N y i hat y i sum i 1 N hat y i y i end aligned nbsp b 1 ln 1 1 e b 1 x i 1 k 1 x i 1 e k 1 e b 1 x i 1 e k 1 displaystyle frac partial partial beta 1 ln frac 1 1 e beta 1 x i1 k 1 frac x i1 e k 1 e beta 1 x i1 e k 1 nbsp b 1 ln 1 1 1 e b 1 x i 1 k 1 x i 1 e b 1 x i 1 e b 1 x i 1 e k 1 displaystyle frac partial partial beta 1 ln left 1 frac 1 1 e beta 1 x i1 k 1 right frac x i1 e beta 1 x i1 e beta 1 x i1 e k 1 nbsp b 1 L b i 1 N x i 1 y i y i i 1 N x i 1 y i y i displaystyle frac partial partial beta 1 L overrightarrow beta sum i 1 N x i1 y i hat y i sum i 1 N x i1 hat y i y i nbsp In a similar way we eventually obtain the desired result See also editCross entropy method Logistic regression Conditional entropy Kullback Leibler distance Maximum likelihood estimation Mutual informationReferences edit Thomas M Cover Joy A Thomas Elements of Information Theory 2nd Edition Wiley p 80 I J Good Maximum entropy for hypothesis formulation especially for multidimensional contingency tables Ann of Math Statistics 1963 The Mathematics of Information Coding Extraction and Distribution by George Cybenko Dianne P O Leary Jorma Rissanen 1999 p 82 Probability for Machine Learning Discover How To Harness Uncertainty With Python Jason Brownlee 2019 p 220 Logistic loss refers to the loss function commonly used to optimize a logistic regression model It may also be referred to as logarithmic loss which is confusing or simply log loss sklearn metrics log loss Noel Mathew Banerjee Arindam D Geraldine Bessie Amali Muthiah Nakarajan Venkataraman March 17 2023 Alternate loss functions for classification and robust regression can improve the accuracy of artificial neural networks arXiv 2303 09935 a href Template Cite journal html title Template Cite journal cite journal a Cite journal requires journal help Murphy Kevin 2012 Machine Learning A Probabilistic Perspective MIT ISBN 978 0262018029 Further reading editde 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 Retrieved from https en wikipedia org w index php title Cross entropy amp oldid 1204338669, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.