def __call__(self, x): """This is the objective function to be minimized with Bayesian optimization. This function changes the training hyperparameters as provided by x and trains a model. This model is then evaluated. The selected optimization criteria is returned by the evaluation function, and will be provided as scalar output. Args: x(np.array): 2-D array with a where every row is a list of options. Returns: np.array: 2-D array with the cost of the objective per row. """ self.opt.script = "generative" y = np.zeros((x.shape[0], 1)) for i in range(x.shape[0]): # Overwrite default options with options provided by the bayesian optimization self.opt = self._set_options(self.opt, x, i) self.opt.mode = 'train' print_flags(self.opt) train(self.opt) self.opt.mode = 'test' y[i], stats = test(self.opt) self._store_stats(stats, vars(self.opt), y[i]) if self.opt.two_test: self.opt.use_test_set = 1 res, stats = test(self.opt) self._store_stats(stats, vars(self.opt), res) self.opt.use_test_set = 0 self.iter += 1 return y
def main(): opt, parser = parse_arguments() opt = predefined(opt) print_flags(opt) if not osp.isdir(opt.out_folder): os.makedirs(opt.out_folder) if opt.script == 'generative': if opt.mode == 'train': train(opt) elif opt.mode == 'test': test(opt) elif opt.mode == 'generate': generate_data(opt) elif opt.mode == 'novelty': novelty(opt) elif opt.mode == 'qualitative': qualitative(opt) else: raise UnknownArgumentError( "--mode not recognized, please choose: [train, test, generate, qualitative, novelty]." ) elif opt.script == 'bayesopt': optimize_bayesian(opt, parser) elif opt.script == 'grid': run_grid(opt, parser) else: raise UnknownArgumentError( "--script not recognized, please choose: [generative, bayesopt, grid]." )
def re_test(self, run_ids): self.opt.script = "generative" self.opt.mode = 'test' for run in run_ids: self.iter = run self.opt.save_suffix = "{}_{}".format(self.opt.bayes_mode, self.iter) y, stats = test(self.opt) self._store_stats(stats, vars(self.opt), y) if self.opt.two_test: self.opt.use_test_set = 1 res, stats = test(self.opt) self._store_stats(stats, vars(self.opt), res) self.opt.use_test_set = 0