Esempio n. 1
0
 def objective(trial):
     lr = trial.suggest_loguniform("lr", 1e-3, 1e-1)
     optimizer = torch.optim.Adam(model.parameters(), lr=lr)
     criterion = nn.CrossEntropyLoss()
     runner = dl.SupervisedRunner()
     runner.train(
         model=model,
         loaders=loaders,
         criterion=criterion,
         optimizer=optimizer,
         callbacks={
             "optuna":
             dl.OptunaPruningCallback(loader_key="valid",
                                      metric_key="loss",
                                      minimize=True,
                                      trial=trial),
             "accuracy":
             dl.AccuracyCallback(num_classes=10,
                                 input_key="logits",
                                 target_key="targets"),
         },
         num_epochs=2,
         valid_metric="accuracy01",
         minimize_valid_metric=False,
     )
     return trial.best_score
Esempio n. 2
0
        def objective(trial):
            lr = trial.suggest_loguniform("lr", 1e-3, 1e-1)
            num_hidden = int(trial.suggest_loguniform("num_hidden", 32, 128))

            loaders = {
                "train":
                DataLoader(
                    MNIST(os.getcwd(),
                          train=False,
                          download=True,
                          transform=ToTensor()),
                    batch_size=32,
                ),
                "valid":
                DataLoader(
                    MNIST(os.getcwd(),
                          train=False,
                          download=True,
                          transform=ToTensor()),
                    batch_size=32,
                ),
            }
            model = nn.Sequential(nn.Flatten(), nn.Linear(784, num_hidden),
                                  nn.ReLU(), nn.Linear(num_hidden, 10))
            optimizer = torch.optim.Adam(model.parameters(), lr=lr)
            criterion = nn.CrossEntropyLoss()

            runner = dl.SupervisedRunner(input_key="features",
                                         output_key="logits",
                                         target_key="targets")
            runner.train(
                engine=engine or dl.DeviceEngine(device),
                model=model,
                criterion=criterion,
                optimizer=optimizer,
                loaders=loaders,
                callbacks={
                    "optuna":
                    dl.OptunaPruningCallback(loader_key="valid",
                                             metric_key="accuracy01",
                                             minimize=False,
                                             trial=trial),
                    "accuracy":
                    dl.AccuracyCallback(input_key="logits",
                                        target_key="targets",
                                        num_classes=10),
                },
                num_epochs=2,
            )
            score = runner.callbacks["optuna"].best_score
            return score