# Posit AI Blog site: Limitless surprise

Amongst deep knowing specialists, Kullback-Leibler divergence (KL divergence) is possibly best understood for its function in training variational autoencoders (VAEs). To find out a helpful hidden area, we do not simply enhance for excellent restoration. Rather, we likewise enforce a prior on the hidden circulation, and objective to keep them close– typically, by decreasing KL divergence.

In this function, KL divergence imitates a guard dog; it is a constraining, regularizing element, and if anthropomorphized, would appear stern and serious. If we leave it at that, nevertheless, we have actually seen simply one side of its character, and are losing out on its enhance, a photo of playfulness, experience, and interest. In this post, we’ll have a look at that other side.

While being influenced by a series of tweets by Simon de Deo, identifying applications of KL divergence in a huge variety of disciplines, we do not desire supply a detailed review here– as pointed out in the preliminary tweet, the subject might quickly fill an entire term of research study.

The a lot more modest objectives of this post, then, are

1. to rapidly evaluate the function of KL divergence in training VAEs, and point out similar-in-character applications;
2. to show that more spirited, daring “opposite” of its character; and
3. in a not-so-entertaining, however– ideally– beneficial way, distinguish KL divergence from associated ideas such as cross entropy, shared info, or totally free energy.

Prior to however, we begin with a meaning and some terms.

## KL divergence in a nutshell

KL divergence is the anticipated worth of the logarithmic distinction in likelihoods according to 2 circulations, ( p) and ( q) Here it remains in its discrete-probabilities variation:

[begin{equation}
D_{KL}(p||q) = sumlimits_{x} p(x) log(frac{p(x)}{q(x)})
tag{1}
end{equation}]

Significantly, it is uneven; that is, ( D _ {KL} (p|| q)) is not the like ( D _ {KL} (q|| p)) (Which is why it is a divergence, not a range) This element will play an essential function in area 2 devoted to the “opposite.”

To worry this asymmetry, KL divergence is often called relative info (as in “info of ( p) relative to ( q)“), or info gain We concur with among our sources that due to the fact that of its universality and significance, KL divergence would most likely have actually should have a more helpful name; such as, specifically, info gain (Which is less uncertain pronunciation-wise, also.)

In numerous maker discovering algorithms, KL divergence appears in the context of variational reasoning Frequently, for reasonable information, specific calculation of the posterior circulation is infeasible. Therefore, some kind of approximation is needed. In variational reasoning, the real posterior ( p ^ *) is estimated by an easier circulation, ( q), from some tractable household.
To guarantee we have a great approximation, we reduce– in theory, a minimum of– the KL divergence of ( q) relative to ( p ^ *), hence changing reasoning by optimization.

In practice, once again for factors of intractability, the KL divergence decreased is that of ( q) relative to an unnormalized circulation ( widetilde {p} )

[begin{equation}
J(q) = D_{KL}(q||widetilde{p})
tag{2}
end{equation}]

where ( widetilde {p} ) is the joint circulation of criteria and information:

[begin{equation}
widetilde{p}(mathbf{x}) = p(mathbf{x}, mathcal{D}) = p^*(mathbf{x}) p(mathcal{D})
tag{3}
end{equation}]

and ( p ^ *) is the real posterior:

[begin{equation}
p^*(mathbf{x}) = p(mathbf{x}|mathcal{D})
tag{4}
end{equation}]

Comparable to that solution (eq. ( 2 ))– for a derivation see — is this, which reveals the optimization goal to be an upper bound on the unfavorable log-likelihood (NLL):

[begin{equation}
J(q) = D_{KL}(q||p^*) – log p(D)
tag{5}
end{equation}]

Yet another solution– once again, see for information– is the one we in fact utilize when training (e.g.) VAEs. This one represents the anticipated NLL plus the KL divergence in between the approximation ( q) and the enforced prior ( p):

[begin{equation}
J(q) = D_{KL}(q||p) – E_q[- log p(mathcal{D}|mathbf{x})]
tag {6}
end {formula}]

Negated, this solution is likewise called the ELBO, for proof lower bound In the VAE post pointed out above, the ELBO was composed

[begin{equation}
ELBO = E[log p(x|z)] – KL( q( z)|| p( z)).
tag {7}
end {formula}]

with ( z) representing the hidden variables (( q( z)) being the approximation, ( p( z)) the previous, typically a multivariate regular).

### Beyond VAEs

Generalizing this “conservative” action pattern of KL divergence beyond VAEs, we can state that it reveals the quality of approximations. A crucial location where approximation occurs is (lossy) compression KL divergence offers a method to measure just how much info is lost when we compress information.

Summarizing, in these and comparable applications, KL divergence is “bad”– although we do not desire it to be no (otherwise, why trouble utilizing the algorithm?), we definitely wish to keep it low. So now, let’s see the opposite.

## KL divergence, hero

In a 2nd classification of applications, KL divergence is not something to be decreased. In these domains, KL divergence is a sign of surprise, dispute, exploratory habits, or knowing: This really is the viewpoint of info gain

### Surprise

