プリンキピア

第5章多層パーセプトロン

5.6.3. Concise Implementation5.6.3 簡潔な実装

With high-level APIs, all we need to do is add a Dropout layer after each fully connected layer, passing in the dropout probability as the only argument to its constructor. During training, the Dropout layer will randomly drop out outputs of the previous layer (or equivalently, the inputs to the subsequent layer) according to the specified dropout probability. When not in training mode, the Dropout layer simply passes the data through during testing.
英語のヒント
高水準のAPIを使えば、各全結合層の後にDropout層を加え、そのコンストラクタへのただ一つの引数としてドロップアウトの確率を渡すだけでよい。訓練中は、Dropout層は指定したドロップアウトの確率に従って、前の層の出力(あるいは同じことだが、次の層への入力)を無作為に脱落させる。訓練モードでないとき、つまりテスト中は、Dropout層は単にデータをそのまま通す。
class DropoutMLP(d2l.Classifier):
    def __init__(self, num_outputs, num_hiddens_1, num_hiddens_2,
                 dropout_1, dropout_2, lr):
        super().__init__()
        self.save_hyperparameters()
        self.net = nn.Sequential(
            nn.Flatten(), nn.LazyLinear(num_hiddens_1), nn.ReLU(),
            nn.Dropout(dropout_1), nn.LazyLinear(num_hiddens_2), nn.ReLU(),
            nn.Dropout(dropout_2), nn.LazyLinear(num_outputs))
Next, we train the model.
英語のヒント
次に、モデルを訓練する。
model = DropoutMLP(**hparams)
trainer.fit(model, data)