Английская Википедия:Cross-entropy

Материал из Онлайн справочника
Версия от 13:20, 22 февраля 2024; EducationBot (обсуждение | вклад) (Новая страница: «{{Английская Википедия/Панель перехода}} {{Short description|Information-theoretic measure}} {{Information theory}} In information theory, the '''cross-entropy''' between two probability distributions <math>p</math> and <math>q</math> 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 esti...»)
(разн.) ← Предыдущая версия | Текущая версия (разн.) | Следующая версия → (разн.)
Перейти к навигацииПерейти к поиску

Шаблон:Short description Шаблон:Information theory In information theory, the cross-entropy between two probability distributions <math>p</math> and <math>q</math> 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 <math>q</math>, rather than the true distribution <math>p</math>.

Definition

The cross-entropy of the distribution <math>q</math> relative to a distribution <math>p</math> over a given set is defined as follows:

<math>H(p, q) = -\operatorname{E}_p[\log q]</math>,

where <math>E_p[\cdot]</math> is the expected value operator with respect to the distribution <math>p</math>.

The definition may be formulated using the Kullback–Leibler divergence <math>D_{\mathrm{KL}}(p \parallel q)</math>, divergence of <math>p</math> from <math>q</math> (also known as the relative entropy of <math>p</math> with respect to <math>q</math>).

<math>H(p, q) = H(p) + D_{\mathrm{KL}}(p \parallel q),</math>

where <math>H(p)</math> is the entropy of <math>p</math>.

For discrete probability distributions <math>p</math> and <math>q</math> with the same support <math>\mathcal{X}</math>, this means

Шаблон:Equation box 1 |cellpadding= 6 |border |border colour = #0073CF |background colour=#F5FFFA}}

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

<math>-\int_\mathcal{X} P(x)\, \log Q(x)\, \mathrm{d}\ \!x = \operatorname{E}_p[-\log Q],</math>

and therefore

Шаблон:Equation box 1

NB: The notation <math>H(p,q)</math> is also used for a different concept, the joint entropy of <math>p</math> and <math>q</math>.

Motivation

In information theory, the Kraft–McMillan theorem establishes that any directly decodable coding scheme for coding a message to identify one value <math>x_i</math> out of a set of possibilities <math>\{x_1,\ldots,x_n\}</math> can be seen as representing an implicit probability distribution <math>q(x_i) = \left(\frac{1}{2}\right)^{\ell_i}</math> over <math>\{x_1, \ldots, x_n\}</math>, where <math>\ell_i</math> is the length of the code for <math>x_i</math> in bits. Therefore, cross-entropy can be interpreted as the expected message-length per datum when a wrong distribution <math>q</math> is assumed while the data actually follows a distribution <math>p</math>. That is why the expectation is taken over the true probability distribution <math>p</math> and not <math>q</math>. Indeed the expected message-length under the true distribution <math>p</math> is

<math display=block> \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). </math>

Estimation

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

<math>H(T,q) = -\sum_{i=1}^N \frac{1}{N} \log_2 q(x_i)</math>

where <math>N</math> is the size of the test set, and <math>q(x)</math> is the probability of event <math>x</math> estimated from the training set. In other words, <math>q(x_i)</math> is the probability estimate of the model that the i-th word of the text is <math>x_i</math>. The sum is averaged over the <math>N</math> words of the test. This is a Monte Carlo estimate of the true cross-entropy, where the test set is treated as samples from <math>p(x)</math>Шаблон:Citation needed.

Relation to maximum likelihood

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 <math>q_{\theta}</math>, with <math>\theta</math> subject to the optimization effort. Consider a given finite sequence of <math>N</math> values <math>x_i</math> from a training set, obtained from conditionally independent sampling. The likelihood assigned to any considered parameter <math>\theta</math> of the model is then given by the product over all probabilities <math>q_{\theta}(X=x_i)</math>. Repeated occurrences are possible, leading to equal factors in the product. If the count of occurrences of the value equal to <math>x_i</math> (for some index <math>i</math>) is denoted by <math>\#x_i</math>, then the frequency of that value equals <math>\#x_i/N</math>. Denote the latter by <math>p(X=x_i)</math>, as it may be understood as empirical approximation to the probability distribution underlying the scenario. Further denote by <math>PP := {\mathrm e}^{ H(p, q_\theta) }</math> the perplexity, which can be seen to equal <math> {\textstyle \prod_{x_i}} q_{\theta}(X=x_i)^{-p(X=x_i)}</math> by the calculation rules for the logarithm, and where the product is over the values without double counting. So

<math>\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) }</math>

or

<math>\log \mathcal{L}(\theta; {\mathbf x}) = -N\cdot H(p, q_\theta).</math>

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

Шаблон:Main

