Runoob Tutorial ArchiveEnglish

Visualizing KL Divergence — How different are two distributions

Visualizing KL Divergence -- How different are two distributions

Adjust the parameters of two Gaussian distributions, calculate and compare KL(P||Q) and KL(Q||P), and intuitively feel the asymmetry of KL Divergence.

After learning this case study, you will understand: KL Divergence is not a "distance" — the information loss is different when looking from P to Q versus from Q to P.


Real-life introduction

**Finding directions in New York using a Beijing map

You have two local maps: the true New York map (P) and the Beijing map (Q). Walking in New York using the Beijing map requires you to expend a lot of extra effort to "correct" the map's erroneous information — this extra expenditure is KL(P||Q). Conversely, walking in Beijing using the New York map — the extra expenditure is KL(Q||P). These two extra expenditures are clearly different because the "ways of error" in the two local maps are different.


Intuitive understanding

Scenario A: P~N(0,1), Q~N(2,1) — Different means but same shape, KL is nearly symmetric.

Scenario B: P~N(0,1), Q~N(0,3) — Same center but different variances. KL(P||Q) = 1.10 (narrow→wide, easy), KL(Q||P) = 1.55 (wide→narrow, difficult). Clearly asymmetric — "covering narrow with wide" is much easier than "covering wide with narrow".


Mathematical definition

[ D_{KL}(P || Q) = \sum_{x} P(x) \cdot \log\frac{P(x)}{Q(x)} ] [ D_{KL}(P || Q) = H(P, Q) - H(P) ]

Since the entropy of P itself is a constant, minimizing cross-entropy = minimizing KL divergence.


Python hands-on practice

Example

import

numpy

as

np

def

gaussian_pdf

(

x

,

mu

,

sigma

)

:

   

return

(

1

/

(

sigma * np.

sqrt

(

2

pi

)

)

)

np.

exp

(

(

x - mu

)

**

2

/

(

2

2

)

)

def

kl_divergence

(

p

,

q

,

eps

=

1e-12

)

:

p

=

np.

clip

(

p

,

eps

,

None

)

q

=

np.

clip

(

q

,

eps

,

None

)

   

return

np.

sum

(

p * np.

log

(

p / q

)

)

x

=

np.

linspace

(

10

,

10

,

2000

)

dx

=

x

[

1

]

[

0

]

Scenario A: Different means

P_A

=

gaussian_pdf

(

x

,

mu

=

0

,

sigma

=

1

)

Q_A

=

gaussian_pdf

(

x

,

mu

=

2

,

sigma

=

1

)

Scenario B: Different variances

P_B

=

gaussian_pdf

(

x

,

mu

=

0

,

sigma

=

1

)

Q_B

=

gaussian_pdf

(

x

,

mu

=

0

,

sigma

=

3

)

print

(

"RUNOOB KL divergence asymmetry verification:\n"

)

print

(

"Scenario A: P~N(0,1) vs Q~N(2,1)"

)

print

(

f

" KL(P||Q) = {kl_divergence(P_A, Q_A):.4f}"

)

print

(

f

" KL(Q||P) = {kl_divergence(Q_A, P_A):.4f}"

)

print

(

f

"\nScenario B: P~N(0,1) vs Q~N(0,3)"

)

print

(

f

" KL(P||Q) = {kl_divergence(P_B, Q_B):.4f} (Narrow->Wide, Easy)"

)

print

(

f

" KL(Q||P) = {kl_divergence(Q_B, P_B):.4f} (wide->narrow, difficult)"

)

print

(

f

RUNOOB Intuition: 'wide covers narrow' is easy (small KL), 'narrow covers wide' is difficult (large KL)

)

Original prompt (Chinese, left untranslated)



RUNOOB KL Divergence Asymmetry Validation:



场景 A:P~N(0,1)  vs  Q~N(2,1)

  KL(P||Q) = 2.0000

  KL(Q||P) = 1.9960



场景 B:P~N(0,1)  vs  Q~N(0,3)

  KL(P||Q) = 1.0990  (窄->宽,容易)

  KL(Q||P) = 1.5490  (宽->窄,困难)



RUNOOB 直觉:'用宽覆盖窄'容易(KL小),'用窄覆盖宽'困难(KL大)


Application scenarios in AI

Scenario How to use KL divergence
VAE Make the encoding distribution close to standard normal — loss = reconstruction error + KL(encoding||N(0,1))
Knowledge Distillation Make the student model's prediction distribution close to the teacher model—minimize KL(teacher||student)
PPO Reinforcement Learning Constrain the difference between new and old policies using KL divergence to prevent excessive updates