第8章現代の畳み込みニューラルネットワーク
8.5.4. LeNet with Batch Normalization8.5.4 バッチ正規化を使うLeNet
To see how to apply
BatchNorm in context, below we apply it to a traditional LeNet model (Section 7.6). Recall that batch normalization is applied after the convolutional layers or fully connected layers but before the corresponding activation functions.英語のヒント
BatchNormを文脈の中でどう当てはめるかを見るため、以下では、伝統的なLeNetのモデル(7.6節)に当てはめる。バッチ正規化は、畳み込み層や全結合層の後、対応する活性化関数の前に当てはめることを思い出そう。class BNLeNetScratch(d2l.Classifier):
def __init__(self, lr=0.1, num_classes=10):
super().__init__()
self.save_hyperparameters()
self.net = nn.Sequential(
nn.LazyConv2d(6, kernel_size=5), BatchNorm(6, num_dims=4),
nn.Sigmoid(), nn.AvgPool2d(kernel_size=2, stride=2),
nn.LazyConv2d(16, kernel_size=5), BatchNorm(16, num_dims=4),
nn.Sigmoid(), nn.AvgPool2d(kernel_size=2, stride=2),
nn.Flatten(), nn.LazyLinear(120),
BatchNorm(120, num_dims=2), nn.Sigmoid(), nn.LazyLinear(84),
BatchNorm(84, num_dims=2), nn.Sigmoid(),
nn.LazyLinear(num_classes))As before, we will train our network on the Fashion-MNIST dataset. This code is virtually identical to that when we first trained LeNet.
英語のヒント
前と同じように、Fashion-MNISTのデータセットでネットワークを訓練する。このコードは、最初にLeNetを訓練したときのものと実質的に同じである。
trainer = d2l.Trainer(max_epochs=10, num_gpus=1)
data = d2l.FashionMNIST(batch_size=128)
model = BNLeNetScratch(lr=0.1)
model.apply_init([next(iter(data.get_dataloader(True)))[0]], d2l.init_cnn)
trainer.fit(model, data)Let’s have a look at the scale parameter
gamma and the shift parameter beta learned from the first batch normalization layer.英語のヒント
最初のバッチ正規化層から学習されたスケールのパラメータ
gammaとシフトのパラメータbetaを見てみよう。model.net[1].gamma.reshape((-1,)), model.net[1].beta.reshape((-1,))(tensor([1.4334, 1.9905, 1.8584, 2.0740, 2.0522, 1.8877], device='cuda:0',
grad_fn=<ViewBackward0>),
tensor([ 0.7354, -1.3538, -0.2567, -0.9991, -0.3028, 1.3125], device='cuda:0',
grad_fn=<ViewBackward0>))