第3章回帰のための線形ニューラルネットワーク
3.7.4. Concise Implementation3.7.4 簡潔な実装
Because weight decay is ubiquitous in neural network optimization, the deep learning framework makes it especially convenient, integrating weight decay into the optimization algorithm itself for easy use in combination with any loss function. Moreover, this integration serves a computational benefit, allowing implementation tricks to add weight decay to the algorithm, without any additional computational overhead. Since the weight decay portion of the update depends only on the current value of each parameter, the optimizer must touch each parameter once anyway.
英語のヒント
重み減衰はニューラルネットワークの最適化のどこにでも出てくるので、深層学習フレームワークは、重み減衰を最適化アルゴリズムそのものに組み込んで、どんな損失関数とも組み合わせて簡単に使えるように、特に便利にしている。さらに、この組み込みは計算上の利点にもなり、追加の計算のオーバーヘッドなしにアルゴリズムに重み減衰を加える実装の工夫ができる。更新のうち重み減衰の部分は各パラメータの今の値だけに依存するので、最適化手法はどのみち各パラメータに一度は触れなければならないからである。
Below, we specify the weight decay hyperparameter directly through
weight_decay when instantiating our optimizer. By default, PyTorch decays both weights and biases simultaneously, but we can configure the optimizer to handle different parameters according to different policies. Here, we only set weight_decay for the weights (the net.weight parameters), hence the bias (the net.bias parameter) will not decay.英語のヒント
下では、最適化手法のインスタンスを作るときに、
weight_decayで重み減衰のハイパーパラメータを直接指定する。既定では、PyTorchは重みとバイアスの両方を同時に減衰させるが、最適化手法を設定して、異なるパラメータを異なる方針で扱うこともできる。ここでは、重み(net.weightパラメータ)にだけweight_decayを設定するので、バイアス(net.biasパラメータ)は減衰しない。class WeightDecay(d2l.LinearRegression):
def __init__(self, wd, lr):
super().__init__(lr)
self.save_hyperparameters()
self.wd = wd
def configure_optimizers(self):
return torch.optim.SGD([
{'params': self.net.weight, 'weight_decay': self.wd},
{'params': self.net.bias}], lr=self.lr)The plot looks similar to that when we implemented weight decay from scratch. However, this version runs faster and is easier to implement, benefits that will become more pronounced as you address larger problems and this work becomes more routine.
英語のヒント
グラフは、重み減衰をゼロから実装したときと似ている。しかし、この版のほうが速く動き、実装しやすい。こうした利点は、より大きな問題に取り組み、この作業が日常的になるにつれて、いっそう際立ってくる。
model = WeightDecay(wd=3, lr=0.01)
model.board.yscale='log'
trainer.fit(model, data)
print('L2 norm of w:', float(l2_penalty(model.get_w_b()[0])))L2 norm of w: 0.013779522851109505So far, we have touched upon one notion of what constitutes a simple linear function. However, even for simple nonlinear functions, the situation can be much more complex. To see this, the concept of reproducing kernel Hilbert space (RKHS) allows one to apply tools introduced for linear functions in a nonlinear context. Unfortunately, RKHS-based algorithms tend to scale poorly to large, high-dimensional data. In this book we will often adopt the common heuristic whereby weight decay is applied to all layers of a deep network.
英語のヒント
ここまで、単純な線形関数を何が構成するかの一つの概念に触れてきた。しかし、単純な非線形関数でさえ、状況はずっと複雑になりうる。これを見るのに、再生核ヒルベルト空間(RKHS)の概念を使えば、線形関数のために導入した道具を非線形の文脈で当てはめられる。残念ながら、RKHSにもとづくアルゴリズムは、大きな高次元のデータへはうまくスケールしない傾向がある。この本では、深いネットワークのすべての層に重み減衰を当てはめるという、よくある経験則を採ることが多い。