プリンキピア

第4章分類のための線形ニューラルネットワーク

4.1.2. Loss Function4.1.2 損失関数

Now that we have a mapping from features 𝐱 to probabilities 𝐲ˆ, we need a way to optimize the accuracy of this mapping. We will rely on maximum likelihood estimation, the very same method that we encountered when providing a probabilistic justification for the mean squared error loss in Section 3.1.3.
英語のヒント
特徴量𝐱から確率𝐲ˆへの写像が手に入ったので、この写像の正確さを最適化する方法が必要である。ここでは最尤推定に頼る。これは、3.1.3節で平均二乗誤差損失の確率的な正当化を示したときに出会ったのと、まさに同じ方法である。

4.1.2.1. Log-Likelihood4.1.2.1 対数尤度

The softmax function gives us a vector 𝐲ˆ, which we can interpret as the (estimated) conditional probabilities of each class, given any input 𝐱, such as yˆ1 = P(y=cat∣𝐱). In the following we assume that for a dataset with features 𝐗 the labels 𝐘 are represented using a one-hot encoding label vector. We can compare the estimates with reality by checking how probable the actual classes are according to our model, given the features:
英語のヒント
ソフトマックス関数はベクトル𝐲ˆを与え、これは任意の入力𝐱が与えられたときの各クラスの(推定された)条件付き確率、たとえばyˆ1 = P(y=cat∣𝐱)と解釈できる。以下では、特徴量𝐗をもつデータセットについて、ラベル𝐘がワンホット符号化のラベルベクトルで表されていると仮定する。特徴量が与えられたとき、実際のクラスがモデルによればどれほど確からしいかを調べることで、推定値を現実と比べられる。
P(𝐘∣𝐗)=∏i=1nP(𝐲(i)∣𝐱(i)).(4.1.6)
We are allowed to use the factorization since we assume that each label is drawn independently from its respective distribution P(𝐲∣𝐱(i)). Since maximizing the product of terms is awkward, we take the negative logarithm to obtain the equivalent problem of minimizing the negative log-likelihood:
英語のヒント
この因数分解を使ってよいのは、各ラベルがそれぞれの分布P(𝐲∣𝐱(i))から独立に取り出されると仮定しているからである。項の積を最大にするのは扱いにくいので、負の対数をとって、負の対数尤度を最小にするという同等の問題を得る。
−log⁡P(𝐘∣𝐗)=∑i=1n−log⁡P(𝐲(i)∣𝐱(i))=∑i=1nl(𝐲(i),𝐲ˆ(i)),(4.1.7)
where for any pair of label 𝐲 and model prediction 𝐲ˆ over q classes, the loss function l is
英語のヒント
ここで、q個のクラスにわたる任意のラベル𝐲とモデルの予測𝐲ˆの組について、損失関数lは次のとおりである。
l(𝐲,𝐲ˆ)=−∑j=1qyjlog⁡yˆj.(4.1.8)
For reasons explained later on, the loss function in (4.1.8) is commonly called the cross-entropy loss. Since 𝐲 is a one-hot vector of length q, the sum over all its coordinates j vanishes for all but one term. Note that the loss l(𝐲,𝐲ˆ) is bounded from below by 0 whenever 𝐲ˆ is a probability vector: no single entry is larger than 1, hence their negative logarithm cannot be lower than 0; l(𝐲,𝐲ˆ)=0 only if we predict the actual label with certainty. This can never happen for any finite setting of the weights because taking a softmax output towards 1 requires taking the corresponding input oi to infinity (or all other outputs oj for j≠i to negative infinity). Even if our model could assign an output probability of 0, any error made when assigning such high confidence would incur infinite loss (−log⁡0=∞).
英語のヒント
後で説明する理由から、(4.1.8)の損失関数は一般に交差エントロピー損失と呼ばれる。𝐲は長さqのワンホットベクトルなので、そのすべての座標jにわたる和は、一つの項を除いてすべて消える。𝐲ˆが確率ベクトルであるかぎり、損失l(𝐲,𝐲ˆ)は下から0で抑えられることに注意してほしい。どの成分も1より大きくないので、その負の対数は0より小さくなりえない。l(𝐲,𝐲ˆ)=0となるのは、実際のラベルを確実に予測するときだけである。これは、重みのどんな有限の設定でも決して起こらない。ソフトマックスの出力を1に近づけるには、対応する入力oiを無限大に(あるいはj≠iのほかの出力ojをすべて負の無限大に)しなければならないからである。たとえモデルが出力確率0を割り当てられたとしても、そのように高い確信を割り当てたときに誤れば、無限大の損失(−log⁡0=∞)をこうむることになる。

