プリンキピア

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

4.5.2. Softmax Revisited4.5.2 ソフトマックス再訪

In Section 4.4 we calculated our model’s output and applied the cross-entropy loss. While this is perfectly reasonable mathematically, it is risky computationally, because of numerical underflow and overflow in the exponentiation.
英語のヒント
4.4節では、モデルの出力を計算し、交差エントロピー損失を当てはめた。これは数学的にはまったく理にかなっているが、計算の面では危うい。指数関数に通すところで、数値のアンダーフローとオーバーフローが起きるからである。
Recall that the softmax function computes probabilities via yˆj=exp⁡(oj)∑kexp⁡(ok). If some of the ok are very large, i.e., very positive, then exp⁡(ok) might be larger than the largest number we can have for certain data types. This is called overflow. Likewise, if every argument is a very large negative number, we will get underflow. For instance, single precision floating point numbers approximately cover the range of 10−38 to 1038. As such, if the largest term in 𝐨 lies outside the interval [−90,90], the result will not be stable. A way round this problem is to subtract o¯=defmaxkok from all entries:
英語のヒント
ソフトマックス関数はyˆj=exp⁡(oj)∑kexp⁡(ok)によって確率を計算することを思い出してほしい。okの一部が非常に大きい、つまり非常に大きな正の値であれば、exp⁡(ok)は、ある種のデータ型で表せる最大の数よりも大きくなるかもしれない。これをオーバーフローという。同様に、どの引数も非常に大きな負の数であれば、アンダーフローが起きる。たとえば、単精度の浮動小数点数は、おおよそ10−38から1038の範囲をカバーする。そのため、𝐨の最大の項が区間[−90,90]の外にあると、結果は安定しない。この問題を回避する一つの方法は、すべての成分からo¯=defmaxkokを引くことである。
yˆj=exp⁡oj∑kexp⁡ok=exp⁡(oj−o¯)exp⁡o¯∑kexp⁡(ok−o¯)exp⁡o¯=exp⁡(oj−o¯)∑kexp⁡(ok−o¯).(4.5.1)
By construction we know that oj−o¯≤0 for all j. As such, for a q-class classification problem, the denominator is contained in the interval [1,q]. Moreover, the numerator never exceeds 1, thus preventing numerical overflow. Numerical underflow only occurs when exp⁡(oj−o¯) numerically evaluates as 0. Nonetheless, a few steps down the road we might find ourselves in trouble when we want to compute log⁡yˆj as log⁡0. In particular, in backpropagation, we might find ourselves faced with a screenful of the dreaded NaN (Not a Number) results.
英語のヒント
作り方から、すべてのjについてoj−o¯≤0であることがわかる。そのため、qクラスの分類問題では、分母は区間[1,q]に収まる。さらに、分子は決して1を超えないので、数値のオーバーフローが防がれる。数値のアンダーフローが起きるのは、exp⁡(oj−o¯)が数値的に0と評価されるときだけである。それでも、何段階か先でlog⁡yˆjを計算しようとすると、それがlog⁡0となって困ったことになるかもしれない。特に誤差逆伝播法では、恐ろしいNaN(Not a Number、非数)の結果が画面いっぱいに並ぶのを目にするかもしれない。
Fortunately, we are saved by the fact that even though we are computing exponential functions, we ultimately intend to take their log (when calculating the cross-entropy loss). By combining softmax and cross-entropy, we can escape the numerical stability issues altogether. We have:
英語のヒント
幸い、指数関数を計算してはいても、最終的には(交差エントロピー損失を計算するときに)その対数をとるつもりだという事実に救われる。ソフトマックスと交差エントロピーを組み合わせれば、数値の安定性の問題をすっかり逃れられる。次が成り立つ。
log⁡yˆj=log⁡exp⁡(oj−o¯)∑kexp⁡(ok−o¯)=oj−o¯−log⁡∑kexp⁡(ok−o¯).(4.5.2)
This avoids both overflow and underflow. We will want to keep the conventional softmax function handy in case we ever want to evaluate the output probabilities by our model. But instead of passing softmax probabilities into our new loss function, we just pass the logits and compute the softmax and its log all at once inside the cross-entropy loss function, which does smart things like the “LogSumExp trick”.
英語のヒント
これでオーバーフローとアンダーフローの両方が避けられる。モデルの出力確率を評価したくなる場合に備えて、従来のソフトマックス関数も手元に置いておきたい。しかし、新しい損失関数にはソフトマックスの確率を渡す代わりに、ロジットをそのまま渡し、交差エントロピー損失関数の内部でソフトマックスとその対数を一度に計算する。この関数は「LogSumExpトリック」のような気の利いたことをしてくれる。
@d2l.add_to_class(d2l.Classifier)  #@save
def loss(self, Y_hat, Y, averaged=True):
    Y_hat = Y_hat.reshape((-1, Y_hat.shape[-1]))
    Y = Y.reshape((-1,))
    return F.cross_entropy(
        Y_hat, Y, reduction='mean' if averaged else 'none')