One domain where surprise, not info per se, governs habits is understanding. For instance, eyetracking research studies (e.g., ) revealed that surprise, as determined by KL divergence, was a much better predictor of visual attention than info, determined by entropy. While these research studies appear to have actually promoted the expression “Bayesian surprise,” this substance is– I believe– not the most helpful one, as neither part includes much info to the other. In Bayesian upgrading, the magnitude of the distinction in between previous and posterior shows the degree of surprise caused by the information– surprise is an essential part of the principle.

Therefore, with KL divergence connected to surprise, and surprise rooted in the basic procedure of Bayesian upgrading, a procedure that might be utilized to explain the course of life itself, KL divergence itself ends up being basic. We might get lured to see it all over. Appropriately, it has actually been utilized in numerous fields to measure unidirectional divergence.

For instance, have actually used it in trading, determining just how much an individual disagrees with the marketplace belief. Greater dispute then represents greater anticipated gains from wagering versus the marketplace.

Closer to the location of deep knowing, it is utilized in inherently inspired support knowing (e.g., ), where an ideal policy must optimize the long-lasting info gain. This is possible due to the fact that like entropy, KL divergence is additive.

Although its asymmetry matters whether you utilize KL divergence for regularization (area 1) or surprise (this area), it ends up being specifically obvious when utilized for discovering and surprise.

### Asymmetry in action

Looking once again at the KL formula

[begin{equation}
D_{KL}(p||q) = sumlimits_{x} p(x) log(frac{p(x)}{q(x)})
tag{1}
end{equation}]

the functions of ( p) and ( q) are basically various. For one, the expectation is calculated over the very first circulation (( p) in ( 1 )). This element is essential due to the fact that the “order” (the particular functions) of ( p) and ( q) might need to be selected according to tractability (which circulation are we able to typical over).

Second of all, the portion inside the ( log) suggests that if ( q) is ever no at a point where ( p) isn’t, the KL divergence will “explode.” What this suggests for circulation evaluation in basic is perfectly detailed in In the context of surprise, it suggests that if I find out something I utilized to believe had likelihood no, I will be “definitely stunned.”

To prevent boundless surprise, we can ensure our previous likelihood is never ever no. However even then, the intriguing thing is that just how much info we acquire in any one occasion depends upon just how much info I had previously Let’s see a basic example.

Presume that in my present understanding of the world, black swans most likely do not exist, however they might … perhaps 1 percent of them is black. Put in a different way, my previous belief of a swan, need to I come across one, being black is ( q = 0.01)

Now in reality I do encounter one, and it’s black.
The info I have actually gotten is:

[begin{equation}
l(p,q) = 0 * log(frac{0}{0.99}) + 1 * log(frac{1}{0.01}) = 6.6 bits
tag{8}
end{equation}]

Alternatively, expect I ‘d been a lot more uncertain prior to; state I ‘d have believed the chances were 50:50.
On seeing a black swan, I get a lot less info:

[begin{equation}
l(p,q) = 0 * log(frac{0}{0.5}) + 1 * log(frac{1}{0.5}) = 1 bit
tag{9}
end{equation}]

This view of KL divergence, in regards to surprise and knowing, is motivating– it might lead one to seeing it in action all over. Nevertheless, we still have the 3rd and last job to manage: rapidly compare KL divergence to other ideas in the location.

### Entropy

All of it starts with entropy, or unpredictability, or info, as developed by Claude Shannon.
Entropy is the typical log likelihood of a circulation:

[begin{equation}
H(X) = – sumlimits_{x=1}^n p(x_i) log(p(x_i))
tag{10}
end{equation}]

As perfectly explained in , this solution was selected to please 4 requirements, among which is what we typically photo as its “essence,” and among which is specifically intriguing.

Regarding the previous, if there are ( n) possible states, entropy is optimum when all states are equiprobable. E.g., for a coin flip unpredictability is greatest when coin predisposition is 0.5.

The latter involves coarse-graining, a modification in “resolution” of the state area. State we have 16 possible states, however we do not truly care at that level of information. We do appreciate 3 specific states, however all the rest are essentially the exact same to us. Then entropy decays additively; overall (fine-grained) entropy is the entropy of the grainy area, plus the entropy of the “lumped-together” group, weighted by their likelihoods.

Subjectively, entropy shows our unpredictability whether an occasion will take place. Remarkably however, it exists in the real world also: For instance, when ice melts, it ends up being more unsure where specific particles are. As reported by , the variety of bits launched when one gram of ice melts has to do with 100 billion terabytes!

As remarkable as it is, info per se might, oftentimes, not be the very best ways of identifying human habits. Returning to the eyetracking example, it is entirely user-friendly that individuals take a look at unexpected parts of images, not at white sound locations, which are the optimum you might get in regards to entropy.

As a deep knowing specialist, you have actually most likely been awaiting the point at which we ‘d point out cross entropy— the most typically utilized loss function in classification.

### Cross entropy

The cross entropy in between circulations ( p) and ( q) is the entropy of ( p) plus the KL divergence of ( p) relative to ( q) If you have actually ever executed your own category network, you most likely acknowledge the amount on the really ideal:

