From d013e22742f84254acdb3023d2f2c28aa0780b7c Mon Sep 17 00:00:00 2001 From: hobogalaxy Date: Wed, 11 Nov 2020 00:56:23 +0100 Subject: [PATCH] add sweep support --- config.yaml | 18 +++++++++--------- execute_sweep.py | 18 ++++++++++++++++++ main.py | 5 +---- utils/lightning_wrapper.py | 8 ++++---- 4 files changed, 32 insertions(+), 17 deletions(-) create mode 100644 execute_sweep.py diff --git a/config.yaml b/config.yaml index 4ec719a..73f1196 100644 --- a/config.yaml +++ b/config.yaml @@ -1,14 +1,14 @@ +num_of_gpus: -1 + hparams: - max_epochs: 30 - batch_size: 128 - learning_rate: 0.001 + max_epochs: 20 + batch_size: 64 + lr: 0.001 accumulate_grad_batches: 1 gradient_clip_val: 0.5 - num_of_gpus: 1 - model: - lin1_size: 128 - lin2_size: 256 - lin3_size: 128 + lin1_size: 128 + lin2_size: 256 + lin3_size: 128 resume: resume_from_ckpt: False @@ -30,7 +30,7 @@ callbacks: mode: "min" early_stop: monitor: "val_loss" - patience: 10 + patience: 100 mode: "min" printing: diff --git a/execute_sweep.py b/execute_sweep.py new file mode 100644 index 0000000..7e2dc14 --- /dev/null +++ b/execute_sweep.py @@ -0,0 +1,18 @@ +import wandb +import main + + +# Load project config +config = main.load_config() + +# Initialize wandb +wandb.init() + +# Replace project config hyperparameters with the ones loaded from wandb sweep server +sweep_hparams = wandb.Config._as_dict(wandb.config) +for key, value in sweep_hparams.items(): + if key != "_wandb": + config["hparams"][key] = value + +# Execute run +main.main(config) diff --git a/main.py b/main.py index a9c7e38..e0d5635 100644 --- a/main.py +++ b/main.py @@ -71,7 +71,7 @@ def main(config): trainer = pl.Trainer( logger=wandb_logger, callbacks=callbacks, - gpus=config["hparams"]["num_of_gpus"], + gpus=config["num_of_gpus"], max_epochs=config["hparams"]["max_epochs"], resume_from_checkpoint=config["resume"]["ckpt_path"] if config["resume"]["resume_from_ckpt"] else None, accumulate_grad_batches=config["hparams"]["accumulate_grad_batches"], @@ -95,9 +95,6 @@ def main(config): # Evaluate model on test set trainer.test() - # Close wandb run - wandb.finish() - def load_config(): with open("config.yaml", "r") as ymlfile: diff --git a/utils/lightning_wrapper.py b/utils/lightning_wrapper.py index a166954..81c5d38 100644 --- a/utils/lightning_wrapper.py +++ b/utils/lightning_wrapper.py @@ -16,10 +16,10 @@ class LitModel(pl.LightningModule): super().__init__() self.save_hyperparameters(config["hparams"]) - self.model = ModelMNISTv1(config=self.hparams.model) + self.model = ModelMNISTv2(config=self.hparams) - def forward(self, data): - return self.model(data) + def forward(self, x): + return self.model(x) # logic for a single training step def training_step(self, batch, batch_idx): @@ -64,4 +64,4 @@ class LitModel(pl.LightningModule): return loss def configure_optimizers(self): - return torch.optim.Adam(self.parameters(), lr=self.hparams.learning_rate) + return torch.optim.Adam(self.parameters(), lr=self.hparams.lr)