4.1.2.2. Softmax and Cross-Entropy Loss4.1.2.2 ソフトマックスと交差エントロピー損失

Since the softmax function and the corresponding cross-entropy loss are so common, it is worth understanding a bit better how they are computed. Plugging (4.1.3) into the definition of the loss in (4.1.8) and using the definition of the softmax we obtain
英語のヒント
ソフトマックス関数と、それに対応する交差エントロピー損失はとてもよく使われるので、それがどう計算されるかをもう少しよく理解しておく価値がある。(4.1.3)を(4.1.8)の損失の定義に代入し、ソフトマックスの定義を使うと、次が得られる。
l(𝐲,𝐲ˆ)=−∑j=1qyjlog⁡exp⁡(oj)∑k=1qexp⁡(ok)=∑j=1qyjlog⁡∑k=1qexp⁡(ok)−∑j=1qyjoj=log⁡∑k=1qexp⁡(ok)−∑j=1qyjoj.(4.1.9)
To understand a bit better what is going on, consider the derivative with respect to any logit oj. We get
英語のヒント
何が起きているかをもう少しよく理解するために、任意のロジットojについての導関数を考えよう。次が得られる。
∂ojl(𝐲,𝐲ˆ)=exp⁡(oj)∑k=1qexp⁡(ok)−yj=softmax(𝐨)j−yj.(4.1.10)
In other words, the derivative is the difference between the probability assigned by our model, as expressed by the softmax operation, and what actually happened, as expressed by elements in the one-hot label vector. In this sense, it is very similar to what we saw in regression, where the gradient was the difference between the observation y and estimate yˆ. This is not a coincidence. In any exponential family model, the gradients of the log-likelihood are given by precisely this term. This fact makes computing gradients easy in practice.
英語のヒント
言い換えれば、導関数は、ソフトマックスの演算で表される、モデルが割り当てた確率と、ワンホットのラベルベクトルの要素で表される、実際に起きたこととの差である。この意味で、これは回帰で見たもの、つまり勾配が観測yと推定値yˆの差であったことにとてもよく似ている。これは偶然ではない。どんな指数型分布族のモデルでも、対数尤度の勾配はまさにこの項で与えられる。この事実のおかげで、実際に勾配を計算するのは簡単になる。
Now consider the case where we observe not just a single outcome but an entire distribution over outcomes. We can use the same representation as before for the label 𝐲. The only difference is that rather than a vector containing only binary entries, say (0,0,1), we now have a generic probability vector, say (0.1,0.2,0.7). The math that we used previously to define the loss l in (4.1.8) still works well, just that the interpretation is slightly more general. It is the expected value of the loss for a distribution over labels. This loss is called the cross-entropy loss and it is one of the most commonly used losses for classification problems. We can demystify the name by introducing just the basics of information theory. In a nutshell, it measures the number of bits needed to encode what we see, 𝐲, relative to what we predict that should happen, 𝐲ˆ. We provide a very basic explanation in the following. For further details on information theory see Cover and Thomas (1999) or MacKay (2003).
英語のヒント
今度は、一つの結果だけでなく、結果にわたる分布全体を観測する場合を考えよう。ラベル𝐲には前と同じ表現を使える。唯一の違いは、たとえば(0,0,1)のように二値の成分だけを含むベクトルではなく、たとえば(0.1,0.2,0.7)のような一般の確率ベクトルになることである。(4.1.8)で損失lを定義するのに使った数学はそのまま通用し、解釈が少し一般的になるだけである。これは、ラベルにわたる分布についての損失の期待値である。この損失を交差エントロピー損失といい、分類問題で最もよく使われる損失の一つである。情報理論のほんの基礎を紹介すれば、この名前の謎は解ける。一言で言えば、これは、起きるべきだと予測したもの𝐲ˆに対して、見たもの𝐲を符号化するのに必要なビット数を測る。以下でごく基本的な説明をする。情報理論の詳細は、Cover and Thomas (1999)やMacKay (2003)を見てほしい。