[begin{equation}
H(p,q) = H(p) + D_{KL}(p||q) = – sum p log(q)
tag{11}
end{equation}]

In info theory-speak, ( H( p, q)) is the anticipated message length per information when ( q) is presumed however ( p) holds true.
Closer to the world of artificial intelligence, for repaired ( p), decreasing cross entropy is comparable to decreasing KL divergence.

### Shared info

Another incredibly essential amount, utilized in numerous contexts and applications, is shared info Once again pointing out DeDeo, “you can think about it as the most basic kind of connection coefficient that you can determine.”

With 2 variables ( X) and ( Y), we can ask: Just how much do we discover ( X) when we discover a specific ( y), ( Y= y)? Balanced over all ( y), this is the conditional entropy:

[begin{equation}
H(X|Y) = – sumlimits_{i} P(y_i) log(H(X|y_i))
tag{12}
end{equation}]

Now shared info is entropy minus conditional entropy:

[begin{equation}
I(X, Y) = H(X) – H(X|Y) = H(Y) – H(Y|X)
tag{13}
end{equation}]

This amount– as needed for a step representing something like connection– is symmetric: If 2 variables ( X) and ( Y) relate, the quantity of info ( X) provides you about ( Y) amounts to that ( Y) provides you about ( X)

KL divergence becomes part of a household of divergences, called f-divergences, utilized to determine directed distinction in between likelihood circulations. Let’s likewise rapidly look another information-theoretic procedure that unlike those, is a range

### Jensen-Shannon range

In mathematics, a range, or metric, besides being non-negative needs to please 2 other requirements: It should be symmetric, and it should follow the triangle inequality

Both requirements are fulfilled by the Jensen-Shannon range With ( m) a mix circulation:

[begin{equation}
m_i = frac{1}{2}(p_i + q_i)
tag{14}
end{equation}]

the Jensen-Shannon range is approximately KL divergences, among ( m) relative to ( p), the other of ( m) relative to ( q):

[begin{equation}
JSD = frac{1}{2}(KL(m||p) + KL(m||q))
tag{15}
end{equation}]

This would be a perfect prospect to utilize were we thinking about (undirected) range in between, not directed surprise brought on by, circulations.

Lastly, let’s conclude with a last term, limiting ourselves to a fast peek at something entire books might be discussed.

### ( Variational) Free Energy

Checking out documents on variational reasoning, you’re quite most likely to hear individuals talking not “simply” about KL divergence and/or the ELBO (which as quickly as you understand what it means, is simply what it is), however likewise, something inexplicably called totally free energy (or: variational totally free energy, because context).

For useful functions, it is sufficient to understand that variational totally free energy is unfavorable the ELBO, that is, represents formula ( 2 ) However for those interested, there is totally free energy as a main principle in thermodynamics

In this post, we’re generally thinking about how ideas relate to KL divergence, and for this, we follow the characterization John Baez gives up his abovementioned talk

Free energy, that is, energy in beneficial kind, is the anticipated energy minus temperature level times entropy:

[begin{equation}
F = [E] -T H.
tag {16}
end {formula}]

Then, the additional totally free energy of a system ( Q)— compared to a system in stability ( P)— is proportional to their KL divergence, that is, the info of ( Q) relative to ( P):

[begin{equation}
F(Q) – F(P) = k T KL(q||p)
tag{17}
end{equation}]

Mentioning totally free energy, there’s likewise the– not uncontroversial– totally free energy concept presumed in neuroscience. However eventually, we need to stop, and we do it here.

## Conclusion

Concluding, this post has actually attempted to do 3 things: Wanting a reader with background generally in deep knowing, begin with the “regular” usage in training variational autoencoders; then reveal the– most likely less familiar– “opposite”; and lastly, supply a run-through of associated terms and their applications.

If you have an interest in digging deeper into the numerous different applications, in a series of various fields, no much better location to begin than from the Twitter thread, pointed out above, that generated this post. Thanks for checking out!

DeDeo, Simon. 2016. ” Details Theory for Intelligent Individuals.”

Friston, Karl. 2010. ” Friston, k.j.: The Free-Energy Concept: A Unified Brain Theory? Nat. Rev. Neurosci. 11, 127-138.” Nature Evaluations. Neuroscience 11 (February): 127– 38. https://doi.org/10.1038/nrn2787
Itti, Laurent, and Pierre Baldi. 2005. ” Bayesian Surprise Brings In Human Attention.” In Advances in Neural Details Processing Systems 18 [Neural Information Processing Systems, NIPS 2005, December 5-8, 2005, Vancouver, British Columbia, Canada], 547– 54. http://papers.nips.cc/paper/2822-bayesian-surprise-attracts-human-attention

Murphy, Kevin. 2012. Artificial Intelligence: A Probabilistic Viewpoint MIT Press.

Sun, Yi, Faustino J. Gomez, and Juergen Schmidhuber. 2011. ” Preparation to Be Stunned: Ideal Bayesian Expedition in Dynamic Environments.” CoRR abs/1103.5708. http://arxiv.org/abs/1103.5708

Zanardo, Enrico. 2017. ” HOW TO STEP ARGUMENT?” In.