第22章付録:深層学習のための数学
22.7.2. Numerical Optimization and the Negative Log-Likelihood22.7.2 数値最適化と負の対数尤度
The previous example is nice, but what if we have billions of parameters and data examples?
英語のヒント
前の例はよいが、何十億ものパラメータとデータの例がある場合はどうだろうか。
First, notice that if we make the assumption that all the data examples are independent, we can no longer practically consider the likelihood itself as it is a product of many probabilities. Indeed, each probability is in , say typically of value about , and the product of is far below machine precision. We cannot work with that directly.
英語のヒント
まず、すべてのデータの例が独立だと仮定すると、尤度そのものは多くの確率の積なので、実際上もはや考えられなくなることに注意しよう。実際、各確率はの中にあり、たとえば典型的には程度の値だとすると、の積は機械精度をはるかに下回る。それを直接扱うことはできない。
However, recall that the logarithm turns products to sums, in which case
英語のヒント
しかし、対数は積を和に変えることを思い出そう。その場合、
(22.7.7)
This number fits perfectly within even a single precision -bit float. Thus, we should consider the log-likelihood, which is
英語のヒント
この数は、単精度のビット浮動小数点数にさえ完全に収まる。したがって、対数尤度を考えるべきである。それは次のとおりである。
(22.7.8)
Since the function is increasing, maximizing the likelihood is the same thing as maximizing the log-likelihood. Indeed in Section 22.9 we will see this reasoning applied when working with the specific example of the naive Bayes classifier.
英語のヒント
関数は増加関数なので、尤度を最大化することは対数尤度を最大化することと同じである。実際、22.9節では、ナイーブベイズ分類器の具体例を扱うときにこの推論が適用されるのを見る。
We often work with loss functions, where we wish to minimize the loss. We may turn maximum likelihood into the minimization of a loss by taking , which is the negative log-likelihood.
英語のヒント
損失を最小化したい損失関数を扱うことが多い。をとることで、最尤を損失の最小化に変えられる。これが負の対数尤度である。
To illustrate this, consider the coin flipping problem from before, and pretend that we do not know the closed form solution. We may compute that
英語のヒント
これを示すため、前のコイン投げの問題を考え、閉じた形の解を知らないふりをしよう。次のように計算できる。
(22.7.9)
This can be written into code, and freely optimized even for billions of coin flips.
英語のヒント
これはコードに書けて、何十億回のコイン投げに対しても自由に最適化できる。
# Set up our data
n_H = 8675309
n_T = 256245
# Initialize our paramteres
theta = torch.tensor(0.5, requires_grad=True)
# Perform gradient descent
lr = 1e-9
for iter in range(100):
loss = -(n_H * torch.log(theta) + n_T * torch.log(1 - theta))
loss.backward()
with torch.no_grad():
theta -= lr * theta.grad
theta.grad.zero_()
# Check output
theta, n_H / (n_H + n_T)(tensor(0.9713, requires_grad=True), 0.9713101437890875)Numerical convenience is not the only reason why people like to use negative log-likelihoods. There are several other reasons why it is preferable.
英語のヒント
数値的な便利さだけが、人々が負の対数尤度を使いたがる理由ではない。それが好ましい理由はほかにもいくつかある。
The second reason we consider the log-likelihood is the simplified application of calculus rules. As discussed above, due to independence assumptions, most probabilities we encounter in machine learning are products of individual probabilities.
英語のヒント
対数尤度を考える2番目の理由は、微積分の規則の適用が簡単になることである。上で論じたように、独立性の仮定のため、機械学習で出会うたいていの確率は個々の確率の積である。
(22.7.10)
This means that if we directly apply the product rule to compute a derivative we get
英語のヒント
これは、導関数を計算するのに積の規則を直接適用すると、次が得られることを意味する。
(22.7.11)
This requires multiplications, along with additions, so it is proportional to quadratic time in the inputs! Sufficient cleverness in grouping terms will reduce this to linear time, but it requires some thought. For the negative log-likelihood we have instead
英語のヒント
これには回の掛け算と回の足し算が必要なので、入力に対して2乗の時間に比例する。項をうまくまとめる工夫を十分にすれば線形時間に減らせるが、少し考える必要がある。負の対数尤度では、代わりに次が得られる。
(22.7.12)
which then gives
英語のヒント
これにより次が得られる。
(22.7.13)
This requires only divides and sums, and thus is linear time in the inputs.
英語のヒント
これには回の割り算と回の和しか必要ないので、入力に対して線形時間である。
The third and final reason to consider the negative log-likelihood is the relationship to information theory, which we will discuss in detail in Section 22.11. This is a rigorous mathematical theory which gives a way to measure the degree of information or randomness in a random variable. The key object of study in that field is the entropy which is
英語のヒント
負の対数尤度を考える3番目にして最後の理由は、情報理論との関係である。これは22.11節で詳しく論じる。これは、確率変数の中の情報やランダムさの度合いを測る方法を与える、厳密な数学的理論である。その分野での主要な研究対象はエントロピーであり、次のとおりである。
(22.7.14)
which measures the randomness of a source. Notice that this is nothing more than the average probability, and thus if we take our negative log-likelihood and divide by the number of data examples, we get a relative of entropy known as cross-entropy. This theoretical interpretation alone would be sufficiently compelling to motivate reporting the average negative log-likelihood over the dataset as a way of measuring model performance.
英語のヒント
これは情報源のランダムさを測る。これは平均の確率にほかならないことに注意しよう。したがって、負の対数尤度をとってデータの例の数で割ると、交差エントロピーとして知られるエントロピーの親類が得られる。この理論的な解釈だけでも、モデルの性能を測る方法として、データセットでの負の対数尤度の平均を報告する十分説得力のある動機になるだろう。