第3章回帰のための線形ニューラルネットワーク
3.5.4. Training3.5.4 訓練
You might have noticed that expressing our model through high-level APIs of a deep learning framework requires fewer lines of code. We did not have to allocate parameters individually, define our loss function, or implement minibatch SGD. Once we start working with much more complex models, the advantages of the high-level API will grow considerably.
英語のヒント
深層学習フレームワークの高水準APIでモデルを表すと、必要なコードの行数が少なくて済むことに気づいたかもしれない。パラメータを個別に割り当てたり、損失関数を定義したり、ミニバッチSGDを実装したりする必要はなかった。ずっと複雑なモデルを扱い始めれば、高水準APIの利点はかなり大きくなる。
Now that we have all the basic pieces in place, the training loop itself is the same as the one we implemented from scratch. So we just call the
fit method (introduced in Section 3.2.4), which relies on the implementation of the fit_epoch method in Section 3.4, to train our model.英語のヒント
model = LinearRegression(lr=0.03)
data = d2l.SyntheticRegressionData(w=torch.tensor([2, -3.4]), b=4.2)
trainer = d2l.Trainer(max_epochs=3)
trainer.fit(model, data)Below, we compare the model parameters learned by training on finite data and the actual parameters that generated our dataset. To access parameters, we access the weights and bias of the layer that we need. As in our implementation from scratch, note that our estimated parameters are close to their true counterparts.
英語のヒント
下では、有限のデータで訓練して学習したモデルのパラメータと、データセットを生成した実際のパラメータを比べる。パラメータにアクセスするには、必要な層の重みとバイアスを取り出す。ゼロからの実装と同じく、推定したパラメータが本当の値に近いことに注意してほしい。
@d2l.add_to_class(LinearRegression) #@save
def get_w_b(self):
return (self.net.weight.data, self.net.bias.data)
w, b = model.get_w_b()
print(f'error in estimating w: {data.w - w.reshape(data.w.shape)}')
print(f'error in estimating b: {data.b - b}')error in estimating w: tensor([ 0.0094, -0.0030])
error in estimating b: tensor([0.0137])