0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

KLダイバージェンスの式と計算例

0
Last updated at Posted at 2025-11-17

KLダイバージェンスの式

連続型確率分布

\begin{align}
\mathrm{KL}(p||q) = \int p(x) \log{\frac{p(x)}{q(x)}} dx = \int \left[ p(x) \log{p(x)} - p(x) \log{q(x)} \right] dx
\end{align}

離散型確率分布

\begin{align}
\mathrm{KL}(p||q) = \sum_{i=1}^{n} p(x_{i}) \log{\frac{p(x_{i})}{q(x_{i})}} = \sum_{i=1}^{n} \left[ p(x_{i}) \log{p(x_{i})} - p(x_{i}) \log{q(x_{i})} \right]
\end{align}

KLダイバージェンスの計算例

連続型確率分布

離散型確率分布

import numpy as np
import matplotlib.pyplot as plt


x = np.array([1, 2])

p = np.array([0.8, 0.2])
q_1 = np.array([0.75, 0.25])
q_2 = np.array([0.5, 0.5])

plt.plot(x, p, label="GroundTruth")
plt.plot(x, q_1, label="Prediction1")
plt.plot(x, q_2, label="Prediction2")

plt.legend()
plt.savefig("distribution.png")

KL1 = np.sum(p*np.log(p/q_1))
KL2 = np.sum(p*np.log(p/q_2))

print("KL of prediction1: {:.5f}".format(KL1))
print("KL of prediction2: {:.5f}".format(KL2))

・実行結果

KL of prediction1: 0.00700
KL of prediction2: 0.19274

distribution.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?