プリンキピア

第19章ハイパーパラメータ最適化

19.2.5. Example: Optimizing the Hyperparameters of a Convolutional Neural Network19.2.5 例:畳み込みニューラルネットワークのハイパーパラメータを最適化する

We now use our new implementation of random search to optimize the batch size and learning rate of the LeNet convolutional neural network from Section 7.6. We being by defining the objective function, which will once more be validation error.
英語のヒント
では、ランダム探索の新しい実装を使って、7.6節のLeNet畳み込みニューラルネットワークのバッチの大きさと学習率を最適化しよう。まず目的関数を定義する。これは今回も検証誤差である。
def hpo_objective_lenet(learning_rate, batch_size, max_epochs=10):  #@save
    model = d2l.LeNet(lr=learning_rate, num_classes=10)
    trainer = d2l.HPOTrainer(max_epochs=max_epochs, num_gpus=1)
    data = d2l.FashionMNIST(batch_size=batch_size)
    model.apply_init([next(iter(data.get_dataloader(True)))[0]], d2l.init_cnn)
    trainer.fit(model=model, data=data)
    validation_error = trainer.validation_error()
    return validation_error
We also need to define the configuration space. Moreover, the first configuration to be evaluated is the default setting used in Section 7.6.
英語のヒント
設定空間も定義する必要がある。さらに、最初に評価する設定は、7.6節で使った既定の設定である。
config_space = {
    "learning_rate": stats.loguniform(1e-2, 1),
    "batch_size": stats.randint(32, 256),
}
initial_config = {
    "learning_rate": 0.1,
    "batch_size": 128,
}
Now we can start our random search:
英語のヒント
これで、ランダム探索を始められる。
searcher = RandomSearcher(config_space, initial_config=initial_config)
scheduler = BasicScheduler(searcher=searcher)
tuner = HPOTuner(scheduler=scheduler, objective=hpo_objective_lenet)
tuner.run(number_of_trials=5)
    error = 0.9000097513198853, runtime = 62.85189199447632
Below we plot the optimization trajectory of the incumbent to get the any-time performance of random search:
英語のヒント
以下に、ランダム探索の任意時点での性能を得るため、暫定最良解の最適化の軌跡をプロットする。
board = d2l.ProgressBoard(xlabel="time", ylabel="error")
for time_stamp, error in zip(
    tuner.cumulative_runtime, tuner.incumbent_trajectory
):
    board.draw(time_stamp, error, "random search", every_n=1)