Both of Cross-entropy and KL divergence are tools to measure the distance between two probability distribution. What is the difference? $$ H(P,Q) = -\sum_x P(x)\log Q(x) $$ $$ KL(P | Q) = \sum_{x} P(x)\log {\frac{P(x)}{Q(x)}} $$ Moreover, minimization of KL is equivalent to minimization of Cross-Entropy.

I want to know them instinctively.

Thank you very much in advance.


You will need some conditions to claim the equivalence between minimizing cross entropy and minimizing KL divergence. I will put your question under the context of classification problems using cross entropy as loss functions.

Let us first recall that entropy is used to measure the uncertainty of a system, which is defined as \begin{equation} S(v)=-\sum_ip(v_i)\log p(v_i)\label{eq:entropy}, \end{equation} for $p(v_i)$ as the probabilities of different states $v_i$ of the system. From an information theory point of view, $S(v)$ is the amount of information is needed for removing the uncertainty.

For instance, the event A I will die eventually is almost certain (maybe we can solve the aging problem for word almost), therefore it has low entropy which requires only the information of the aging problem cannot be solved to make it certain. However, the event B The president will die in 50 years is much more uncertain than A, thus it needs more information to remove the uncertainties.

Now look at the definition of KL divergence between events A and B \begin{equation} D_{KL}(A\parallel B) = \sum_ip_A(v_i)\log p_A(v_i) - p_A(v_i)\log p_B(v_i)\label{eq:kld}, \end{equation} where the first term of the right hand side is the entropy of event A, the second term can be interpreted as the expectation of event B in terms of event A. And the $D_{KL}$ describes how different B is from A from the perspective of A.

To relate cross entropy to entropy and KL divergence, we formalize the cross entropy in terms of events A and B as \begin{equation} H(A, B) = -\sum_ip_A(v_i)\log p_B(v_i)\label{eq:crossentropy}. \end{equation} From the definitions, we can easily see \begin{equation} H(A, B) = D_{KL}(A\parallel B)+S_A\label{eq:entropyrelation}. \end{equation} If $S_A$ is a constant, then minimizing $H(A, B)$ is equivalent to minimizing $D_{KL}(A\parallel B)$.

A further question follows naturally as how the entropy can be a constant. In a machine learning task, we start with a dataset (denoted as $P(\mathcal D)$) which represent the problem to be solved, and the learning purpose is to make the model estimated distribution (denoted as $P(model)$) as close as possible to true distribution of the problem (denoted as $P(truth)$). $P(truth)$ is unknown and represented by $P(\mathcal D)$. Therefore in an ideal world, we expect \begin{equation} P(model)\approx P(\mathcal D) \approx P(truth) \end{equation} and minimize $D_{KL}(P(\mathcal D)\parallel P(model))$. And luckily, in practice $\mathcal D$ is given, which means its entropy $S(D)$ is fixed as a constant.

| cite | improve this answer | |
  • $\begingroup$ Thank you for your answer. It deepened my understanding. So when we have a dataset, it is more effective to minimize cross- entropy rather than KL, right? However, I cannot understand the proper use of them. In other words, when should I minimize KL or cross entropy? $\endgroup$ – Jourd Jul 19 '18 at 14:00
  • 1
    $\begingroup$ After reading your answer, I think it is no use to minimize KL because we always have a dataset, P(D). $\endgroup$ – Jourd Jul 19 '18 at 14:03
  • $\begingroup$ Ideally, one would choose KL divergence to measure the distance between two distributions. In the context of classification, the cross-entropy loss usually arises from the negative log likelihood, for example, when you choose Bernoulli distribution to model your data. $\endgroup$ – doubllle Jul 19 '18 at 14:14
  • $\begingroup$ You might want to look at this great post. The symmetry is not problem in classification as the goal of machine learning models is to make predicted distribution as close as possible to the fixed P(D), though regularizations are usually added to avoid overfitting. $\endgroup$ – doubllle Jul 19 '18 at 14:35
  • $\begingroup$ I understood the asymmetry of KL. However, I haven't understood how to use minimization of KL or Cross-Entropy differently yet. It means that when should I minimize KL and when should I minimize Cross-Entropy. I think $S_A$ is always a constant, isn't it? $\endgroup$ – Jourd Jul 19 '18 at 22:15

