第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 = . 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:
英語のヒント
ソフトマックス関数はベクトルを与え、これは任意の入力が与えられたときの各クラスの(推定された)条件付き確率、たとえば = と解釈できる。以下では、特徴量をもつデータセットについて、ラベルがワンホット符号化のラベルベクトルで表されていると仮定する。特徴量が与えられたとき、実際のクラスがモデルによればどれほど確からしいかを調べることで、推定値を現実と比べられる。
(4.1.6)
We are allowed to use the factorization since we assume that each label is drawn independently from its respective distribution . Since maximizing the product of terms is awkward, we take the negative logarithm to obtain the equivalent problem of minimizing the negative log-likelihood:
英語のヒント
この因数分解を使ってよいのは、各ラベルがそれぞれの分布から独立に取り出されると仮定しているからである。項の積を最大にするのは扱いにくいので、負の対数をとって、負の対数尤度を最小にするという同等の問題を得る。
(4.1.7)
where for any pair of label and model prediction over classes, the loss function is
英語のヒント
ここで、個のクラスにわたる任意のラベルとモデルの予測の組について、損失関数は次のとおりである。
(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 , the sum over all its coordinates vanishes for all but one term. Note that the loss is bounded from below by whenever is a probability vector: no single entry is larger than , hence their negative logarithm cannot be lower than ; 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 requires taking the corresponding input to infinity (or all other outputs for to negative infinity). Even if our model could assign an output probability of , any error made when assigning such high confidence would incur infinite loss ().
英語のヒント
後で説明する理由から、(4.1.8)の損失関数は一般に交差エントロピー損失と呼ばれる。は長さのワンホットベクトルなので、そのすべての座標にわたる和は、一つの項を除いてすべて消える。が確率ベクトルであるかぎり、損失は下からで抑えられることに注意してほしい。どの成分もより大きくないので、その負の対数はより小さくなりえない。となるのは、実際のラベルを確実に予測するときだけである。これは、重みのどんな有限の設定でも決して起こらない。ソフトマックスの出力をに近づけるには、対応する入力を無限大に(あるいはのほかの出力をすべて負の無限大に)しなければならないからである。たとえモデルが出力確率を割り当てられたとしても、そのように高い確信を割り当てたときに誤れば、無限大の損失()をこうむることになる。
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.9)
To understand a bit better what is going on, consider the derivative with respect to any logit . We get
英語のヒント
何が起きているかをもう少しよく理解するために、任意のロジットについての導関数を考えよう。次が得られる。
(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 and estimate . 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.
英語のヒント
言い換えれば、導関数は、ソフトマックスの演算で表される、モデルが割り当てた確率と、ワンホットのラベルベクトルの要素で表される、実際に起きたこととの差である。この意味で、これは回帰で見たもの、つまり勾配が観測と推定値の差であったことにとてもよく似ている。これは偶然ではない。どんな指数型分布族のモデルでも、対数尤度の勾配はまさにこの項で与えられる。この事実のおかげで、実際に勾配を計算するのは簡単になる。
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 , we now have a generic probability vector, say . The math that we used previously to define the loss 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).
英語のヒント
今度は、一つの結果だけでなく、結果にわたる分布全体を観測する場合を考えよう。ラベルには前と同じ表現を使える。唯一の違いは、たとえばのように二値の成分だけを含むベクトルではなく、たとえばのような一般の確率ベクトルになることである。(4.1.8)で損失を定義するのに使った数学はそのまま通用し、解釈が少し一般的になるだけである。これは、ラベルにわたる分布についての損失の期待値である。この損失を交差エントロピー損失といい、分類問題で最もよく使われる損失の一つである。情報理論のほんの基礎を紹介すれば、この名前の謎は解ける。一言で言えば、これは、起きるべきだと予測したものに対して、見たものを符号化するのに必要なビット数を測る。以下でごく基本的な説明をする。情報理論の詳細は、Cover and Thomas (1999)やMacKay (2003)を見てほしい。