プリンキピア

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

19.3.1. Objective Function19.3.1 目的関数

First, we have to define a new objective function such that it now returns the performance back to Syne Tune via the report callback.
英語のヒント
まず、reportコールバックを通じて性能をSyne Tuneに返すような、新しい目的関数を定義しなければならない。
def hpo_objective_lenet_synetune(learning_rate, batch_size, max_epochs):
    from syne_tune import Reporter
    from d2l import torch as d2l

    model = d2l.LeNet(lr=learning_rate, num_classes=10)
    trainer = d2l.HPOTrainer(max_epochs=1, num_gpus=1)
    data = d2l.FashionMNIST(batch_size=batch_size)
    model.apply_init([next(iter(data.get_dataloader(True)))[0]], d2l.init_cnn)
    report = Reporter()
    for epoch in range(1, max_epochs + 1):
        if epoch == 1:
            # Initialize the state of Trainer
            trainer.fit(model=model, data=data)
        else:
            trainer.fit_epoch()
        validation_error = trainer.validation_error().cpu().detach().numpy()
        report(epoch=epoch, validation_error=float(validation_error))
Note that the PythonBackend of Syne Tune requires dependencies to be imported inside the function definition.
英語のヒント
Syne TuneのPythonBackendは、依存するものを関数の定義の中でインポートする必要があることに注意しよう。