Example #1
0
    def testing_phase(self) -> None:
        """
        Evaluates model using test set data.
        """
        self.test_dataloader = self.get_dataloader(self.test_h5_path,
                                                   "test set")
        self.load_training_set_properties()
        self.restart_epoch = util.get_restart_epoch()

        print(
            f"* Loading model from previous saved state (Epoch {self.restart_epoch}).",
            flush=True)
        model_path = self.constants.job_dir + f"model_restart_{self.restart_epoch}.pth"
        self.model = self.create_model()
        try:
            # for loading models created using GraphINVENT v1.0 (will raise an exception
            # if model was created with GraphINVENT v2.0)
            self.model.state_dict = torch.load(model_path).state_dict()
        except AttributeError:
            # for loading models created using GraphINVENT v2.0
            self.model.load_state_dict(torch.load(model_path))

        self.model.eval()
        with torch.no_grad():
            self.generate_graphs(n_samples=self.constants.n_samples)

            print("* Evaluating model.", flush=True)
            self.analyzer.model = self.model
            self.analyzer.evaluate_model(nll_per_action=self.nll_per_action)

        self.print_time_elapsed()
Example #2
0
    def testing_phase(self):
        """ Evaluates model using test set data.
        """
        self.test_dataloader = self.get_dataloader(self.test_h5_path,
                                                   "test set")
        self.get_ts_properties()

        self.restart_epoch = util.get_restart_epoch()
        print(
            f"* Loading model from previous saved state (Epoch {self.restart_epoch}).",
            flush=True)
        self.model = torch.load(self.C.job_dir +
                                f"model_restart_{self.restart_epoch}.pth")

        self.model.eval()
        with torch.no_grad():
            self.generate_graphs(n_samples=self.C.n_samples)

            print("* Evaluating model.", flush=True)
            anal.evaluate_model(valid_dataloader=self.test_dataloader,
                                train_dataloader=self.train_dataloader,
                                nll_per_action=self.nll_per_action,
                                model=self.model)

        self.print_time_elapsed()
Example #3
0
    def define_model_and_optimizer(self) -> Tuple[time.time, time.time]:
        """
        Defines the model, optimizer, and scheduler.
        """
        print("* Defining model.", flush=True)
        job_dir = self.constants.job_dir

        self.model = self.create_model()

        if self.constants.restart:
            print("-- Loading model from previous saved state.", flush=True)
            self.restart_epoch = util.get_restart_epoch()

            try:
                # for loading models created using GraphINVENT v1.0 (will raise an exception
                # if model was created with GraphINVENT v2.0)
                self.model.state_dict = torch.load(
                    f"{job_dir}model_restart_{self.restart_epoch}.pth"
                ).state_dict()
            except AttributeError:
                # for loading models created using GraphINVENT v2.0
                self.model.load_state_dict(
                    torch.load(
                        f"{job_dir}model_restart_{self.restart_epoch}.pth"))

            print(
                f"-- Backing up as {job_dir}model_restart_{self.restart_epoch}_restarted.pth.",
                flush=True)
            shutil.copyfile(
                f"{job_dir}model_restart_{self.restart_epoch}.pth",
                f"{job_dir}model_restart_{self.restart_epoch}_restarted.pth",
            )
        else:
            self.restart_epoch = 0

        start_epoch = self.restart_epoch + 1
        end_epoch = start_epoch + self.constants.epochs

        print("-- Defining optimizer.", flush=True)
        self.optimizer = torch.optim.Adam(params=self.model.parameters(),
                                          lr=self.constants.init_lr)
        print("-- Defining scheduler.", flush=True)
        max_allowable_lr = self.constants.max_rel_lr * self.constants.init_lr
        n_batches = len(self.train_dataloader)
        self.scheduler = torch.optim.lr_scheduler.OneCycleLR(
            optimizer=self.optimizer,
            max_lr=max_allowable_lr,
            steps_per_epoch=n_batches,
            epochs=self.constants.epochs)

        return start_epoch, end_epoch
Example #4
0
    def define_model_and_optimizer(self):
        """ Defines the model (`self.model`) and the optimizer (`self.optimizer`).
        """
        print("* Defining model and optimizer.", flush=True)
        job_dir = self.C.job_dir

        if self.C.restart:
            print("-- Loading model from previous saved state.", flush=True)
            self.restart_epoch = util.get_restart_epoch()
            self.model = torch.load(
                f"{job_dir}model_restart_{self.restart_epoch}.pth")

            print(
                f"-- Backing up as "
                f"{job_dir}model_restart_{self.restart_epoch}_restarted.pth.",
                flush=True,
            )
            shutil.copyfile(
                f"{job_dir}model_restart_{self.restart_epoch}.pth",
                f"{job_dir}model_restart_{self.restart_epoch}_restarted.pth",
            )

        else:
            print("-- Initializing model from scratch.", flush=True)
            self.model = models.initialize_model()

            self.restart_epoch = 0

        start_epoch = self.restart_epoch + 1
        end_epoch = start_epoch + self.C.epochs

        print("-- Defining optimizer.", flush=True)
        self.optimizer = torch.optim.Adam(
            params=self.model.parameters(),
            lr=self.C.init_lr,
            weight_decay=self.C.weight_decay,
        )

        return start_epoch, end_epoch