プリンキピア

第22章付録:深層学習のための数学

22.11.4. Kullback–Leibler Divergence22.11.4 カルバック・ライブラー・ダイバージェンス

As what we have discussed in Section 2.3, we can use norms to measure distance between two points in space of any dimensionality. We would like to be able to do a similar task with probability distributions. There are many ways to go about this, but information theory provides one of the nicest. We now explore the Kullback–Leibler (KL) divergence, which provides a way to measure if two distributions are close together or not.
英語のヒント
2.3節で論じたように、ノルムを使えば、任意の次元の空間の2点間の距離を測れる。確率分布についても同じような作業ができるとよい。その方法はたくさんあるが、情報理論はその中でも最もよいものの一つを与える。ここでは、二つの分布が近いかどうかを測る方法を与える、カルバック・ライブラー(KL)ダイバージェンスを探る。

22.11.4.1. Definition22.11.4.1 定義

Given a random variable X that follows the probability distribution P with a p.d.f. or a p.m.f. p(x), and we estimate P by another probability distribution Q with a p.d.f. or a p.m.f. q(x). Then the Kullback–Leibler (KL) divergence (or relative entropy) between P and Q is
英語のヒント
p.d.f.あるいはp.m.f.p(x)をもつ確率分布Pに従う確率変数Xが与えられ、p.d.f.あるいはp.m.f.q(x)をもつ別の確率分布QでPを推定するとする。すると、PとQの間のカルバック・ライブラー(KL)ダイバージェンス(あるいは相対エントロピー)は次のとおりである。
DKL(P∥Q)=Ex∼P[log⁡p(x)q(x)].(22.11.21)
As with the pointwise mutual information (22.11.20), we can again provide an interpretation of the logarithmic term: −log⁡q(x)p(x)=−log⁡(q(x))−(−log⁡(p(x))) will be large and positive if we see x far more often under P than we would expect for Q, and large and negative if we see the outcome far less than expected. In this way, we can interpret it as our relative surprise at observing the outcome compared to how surprised we would be observing it from our reference distribution.
英語のヒント
点ごとの相互情報量(式(22.11.20))と同じように、対数の項にもやはり解釈を与えられる。−log⁡q(x)p(x)=−log⁡(q(x))−(−log⁡(p(x)))は、Qについて予想されるよりもPのもとでxをはるかに頻繁に見るなら大きく正になり、予想よりはるかに少なくしかその結果を見ないなら大きく負になる。このようにして、これは、基準の分布からその結果を観測したときにどれだけ驚くかと比べた、その結果を観測したときの相対的な驚きとして解釈できる。
Let’s implement the KL divergence from Scratch.
英語のヒント
KLダイバージェンスをゼロから実装しよう。
def kl_divergence(p, q):
    kl = p * torch.log2(p / q)
    out = nansum(kl)
    return out.abs().item()

22.11.4.2. KL Divergence Properties22.11.4.2 KLダイバージェンスの性質

Let’s take a look at some properties of the KL divergence (22.11.21).
英語のヒント
KLダイバージェンス(式(22.11.21))のいくつかの性質を見てみよう。
  • KL divergence is non-symmetric, i.e., there are P,Q such that
    英語のヒント
    KLダイバージェンスは非対称である。つまり、次を満たすP,Qが存在する。
    DKL(P∥Q)≠DKL(Q∥P).(22.11.22)
  • KL divergence is non-negative, i.e.,
    英語のヒント
    KLダイバージェンスは非負である。つまり、
    DKL(P∥Q)≥0.(22.11.23)
    Note that the equality holds only when P=Q.
    英語のヒント
    等号が成り立つのはP=Qのときだけであることに注意しよう。
  • If there exists an x such that p(x)>0 and q(x)=0, then DKL(P∥Q)=∞.
    英語のヒント
    p(x)>0かつq(x)=0となるxが存在するなら、DKL(P∥Q)=∞である。
  • There is a close relationship between KL divergence and mutual information. Besides the relationship shown in Fig. 22.11.1, I(X,Y) is also numerically equivalent with the following terms:
    英語のヒント
    KLダイバージェンスと相互情報量の間には密接な関係がある。図22.11.1に示した関係に加えて、I(X,Y)は次の項とも数値的に等しい。
    1. DKL(P(X,Y) ∥ P(X)P(Y));
    2. EY{DKL(P(X∣Y) ∥ P(X))};
    3. EX{DKL(P(Y∣X) ∥ P(Y))}.
    For the first term, we interpret mutual information as the KL divergence between P(X,Y) and the product of P(X) and P(Y), and thus is a measure of how different the joint distribution is from the distribution if they were independent. For the second term, mutual information tells us the average reduction in uncertainty about Y that results from learning the value of the X’s distribution. Similarly to the third term.
    英語のヒント
    1番目の項については、相互情報量をP(X,Y)と、P(X)とP(Y)の積との間のKLダイバージェンスと解釈する。したがってこれは、同時分布が、独立だった場合の分布とどれだけ異なるかの尺度である。2番目の項については、相互情報量は、Xの分布の値を知ることから生じる、Yについての不確かさの平均の減少を教えてくれる。3番目の項も同様である。

22.11.4.3. Example22.11.4.3 例

Let’s go through a toy example to see the non-symmetry explicitly.
英語のヒント
おもちゃの例をたどって、非対称性をはっきり見てみよう。
First, let’s generate and sort three tensors of length 10,000: an objective tensor p which follows a normal distribution N(0,1), and two candidate tensors q1 and q2 which follow normal distributions N(−1,1) and N(1,1) respectively.
英語のヒント
まず、長さ10,000のテンソルを三つ生成して並べ替えよう。正規分布N(0,1)に従う目的のテンソルpと、それぞれ正規分布N(−1,1)とN(1,1)に従う二つの候補のテンソルq1とq2である。
torch.manual_seed(1)

tensor_len = 10000
p = torch.normal(0, 1, (tensor_len, ))
q1 = torch.normal(-1, 1, (tensor_len, ))
q2 = torch.normal(1, 1, (tensor_len, ))

p = torch.sort(p)[0]
q1 = torch.sort(q1)[0]
q2 = torch.sort(q2)[0]
Since q1 and q2 are symmetric with respect to the y-axis (i.e., x=0), we expect a similar value of KL divergence between DKL(p∥q1) and DKL(p∥q2). As you can see below, there is only a less than 3% off between DKL(p∥q1) and DKL(p∥q2).
英語のヒント
q1とq2はy軸について(つまりx=0について)対称なので、DKL(p∥q1)とDKL(p∥q2)の間でKLダイバージェンスの値が似ていると予想する。以下に見るように、DKL(p∥q1)とDKL(p∥q2)の間には3%未満の差しかない。
kl_pq1 = kl_divergence(p, q1)
kl_pq2 = kl_divergence(p, q2)
similar_percentage = abs(kl_pq1 - kl_pq2) / ((kl_pq1 + kl_pq2) / 2) * 100

kl_pq1, kl_pq2, similar_percentage
(8582.0341796875, 8828.3095703125, 2.8290698237936858)
In contrast, you may find that DKL(q2∥p) and DKL(p∥q2) are off a lot, with around 40% off as shown below.
英語のヒント
対照的に、DKL(q2∥p)とDKL(p∥q2)は、以下に示すように約40%と大きくずれていることがわかるだろう。
kl_q2p = kl_divergence(q2, p)
differ_percentage = abs(kl_q2p - kl_pq2) / ((kl_q2p + kl_pq2) / 2) * 100

kl_q2p, differ_percentage
(14130.125, 46.18621024399691)