Cross-entropy minimization is frequently used in optimization and rare-event probability estimation. When comparing a distribution <math>q</math> against a fixed reference distribution <math>p</math>, cross-entropy and KL divergence are identical up to an additive constant (since <math>p</math> is fixed): According to the Gibbs' inequality, both take on their minimal values when <math>p = q</math>, which is <math>0</math> for KL divergence, and <math>\mathrm{H}(p)</math> 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 <math>q</math> is the fixed prior reference distribution, and the distribution <math>p</math> is optimized to be as close to <math>q</math> 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 <math>D_{\mathrm{KL}}(p \parallel q)</math>, rather than <math>H(p, q)</math>. In fact, cross-entropy is another name for relative entropy; see Cover and Thomas[1] and Good.[2] On the other hand, <math>H(p, q)</math> 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 and optimization. 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.[3] The true probability <math>p_i</math> is the true label, and the given distribution <math>q_i</math> is the predicted value of the current model. This is also known as the log loss (or logarithmic loss[4] or logistic loss);[5] the terms "log loss" and "cross-entropy loss" are used interchangeably.[6]

More specifically, consider a binary regression model which can be used to classify observations into two possible classes (often simply labelled <math>0</math> and <math>1</math>). The output of the model for a given observation, given a vector of input features <math> x </math>, 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 <math>g(z) = 1/(1+e^{-z})</math> where <math> z </math> is some function of the input vector <math>x</math>, commonly just a linear function. The probability of the output <math>y=1</math> is given by

<math>q_{y=1} = \hat{y} \equiv g(\mathbf{w}\cdot\mathbf{x}) = \frac 1 {1+e^{-\mathbf{w}\cdot\mathbf{x}}},</math>

where the vector of weights <math>\mathbf{w}</math> is optimized through some appropriate algorithm such as gradient descent. Similarly, the complementary probability of finding the output <math>y=0</math> is simply given by

<math>q_{y=0} = 1-\hat{y}.</math>

Having set up our notation, <math>p\in\{y,1-y\}</math> and <math>q\in\{\hat{y},1-\hat{y}\}</math>, we can use cross-entropy to get a measure of dissimilarity between <math>p</math> and <math>q</math>:

<math>H(p,q)\ =\ -\sum_i p_i\log q_i\ =\ -y\log\hat{y} - (1-y)\log(1-\hat{y}).</math>
Файл:Comparison of Loss functions for binary classification.png
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.[7] For example, suppose we have <math>N</math> samples with each sample indexed by <math>n=1,\dots,N</math>. The average of the loss function is then given by:

<math>\begin{align}
   J(\mathbf{w})\ &=\ \frac1N\sum_{n=1}^N H(p_n,q_n)\ =\ -\frac1N\sum_{n=1}^N\ \bigg[y_n  \log \hat y_n + (1 - y_n)  \log (1 - \hat y_n)\bigg]\,,

\end{align}</math>

where <math>\hat{y}_n\equiv g(\mathbf{w}\cdot\mathbf{x}_n) = 1/(1+e^{-\mathbf{w}\cdot\mathbf{x}_n}) </math>, with <math>g(z)</math> the logistic function as before.

Шаблон:Duplication span (In this case, the binary label is often denoted by {−1,+1}.[8])

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

<math>X^T=\begin{pmatrix}

1&x_{11}&\dots&x_{1p}\\ 1&x_{21}&\cdots&x_{2p}\\ \vdots & \vdots && \vdots \\ 1&x_{n1}&\cdots&x_{np}\\ \end{pmatrix}\in \mathbb{R}^{n\times(p+1)},</math>

<math>\hat{y_i} = \hat{f}(x_{i1},\dots,x_{ip}) = \frac{1}{1+\exp(-\beta_0-\beta_1x_{i1}-\dots-\beta_px_{ip})},</math>
<math>L(\overrightarrow{\beta})=-\sum_{i=1}^N [y_i\log \hat{y}_i+(1-y_i)\log(1-\hat{y}_i)].</math>

Then we have the result

<math>\frac{\partial}{\partial\overrightarrow{\beta}}L(\overrightarrow{\beta})=X^T(\hat{Y}-Y).</math>

The proof is as follows. For any <math>\hat{y}_i</math>, we have

<math>\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}},</math>
<math>\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}},</math>
<math>\begin{align}

\frac{\partial}{\partial\beta_0}L(\overrightarrow{\beta})&=-\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]\\

   &=-\sum_{i=1}^{N}[y_i-\hat{y}_i] = \sum_{i=1}^{N}(\hat{y}_i-y_i),

\end{align}</math>

<math>\frac{\partial}{\partial \beta_1}\ln \frac{1}{1+e^{-\beta_1x_{i1}+k_1}} = \frac{x_{i1}e^{k_1}}{e^{\beta_1x_{i1}}+e^{k_1}},</math>
<math>\frac{\partial}{\partial \beta_1}\ln\left[1-\frac{1}{1+e^{-\beta_1x_{i1}+k_1}}\right] = \frac{-x_{i1}e^{\beta_1x_{i1}}}{e^{\beta_1x_{i1}}+e^{k_1}},</math>
<math>\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).</math>

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

See also

References

Шаблон:Reflist

Further reading

  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. Anqi Mao, Mehryar Mohri, Yutao Zhong. Cross-entropy loss functions: Theoretical analysis and applications. ICML 2023. https://arxiv.org/pdf/2304.07288.pdf
  4. The Mathematics of Information Coding, Extraction and Distribution, by George Cybenko, Dianne P. O'Leary, Jorma Rissanen, 1999, p. 82
  5. 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."
  6. sklearn.metrics.log_loss
  7. Шаблон:Cite journal
  8. Шаблон:Cite book