第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 that follows the probability distribution with a p.d.f. or a p.m.f. , and we estimate by another probability distribution with a p.d.f. or a p.m.f. . Then the Kullback–Leibler (KL) divergence (or relative entropy) between and is
英語のヒント
p.d.f.あるいはp.m.f.をもつ確率分布に従う確率変数が与えられ、p.d.f.あるいはp.m.f.をもつ別の確率分布でを推定するとする。すると、との間のカルバック・ライブラー(KL)ダイバージェンス(あるいは相対エントロピー)は次のとおりである。
(22.11.21)
As with the pointwise mutual information (22.11.20), we can again provide an interpretation of the logarithmic term: will be large and positive if we see far more often under than we would expect for , 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))と同じように、対数の項にもやはり解釈を与えられる。は、について予想されるよりものもとでをはるかに頻繁に見るなら大きく正になり、予想よりはるかに少なくしかその結果を見ないなら大きく負になる。このようにして、これは、基準の分布からその結果を観測したときにどれだけ驚くかと比べた、その結果を観測したときの相対的な驚きとして解釈できる。
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 such that
英語のヒント
KLダイバージェンスは非対称である。つまり、次を満たすが存在する。(22.11.22) - KL divergence is non-negative, i.e.,
英語のヒント
KLダイバージェンスは非負である。つまり、(22.11.23)Note that the equality holds only when .英語のヒント
等号が成り立つのはのときだけであることに注意しよう。 - If there exists an such that and , then .
英語のヒント
かつとなるが存在するなら、である。 - There is a close relationship between KL divergence and mutual information. Besides the relationship shown in Fig. 22.11.1, is also numerically equivalent with the following terms:
英語のヒント
KLダイバージェンスと相互情報量の間には密接な関係がある。図22.11.1に示した関係に加えて、は次の項とも数値的に等しい。For the first term, we interpret mutual information as the KL divergence between and the product of and , 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 that results from learning the value of the ’s distribution. Similarly to the third term.英語のヒント
1番目の項については、相互情報量をと、との積との間のKLダイバージェンスと解釈する。したがってこれは、同時分布が、独立だった場合の分布とどれだけ異なるかの尺度である。2番目の項については、相互情報量は、の分布の値を知ることから生じる、についての不確かさの平均の減少を教えてくれる。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 : an objective tensor which follows a normal distribution , and two candidate tensors and which follow normal distributions and respectively.
英語のヒント
まず、長さのテンソルを三つ生成して並べ替えよう。正規分布に従う目的のテンソルと、それぞれ正規分布とに従う二つの候補のテンソルとである。
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 and are symmetric with respect to the y-axis (i.e., ), we expect a similar value of KL divergence between and . As you can see below, there is only a less than 3% off between and .
英語のヒント
とはy軸について(つまりについて)対称なので、との間でKLダイバージェンスの値が似ていると予想する。以下に見るように、との間には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 and are off a lot, with around 40% off as shown below.
英語のヒント
対照的に、とは、以下に示すように約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)