第5章多層パーセプトロン
5.4.1. Vanishing and Exploding Gradients5.4.1 勾配消失と勾配爆発
Consider a deep network with layers, input and output . With each layer defined by a transformation parametrized by weights , whose hidden layer output is (let ), our network can be expressed as:
英語のヒント
層からなり、入力が、出力がの深いネットワークを考えよう。各層は、重みでパラメータ化された変換で定義され、その隠れ層の出力を(とする)とすると、ネットワークは次のように表せる。
(5.4.1)
If all the hidden layer output and the input are vectors, we can write the gradient of with respect to any set of parameters as follows:
英語のヒント
隠れ層の出力と入力がすべてベクトルなら、任意のパラメータの組についてのの勾配は、次のように書ける。
(5.4.2)
In other words, this gradient is the product of matrices and the gradient vector . Thus we are susceptible to the same problems of numerical underflow that often crop up when multiplying together too many probabilities. When dealing with probabilities, a common trick is to switch into log-space, i.e., shifting pressure from the mantissa to the exponent of the numerical representation. Unfortunately, our problem above is more serious: initially the matrices may have a wide variety of eigenvalues. They might be small or large, and their product might be very large or very small.
英語のヒント
言い換えれば、この勾配は、個の行列と勾配ベクトルの積である。したがって、あまりに多くの確率を掛け合わせるときによく持ち上がるのと同じ、数値のアンダーフローの問題を受けやすい。確率を扱うときのよくある技は、対数空間に切り替えること、つまり数値表現の負担を仮数部から指数部へ移すことである。残念ながら、上の問題はもっと深刻である。最初は、行列はさまざまな固有値をもちうる。それらは小さいことも大きいこともあり、その積はとても大きくなることも、とても小さくなることもある。
The risks posed by unstable gradients go beyond numerical representation. Gradients of unpredictable magnitude also threaten the stability of our optimization algorithms. We may be facing parameter updates that are either (i) excessively large, destroying our model (the exploding gradient problem); or (ii) excessively small (the vanishing gradient problem), rendering learning impossible as parameters hardly move on each update.
英語のヒント
不安定な勾配がもたらす危険は、数値表現の問題にとどまらない。大きさが予測できない勾配は、最適化アルゴリズムの安定性も脅かす。パラメータの更新が、(i)大きすぎてモデルを壊してしまう(勾配爆発問題)か、(ii)小さすぎて(勾配消失問題)、更新のたびにパラメータがほとんど動かず、学習が不可能になるか、のどちらかになっているかもしれない。
5.4.1.1. Vanishing Gradients5.4.1.1 勾配消失
One frequent culprit causing the vanishing gradient problem is the choice of the activation function that is appended following each layer’s linear operations. Historically, the sigmoid function (introduced in Section 5.1) was popular because it resembles a thresholding function. Since early artificial neural networks were inspired by biological neural networks, the idea of neurons that fire either fully or not at all (like biological neurons) seemed appealing. Let’s take a closer look at the sigmoid to see why it can cause vanishing gradients.
英語のヒント
勾配消失問題を引き起こすよくある犯人の一つは、各層の線形演算の後に付け加えられる活性化関数の選び方である。歴史的には、シグモイド関数(5.1節で紹介した)がよく使われていた。閾値関数に似ているからである。初期の人工ニューラルネットワークは生物のニューラルネットワークに着想を得ていたので、(生物のニューロンのように)完全に発火するかまったく発火しないかのどちらかであるニューロンという考えは、魅力的に思えた。シグモイドがなぜ勾配消失を引き起こしうるのかを見るため、もう少し詳しく見てみよう。
x = torch.arange(-8.0, 8.0, 0.1, requires_grad=True)
y = torch.sigmoid(x)
y.backward(torch.ones_like(x))
d2l.plot(x.detach().numpy(), [y.detach().numpy(), x.grad.numpy()],
legend=['sigmoid', 'gradient'], figsize=(4.5, 2.5))As you can see, the sigmoid’s gradient vanishes both when its inputs are large and when they are small. Moreover, when backpropagating through many layers, unless we are in the Goldilocks zone, where the inputs to many of the sigmoids are close to zero, the gradients of the overall product may vanish. When our network boasts many layers, unless we are careful, the gradient will likely be cut off at some layer. Indeed, this problem used to plague deep network training. Consequently, ReLUs, which are more stable (but less neurally plausible), have emerged as the default choice for practitioners.
英語のヒント
見てのとおり、シグモイドの勾配は、入力が大きいときにも小さいときにも消える。さらに、多くの層を通って逆伝播するとき、多くのシグモイドへの入力が0に近い、ちょうどよい範囲(ゴルディロックスゾーン)にいるのでなければ、全体の積の勾配は消えてしまうかもしれない。ネットワークが多くの層を誇っていると、注意しないかぎり、勾配はどこかの層で断ち切られてしまう可能性が高い。実際、この問題はかつて深いネットワークの訓練を悩ませていた。その結果、(神経科学的なもっともらしさは劣るが)より安定したReLUが、実践者の既定の選択として台頭してきた。
5.4.1.2. Exploding Gradients5.4.1.2 勾配爆発
The opposite problem, when gradients explode, can be similarly vexing. To illustrate this a bit better, we draw 100 Gaussian random matrices and multiply them with some initial matrix. For the scale that we picked (the choice of the variance ), the matrix product explodes. When this happens because of the initialization of a deep network, we have no chance of getting a gradient descent optimizer to converge.
英語のヒント
勾配が爆発するという逆の問題も、同じように厄介になりうる。これをもう少しよく示すため、ガウス分布に従う無作為な行列を100個取り出し、それらをある初期の行列に掛け合わせる。選んだスケール(分散の選択)では、行列の積は爆発する。深いネットワークの初期化のせいでこれが起きると、勾配降下法のオプティマイザを収束させる見込みはない。
M = torch.normal(0, 1, size=(4, 4))
print('a single matrix \n',M)
for i in range(100):
M = M @ torch.normal(0, 1, size=(4, 4))
print('after multiplying 100 matrices\n', M)a single matrix
tensor([[-0.8755, -1.2171, 1.3316, 0.1357],
[ 0.4399, 1.4073, -1.9131, -0.4608],
[-2.1420, 0.3643, -0.5267, 1.0277],
[-0.1734, -0.7549, 2.3024, 1.3085]])
after multiplying 100 matrices
tensor([[-2.9185e+23, 1.3915e+25, -1.1865e+25, 1.4354e+24],
[ 4.9142e+23, -2.3430e+25, 1.9979e+25, -2.4169e+24],
[ 2.6578e+23, -1.2672e+25, 1.0805e+25, -1.3072e+24],
[-5.2223e+23, 2.4899e+25, -2.1231e+25, 2.5684e+24]])5.4.1.3. Breaking the Symmetry5.4.1.3 対称性を破る
Another problem in neural network design is the symmetry inherent in their parametrization. Assume that we have a simple MLP with one hidden layer and two units. In this case, we could permute the weights of the first layer and likewise permute the weights of the output layer to obtain the same function. There is nothing special differentiating the first and second hidden units. In other words, we have permutation symmetry among the hidden units of each layer.
英語のヒント
ニューラルネットワークの設計におけるもう一つの問題は、そのパラメータ化に本来備わっている対称性である。隠れ層が一つでユニットが二つの、単純なMLPがあるとしよう。この場合、第1層の重みを並べ替え、同じように出力層の重みも並べ替えれば、同じ関数が得られる。1番目と2番目の隠れユニットを区別する特別なものは何もない。言い換えれば、各層の隠れユニットのあいだには置換対称性がある。
This is more than just a theoretical nuisance. Consider the aforementioned one-hidden-layer MLP with two hidden units. For illustration, suppose that the output layer transforms the two hidden units into only one output unit. Imagine what would happen if we initialized all the parameters of the hidden layer as for some constant . In this case, during forward propagation either hidden unit takes the same inputs and parameters producing the same activation which is fed to the output unit. During backpropagation, differentiating the output unit with respect to parameters gives a gradient all of whose elements take the same value. Thus, after gradient-based iteration (e.g., minibatch stochastic gradient descent), all the elements of still take the same value. Such iterations would never break the symmetry on their own and we might never be able to realize the network’s expressive power. The hidden layer would behave as if it had only a single unit. Note that while minibatch stochastic gradient descent would not break this symmetry, dropout regularization (to be introduced later) would!
英語のヒント
これは単なる理論上の厄介ごとにとどまらない。前述の、隠れユニットが二つの、隠れ層が一つのMLPを考えよう。説明のため、出力層が二つの隠れユニットを一つの出力ユニットだけに変換するとしよう。ある定数について、隠れ層のパラメータをすべてと初期化したら何が起きるかを想像してほしい。この場合、順伝播のあいだ、どちらの隠れユニットも同じ入力とパラメータを受け取り、同じ活性を生み出して、それが出力ユニットに送られる。逆伝播のあいだは、出力ユニットをパラメータについて微分すると、すべての要素が同じ値をとる勾配が得られる。したがって、勾配にもとづく反復(たとえばミニバッチ確率的勾配降下法)の後でも、のすべての要素は同じ値をとったままである。そうした反復はそれ自体では決して対称性を破らず、ネットワークの表現力を引き出すことは決してできないかもしれない。隠れ層は、まるでユニットが一つしかないかのようにふるまうだろう。ミニバッチ確率的勾配降下法はこの対称性を破らないが、(後で紹介する)ドロップアウト正則化なら破れることに注意してほしい!