プリンキピア

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

22.11.5. Cross-Entropy22.11.5 交差エントロピー

If you are curious about applications of information theory in deep learning, here is a quick example. We define the true distribution P with probability distribution p(x), and the estimated distribution Q with probability distribution q(x), and we will use them in the rest of this section.
英語のヒント
深層学習における情報理論の応用に興味があるなら、ここに手短な例がある。確率分布p(x)をもつ真の分布Pと、確率分布q(x)をもつ推定された分布Qを定義し、この節の残りでこれらを使う。
Say we need to solve a binary classification problem based on given n data examples {x1,…,xn}. Assume that we encode 1 and 0 as the positive and negative class label yi respectively, and our neural network is parametrized by θ. If we aim to find a best θ so that yˆi=pθ(yi∣xi), it is natural to apply the maximum log-likelihood approach as was seen in Section 22.7. To be specific, for true labels yi and predictions yˆi=pθ(yi∣xi), the probability to be classified as positive is πi=pθ(yi=1∣xi). Hence, the log-likelihood function would be
英語のヒント
与えられたn個のデータの例{x1,…,xn}にもとづいて、二値分類の問題を解く必要があるとしよう。1と0をそれぞれ正と負のクラスラベルyiとして符号化し、ニューラルネットワークはθでパラメータ化されているとする。yˆi=pθ(yi∣xi)となるような最良のθを見つけることを目指すなら、22.7節で見たように最大対数尤度のやり方を適用するのが自然である。具体的には、真のラベルyiと予測yˆi=pθ(yi∣xi)について、正に分類される確率はπi=pθ(yi=1∣xi)である。したがって、対数尤度関数は次のようになる。
l(θ)=log⁡L(θ)=log⁡∏i=1nπiyi(1−πi)1−yi=∑i=1nyilog⁡(πi)+(1−yi)log⁡(1−πi).(22.11.24)
Maximizing the log-likelihood function l(θ) is identical to minimizing −l(θ), and hence we can find the best θ from here. To generalize the above loss to any distributions, we also called −l(θ) the cross-entropy loss CE(y,yˆ), where y follows the true distribution P and yˆ follows the estimated distribution Q.
英語のヒント
対数尤度関数l(θ)を最大化することは−l(θ)を最小化することと同じなので、ここから最良のθを見つけられる。上の損失を任意の分布に一般化するため、−l(θ)を交差エントロピー損失CE(y,yˆ)とも呼ぶ。ここでyは真の分布Pに従い、yˆは推定された分布Qに従う。
This was all derived by working from the maximum likelihood point of view. However, if we look closely we can see that terms like log⁡(πi) have entered into our computation which is a solid indication that we can understand the expression from an information theoretic point of view.
英語のヒント
これはすべて、最尤の観点から作業して導いたものである。しかし、よく見ると、log⁡(πi)のような項が計算に入り込んでいることがわかる。これは、この式を情報理論の観点から理解できることのしっかりしたしるしである。

22.11.5.1. Formal Definition22.11.5.1 形式的な定義

Like KL divergence, for a random variable X, we can also measure the divergence between the estimating distribution Q and the true distribution P via cross-entropy,
英語のヒント
KLダイバージェンスと同じように、確率変数Xについて、推定する分布Qと真の分布Pの間の隔たりを、交差エントロピーによって測ることもできる。
CE(P,Q)=−Ex∼P[log⁡(q(x))].(22.11.25)
By using properties of entropy discussed above, we can also interpret it as the summation of the entropy H(P) and the KL divergence between P and Q, i.e.,
英語のヒント
上で論じたエントロピーの性質を使えば、これをエントロピーH(P)と、PとQの間のKLダイバージェンスの和として解釈することもできる。つまり、
CE(P,Q)=H(P)+DKL(P∥Q).(22.11.26)
We can implement the cross-entropy loss as below.
英語のヒント
交差エントロピー損失は以下のように実装できる。
def cross_entropy(y_hat, y):
    ce = -torch.log(y_hat[range(len(y_hat)), y])
    return ce.mean()
Now define two tensors for the labels and predictions, and calculate the cross-entropy loss of them.
英語のヒント
では、ラベルと予測のための二つのテンソルを定義し、それらの交差エントロピー損失を計算しよう。
labels = torch.tensor([0, 2])
preds = torch.tensor([[0.3, 0.6, 0.1], [0.2, 0.3, 0.5]])

cross_entropy(preds, labels)
tensor(0.9486)

22.11.5.2. Properties22.11.5.2 性質

As alluded in the beginning of this section, cross-entropy (22.11.25) can be used to define a loss function in the optimization problem. It turns out that the following are equivalent:
英語のヒント
この節の初めでほのめかしたように、交差エントロピー(式(22.11.25))は、最適化問題で損失関数を定義するのに使える。次のものが同値であることがわかる。
  1. Maximizing predictive probability of Q for distribution P, (i.e., Ex∼P[log⁡(q(x))]);
    英語のヒント
    分布Pに対するQの予測確率を最大化すること(つまりEx∼P[log⁡(q(x))])。
  2. Minimizing cross-entropy CE(P,Q);
    英語のヒント
    交差エントロピーCE(P,Q)を最小化すること。
  3. Minimizing the KL divergence DKL(P∥Q).
    英語のヒント
    KLダイバージェンスDKL(P∥Q)を最小化すること。
The definition of cross-entropy indirectly proves the equivalent relationship between objective 2 and objective 3, as long as the entropy of true data H(P) is constant.
英語のヒント
真のデータのエントロピーH(P)が一定であるかぎり、交差エントロピーの定義は、目的2と目的3の間の同値関係を間接的に証明している。

