def test_optimizer_Thompson(self): hyperparam_config = [ HyperParameter(0, 5, 1), HyperParameter(0, 5, 1) ] tested_points = np.array([ [0, 0], [0, 4], [2, 2], [4, 4], [4, 0], [1, 1] ], dtype=float) values = np.array([ 2, 2, 2, 2, 2, 0 ], dtype=float) gp_settings = dict( kernel=rbf, kernel_params=(0.1, 0.2), noise=1e-6 ) N = 200 draws = np.empty((N, 2)) for idx in range(N): draws[idx, :] = propose_points(tested_points, values, hyperparam_config, 1, acquisition_function="Thompson", gp_settings=gp_settings) empiricammean = np.mean(draws, axis=0) np.testing.assert_allclose(empiricammean, np.array([1, 1]), atol=2e-1)
def test_optimizer_UCB(self): hyperparam_config = [ HyperParameter(0, 4, 1), HyperParameter(0, 4, 1) ] tested_points = np.array([ [0, 0], [0, 4], [4, 0], [4, 4], [1, 2], [2, 1] ], dtype=float) values = np.array([ 2, 2, 2, 2, 0, 0 ], dtype=float) gp_settings = dict( kernel=rbf, kernel_params=(0.1, 0.2), noise=1e-6 ) draw = propose_points(tested_points, values, hyperparam_config, 1, acquisition_function="UCB", gp_settings=gp_settings) np.testing.assert_allclose(draw.squeeze(), np.array([2, 2]), atol=2e-1)
def test_get_hypergrid(self): hyperparam_config = [HyperParameter(0, 4, 1), HyperParameter(0, 5, 2)] grid = get_hypergrid(hyperparam_config) target = np.array([[0., 0.], [0., 2.], [0., 4.], [1., 0.], [1., 2.], [1., 4.], [2., 0.], [2., 2.], [2., 4.], [3., 0.], [3., 2.], [3., 4.], [4., 0.], [4., 2.], [4., 4.]]) np.testing.assert_almost_equal(grid, target, decimal=5)
def main(): # ------ Define hyperparameters with bounds and stepsize hyperparam_config = [ HyperParameter(0, 3, 1), HyperParameter(0, 5, 2) ] # ------ Define previously tested points and function values at those points tested_points = np.array([ [0, 2], [2, 0], [1, 4] ], dtype=float) values = np.array([1, 2, 3], dtype=float) # ------ Decide the number of points to be proposed each iteration num_points = 4 # ------ Define the GP parameters including the kernel and its parameters gp_settings = dict( kernel=rbf, kernel_params=(0.1, 1), noise=1e-6 ) # ------ Ask the optimizer for new points new_points = propose_points(tested_points, values, hyperparam_config, num_points=num_points, seed=123, acquisition_function="Thompson", gp_settings=gp_settings) # ------ pritn the proposed points print("Proposed points:") print(new_points)
def test_rescale_hypergrid(self): hyperparam_config = [HyperParameter(0, 4, 1), HyperParameter(0, 5, 2)] hyperparam_grid = np.array([[1, 2], [2, 3], [3, 4], [4, 5]], dtype=float) result = rescale_hypergrid(hyperparam_grid, hyperparam_config) target = np.array([[0.25, 0.4], [0.5, 0.6], [0.75, 0.8], [1., 1.]]) np.testing.assert_almost_equal(result, target, decimal=5)
def test_prune_hypergrid(self): hyperparam_config = [HyperParameter(0, 4, 1), HyperParameter(0, 5, 2)] tested_points = np.array([[0, 4.0], [1, 0], [3, 2], [4, 2]]) fullgrid = get_hypergrid(hyperparam_config) grid = prune_hypergrid(fullgrid, tested_points=tested_points) target = np.array([[0., 0.], [0., 2.], [1., 2.], [1., 4.], [2., 0.], [2., 2.], [2., 4.], [3., 0.], [3., 4.], [4., 0.], [4., 4.]]) np.testing.assert_almost_equal(grid, target, decimal=5)
def main(): # ------ Define hyperparameters with bounds and stepsize hyperparam_config = [ HyperParameter(-5, 5, 1), HyperParameter(-5, 5, 1) ] # ------ Find the minimum and the value at the minumum best_point, best_value = minimize_function(funct, hyperparam_config, extra_function_args=(), tolerance=1e-2, max_iterations=100, seed=123) print("Best point {point} with the value of {value}".format(point=best_point, value=best_value))
def main(): # ------ Define hyperparameters with bounds and stepsize hyperparam_config = [ HyperParameter(0, 4, 1), HyperParameter(-2, 2, 0.25) ] # ------ Define previously tested points and function values at those points tested_points = np.empty((0, len(hyperparam_config))) tested_values = np.empty((0, 1)) # ------ Decide the number of points to be proposed each iteration, # ------ here one is chosen to use UCB acquisition function num_points = 1 # ------ Define the GP parameters including the kernel and its parameters gp_settings = dict( kernel=rbf, kernel_params=(0.1, 1), noise=1e-6 ) old_minimum = np.inf stopping_counter = 0 tolerance = 1e-2 for idx in range(MAXIMUM_ITER): # ------ Ask the optimizer for new points new_points = propose_points(tested_points, tested_values, hyperparam_config, num_points=num_points, acquisition_function="UCB", gp_settings=gp_settings) new_values = function2minimize(new_points) new_values = np.expand_dims(new_values, 1) tested_points = np.concatenate((tested_points, new_points), axis=0) tested_values = np.concatenate((tested_values, new_values), axis=0) # ------ Compute the stopping criterion if np.abs(old_minimum - np.min(tested_values)) < tolerance: stopping_counter += 1 else: stopping_counter = 0 if stopping_counter >= 2: break old_minimum = np.min(tested_values) # ------ pritn the proposed points best_point = np.argmin(tested_values, axis=0).item() print(tested_points[best_point], tested_values[best_point])
def test_minimize(self): def funct(x): return np.sum(np.square(x)) hyperparam_config = [ HyperParameter(-5, 5, 1), HyperParameter(-5, 5, 1) ] best_point, best_value = minimize_function(funct, hyperparam_config, extra_function_args=(), tolerance=1e-2, seed=123) np.testing.assert_allclose(best_point, np.array([0, 0]), atol=1e-5) np.testing.assert_allclose(best_value, np.array([0]), atol=1e-5)
def test_get_new_unique_points_UCB(self): hyperparam_config = [HyperParameter(0, 3, 1), HyperParameter(0, 5, 2)] grid = get_hypergrid(hyperparam_config) known_points = np.array([[0, 0]]) values = np.array([-2]) gp_settings = dict(kernel=rbf, kernel_params=(0.02, 0.25), noise=1e-6) gp = GaussianProcessRegression(gp_settings["kernel"], *gp_settings["kernel_params"], noise=gp_settings["noise"]) gp.fit(known_points, values, grid) ucb_tradeoff = 0.5 new_points = _get_new_unique_point_UCB(grid, gp, ucb_tradeoff) target = np.array([[0., 0.]]) np.testing.assert_allclose(new_points, target, atol=1e-5)
def test_get_new_unique_points_Thompson(self): hyperparam_config = [HyperParameter(0, 3, 1), HyperParameter(0, 5, 2)] grid = get_hypergrid(hyperparam_config) num_points = 5 gp_settings = dict(kernel=rbf, kernel_params=(0.02, 0.25), noise=1e-6) gp = GaussianProcessRegression(gp_settings["kernel"], *gp_settings["kernel_params"], noise=gp_settings["noise"]) gp.initialize(grid) new_points = _get_new_unique_points_Thompson(grid, gp, num_points) for i in range(len(new_points)): for j in range(i + 1, len(new_points)): self.assertFalse(np.array_equal(new_points[i], new_points[j]))
def main(): # ------ Define hyperparameters with bounds and stepsize hyperparam_config = [HyperParameter(-5, 5, 1), HyperParameter(-5, 5, 1)] # ------ Define the GP parameters including the kernel and its parameters gp_settings = dict(kernel=rbf, kernel_params=(0.1, 1), noise=1e-6) # ------ Find the minimum and the value at the minumum best_point, best_value = minimize_function(funct, hyperparam_config, extra_function_args=(), tolerance=1e-2, max_iterations=100, seed=123, acquisition_function="UCB", gp_settings=gp_settings) print("Best point {point} with the value of {value}".format( point=best_point, value=best_value))
def test_get_new_unique_point(self): hyperparam_config = [ HyperParameter(0, 4, 1), HyperParameter(0, 5, 2) ] grid = get_hypergrid(hyperparam_config) previous_points = np.array([ [0, 4], [1, 0], [3, 2], [4, 2] ]) for idx in range(100): self.assertTrue(not_in_array(_get_new_unique_point(previous_points, grid, 100), previous_points)) # Check error with self.assertRaises(OptimizerError): _get_new_unique_point(previous_points, previous_points)
def test_get_new_unique_point_Thompson(self): hyperparam_config = [HyperParameter(0, 3, 1), HyperParameter(0, 5, 2)] grid = get_hypergrid(hyperparam_config) previous_points = np.array([[0, 4], [1, 0], [3, 2], [4, 2]]) gp_settings = dict(kernel=rbf, kernel_params=(0.02, 0.25), noise=1e-6) gp = GaussianProcessRegression(gp_settings["kernel"], *gp_settings["kernel_params"], noise=gp_settings["noise"]) gp.initialize(grid) for idx in range(100): self.assertTrue( not_in_array( _get_single_point_Thompson(previous_points, grid, gp, 100), previous_points)) # Check error gp.initialize(previous_points) with self.assertRaises(OptimizerError): _get_single_point_Thompson(previous_points, previous_points, gp)
def main(): # ------ Define hyperparameters with bounds and stepsize hyperparam_config = [HyperParameter(0, 3, 1), HyperParameter(0, 5, 2)] # ------ Define previously tested points and function values at those points # ------ Here, valus do not make a difference as the random optimizer draws from a uniform distributon tested_points = np.array([[0, 2], [2, 0], [1, 4]], dtype=float) values = None # ------ Decide the number of points to be proposed each iteration num_points = 4 # ------ Ask the optimizer for new points new_points = propose_points(tested_points, values, hyperparam_config, num_points=num_points, seed=123) # ------ pritn the proposed points print(result)
def main(): # ------ Define hyperparameters with bounds and stepsize hyperparam_config = [HyperParameter(0, 4, 1), HyperParameter(-2, 2, 0.25)] # ------ Define previously tested points and function values at those points tested_points = np.empty((0, len(hyperparam_config))) tested_values = np.empty((0, 1)) # ------ Decide the number of points to be proposed each iteration num_points = 4 old_minimum = np.inf stopping_counter = 0 tolerance = 1e-2 for idx in range(MAXIMUM_ITER): # ------ Ask the optimizer for new points new_points = propose_points(tested_points, tested_values, hyperparam_config, num_points=num_points) new_values = function2minimize(new_points) new_values = np.expand_dims(new_values, 1) tested_points = np.concatenate((tested_points, new_points), axis=0) tested_values = np.concatenate((tested_values, new_values), axis=0) # ------ Compute the stopping criterion if np.abs(old_minimum - np.min(tested_values)) < tolerance: stopping_counter += 1 else: stopping_counter = 0 if stopping_counter >= 5: break old_minimum = np.min(tested_values) # ------ pritn the proposed points best_point = np.argmin(tested_values, axis=0).item() print(tested_points[best_point], tested_values[best_point])
def test_propose_points(self): hyperparam_config = [ HyperParameter(0, 4, 1), HyperParameter(0, 5, 2) ] tested_points = np.array([ [0, 4], [1, 0], [3, 2], [4, 2] ]) target = np.array([[1., 2.], [2., 4.], [0., 2.], [1., 4.], [4., 4.], [4., 0.], [0., 0.], [2., 0.], [3., 0.], [3., 4.], [2., 2.]]) result = propose_points(tested_points, None, hyperparam_config, 11, seed=123) # print(repr(result)) np.testing.assert_almost_equal(result, target, decimal=5) target = np.array([[1., 2.], [2., 4.], [0., 2.], [1., 4.], [4., 4.], [4., 0.]]) result = propose_points(tested_points, None, hyperparam_config, 6, seed=123) # print(repr(result)) np.testing.assert_almost_equal(result, target, decimal=5) # Check error with self.assertRaises(OptimizerError): propose_points(tested_points, None, hyperparam_config, 20, seed=123)
def test_propose_points_Thompson(self): hyperparam_config = [ HyperParameter(0, 3, 1), HyperParameter(0, 5, 2) ] tested_points = np.array([ [0, 2], [2, 0], [1, 4], ], dtype=float) target = np.array([[2., 4.], [0., 0.], [3., 0.], [1., 2.], [3., 2.], [1., 0.], [0., 4.], [3., 4.], [2., 2.]]) values = np.array([1, 2, 3], dtype=float) result = propose_points(tested_points, values, hyperparam_config, 9, acquisition_function="Thompson", seed=123) # print(repr(result)) np.testing.assert_almost_equal(result, target, decimal=5) target = np.array([[2., 4.], [0., 0.], [3., 0.], [1., 2.]]) values = np.array([1, 2, 3], dtype=float) result = propose_points(tested_points, values, hyperparam_config, 4, seed=123) # print(repr(result)) np.testing.assert_almost_equal(result, target, decimal=5) # Check error with self.assertRaises(OptimizerError): propose_points(tested_points, values, hyperparam_config, 20, seed=123)