第22章付録:深層学習のための数学
22.2.6. A Useful Application: The Growth of Iterated Maps22.2.6 役に立つ応用:反復写像の増大
Now that we understand what eigenvectors are in principle, let’s see how they can be used to provide a deep understanding of a problem central to neural network behavior: proper weight initialization.
英語のヒント
固有ベクトルが原理的に何であるかがわかったので、それを使って、ニューラルネットワークのふるまいの中心にある問題、つまり適切な重みの初期化について、どう深い理解が得られるかを見よう。
22.2.6.1. Eigenvectors as Long Term Behavior22.2.6.1 長期的なふるまいとしての固有ベクトル
The full mathematical investigation of the initialization of deep neural networks is beyond the scope of the text, but we can see a toy version here to understand how eigenvalues can help us see how these models work. As we know, neural networks operate by interspersing layers of linear transformations with non-linear operations. For simplicity here, we will assume that there is no non-linearity, and that the transformation is a single repeated matrix operation , so that the output of our model is
英語のヒント
深層ニューラルネットワークの初期化を数学的に完全に調べることは本書の範囲を超えるが、ここでおもちゃの版を見て、これらのモデルがどう働くかを理解するのに固有値がどう役立つかを理解できる。知ってのとおり、ニューラルネットワークは、線形変換の層と非線形の演算を交互にはさむことで動く。ここでは簡単にするため、非線形性はなく、変換は一つの繰り返される行列演算であるとする。すると、モデルの出力は次のようになる。
(22.2.20)
When these models are initialized, is taken to be a random matrix with Gaussian entries, so let’s make one of those. To be concrete, we start with a mean zero, variance one Gaussian distributed matrix.
英語のヒント
これらのモデルを初期化するときは、はガウス分布に従う成分をもつランダムな行列にとられるので、そうした行列を一つ作ろう。具体的には、平均0、分散1のガウス分布に従う行列から始める。
torch.manual_seed(42)
k = 5
A = torch.randn(k, k, dtype=torch.float64)
Atensor([[ 0.2996, 0.2424, 0.2832, -0.2329, 0.6712],
[ 0.7818, -1.7903, -1.7484, 0.1735, -0.1182],
[-1.7446, -0.4695, 0.4573, 0.5177, -0.2771],
[-0.6641, 0.6551, 0.2616, -1.5265, -0.3311],
[-0.6378, 0.1072, 0.7096, 0.3009, -0.2869]], dtype=torch.float64)22.2.6.2. Behavior on Random Data22.2.6.2 ランダムなデータに対するふるまい
For simplicity in our toy model, we will assume that the data vector we feed in is a random five dimensional Gaussian vector. Let’s think about what we want to have happen. For context, lets think of a generic ML problem, where we are trying to turn input data, like an image, into a prediction, like the probability the image is a picture of a cat. If repeated application of stretches a random vector out to be very long, then small changes in input will be amplified into large changes in output—tiny modifications of the input image would lead to vastly different predictions. This does not seem right!
英語のヒント
このおもちゃのモデルでは簡単にするため、入力するデータのベクトルは、ランダムな5次元のガウスベクトルであるとする。何が起きてほしいかを考えよう。文脈として、一般的なMLの問題を考える。そこでは、画像のような入力データを、その画像が猫の写真である確率のような予測に変えようとしている。を繰り返し適用するとランダムなベクトルがとても長く引き伸ばされるなら、入力の小さな変化が出力の大きな変化に増幅される。入力画像のほんのわずかな修正が、まったく違う予測につながってしまう。これは正しくなさそうである。
On the flip side, if shrinks random vectors to be shorter, then after running through many layers, the vector will essentially shrink to nothing, and the output will not depend on the input. This is also clearly not right either!
英語のヒント
逆に、がランダムなベクトルを縮めて短くするなら、多くの層を通った後、ベクトルは本質的に縮んでなくなり、出力は入力に依存しなくなる。これも明らかに正しくない。
We need to walk the narrow line between growth and decay to make sure that our output changes depending on our input, but not much!
英語のヒント
出力が入力に応じて変わるが、変わりすぎないようにするには、増大と減衰の間の細い線の上を歩く必要がある。
Let’s see what happens when we repeatedly multiply our matrix against a random input vector, and keep track of the norm.
英語のヒント
行列をランダムな入力ベクトルに繰り返し掛けたときに何が起きるかを、ノルムを追いかけながら見よう。
# Calculate the sequence of norms after repeatedly applying `A`
v_in = torch.randn(k, 1, dtype=torch.float64)
norm_list = [torch.norm(v_in).item()]
for i in range(1, 100):
v_in = A @ v_in
norm_list.append(torch.norm(v_in).item())
d2l.plot(torch.arange(0, 100), norm_list, 'Iteration', 'Value')The norm is growing uncontrollably! Indeed if we take the list of quotients, we will see a pattern.
英語のヒント
ノルムが手に負えないほど増大している。実際、商のリストをとると、あるパターンが見える。
# Compute the scaling factor of the norms
norm_ratio_list = []
for i in range(1, 100):
norm_ratio_list.append(norm_list[i]/norm_list[i - 1])
d2l.plot(torch.arange(1, 100), norm_ratio_list, 'Iteration', 'Ratio')If we look at the last portion of the above computation, we see that the random vector is stretched by a factor of
1.974459321485[...], where the portion at the end shifts a little, but the stretching factor is stable.英語のヒント
上の計算の最後の部分を見ると、ランダムなベクトルは
1.974459321485[...]倍に引き伸ばされていることがわかる。末尾の部分は少し変動するが、引き伸ばしの倍率は安定している。22.2.6.3. Relating Back to Eigenvectors22.2.6.3 固有ベクトルに戻って関係づける
We have seen that eigenvectors and eigenvalues correspond to the amount something is stretched, but that was for specific vectors, and specific stretches. Let’s take a look at what they are for . A bit of a caveat here: it turns out that to see them all, we will need to go to complex numbers. You can think of these as stretches and rotations. By taking the norm of the complex number (square root of the sums of squares of real and imaginary parts) we can measure that stretching factor. Let’s also sort them.
英語のヒント
固有ベクトルと固有値が、何かが引き伸ばされる量に対応することを見てきたが、それは特定のベクトルと特定の引き伸ばしについてであった。についてそれらが何であるかを見てみよう。ここで少し注意がある。それらをすべて見るには、複素数を使う必要があることがわかる。これらは引き伸ばしと回転と考えられる。複素数のノルム(実部と虚部の二乗和の平方根)をとることで、その引き伸ばしの倍率を測れる。それらを並べ替えもしよう。
# Compute the eigenvalues
eigs = torch.linalg.eig(A).eigenvalues.tolist()
norm_eigs = [torch.abs(torch.tensor(x)) for x in eigs]
norm_eigs.sort()
print(f'norms of eigenvalues: {norm_eigs}')norms of eigenvalues: [tensor(0.3490), tensor(1.1296), tensor(1.1296), tensor(1.1828), tensor(2.4532)]22.2.6.4. An Observation22.2.6.4 一つの観察
We see something a bit unexpected happening here: that number we identified before for the long term stretching of our matrix applied to a random vector is exactly (accurate to thirteen decimal places!) the largest eigenvalue of . This is clearly not a coincidence!
英語のヒント
ここで少し予想外のことが起きている。前に見つけた、ランダムなベクトルに行列を適用したときの長期的な引き伸ばしの数は、ちょうど(小数点以下13桁まで正確に)の最大の固有値になっている。これは明らかに偶然ではない。
But, if we now think about what is happening geometrically, this starts to make sense. Consider a random vector. This random vector points a little in every direction, so in particular, it points at least a little bit in the same direction as the eigenvector of associated with the largest eigenvalue. This is so important that it is called the principle eigenvalue and principle eigenvector. After applying , our random vector gets stretched in every possible direction, as is associated with every possible eigenvector, but it is stretched most of all in the direction associated with this principle eigenvector. What this means is that after apply in , our random vector is longer, and points in a direction closer to being aligned with the principle eigenvector. After applying the matrix many times, the alignment with the principle eigenvector becomes closer and closer until, for all practical purposes, our random vector has been transformed into the principle eigenvector! Indeed this algorithm is the basis for what is known as the power iteration for finding the largest eigenvalue and eigenvector of a matrix. For details see, for example, (Golub and Van Loan, 1996).
英語のヒント
しかし、何が幾何学的に起きているかを考えると、これは理にかなってくる。ランダムなベクトルを考えよう。このランダムなベクトルはあらゆる方向を少しずつ向いているので、とくに、最大の固有値に対応するの固有ベクトルと同じ方向も、少なくとも少しは向いている。これはとても重要なので、主固有値、主固有ベクトルと呼ばれる。を適用した後、ランダムなベクトルは、あらゆる固有ベクトルに対応するあらゆる方向に引き伸ばされるが、最も大きく引き伸ばされるのはこの主固有ベクトルに対応する方向である。これが意味するのは、を適用した後、ランダムなベクトルはより長くなり、主固有ベクトルにより近くそろった方向を向くということである。行列を何度も適用すると、主固有ベクトルとのそろい方はどんどん近くなり、ついには、実用上はランダムなベクトルが主固有ベクトルに変換されてしまう。実際、このアルゴリズムは、行列の最大の固有値と固有ベクトルを見つけるためのべき乗法として知られるものの基礎である。詳しくは、たとえばGolub and Van Loan, 1996を参照されたい。
22.2.6.5. Fixing the Normalization22.2.6.5 正規化を直す
Now, from above discussions, we concluded that we do not want a random vector to be stretched or squished at all, we would like random vectors to stay about the same size throughout the entire process. To do so, we now rescale our matrix by this principle eigenvalue so that the largest eigenvalue is instead now just one. Let’s see what happens in this case.
英語のヒント
さて、上の議論から、ランダムなベクトルが引き伸ばされたり押しつぶされたりするのはまったく望まず、ランダムなベクトルが過程全体を通じてだいたい同じ大きさにとどまってほしいと結論した。そうするために、行列をこの主固有値で割って尺度を直し、最大の固有値が今度はちょうど1になるようにする。この場合に何が起きるかを見よう。
# Rescale the matrix `A`
A /= norm_eigs[-1]
# Do the same experiment again
v_in = torch.randn(k, 1, dtype=torch.float64)
norm_list = [torch.norm(v_in).item()]
for i in range(1, 100):
v_in = A @ v_in
norm_list.append(torch.norm(v_in).item())
d2l.plot(torch.arange(0, 100), norm_list, 'Iteration', 'Value')We can also plot the ratio between consecutive norms as before and see that indeed it stabilizes.
英語のヒント
前と同じように連続するノルムの比をプロットすると、実際に安定することがわかる。
# Also plot the ratio
norm_ratio_list = []
for i in range(1, 100):
norm_ratio_list.append(norm_list[i]/norm_list[i-1])
d2l.plot(torch.arange(1, 100), norm_ratio_list, 'Iteration', 'Ratio')