def compute_deviation(solver_name, vfunc_id, iterations, N, fname): project = HyppopyProject() project.add_hyperparameter(name="axis_00", domain="uniform", data=[0, 1], dtype="float") project.add_hyperparameter(name="axis_01", domain="uniform", data=[0, 1], dtype="float") project.add_hyperparameter(name="axis_02", domain="uniform", data=[0, 1], dtype="float") project.add_hyperparameter(name="axis_03", domain="uniform", data=[0, 1], dtype="float") project.add_hyperparameter(name="axis_04", domain="uniform", data=[0, 1], dtype="float") vfunc = VirtualFunction() vfunc.load_default(vfunc_id) minima = vfunc.minima() def my_loss_function(data, params): return vfunc(**params) blackbox = BlackboxFunction(data=[], blackbox_func=my_loss_function) results = {} results["gt"] = [] for mini in minima: results["gt"].append(np.median(mini[0])) for iter in iterations: results[iter] = {"minima": {}, "loss": None} for i in range(vfunc.dims()): results[iter]["minima"]["axis_0{}".format(i)] = [] project.add_settings(section="solver", name="max_iterations", value=iter) project.add_settings(section="custom", name="use_solver", value=solver_name) solver = SolverPool.get(project=project) solver.blackbox = blackbox axis_minima = [] best_losses = [] for i in range(vfunc.dims()): axis_minima.append([]) for n in range(N): print("\rSolver={} iteration={} round={}".format(solver, iter, n), end="") solver.run(print_stats=False) df, best = solver.get_results() best_row = df['losses'].idxmin() best_losses.append(df['losses'][best_row]) for i in range(vfunc.dims()): tmp = df['axis_0{}'.format(i)][best_row] axis_minima[i].append(tmp) for i in range(vfunc.dims()): results[iter]["minima"]["axis_0{}".format(i)] = [np.mean(axis_minima[i]), np.std(axis_minima[i])] results[iter]["loss"] = [np.mean(best_losses), np.std(best_losses)] file = open(fname, 'wb') pickle.dump(results, file) file.close()
domain="uniform", data=[0, 1], dtype="float") project.add_hyperparameter(name="axis_02", domain="uniform", data=[0, 1], dtype="float") project.add_hyperparameter(name="axis_03", domain="uniform", data=[0, 1], dtype="float") project.add_hyperparameter(name="axis_04", domain="uniform", data=[0, 1], dtype="float") project.add_settings(section="solver", name="max_iterations", value=500) project.add_settings(section="custom", name="use_solver", value="randomsearch") plt.ion() fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(12, 8), sharey=True) plot_data = { "iterations": [], "loss": [], "axis_00": [], "axis_01": [], "axis_02": [], "axis_03": [], "axis_04": [] }
"kernel": { "domain": "categorical", "data": ["linear", "sigmoid", "poly", "rbf"], "type": "str" } }, "settings": { "solver": { "max_iterations": 500 }, "custom": {} }} # When creating a HyppopyProject instance we # pass the config dictionary to the constructor. project = HyppopyProject(config=config) # When building the project programmatically we can also use the methods # add_hyperparameter and add_settings project.clear() project.add_hyperparameter(name="C", domain="uniform", data=[0.0001, 20], dtype="float") project.add_hyperparameter(name="kernel", domain="categorical", data=["linear", "sigmoid"], dtype="str") project.add_settings(section="solver", name="max_iterations", value=500) # The custom section can be used freely project.add_settings(section="custom", name="my_var", value=10) # Settings are automatically transformed to member variables of the project class with the section as prefix if project.solver_max_iterations < 1000 and project.custom_my_var == 10: print("Project configured!")
def test_project_creation(self): config = { "hyperparameter": { "C": { "domain": "uniform", "data": [0.0001, 20], "type": "float" }, "kernel": { "domain": "categorical", "data": ["linear", "sigmoid", "poly", "rbf"], "type": "str" } }, "settings": { "solver": { "max_iterations": 300 }, "custom": { "param1": 1, "param2": 2, "function": foo } }} project = HyppopyProject() project.set_config(config) self.assertEqual(project.hyperparameter["C"]["domain"], "uniform") self.assertEqual(project.hyperparameter["C"]["data"], [0.0001, 20]) self.assertEqual(project.hyperparameter["C"]["type"], "float") self.assertEqual(project.hyperparameter["kernel"]["domain"], "categorical") self.assertEqual(project.hyperparameter["kernel"]["data"], ["linear", "sigmoid", "poly", "rbf"]) self.assertEqual(project.hyperparameter["kernel"]["type"], "str") self.assertEqual(project.solver_max_iterations, 300) self.assertEqual(project.custom_param1, 1) self.assertEqual(project.custom_param2, 2) self.assertEqual(project.custom_function(2, 3), 5) self.assertTrue(project.get_typeof("C") is float) self.assertTrue(project.get_typeof("kernel") is str) project.clear() self.assertTrue(len(project.hyperparameter) == 0) self.assertTrue(len(project.settings) == 0) self.assertTrue("solver_max_iterations" not in project.__dict__.keys()) self.assertTrue("custom_param1" not in project.__dict__.keys()) self.assertTrue("custom_param2" not in project.__dict__.keys()) self.assertTrue("custom_function" not in project.__dict__.keys()) project.add_hyperparameter(name="C", domain="uniform", data=[0.0001, 20], dtype="float") project.add_hyperparameter(name="kernel", domain="categorical", data=["linear", "sigmoid", "poly", "rbf"], dtype="str") self.assertEqual(project.hyperparameter["C"]["domain"], "uniform") self.assertEqual(project.hyperparameter["C"]["data"], [0.0001, 20]) self.assertEqual(project.hyperparameter["C"]["type"], "float") self.assertEqual(project.hyperparameter["kernel"]["domain"], "categorical") self.assertEqual(project.hyperparameter["kernel"]["data"], ["linear", "sigmoid", "poly", "rbf"]) self.assertEqual(project.hyperparameter["kernel"]["type"], "str") project.add_settings("solver", "max_iterations", 500) self.assertEqual(project.solver_max_iterations, 500) project.add_settings("solver", "max_iterations", 200) self.assertEqual(project.solver_max_iterations, 200)