第6章構築者のための手引き
6.3. Parameter Initialization6.3 パラメータの初期化
Now that we know how to access the parameters, let’s look at how to initialize them properly. We discussed the need for proper initialization in Section 5.4. The deep learning framework provides default random initializations to its layers. However, we often want to initialize our weights according to various other protocols. The framework provides most commonly used protocols, and also allows to create a custom initializer.
英語のヒント
パラメータにアクセスする方法がわかったので、パラメータを適切に初期化する方法を見てみよう。適切な初期化が必要なことは5.4節で論じた。深層学習のフレームワークは、層に既定の無作為な初期化を用意している。しかし、ほかのさまざまな手順に従って重みを初期化したいことも多い。フレームワークは、最もよく使われる手順を提供しているうえ、独自の初期化器を作ることもできる。
import torch
from torch import nnBy default, PyTorch initializes weight and bias matrices uniformly by drawing from a range that is computed according to the input and output dimension. PyTorch’s
nn.init module provides a variety of preset initialization methods.英語のヒント
既定では、PyTorchは、入力と出力の次元にもとづいて計算した範囲から値を取り出して、重みとバイアスの行列を一様に初期化する。PyTorchの
nn.initモジュールには、さまざまな初期化の方法があらかじめ用意されている。net = nn.Sequential(nn.LazyLinear(8), nn.ReLU(), nn.LazyLinear(1))
X = torch.rand(size=(2, 4))
net(X).shapetorch.Size([2, 1])