プリンキピア

第4章分類のための線形ニューラルネットワーク

4.3.1. The Classifier Class4.3.1 Classifierクラス

We define the Classifier class below. In the validation_step we report both the loss value and the classification accuracy on a validation batch. We draw an update for every num_val_batches batches. This has the benefit of generating the averaged loss and accuracy on the whole validation data. These average numbers are not exactly correct if the final batch contains fewer examples, but we ignore this minor difference to keep the code simple.
英語のヒント
下でClassifierクラスを定義する。validation_stepでは、検証用のバッチについて、損失の値と分類の正解率の両方を報告する。num_val_batches個のバッチごとに更新を描く。こうすると、検証データ全体での平均の損失と正解率が得られるという利点がある。最後のバッチに含まれる事例が少ないと、これらの平均値は厳密には正しくないが、コードを簡単に保つため、この小さな違いは無視する。
class Classifier(d2l.Module):  #@save
    """The base class of classification models."""
    def validation_step(self, batch):
        Y_hat = self(*batch[:-1])
        self.plot('loss', self.loss(Y_hat, batch[-1]), train=False)
        self.plot('acc', self.accuracy(Y_hat, batch[-1]), train=False)
By default we use a stochastic gradient descent optimizer, operating on minibatches, just as we did in the context of linear regression.
英語のヒント
既定では、線形回帰の文脈でしたのとちょうど同じように、ミニバッチ上で働く確率的勾配降下法のオプティマイザを使う。
@d2l.add_to_class(d2l.Module)  #@save
def configure_optimizers(self):
    return torch.optim.SGD(self.parameters(), lr=self.lr)