プリンキピア

第6章構築者のための手引き

6.4. Lazy Initialization6.4 遅延初期化

So far, it might seem that we got away with being sloppy in setting up our networks. Specifically, we did the following unintuitive things, which might not seem like they should work:
英語のヒント
ここまで、ネットワークの準備をいいかげんにしても、なんとかやり過ごせてきたように見えるかもしれない。具体的には、次のような直観に反すること、うまくいくはずがないように思えることをしてきた。
  • We defined the network architectures without specifying the input dimensionality.
    英語のヒント
    入力の次元を指定せずに、ネットワークの構造を定義した。
  • We added layers without specifying the output dimension of the previous layer.
    英語のヒント
    前の層の出力の次元を指定せずに、層を加えた。
  • We even “initialized” these parameters before providing enough information to determine how many parameters our models should contain.
    英語のヒント
    モデルがいくつのパラメータを含むべきかを決めるのに十分な情報を与える前に、これらのパラメータを「初期化」することさえした。
You might be surprised that our code runs at all. After all, there is no way the deep learning framework could tell what the input dimensionality of a network would be. The trick here is that the framework defers initialization, waiting until the first time we pass data through the model, to infer the sizes of each layer on the fly.
英語のヒント
コードがそもそも動くことに驚くかもしれない。何しろ、深層学習のフレームワークには、ネットワークの入力の次元がいくつになるかを知るすべがない。ここでの仕掛けは、フレームワークが初期化を先延ばしにし、初めてデータをモデルに通すときまで待って、その場で各層の大きさを推論することである。
Later on, when working with convolutional neural networks, this technique will become even more convenient since the input dimensionality (e.g., the resolution of an image) will affect the dimensionality of each subsequent layer. Hence the ability to set parameters without the need to know, at the time of writing the code, the value of the dimension can greatly simplify the task of specifying and subsequently modifying our models. Next, we go deeper into the mechanics of initialization.
英語のヒント
後で畳み込みニューラルネットワークを扱うときには、この手法はさらに便利になる。入力の次元(たとえば画像の解像度)が、その後の各層の次元に影響するからである。そのため、コードを書く時点で次元の値を知らなくてもパラメータを設定できる能力は、モデルを指定し、その後に修正する作業を大いに簡単にしうる。次に、初期化の仕組みにさらに深く踏み込む。
import torch
from torch import nn
from d2l import torch as d2l
To begin, let’s instantiate an MLP.
英語のヒント
まず、MLPをインスタンス化しよう。
net = nn.Sequential(nn.LazyLinear(256), nn.ReLU(), nn.LazyLinear(10))
At this point, the network cannot possibly know the dimensions of the input layer’s weights because the input dimension remains unknown.
英語のヒント
この時点では、入力の次元がまだわからないので、ネットワークは入力層の重みの次元を知りようがない。
Consequently the framework has not yet initialized any parameters. We confirm by attempting to access the parameters below.
英語のヒント
その結果、フレームワークはまだどのパラメータも初期化していない。下でパラメータにアクセスしようとして、それを確かめる。
net[0].weight
<UninitializedParameter>
Next let’s pass data through the network to make the framework finally initialize parameters.
英語のヒント
次に、ネットワークにデータを通して、フレームワークにようやくパラメータを初期化させよう。
X = torch.rand(2, 20)
net(X)

net[0].weight.shape
torch.Size([256, 20])
As soon as we know the input dimensionality, 20, the framework can identify the shape of the first layer’s weight matrix by plugging in the value of 20. Having recognized the first layer’s shape, the framework proceeds to the second layer, and so on through the computational graph until all shapes are known. Note that in this case, only the first layer requires lazy initialization, but the framework initializes sequentially. Once all parameter shapes are known, the framework can finally initialize the parameters.
英語のヒント
入力の次元が20だとわかるとすぐに、フレームワークは20という値を代入して、最初の層の重み行列の形状を特定できる。最初の層の形状がわかれば、フレームワークは2番目の層へと進み、計算グラフをたどって、すべての形状がわかるまで続ける。この場合、遅延初期化が必要なのは最初の層だけだが、フレームワークは順に初期化していくことに注意してほしい。すべてのパラメータの形状がわかったら、フレームワークはようやくパラメータを初期化できる。
The following method passes in dummy inputs through the network for a dry run to infer all parameter shapes and subsequently initializes the parameters. It will be used later when default random initializations are not desired.
英語のヒント
次のメソッドは、ダミーの入力をネットワークに通す予行演習ですべてのパラメータの形状を推論し、その後パラメータを初期化する。これは、後で既定の無作為な初期化が望ましくないときに使う。
@d2l.add_to_class(d2l.Module)  #@save
def apply_init(self, inputs, init=None):
    self.forward(*inputs)
    if init is not None:
        self.net.apply(init)