エントロピーは「この分布が平均してどれだけの情報を持つか」に答えます。今回は、データは同じでも、予測に使う分布が間違っていたら、どれだけ余分なコストがかかるかを考えます。
エントロピーは情報源自身の平均情報量、交差エントロピーは別の分布で割り当てた情報長の平均、KL 発散はその差です。
分布を間違えると、何が余分になる?
真の分布を p、モデルを q とします。モデルは結果 x に理想情報長 −log2q(x) を割り当てます。よく起こると思う結果ほど、長さは短くなります。
データは依然として p に従うため、平均の重みは p です。
H(p,q)=−x∑p(x)log2q(x)
これが交差エントロピーです。モデルと真の分布が一致すれば、H(p,p)=H(p) になります。
−log2q(x) は小数にもなる理想情報長です。実際の記号単位の二進接頭符号の長さは整数でなければなりません。適切な独立同分布の長いブロック符号では、記号当たり平均長を対応する交差エントロピーに近づけられますが、任意の有限符号の正確な長さと同一ではありません。Stanford のデータ圧縮講義も参照してください。
図を準備しています
データを固定して、符号化の予測だけを変えるp(A)=0.75 を固定し、q(A) だけを調整します。棒は q による理想情報長、破線は p による長さです。平均を一定の情報源エントロピーと追加の KL コストに分けます。
{"id":"kl-coding-cost","title":"データを固定して、符号化の予測だけを変える","summary":"p(A)=0.75 を固定し、q(A) だけを調整します。棒は q による理想情報長、破線は p による長さです。平均を一定の情報源エントロピーと追加の KL コストに分けます。","height":900,"html":"<div class=\"eyebrow\" data-i18n=\"eyebrow\"></div><h2 data-i18n=\"heading\"></h2><p class=\"hint\" data-i18n=\"intro\"></p>\n<div class=\"source\"><span data-i18n=\"source\"></span><div class=\"source-band\"><span>A · 75%</span><span>B · 25%</span></div></div>\n<div class=\"control\"><label for=\"model-q\" data-i18n=\"control\"></label><output id=\"q-value\" for=\"model-q\"></output></div><input type=\"range\" id=\"model-q\" min=\"1\" max=\"99\" step=\"1\" value=\"25\"><div class=\"scale\"><span>1%</span><span>50%</span><span>99%</span></div>\n<div class=\"legend\"><span><i class=\"target\"></i><span data-i18n=\"marker\"></span></span></div>\n<div class=\"cost-row\"><div class=\"row-label\"><strong>A <small>p = 0.75</small></strong><span id=\"cost-a\" class=\"number\"></span></div><div class=\"cost-track\"><span id=\"length-a\"></span><i id=\"ideal-a\"></i></div></div>\n<div class=\"cost-row\"><div class=\"row-label\"><strong>B <small>p = 0.25</small></strong><span id=\"cost-b\" class=\"number\"></span></div><div class=\"cost-track second\"><span id=\"length-b\"></span><i id=\"ideal-b\"></i></div></div>\n<div class=\"scale\"><span>0 bit</span><span>7 bit</span></div>\n<div class=\"summary\"><div class=\"summary-line\"><span data-i18n=\"average\"></span><strong><output id=\"cross-entropy\"></output> bit</strong></div><div class=\"average-track\"><span id=\"base-cost\"></span><span id=\"extra-cost\"></span></div><div class=\"scale\"><span>0 bit</span><span>7 bit</span></div>\n<div class=\"cost-equation\"><span>H(p) = <strong id=\"entropy\"></strong></span><span>+ KL = <strong id=\"kl\"></strong></span></div></div>\n<div class=\"copy-stack\"><p id=\"explanation\"></p><p class=\"reserve\" aria-hidden=\"true\" data-i18n=\"matched\"></p><p class=\"reserve\" aria-hidden=\"true\" data-i18n=\"mismatch\"></p></div><p class=\"note\" data-i18n=\"note\"></p>","css":".control { display:flex; align-items:center; justify-content:space-between; gap:12px; margin-top:22px; }\n.control output { min-width:4.5em; flex-shrink:0; text-align:center; font-size:14px; font-weight:600; font-variant-numeric:tabular-nums; color:var(--accent); padding:3px 10px; background:var(--accent-soft); border-radius:6px; }\n.scale { display:flex; justify-content:space-between; font-size:10px; color:var(--muted); margin-top:5px; }\n.note { color:var(--muted); font-size:11px; line-height:1.7; margin-top:16px; }\n.copy-stack { display:grid; font-size:13px; line-height:1.75; margin-top:20px; }\n.copy-stack > * { grid-area:1/1; min-width:0; }\n.reserve { visibility:hidden; pointer-events:none; user-select:none; }\n.number { font-variant-numeric:tabular-nums; }\n.legend { display:flex; flex-wrap:wrap; gap:6px 16px; font-size:12px; color:var(--muted); margin:20px 0 12px; }\n.legend span { display:inline-flex; align-items:center; gap:6px; }\n.legend i { display:inline-block; width:16px; height:0; border-top:2px solid var(--accent); }\n.legend .target { border-color:var(--muted); border-top-style:dashed; }\n.summary { border-top:1px solid var(--rule); margin-top:22px; padding-top:16px; }\n.summary-line { display:flex; justify-content:space-between; flex-wrap:wrap; gap:8px 16px; align-items:baseline; font-size:13px; color:var(--muted); }\n.summary-line strong { font-size:23px; font-weight:500; color:var(--ink); }\n@media(max-width:480px) { .copy-stack { font-size:12px; } }\n.source { margin-top:22px; font-size:12px; color:var(--muted); }\n.source-band { display:flex; height:30px; margin-top:8px; border-radius:4px; overflow:hidden; font-size:12px; }\n.source-band span { display:grid; place-items:center; width:75%; background:var(--surface); color:var(--ink); }\n.source-band span+span { width:25%; background:var(--rule); color:var(--ink); }\n.cost-row { margin-top:20px; }\n.row-label { display:flex; align-items:baseline; justify-content:space-between; flex-wrap:wrap; gap:6px; font-size:13px; margin-bottom:9px; }\n.row-label small { color:var(--muted); font-weight:400; margin-left:5px; font-size:11px; }\n.cost-track,.average-track { height:22px; position:relative; background:var(--surface); border-radius:3px; }\n.cost-track > span { display:block; height:100%; background:var(--ink); border-radius:3px; }\n.cost-track.second > span { background:var(--ink); }\n.cost-track i { position:absolute; top:-4px; bottom:-4px; border-left:2px dashed var(--muted); }\n.average-track { display:flex; margin-top:14px; overflow:hidden; }\n#base-cost { background:var(--muted); } #extra-cost { background:var(--accent); }\n.cost-equation { display:flex; justify-content:space-between; gap:8px; font-size:13px; margin-top:14px; font-variant-numeric:tabular-nums; }\n.cost-equation span:first-child { color:var(--muted); } .cost-equation span+span { color:var(--accent); }\n","js":"const input=document.querySelector('#model-q'), p=.75;\nconst h=viz.entropy([p,1-p]);\ndocument.querySelector('#ideal-a').style.left=(-Math.log2(p)/7*100)+'%';\ndocument.querySelector('#ideal-b').style.left=(-Math.log2(1-p)/7*100)+'%';\ndocument.querySelector('#base-cost').style.width=(h/7*100)+'%';\ndocument.querySelector('#entropy').textContent=viz.number(h);\nfunction draw(){\n const q=Number(input.value)/100, a=-Math.log2(q), b=-Math.log2(1-q), cross=p*a+(1-p)*b, kl=Math.max(0,cross-h);\n document.querySelector('#q-value').textContent=viz.number(q*100,0)+'%';\n document.querySelector('#cost-a').textContent=`−log₂ ${viz.number(q)} ≈ ${viz.number(a)} bit`;\n document.querySelector('#cost-b').textContent=`−log₂ ${viz.number(1-q)} ≈ ${viz.number(b)} bit`;\n document.querySelector('#length-a').style.width=(a/7*100)+'%';document.querySelector('#length-b').style.width=(b/7*100)+'%';\n document.querySelector('#cross-entropy').textContent=viz.number(cross);document.querySelector('#kl').textContent=viz.number(kl);\n document.querySelector('#extra-cost').style.width=(kl/7*100)+'%';\n document.querySelector('#explanation').textContent=viz.t(q===p?'matched':'mismatch');\n}\ninput.addEventListener('input',draw);draw();","audio":false,"strings":{"average":"平均理想情報長 H(p,q)","control":"モデルが考える A の確率 q(A)","eyebrow":"01 / 間違ったモデルのコスト","heading":"よく起こる結果を過小評価すると、何 bit 余分になる?","intro":"真の頻度は変わりません。モデルの予測を動かし、A、B の長さと重み付きの合計を見てください。","marker":"破線:真の p で割り当てる長さ","matched":"q と p が一致すると、両方の長さが破線に揃います。平均は H(p) で、追加の KL コストは 0 です。","mismatch":"一つの結果が安くなっても、真の頻度で重み付けした平均は H(p) を下回りません。灰色の基礎部分は一定で、青が追加の平均 KL コストです。","note":"長さは −log₂q(x) で、小数にもなります。実際の整数長の符号語ではありません。有限の目盛りのため操作範囲は 1%〜99% です。起こり得る結果に確率 0 を与えるとコストは発散します。","source":"固定した真の分布 p"}}
相対エントロピー / KL 発散は次のように定義されます。
DKL(p∥q)=x∑p(x)log2q(x)p(x)=H(p,q)−H(p)
DKL(p∥q)≥0 で、等号は分布が一致するときに成立します。これは平均としての追加コストです。個別の結果では q のほうが安くなり、項が負になる場合があります。KL 全体が非負でも、各項がすべて非負とは限りません。
p(x)>0 なのに q(x)=0 なら、実際に起こり得る結果に無限の情報長を割り当てることになり、交差エントロピーとこの向きの KL は無限大です。p(x)=0 の項は極限の約束により 0 とします。
向きを入れ替えると、なぜ答えが変わる?
DKL(p∥q) は p の下で平均します。DKL(q∥p) では、重みが q に変わり、対数の中の比も逆数になります。
以下では p=(0.5,0.5)、q=(0.9,0.1) を固定し、計算の向きだけを交換します。ゼロ線の上は正、下は負です。符号付き面積の和が KL になります。
図を準備しています
向きを交換すると、平均の重みも変わる同じ固定分布の組で KL の向きを切り替えます。長方形の幅は平均の重み、符号付きの高さは対数比です。符号付き面積の和は約 0.74 bit と 0.53 bit になります。
{"id":"kl-direction","title":"向きを交換すると、平均の重みも変わる","summary":"同じ固定分布の組で KL の向きを切り替えます。長方形の幅は平均の重み、符号付きの高さは対数比です。符号付き面積の和は約 0.74 bit と 0.53 bit になります。","height":900,"html":"<div class=\"eyebrow\" data-i18n=\"eyebrow\"></div><h2 data-i18n=\"heading\"></h2><p class=\"hint\" data-i18n=\"intro\"></p>\n<div class=\"fixed-pair\"><span>p = (0.5, 0.5)</span><span>q = (0.9, 0.1)</span></div>\n<div class=\"direction-control\" role=\"group\" data-i18n-label=\"control\"><button type=\"button\" data-direction=\"forward\">KL(p ‖ q)</button><button type=\"button\" data-direction=\"reverse\">KL(q ‖ p)</button></div>\n<div class=\"direction-caption\"><span id=\"weight-label\"></span><span data-i18n=\"height\"></span></div>\n<div class=\"signed-chart\" role=\"img\" data-i18n-label=\"chart\"><div class=\"tick top\">+2.5</div><div class=\"zero\"><i>0</i></div><div class=\"tick bottom\">−2.5</div><div class=\"signed-area a\" id=\"term-a\"></div><div class=\"signed-area b\" id=\"term-b\"></div></div>\n<div class=\"weight-labels\"><span id=\"weight-a\"></span><span id=\"weight-b\"></span></div>\n<div class=\"term-equations\"><div><strong>A</strong><span id=\"expression-a\"></span></div><div><strong>B</strong><span id=\"expression-b\"></span></div></div>\n<div class=\"summary summary-line\"><span id=\"direction-formula\"></span><strong><output id=\"divergence\"></output> bit</strong></div>\n<div class=\"copy-stack\"><p id=\"explanation\"></p><p class=\"reserve\" data-i18n=\"forward\" aria-hidden=\"true\"></p><p class=\"reserve\" data-i18n=\"reverse\" aria-hidden=\"true\"></p></div><p class=\"note\" data-i18n=\"note\"></p>","css":".control { display:flex; align-items:center; justify-content:space-between; gap:12px; margin-top:22px; }\n.control output { min-width:4.5em; flex-shrink:0; text-align:center; font-size:14px; font-weight:600; font-variant-numeric:tabular-nums; color:var(--accent); padding:3px 10px; background:var(--accent-soft); border-radius:6px; }\n.scale { display:flex; justify-content:space-between; font-size:10px; color:var(--muted); margin-top:5px; }\n.note { color:var(--muted); font-size:11px; line-height:1.7; margin-top:16px; }\n.copy-stack { display:grid; font-size:13px; line-height:1.75; margin-top:20px; }\n.copy-stack > * { grid-area:1/1; min-width:0; }\n.reserve { visibility:hidden; pointer-events:none; user-select:none; }\n.number { font-variant-numeric:tabular-nums; }\n.legend { display:flex; flex-wrap:wrap; gap:6px 16px; font-size:12px; color:var(--muted); margin:20px 0 12px; }\n.legend span { display:inline-flex; align-items:center; gap:6px; }\n.legend i { display:inline-block; width:16px; height:0; border-top:2px solid var(--accent); }\n.legend .target { border-color:var(--muted); border-top-style:dashed; }\n.summary { border-top:1px solid var(--rule); margin-top:22px; padding-top:16px; }\n.summary-line { display:flex; justify-content:space-between; flex-wrap:wrap; gap:8px 16px; align-items:baseline; font-size:13px; color:var(--muted); }\n.summary-line strong { font-size:23px; font-weight:500; color:var(--ink); }\n@media(max-width:480px) { .copy-stack { font-size:12px; } }\n.fixed-pair { display:flex; flex-wrap:wrap; gap:8px 20px; margin-top:20px; font-size:13px; color:var(--muted); }\n.direction-control { display:flex; padding:4px; gap:4px; background:var(--surface); border:1px solid var(--rule); border-radius:8px; margin:18px 0; }\n.direction-control button { flex:1; border-color:transparent; background:transparent; }\n.direction-control button[aria-pressed='true'] { background:var(--paper); border-color:var(--rule); color:var(--accent); }\n.direction-caption { display:flex; flex-wrap:wrap; justify-content:space-between; gap:5px 12px; font-size:11px; color:var(--muted); margin-bottom:16px; }\n.signed-chart { height:220px; position:relative; margin-left:32px; border-top:1px solid var(--rule); border-bottom:1px solid var(--rule); }\n.tick { position:absolute; left:-34px; width:28px; color:var(--muted); font-size:10px; text-align:right; }\n.tick.top { top:-7px; } .tick.bottom { bottom:-7px; }\n.zero { position:absolute; top:50%; inset-inline:0; border-top:1px solid var(--muted); }\n.zero i { position:absolute; left:-30px; width:24px; top:-9px; text-align:right; color:var(--muted); font-size:11px; font-style:normal; }\n.signed-area { position:absolute; box-shadow:inset 0 0 0 1px currentColor; background:var(--accent-soft); color:var(--accent); }\n.signed-area.b { background:var(--second-soft); color:var(--second); }\n.weight-labels { display:flex; justify-content:space-between; margin:12px 0 0 32px; font-size:12px; font-variant-numeric:tabular-nums; }\n.weight-labels span:first-child { color:var(--accent); } .weight-labels span+span { color:var(--second); }\n.term-equations { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); gap:12px; margin-top:20px; font-size:12px; font-variant-numeric:tabular-nums; }\n.term-equations > div { border-left:2px solid currentColor; padding-left:10px; color:var(--accent); }\n.term-equations > div+div { color:var(--second); }\n.term-equations span { display:block; min-height:3.4em; line-height:1.7; }\n","js":"function draw(direction){\n const forward=direction==='forward', weights=forward?[.5,.5]:[.9,.1], reference=forward?[.9,.1]:[.5,.5];let sum=0;\n for(let i=0;i<2;i++){\n const key=i?'b':'a', log=Math.log2(weights[i]/reference[i]), value=weights[i]*log;sum+=value;\n const rect=document.querySelector('#term-'+key);rect.style.width=weights[i]*100+'%';rect.style.left=(i?weights[0]*100:0)+'%';rect.style.height=Math.abs(log)/5*100+'%';rect.style.top=(log>0?50-log/5*100:50)+'%';\n document.querySelector('#weight-'+key).textContent=`${key.toUpperCase()}: ${viz.number(weights[i])}`;\n document.querySelector('#expression-'+key).textContent=`${viz.number(weights[i])} × (${log>0?'+':''}${viz.number(log)}) ≈ ${value>0?'+':''}${viz.number(value)}`;\n }\n document.querySelector('#weight-label').textContent=viz.t(forward?'weightP':'weightQ');\n document.querySelector('#direction-formula').textContent=forward?'KL(p ‖ q)':'KL(q ‖ p)';\n document.querySelector('#divergence').textContent=viz.number(sum);\n document.querySelector('#explanation').textContent=viz.t(direction);\n document.querySelectorAll('[data-direction]').forEach(b=>b.setAttribute('aria-pressed',String(b.dataset.direction===direction)));\n}\ndocument.querySelectorAll('[data-direction]').forEach(b=>b.addEventListener('click',()=>draw(b.dataset.direction)));draw('forward');","audio":false,"strings":{"chart":"符号付き面積図。幅は平均の重み、高さは対数比。ゼロより上は加算、下は減算です。","control":"KL の計算方向","eyebrow":"02 / 二つの異なる期待値","forward":"p で平均すると A、B は半分ずつです。A の負の項と B の正の項を足すと、約 0.74 bit になります。","heading":"同じ p と q でも、なぜ答えが違う?","height":"高さ:符号付き log₂ 比","intro":"向きだけを切り替えます。青は常に A、金色は B です。どちらが広がり、どちらがゼロ線の上へ移るかを見てください。","note":"ゼロ線の上を足し、下を引きます。KL 全体は非負でも、各項は負になり得ます。両状態は同じ目盛りです。固定分布から計算し、表示値を丸めています。","reverse":"q では A の重みが 9 割、B が 1 割になり、対数比も反転します。符号付き面積の和は約 0.53 bit に変わります。","weightP":"幅:p による重み","weightQ":"幅:q による重み"}}
結果は約 0.737 bit と 0.531 bit です。丸め誤差ではなく、二つの異なる期待値です。KL は距離に必要な対称性を満たさず、一般の三角不等式も満たしません。
台に関する条件にも向きがあります。p に質量があり q がゼロの領域では DKL(p∥q) が発散します。逆向きでは、q に質量があり p がゼロの領域を調べます。連続分布では和を積分に置き換え、孤立した一点ではなく、非ゼロの確率質量を持つ領域として考えます。
交差エントロピーが分類損失になる理由
目標 p を固定し、モデル qθ だけを調整すると、
H(p,qθ)=H(p)+DKL(p∥qθ)
が成り立ちます。H(p) はモデルのパラメータに依存しないため、交差エントロピーの最小化はこの向きの KL の最小化と等価です。
ラベル付き観測 (xi,yi) では、観測ラベルを one-hot の経験的な目標で表すのが一般的です。これは真の条件付き分布 p(y∣xi) が確定的だという意味ではありません。この目標の交差エントロピーは、
ℓi=−logqθ(yi∣xi)
です。与えられた標本に対して条件付き尤度の積を用いると、
i∑−logqθ(yi∣xi)=−logi∏qθ(yi∣xi)
となります。したがって経験交差エントロピーの最小化は、対応する条件付き尤度の最大化です。母集団での交差エントロピーは真のデータ分布による期待値で、訓練標本の平均はその経験的な推定です。
実装では自然対数を使うことが多く、その単位は nat です。本文では底 2 の対数を使い、単位を bit とします。正の定数倍の違いなので、最小化する解は同じです。自己回帰言語モデルの token 単位の負の対数尤度も同じ連鎖分解を使います。LLM における情報論を参照してください。
Forward / reverse KL:同じモデル族、異なる目標
p を固定して q を近似します。DKL(p∥q) を forward KL、DKL(q∥p) を reverse KL と呼びます。モデル族が p を表現できれば、どちらも q=p でゼロにできます。 違いが目立つのは、表現力が制限されている場合です。
まず静的な模式図で二つの典型的な形を見比べます。目標の二つの山が十分に離れ、近似が単一のガウス分布に限られる場合、forward は両側を覆うように広がり、reverse は片側に集中することがあります。曲線は形の説明であり、厳密な数値フィッティングではありません。
灰色の破線が目標 p、色付きの実線が近似 q です。両図の座標尺度は共通です。十分に離れた峰での典型的な形であり、すべての制約付きモデルに必ず生じる結果ではありません。
ここでは目標を単位分散の二つのガウス分布の等重み混合とし、近似を単一のガウス分布に限定します。
p(x)=21N(x;−d/2,1)+21N(x;d/2,1),q(x)=N(x;μ,σ2)
変えるのは成分の中心間隔 d だけです。成分が近いと混合分布は単峰になり得ます。各図は自分の目的関数に対して μ,σ を再調整し、同一の座標目盛りを使います。
図を準備しています
山を離して、制約されたモデルの選択を見る目標 p は単位分散の二つのガウス分布の等重み混合で、q は単一のガウスに限定されます。間隔を変えると、forward と reverse KL で再近似します。座標は同じ目盛りです。
{"id":"kl-mode-fitting","title":"山を離して、制約されたモデルの選択を見る","summary":"目標 p は単位分散の二つのガウス分布の等重み混合で、q は単一のガウスに限定されます。間隔を変えると、forward と reverse KL で再近似します。座標は同じ目盛りです。","height":900,"html":"<div class=\"eyebrow\" data-i18n=\"eyebrow\"></div><h2 data-i18n=\"heading\"></h2><p class=\"hint\" data-i18n=\"intro\"></p>\n<div class=\"control\"><label for=\"separation\" data-i18n=\"control\"></label><output id=\"separation-value\" for=\"separation\"></output></div><input type=\"range\" id=\"separation\" min=\"0\" max=\"80\" step=\"1\" value=\"60\"><div class=\"scale\"><span>0</span><span>4</span><span>8</span></div>\n<div class=\"legend\"><span><i class=\"target\"></i><span data-i18n=\"target\"></span></span><span><i></i><span data-i18n=\"approximation\"></span></span></div>\n<div class=\"fit-panels\">\n <section class=\"fit-panel\"><h3>Forward · KL(p ‖ q)</h3><svg viewBox=\"0 0 480 200\" preserveAspectRatio=\"none\" role=\"img\" data-i18n-label=\"forwardChart\"><path class=\"fit-fill\" id=\"forward-fill\"/><path class=\"target-curve\" id=\"target-forward\"/><path class=\"fit-curve\" id=\"forward-curve\"/></svg><div class=\"scale\"><span>−12</span><span>0</span><span>12</span></div><div class=\"fit-parameters\" id=\"forward-parameters\"></div><div class=\"fit-result\">KL ≈ <strong id=\"forward-kl\"></strong> bit</div></section>\n <section class=\"fit-panel reverse\"><h3>Reverse · KL(q ‖ p)</h3><svg viewBox=\"0 0 480 200\" preserveAspectRatio=\"none\" role=\"img\" data-i18n-label=\"reverseChart\"><path class=\"fit-fill\" id=\"reverse-fill\"/><path class=\"target-curve\" id=\"target-reverse\"/><path class=\"fit-curve\" id=\"reverse-curve\"/></svg><div class=\"scale\"><span>−12</span><span>0</span><span>12</span></div><div class=\"fit-parameters\" id=\"reverse-parameters\"></div><div class=\"fit-result\">KL ≈ <strong id=\"reverse-kl\"></strong> bit</div></section>\n</div>\n<div class=\"copy-stack\"><p id=\"explanation\"></p><p class=\"reserve\" data-i18n=\"overlap\" aria-hidden=\"true\"></p><p class=\"reserve\" data-i18n=\"separated\" aria-hidden=\"true\"></p></div><details class=\"audit-details\"><summary data-i18n=\"detailLabel\"></summary><p class=\"note\" data-i18n=\"note\"></p></details>","css":".control { display:flex; align-items:center; justify-content:space-between; gap:12px; margin-top:22px; }\n.control output { min-width:4.5em; flex-shrink:0; text-align:center; font-size:14px; font-weight:600; font-variant-numeric:tabular-nums; color:var(--accent); padding:3px 10px; background:var(--accent-soft); border-radius:6px; }\n.scale { display:flex; justify-content:space-between; font-size:10px; color:var(--muted); margin-top:5px; }\n.note { color:var(--muted); font-size:11px; line-height:1.7; margin-top:16px; }\n.copy-stack { display:grid; font-size:13px; line-height:1.75; margin-top:20px; }\n.copy-stack > * { grid-area:1/1; min-width:0; }\n.reserve { visibility:hidden; pointer-events:none; user-select:none; }\n.number { font-variant-numeric:tabular-nums; }\n.legend { display:flex; flex-wrap:wrap; gap:6px 16px; font-size:12px; color:var(--muted); margin:20px 0 12px; }\n.legend span { display:inline-flex; align-items:center; gap:6px; }\n.legend i { display:inline-block; width:16px; height:0; border-top:2px solid var(--accent); }\n.legend .target { border-color:var(--muted); border-top-style:dashed; }\n.summary { border-top:1px solid var(--rule); margin-top:22px; padding-top:16px; }\n.summary-line { display:flex; justify-content:space-between; flex-wrap:wrap; gap:8px 16px; align-items:baseline; font-size:13px; color:var(--muted); }\n.summary-line strong { font-size:23px; font-weight:500; color:var(--ink); }\n@media(max-width:480px) { .copy-stack { font-size:12px; } }\n.fit-panels { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); gap:16px; }\n.fit-panel { padding:14px; border:1px solid var(--rule); border-radius:8px; min-width:0; color:var(--accent); }\n.fit-panel.reverse { color:var(--second); }\n.fit-panel h3 { font-size:13px; margin-bottom:14px; }\n.fit-panel svg { height:180px; border-bottom:1px solid var(--rule); overflow:visible; }\n.target-curve { fill:none; stroke:var(--muted); stroke-width:1.5px; stroke-dasharray:5 4; vector-effect:non-scaling-stroke; }\n.fit-curve { fill:none; stroke:currentColor; stroke-width:2px; vector-effect:non-scaling-stroke; }\n.fit-fill { fill:currentColor; opacity:.09; stroke:none; }\n.fit-parameters { margin-top:14px; font-size:12px; font-variant-numeric:tabular-nums; color:var(--muted); }\n.fit-result { font-size:13px; margin-top:6px; font-variant-numeric:tabular-nums; }\n.fit-result strong { font-size:19px; font-weight:500; }\n@media(max-width:540px) { .fit-panels { grid-template-columns:1fr; gap:14px; } .fit-panel svg { height:154px; } }\n\n@media(max-width:540px){.fit-panel{padding:10px}.fit-panel h3{margin-bottom:6px}.fit-panel svg{height:112px}.fit-parameters{margin-top:8px}.fit-panels{gap:10px}.legend{margin:12px 0 8px}.copy-stack{margin-top:12px}.fit-result strong{font-size:17px}}\n\n.audit-details { border-top:1px solid var(--rule); margin-top:14px; padding-top:8px; }\n.audit-details summary { cursor:pointer; color:var(--accent); font-size:13px; min-height:36px; padding:6px 0; }\n.audit-details summary:focus-visible { outline:2px solid var(--accent); outline-offset:2px; }\n.audit-details .formula { border:0; margin-top:8px; padding:8px 0; }\n","js":"// Reproduce with scripts/build-kl-mode-fits.py: [separation, reverse mu, sigma, forward KL, reverse KL].\nconst fits=[[0.0,0.0,1.0,0.0,0],[0.1,0.0,1.0012492,0.0,0.0],[0.2,0.0,1.0049876,0.0,0.0],[0.3,0.0,1.0111873,0.0,0.0],[0.4,0.0,1.0198032,3e-07,3e-07],[0.5,0.0,1.0307727,1.5e-06,1.5e-06],[0.6,0.0,1.0440164,5.7e-06,5.8e-06],[0.7,0.0,1.059438,1.77e-05,1.8e-05],[0.8,0.0,1.0769242,4.63e-05,4.73e-05],[0.9,0.0,1.0963454,0.0001056,0.0001087],[1.0,0.0,1.1175572,0.0002168,0.0002252],[1.1,0.0,1.1404032,0.000409,0.0004285],[1.2,0.0,1.1647197,0.0007193,0.0007602],[1.3,0.0,1.1903399,0.0011929,0.0012714],[1.4,0.0,1.2170998,0.0018823,0.0020226],[1.5,0.0,1.2448426,0.002846,0.0030818],[1.6,0.0,1.2734227,0.0041473,0.0045233],[1.7,0.0,1.302709,0.0058523,0.0064257],[1.8,0.0,1.3325862,0.0080282,0.0088699],[1.9,0.0,1.362955,0.0107413,0.0119372],[2.0,0.0,1.3937323,0.0140558,0.0157079],[2.1,0.0,1.4248491,0.0180318,0.0202596],[2.2,0.0,1.45625,0.022724,0.0256666],[2.3,0.0,1.4878907,0.0281808,0.0319989],[2.4,0.0,1.5197363,0.0344436,0.0393218],[2.5,0.0,1.5517602,0.0415456,0.0476957],[2.6,0.0,1.583942,0.0495122,0.0571758],[2.7,0.0,1.6162665,0.0583602,0.0678126],[2.8,0.0,1.6487225,0.0680986,0.0796516],[2.9,0.0,1.681302,0.0787277,0.0927341],[3.0,0.0,1.7139994,0.0902409,0.1070967],[3.1,0.0,1.7468106,0.1026237,0.1227723],[3.2,0.0,1.7797332,0.1158555,0.1397901],[3.3,0.0,1.8127654,0.1299096,0.158176],[3.4,0.0,1.8459063,0.1447539,0.1779527],[3.5,0.0,1.8791551,0.1603519,0.1991403],[3.6,0.0,1.9125117,0.1766633,0.2217564],[3.7,0.0,1.9459756,0.1936446,0.2458162],[3.8,0.0,1.9795469,0.21125,0.2713331],[3.9,0.0,2.0132252,0.2294319,0.2983183],[4.0,0.0,2.0470104,0.2481418,0.3267819],[4.1,0.0,2.0809022,0.2673303,0.3567322],[4.2,0.0,2.1149001,0.2869485,0.3881762],[4.3,0.0,2.1490037,0.3069477,0.4211199],[4.4,0.0,2.1832125,0.3272804,0.4555682],[4.5,0.0,2.2175256,0.3479002,0.4915251],[4.6,0.0,2.2519424,0.3687623,0.5289937],[4.7,0.0,2.2864621,0.3898241,0.5679765],[4.8,0.0,2.3210837,0.4110448,0.6084753],[4.9,0.0,2.3558064,0.4323859,0.6504913],[5.0,0.0,2.3906291,0.4538115,0.6940253],[5.1,0.0,2.4255508,0.4752878,0.7390775],[5.2,0.0,2.4605704,0.4967838,0.7856479],[5.3,0.0,2.4956867,0.5182707,0.8337359],[5.4,0.0,2.5308987,0.5397224,0.8833408],[5.5,0.0,2.5662052,0.561115,0.9344615],[5.6,0.0,2.6016049,0.5824272,0.9870969],[5.7,2.8232538,1.0376737,0.6036395,0.9892609],[5.8,2.8776473,1.0320905,0.6247351,0.9910001],[5.9,2.9312827,1.0273731,0.6456988,0.9924587],[6.0,2.984306,1.0233686,0.6665173,0.9936836],[6.1,3.0368302,1.0199572,0.6871795,0.9947129],[6.2,3.0889439,1.0170438,0.7076754,0.9955781],[6.3,3.1407178,1.0145509,0.7279969,0.9963054],[6.4,3.192209,1.0124153,0.7481373,0.9969165],[6.5,3.2434638,1.0105845,0.7680908,0.9974298],[6.6,3.2945204,1.0090144,0.7878531,0.9978606],[6.7,3.3454103,1.0076681,0.8074209,0.9982218],[6.8,3.3961598,1.0065139,0.8267918,0.9985243],[6.9,3.4467909,1.0055253,0.8459641,0.9987774],[7.0,3.4973219,1.0046791,0.8649369,0.9989888],[7.1,3.5477684,1.0039557,0.88371,0.9991652],[7.2,3.5981434,1.003338,0.9022837,0.9993121],[7.3,3.648458,1.0028114,0.9206589,0.9994342],[7.4,3.6987216,1.0023631,0.9388367,0.9995356],[7.5,3.7489421,1.0019822,0.9568188,0.9996196],[7.6,3.7991262,1.0016592,0.974607,0.999689],[7.7,3.8492798,1.0013857,0.9922035,0.9997463],[7.8,3.8994076,1.0011547,1.0096105,0.9997935],[7.9,3.9495137,1.00096,1.0268306,0.9998322],[8.0,3.9996017,1.0007963,1.0438664,0.999864]];\nconst input=document.querySelector('#separation');\nconst normal=(x,mu,sigma)=>Math.exp(-.5*((x-mu)/sigma)**2)/(sigma*Math.sqrt(2*Math.PI));\nfunction curve(fn){return Array.from({length:481},(_,i)=>`${i?'L':'M'}${i},${200-200*fn(-12+i/20)/.45}`).join(' ');}\nfunction draw(){\n const [d,mu,sigma,fkl,rkl]=fits[Number(input.value)], a=d/2, forwardSigma=Math.sqrt(1+a*a);\n const target=curve(x=>(normal(x,-a,1)+normal(x,a,1))/2), f=curve(x=>normal(x,0,forwardSigma)), r=curve(x=>normal(x,mu,sigma));\n document.querySelector('#target-forward').setAttribute('d',target);document.querySelector('#target-reverse').setAttribute('d',target);\n document.querySelector('#forward-curve').setAttribute('d',f);document.querySelector('#reverse-curve').setAttribute('d',r);\n document.querySelector('#forward-fill').setAttribute('d',f+'L480 200H0Z');document.querySelector('#reverse-fill').setAttribute('d',r+'L480 200H0Z');\n document.querySelector('#separation-value').textContent=viz.number(d,1);\n document.querySelector('#forward-parameters').textContent=`μ = 0.00 · σ = ${viz.number(forwardSigma)}`;\n document.querySelector('#reverse-parameters').textContent=`μ = ${viz.number(mu)} · σ = ${viz.number(sigma)}`;\n document.querySelector('#forward-kl').textContent=viz.number(fkl);document.querySelector('#reverse-kl').textContent=viz.number(rkl);\n document.querySelector('#explanation').textContent=viz.t(mu>.5?'separated':'overlap');\n}\ninput.addEventListener('input',draw);draw();","audio":false,"strings":{"approximation":"実線:近似 q","control":"成分の中心間隔 d","detailLabel":"近似の方法と条件","eyebrow":"03 / モデル制約と近似の向き","forwardChart":"間隔を変えたときの双峰目標と forward KL による単一ガウス近似。","heading":"一つのガウスで、二つの山にどう近づく?","intro":"変えるのは山の間隔だけです。灰色の破線が目標 p、実線がそれぞれの目的で近似したガウス q です。","note":"両図とも横軸 x は −12〜12、縦軸の密度は 0〜0.45 です。Forward はモーメント一致、reverse は複数初期値からの数値最適化で対称解の片側を表示します。二つの KL 値は共通の評価尺度ではありません。","overlap":"成分が近いと目標が単峰になり、どちらの向きも中央で近似できます。Reverse KL が常に片側を選ぶわけではなく、間隔とモデルの制約に依存します。","reverseChart":"間隔を変えたときの双峰目標と reverse KL による単一ガウス近似。","separated":"十分に離れると、forward は両側を捉えるために広がります。Reverse KL の数値的な最適解の一つは片側に寄り、中央の低密度領域の質量を減らします。","target":"破線:目標 p"}}
Forward KL の最適なガウス近似は p の平均と分散に一致し、μ=0、σ2=1+d2/4 です。山が離れるほど広がります。Reverse KL は q の下で平均するため、十分に離れた場合、一方に寄ることで中央の低密度領域に置く質量を減らせます。
これは条件付きの近似傾向であり、「forward は必ず覆い、reverse は必ず山を選ぶ」という普遍的な定理ではありません。間隔、混合比、モデル族、最適化結果が影響します。図の reverse 解は複数初期値からの数値最適化によるものです。対称な左側の解も同じ値であり、右側を表示することは優劣を意味しません。Bishop の PRML 第 10.1 節も参照してください。
変分推論では近似事後分布から真の事後分布への KL を最小化することがよくあります。一方、RLHF の DKL(πθ∥πref) は、参照モデルからの方策の逸脱を抑える正則化項で、報酬などと組み合わせます。ここでの二つの山の近似に単純に置き換えることはできません。
Jensen–Shannon 発散:共通の基準を使う
m=(p+q)/2 とし、
JS(p,q)=21DKL(p∥m)+21DKL(q∥m)
と定義します。どちらかに質量がある場所では m も非ゼロなので、台が重ならない分布同士を直接 KL で比較したときの無限大を避けられます。底 2 では 0≤JS≤1 bit です。対称ですが、距離となるのは JS であり、JS そのものではないことに注意してください。
例えば p=(1,0)、q=(0,1) なら、両方向の KL は無限大です。一方 m=(0.5,0.5) への KL は両側とも 1 bit なので、JS は 1 bit です。
元の GAN との関係にも条件があります。固定した生成器に対して判別器が最適になったとき、元の minimax 目的に代入すると、生成器側の値は −ln4+2JSln(pdata,pg) になります。これは自然対数の JS であり、「判別器が JS を最小化する」という意味ではありません。よく使われる非飽和型の生成器損失も、この式そのものではありません。Goodfellow らの原論文、第 3、4.1 節を参照してください。
対照と参考文献
| 量 | 平均の重み | 意味 |
| H(p) | p | 情報源の平均自己情報量 |
| H(p,q) | p | モデル q が割り当てる理想情報長の平均 |
| DKL(p∥q) | p | H(p) に対する平均追加コスト |
| DKL(q∥p) | q | 情報源と基準を交換した別の期待値 |
| JS(p,q) | 両側を半分ずつ | 共通の混合分布への平均 KL |