def test_interruption(self) -> None: ax_client = AxClient() ax_client.create_experiment( name="test", parameters=[ # pyre-fixme[6]: expected union that should include {"name": "x1", "type": "range", "bounds": [-5.0, 10.0]}, {"name": "x2", "type": "range", "bounds": [0.0, 15.0]}, ], objective_name="branin", minimize=True, ) for i in range(6): parameterization, trial_index = ax_client.get_next_trial() self.assertFalse( # There should be non-complete trials. all(t.status.is_terminal for t in ax_client.experiment.trials.values()) ) x1, x2 = parameterization.get("x1"), parameterization.get("x2") ax_client.complete_trial( trial_index, raw_data=checked_cast( float, branin(checked_cast(float, x1), checked_cast(float, x2)) ), ) old_client = ax_client serialized = ax_client.to_json_snapshot() ax_client = AxClient.from_json_snapshot(serialized) self.assertEqual(len(ax_client.experiment.trials.keys()), i + 1) self.assertIsNot(ax_client, old_client) self.assertTrue( # There should be no non-complete trials. all(t.status.is_terminal for t in ax_client.experiment.trials.values()) )
def test_init_position_saved(self): ax_client = AxClient(random_seed=239) ax_client.create_experiment( parameters=[ {"name": "x1", "type": "range", "bounds": [-5.0, 10.0]}, {"name": "x2", "type": "range", "bounds": [0.0, 15.0]}, ], name="sobol_init_position_test", ) for _ in range(4): # For each generated trial, snapshot the client before generating it, # then recreate client, regenerate the trial and compare the trial # generated before and after snapshotting. If the state of Sobol is # recorded correctly, the newly generated trial will be the same as # the one generated before the snapshotting. serialized = ax_client.to_json_snapshot() params, idx = ax_client.get_next_trial() ax_client = AxClient.from_json_snapshot(serialized) with self.subTest(ax=ax_client, params=params, idx=idx): new_params, new_idx = ax_client.get_next_trial() self.assertEqual(params, new_params) self.assertEqual(idx, new_idx) self.assertEqual( ax_client.experiment.trials[idx]._generator_run._model_kwargs[ "init_position" ], idx + 1, ) ax_client.complete_trial(idx, branin(params.get("x1"), params.get("x2")))
def get_client(self): """Instantiate a new AxClient from previous snapshot""" if self._client_state is not None: # Copy client state because `from_json_snapshot` modifies it... client = AxClient.from_json_snapshot( copy.deepcopy(self._client_state)) else: client = AxClient( random_seed=self.seed, enforce_sequential_optimization=False, verbose_logging=False, ) client.create_experiment( parameters=orion_space_to_axoptimizer_space(self.space), choose_generation_strategy_kwargs={ "num_initialization_trials": self.n_initial_trials, "max_parallelism_override": self.max_trials, }, objectives={ "objective": ObjectiveProperties(minimize=True), **{ o: ObjectiveProperties(minimize=True) for o in self.extra_objectives }, }, outcome_constraints=self.constraints, ) yield client self._client_state = client.to_json_snapshot()
def test_unnamed_experiment_snapshot(self): ax_client = AxClient(random_seed=239) ax_client.create_experiment( parameters=[ {"name": "x", "type": "range", "bounds": [-5.0, 10.0]}, {"name": "y", "type": "range", "bounds": [0.0, 15.0]}, ] ) serialized = ax_client.to_json_snapshot() ax_client = AxClient.from_json_snapshot(serialized) self.assertIsNone(ax_client.experiment._name)
print("results", results) mean = np.mean(results) SEM = np.std(results) / np.sqrt(len(results)) # pool.close() # no more tasks # pool.join() # wrap up current tasks return {"objective": (mean, SEM)} # Initialize client ax_client = AxClient() try: run_mode = "frozen_convolution_pretrained_relu" with open(f"hyperparameters_{run_mode}.pl", "rb") as handle: hyper = pickle.load(handle) v = hyper["axclient"].copy() ax_client = ax_client.from_json_snapshot(v) hyper["best_params"] except: ax_client.create_experiment(parameters=[{ "name": "batch_size", "type": "range", "bounds": [1, 5000], "value_type": "int" }, { "name": "train_size", "type": "range", "bounds": [100, 5000], "value_type": "int" }, { "name": "n_epochs", "type": "range",
import pickle run_mode = "frozen_convolution_no_center_relu" with open(f"hyperparameters_{run_mode}.pl", "rb") as handle: hyper = pickle.load(handle) from ax import RangeParameter, ParameterType from ax.service.ax_client import AxClient from ax.plot.contour import plot_contour from ax.plot.trace import optimization_trace_single_method from ax.service.managed_loop import optimize from ax.utils.notebook.plotting import render, init_notebook_plotting from ax.utils.tutorials.cnn_utils import load_mnist, train, evaluate # Initialize client ax = AxClient() ax = ax.from_json_snapshot(hyper["axclient"]) print(ax.get_trial_parameters(10))