22.11.5.3. Cross-Entropy as An Objective Function of Multi-class Classification22.11.5.3 多クラス分類の目的関数としての交差エントロピー

If we dive deep into the classification objective function with cross-entropy loss CE, we will find minimizing CE is equivalent to maximizing the log-likelihood function L.
英語のヒント
交差エントロピー損失CEを使った分類の目的関数を深く調べると、CEを最小化することは、対数尤度関数Lを最大化することと同値であることがわかる。
To begin with, suppose that we are given a dataset with n examples, and it can be classified into k-classes. For each data example i, we represent any k-class label 𝐲i=(yi1,…,yik) by one-hot encoding. To be specific, if the example i belongs to class j, then we set the j-th entry to 1, and all other components to 0, i.e.,
英語のヒント
まず、n個の例をもつデータセットが与えられ、それがkクラスに分類できるとする。各データの例iについて、任意のkクラスのラベル𝐲i=(yi1,…,yik)をワンホット符号化で表す。具体的には、例iがクラスjに属するなら、j番目の成分を1に、ほかのすべての成分を0に設定する。つまり、
yij={1j∈J;0otherwise.(22.11.27)
For instance, if a multi-class classification problem contains three classes A, B, and C, then the labels 𝐲i can be encoded in {A:(1,0,0);B:(0,1,0);C:(0,0,1)}.
英語のヒント
たとえば、多クラス分類の問題が三つのクラスA、B、Cを含むなら、ラベル𝐲iは{A:(1,0,0);B:(0,1,0);C:(0,0,1)}のように符号化できる。
Assume that our neural network is parametrized by θ. For true label vectors 𝐲i and predictions
英語のヒント
ニューラルネットワークがθでパラメータ化されているとする。真のラベルベクトル𝐲iと予測について、
𝐲ˆi=pθ(𝐲i∣𝐱i)=∑j=1kyijpθ(yij∣𝐱i).(22.11.28)
Hence, the cross-entropy loss would be
英語のヒント
したがって、交差エントロピー損失は次のようになる。
CE(𝐲,𝐲ˆ)=−∑i=1n𝐲ilog⁡𝐲ˆi=−∑i=1n∑j=1kyijlog⁡pθ(yij∣𝐱i).(22.11.29)
On the other side, we can also approach the problem through maximum likelihood estimation. To begin with, let’s quickly introduce a k-class multinoulli distribution. It is an extension of the Bernoulli distribution from binary class to multi-class. If a random variable 𝐳=(z1,…,zk) follows a k-class multinoulli distribution with probabilities 𝐩= (p1,…,pk), i.e.,
英語のヒント
他方で、最尤推定によってこの問題に取り組むこともできる。まず、kクラスのマルチヌーイ分布を手短に紹介しよう。これは、ベルヌーイ分布を二値クラスから多クラスへ拡張したものである。確率変数𝐳=(z1,…,zk)が確率𝐩=(p1,…,pk)をもつkクラスのマルチヌーイ分布に従うなら、つまり、
p(𝐳)=p(z1,…,zk)=Multi(p1,…,pk), where ∑i=1kpi=1,(22.11.30)
then the joint probability mass function(p.m.f.) of 𝐳 is
英語のヒント
すると、𝐳の同時確率質量関数(p.m.f.)は次のとおりである。
𝐩𝐳=∏j=1kpjzj.(22.11.31)
It can be seen that the label of each data example, 𝐲i, is following a k-class multinoulli distribution with probabilities 𝝅= (π1,…,πk). Therefore, the joint p.m.f. of each data example 𝐲i is π𝐲i=∏j=1kπjyij. Hence, the log-likelihood function would be
英語のヒント
各データの例のラベル𝐲iは、確率𝝅=(π1,…,πk)をもつkクラスのマルチヌーイ分布に従っていることがわかる。したがって、各データの例𝐲iの同時p.m.f.はπ𝐲i=∏j=1kπjyijである。したがって、対数尤度関数は次のようになる。
l(θ)=log⁡L(θ)=log⁡∏i=1n𝝅𝐲i=log⁡∏i=1n∏j=1kπjyij=∑i=1n∑j=1kyijlog⁡πj.(22.11.32)
Since in maximum likelihood estimation, we maximizing the objective function l(θ) by having πj=pθ(yij∣𝐱i). Therefore, for any multi-class classification, maximizing the above log-likelihood function l(θ) is equivalent to minimizing the CE loss CE(y,yˆ).
英語のヒント
最尤推定では、πj=pθ(yij∣𝐱i)とすることで目的関数l(θ)を最大化する。したがって、任意の多クラス分類について、上の対数尤度関数l(θ)を最大化することは、CE損失CE(y,yˆ)を最小化することと同値である。
To test the above proof, let’s apply the built-in measure NegativeLogLikelihood. Using the same labels and preds as in the earlier example, we will get the same numerical loss as the previous example up to the 5 decimal place.
英語のヒント
上の証明を検証するため、組み込みの尺度NegativeLogLikelihoodを適用しよう。前の例と同じlabelsとpredsを使うと、前の例と小数点以下5桁まで同じ数値の損失が得られる。
# Implementation of cross-entropy loss in PyTorch combines `nn.LogSoftmax()`
# and `nn.NLLLoss()`
nll_loss = NLLLoss()
loss = nll_loss(torch.log(preds), labels)
loss
tensor(0.9486)