I suppose it is because the models usually work with the samples packed in mini-batches. For KL divergence and Cross-Entropy, their relation can be written as $$H(q, p) = D_{KL}(p, q)+H(p) = -\sum_i{p_ilog(q_i)}$$ From the equation, we could see that KL divergence can depart into a Cross-Entropy of p and q (the first part), and a global entropy of ground truth p (the second part).

In many machine learning projects, minibatch is involved to expedite training, where the $p'$ of a minibatch may be different from the global $p$. In such a case, Cross-Entropy is relatively more robust in practice while KL divergence needs a more stable H(p) to finish her job.

| cite | improve this answer | |
  • 1
    $\begingroup$ This answer is what I was looking for. In my own current experience, which involves learning a target probabilities, BCE is way more robust than KL. Basically, KL was unusable. KL and BCE aren't "equivalent" loss functions. $\endgroup$ – Nicholas Leonard Nov 29 '19 at 16:31

This is how I think about it:

$$ D_{KL}(p(y_i | x_i) \:||\: q(y_i | x_i, \theta)) = H(p(y_i | x_i, \theta), q(y_i | x_i, \theta)) - H(p(y_i | x_i, \theta)) \tag{1}\label{eq:kl} $$

where $p$ and $q$ are two probability distributions. In machine learning, we typically know $p$, which is the distribution of the target. For example, in a binary classification problem, $\mathcal{Y} = \{0, 1\}$, so if $y_i = 1$, $p(y_i = 1 | x) = 1$ and $p(y_i = 0 | x) = 0$, and vice versa. Given each $y_i \: \forall \: i = 1, 2, \ldots, N$, where $N$ is the total number of points in the dataset, we typically want to minimize the KL divergence $D_{KL}(p,q)$ between the distribution of the target $p(y_i | x)$ and our predicted distribution $q(y_i | x, \theta)$, averaged over all $i$. (We do so by tuning our model parameters $\theta$. Thus, for each training example, the model is spitting out a distribution over the class labels $0$ and $1$.) For each example, since the target is fixed, its distribution never changes. Thus, $H(p(y_i | x_i))$ is constant for each $i$, regardless of what our current model parameters $\theta$ are. Thus, the minimizer of $D_{KL}(p,q)$ is equal to the minimizer of $H(p, q)$.

If you had a situation where $p$ and $q$ were both variable (say, in which $x_1\sim p$ and $x_2\sim q$ were two latent variables) and wanted to match the two distributions, then you would have to choose between minimizing $D_{KL}$ and minimizing $H(p, q)$. This is because minimizing $D_{KL}$ implies maximizing $H(p)$ while minimizing $H(p, q)$ implies minimizing $H(p)$. To see the latter, we can solve equation (\ref{eq:kl}) for $H(p,q)$: $$ H(p,q) = D_{KL}(p,q) + H(p) \tag{2}\label{eq:hpq} $$ The former would yield a broad distribution for $p$ while the latter would yield one that is concentrated in one or a few modes. Note that it is your choice as a ML practitioner whether you want to minimize $D_{KL}(p, q)$ or $D_{KL}(q, p)$. A small discussion of this is given in the context of variational inference (VI) below.

In VI, you must choose between minimizing $D_{KL}(p,q)$ and $D_{KL}(q,p)$, which are not equal since KL divergence is not symmetric. If we once again treat $p$ as known, then minimizing $D_{KL}(p, q)$ would result in a distribution $q$ that is sharp and focused on one or a few areas while minimizing $D_{KL}(q, p)$ would result in a distribution $q$ that is wide and covers a broad range of the domain of $q$. Again, the latter is because minimizing $D_{KL}(q, p)$ implies maximizing the entropy of $q$.

| cite | improve this answer | |
  • $\begingroup$ In equation (1) on the left side you don't have $\theta$ in $p(y_i|x_i)$, whereas on the right side you have $p(y_i|x_i, \theta)$. Why? Also in the 5-th row you should use $x_i$ instead of $x$. $\endgroup$ – Rodvi May 19 at 13:45
  • $\begingroup$ Also, will the entropy $H(p)$ be typically constant in the case of generative classifiers $q(y,x|\theta)$, in the case of regression models, and in the case of non-parametric models (not assuming latent variable case)? $\endgroup$ – Rodvi May 19 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.