第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 with probability distribution , and the estimated distribution with probability distribution , and we will use them in the rest of this section.
英語のヒント
深層学習における情報理論の応用に興味があるなら、ここに手短な例がある。確率分布をもつ真の分布と、確率分布をもつ推定された分布を定義し、この節の残りでこれらを使う。
Say we need to solve a binary classification problem based on given data examples {}. Assume that we encode and as the positive and negative class label respectively, and our neural network is parametrized by . If we aim to find a best so that , it is natural to apply the maximum log-likelihood approach as was seen in Section 22.7. To be specific, for true labels and predictions , the probability to be classified as positive is . Hence, the log-likelihood function would be
英語のヒント
与えられた個のデータの例{}にもとづいて、二値分類の問題を解く必要があるとしよう。とをそれぞれ正と負のクラスラベルとして符号化し、ニューラルネットワークはでパラメータ化されているとする。となるような最良のを見つけることを目指すなら、22.7節で見たように最大対数尤度のやり方を適用するのが自然である。具体的には、真のラベルと予測について、正に分類される確率はである。したがって、対数尤度関数は次のようになる。
(22.11.24)
Maximizing the log-likelihood function is identical to minimizing , and hence we can find the best from here. To generalize the above loss to any distributions, we also called the cross-entropy loss , where follows the true distribution and follows the estimated distribution .
英語のヒント
対数尤度関数を最大化することはを最小化することと同じなので、ここから最良のを見つけられる。上の損失を任意の分布に一般化するため、を交差エントロピー損失とも呼ぶ。ここでは真の分布に従い、は推定された分布に従う。
This was all derived by working from the maximum likelihood point of view. However, if we look closely we can see that terms like have entered into our computation which is a solid indication that we can understand the expression from an information theoretic point of view.
英語のヒント
これはすべて、最尤の観点から作業して導いたものである。しかし、よく見ると、のような項が計算に入り込んでいることがわかる。これは、この式を情報理論の観点から理解できることのしっかりしたしるしである。
22.11.5.1. Formal Definition22.11.5.1 形式的な定義
Like KL divergence, for a random variable , we can also measure the divergence between the estimating distribution and the true distribution via cross-entropy,
英語のヒント
KLダイバージェンスと同じように、確率変数について、推定する分布と真の分布の間の隔たりを、交差エントロピーによって測ることもできる。
(22.11.25)
By using properties of entropy discussed above, we can also interpret it as the summation of the entropy and the KL divergence between and , i.e.,
英語のヒント
上で論じたエントロピーの性質を使えば、これをエントロピーと、との間のKLダイバージェンスの和として解釈することもできる。つまり、
(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))は、最適化問題で損失関数を定義するのに使える。次のものが同値であることがわかる。
- Maximizing predictive probability of for distribution , (i.e., );
英語のヒント
分布に対するの予測確率を最大化すること(つまり)。 - Minimizing cross-entropy ;
英語のヒント
交差エントロピーを最小化すること。 - Minimizing the KL divergence .
英語のヒント
KLダイバージェンスを最小化すること。
The definition of cross-entropy indirectly proves the equivalent relationship between objective 2 and objective 3, as long as the entropy of true data is constant.
英語のヒント
真のデータのエントロピーが一定であるかぎり、交差エントロピーの定義は、目的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 , we will find minimizing is equivalent to maximizing the log-likelihood function .
英語のヒント
交差エントロピー損失を使った分類の目的関数を深く調べると、を最小化することは、対数尤度関数を最大化することと同値であることがわかる。
To begin with, suppose that we are given a dataset with examples, and it can be classified into -classes. For each data example , we represent any -class label by one-hot encoding. To be specific, if the example belongs to class , then we set the -th entry to , and all other components to , i.e.,
英語のヒント
まず、個の例をもつデータセットが与えられ、それがクラスに分類できるとする。各データの例について、任意のクラスのラベルをワンホット符号化で表す。具体的には、例がクラスに属するなら、番目の成分をに、ほかのすべての成分をに設定する。つまり、
(22.11.27)
For instance, if a multi-class classification problem contains three classes , , and , then the labels can be encoded in {}.
英語のヒント
たとえば、多クラス分類の問題が三つのクラス、、を含むなら、ラベルは{}のように符号化できる。
Assume that our neural network is parametrized by . For true label vectors and predictions
英語のヒント
ニューラルネットワークがでパラメータ化されているとする。真のラベルベクトルと予測について、
(22.11.28)
Hence, the cross-entropy loss would be
英語のヒント
したがって、交差エントロピー損失は次のようになる。
(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 -class multinoulli distribution. It is an extension of the Bernoulli distribution from binary class to multi-class. If a random variable follows a -class multinoulli distribution with probabilities (), i.e.,
英語のヒント
他方で、最尤推定によってこの問題に取り組むこともできる。まず、クラスのマルチヌーイ分布を手短に紹介しよう。これは、ベルヌーイ分布を二値クラスから多クラスへ拡張したものである。確率変数が確率()をもつクラスのマルチヌーイ分布に従うなら、つまり、
(22.11.30)
then the joint probability mass function(p.m.f.) of is
英語のヒント
すると、の同時確率質量関数(p.m.f.)は次のとおりである。
(22.11.31)
It can be seen that the label of each data example, , is following a -class multinoulli distribution with probabilities (). Therefore, the joint p.m.f. of each data example is Hence, the log-likelihood function would be
英語のヒント
各データの例のラベルは、確率()をもつクラスのマルチヌーイ分布に従っていることがわかる。したがって、各データの例の同時p.m.f.はである。したがって、対数尤度関数は次のようになる。
(22.11.32)
Since in maximum likelihood estimation, we maximizing the objective function by having . Therefore, for any multi-class classification, maximizing the above log-likelihood function is equivalent to minimizing the CE loss .
英語のヒント
最尤推定では、とすることで目的関数を最大化する。したがって、任意の多クラス分類について、上の対数尤度関数を最大化することは、CE損失を最小化することと同値である。
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)
losstensor(0.9486)