def test(self): X_lower = np.array([0]) X_upper = np.array([1]) X = init_random_uniform(X_lower, X_upper, 10) Y = np.sin(X) kernel = george.kernels.Matern52Kernel(np.ones([1]), ndim=1) prior = TophatPrior(-2, 2) model = GaussianProcess(kernel, prior=prior) model.train(X, Y) rec = BestObservation(model, X_lower, X_upper) startpoints = init_random_uniform(X_lower, X_upper, 10) inc, inc_val = rec.estimate_incumbent(startpoints) # Check shapes assert len(inc.shape) == 2 assert inc.shape[0] == 1 assert inc.shape[1] == X_lower.shape[0] assert len(inc_val.shape) == 2 assert inc_val.shape[0] == 1 assert inc_val.shape[1] == 1 # Check if incumbent is in the bounds assert not np.any([np.any(inc[:, i] < X_lower[i]) for i in range(X_lower.shape[0])]) assert not np.any([np.any(inc[:, i] > X_upper[i]) for i in range(X_upper.shape[0])])
def test_cmaes(self): X_lower = np.array([0, 0]) X_upper = np.array([1, 1]) X = init_random_uniform(X_lower, X_upper, 10) Y = np.sin(X[:, 0])[:, np.newaxis] kernel = george.kernels.Matern52Kernel(np.ones([1, 1]), ndim=2) prior = TophatPrior(-2, 2) model = GaussianProcess(kernel, prior=prior) model.train(X, Y) rec = PosteriorMeanOptimization(model, X_lower, X_upper, method="cmaes") startpoints = init_random_uniform(X_lower, X_upper, 10) inc, inc_val = rec.estimate_incumbent(startpoints) # Check shapes assert len(inc.shape) == 2 assert inc.shape[0] == 1 assert inc.shape[1] == X_lower.shape[0] assert len(inc_val.shape) == 2 assert inc_val.shape[0] == 1 assert inc_val.shape[1] == 1 # Check if incumbent is in the bounds assert not np.any([np.any(inc[:, i] < X_lower[i]) for i in range(X_lower.shape[0])]) assert not np.any([np.any(inc[:, i] > X_upper[i]) for i in range(X_upper.shape[0])])
def test_cmaes(self): X_lower = np.array([0, 0]) X_upper = np.array([1, 1]) X = init_random_uniform(X_lower, X_upper, 10) Y = np.sin(X[:, 0])[:, np.newaxis] kernel = george.kernels.Matern52Kernel(np.ones([1, 1]), ndim=2) prior = TophatPrior(-2, 2) model = GaussianProcess(kernel, prior=prior) model.train(X, Y) rec = PosteriorMeanAndStdOptimization(model, X_lower, X_upper, method="cmaes") startpoints = init_random_uniform(X_lower, X_upper, 10) inc, inc_val = rec.estimate_incumbent(startpoints) # Check shapes assert len(inc.shape) == 2 assert inc.shape[0] == 1 assert inc.shape[1] == X_lower.shape[0] assert len(inc_val.shape) == 2 assert inc_val.shape[0] == 1 assert inc_val.shape[1] == 1 # Check if incumbent is in the bounds assert not np.any([np.any(inc[:, i] < X_lower[i]) for i in range(X_lower.shape[0])]) assert not np.any([np.any(inc[:, i] > X_upper[i]) for i in range(X_upper.shape[0])])
def suggest_configuration(self): if self.X is None and self.Y is None: new_x = init_random_uniform(self.X_lower, self.X_upper, N=1, rng=self.rng) elif self.X.shape[0] == 1: # We need at least 2 data points to train a GP Xopt = init_random_uniform(self.X_lower, self.X_upper, N=1, rng=self.rng) else: prior = DNGOPrior() model = DNGO(batch_size=100, num_epochs=20000, learning_rate=0.1, momentum=0.9, l2=1e-16, adapt_epoch=5000, n_hypers=20, prior=prior, do_optimize=True, do_mcmc=True) #acquisition_func = EI(model, task.X_lower, task.X_upper) lo = np.ones([model.n_units_3]) * -1 up = np.ones([model.n_units_3]) ei = LogEI(model, lo, up) acquisition_func = IntegratedAcquisition(model, ei, self.X_lower, self.X_upper) maximizer = Direct(acquisition_func, self.X_lower, self.X_upper) model.train(self.X, self.Y) acquisition_func.update(model) new_x = maximizer.maximize() # Map from [0, 1]^D space back to original space next_config = Configuration(self.config_space, vector=new_x[0, :]) # Transform to sacred configuration result = configspace_config_to_sacred(next_config) return result
def suggest_configuration(self): if self.X is None and self.y is None: new_x = init_random_uniform(self.lower, self.upper, n_points=1, rng=self.rng)[0, :] elif self.X.shape[0] == 1: # We need at least 2 data points to train a GP new_x = init_random_uniform(self.lower, self.upper, n_points=1, rng=self.rng)[0, :] else: cov_amp = 1 n_dims = self.lower.shape[0] initial_ls = np.ones([n_dims]) exp_kernel = george.kernels.Matern52Kernel(initial_ls, ndim=n_dims) kernel = cov_amp * exp_kernel prior = DefaultPrior(len(kernel) + 1) model = GaussianProcessMCMC(kernel, prior=prior, n_hypers=self.n_hypers, chain_length=self.chain_length, burnin_steps=self.burnin, normalize_input=False, normalize_output=True, rng=self.rng, lower=self.lower, upper=self.upper) a = LogEI(model) acquisition_func = MarginalizationGPMCMC(a) max_func = Direct(acquisition_func, self.lower, self.upper, verbose=False) model.train(self.X, self.y) acquisition_func.update(model) new_x = max_func.maximize() next_config = Configuration(self.config_space, vector=new_x) # Transform to sacred configuration result = configspace_config_to_sacred(next_config) return result
def setUp(self): self.task = TestTask() kernel = george.kernels.Matern52Kernel(np.ones([self.task.n_dims]) * 0.01, ndim=self.task.n_dims) noise_kernel = george.kernels.WhiteKernel(1e-9, ndim=self.task.n_dims) kernel = 3000 * (kernel + noise_kernel) prior = default_priors.TophatPrior(-2, 2) model = GaussianProcess(kernel, prior=prior) cost_kernel = george.kernels.Matern52Kernel(np.ones([self.task.n_dims]) * 0.01, ndim=self.task.n_dims) cost_noise_kernel = george.kernels.WhiteKernel(1e-9, ndim=self.task.n_dims) cost_kernel = 3000 * (cost_kernel + cost_noise_kernel) prior = default_priors.TophatPrior(-2, 2) cost_model = GaussianProcess(cost_kernel, prior=prior) X = init_random_uniform(self.task.X_lower, self.task.X_upper, 3) Y, C = self.task.evaluate(X) model.train(X, Y, do_optimize=False) cost_model.train(X, C, do_optimize=False) self.acquisition_func = InformationGainPerUnitCost(model, cost_model, self.task.X_lower, self.task.X_upper, self.task.is_env) self.acquisition_func.update(model, cost_model)
def maximize(self): """ Maximizes the given acquisition function. Returns ------- np.ndarray(N,D) Point with the highest acquisition value. """ verbose_level = -9 if self.verbose: verbose_level = 0 start_point = init_random_uniform(self.lower, self.upper, 1, self.rng) def obj_func(x): a = self.objective_func(x[None, :]) return a[0] res = cma.fmin(obj_func, x0=start_point[0], sigma0=0.6, restarts=self.restarts, options={"bounds": [self.lower, self.upper], "verbose": verbose_level, "verb_log": sys.maxsize, "maxfevals": self.n_func_evals}) if res[0] is None: logging.error("CMA-ES did not find anything. \ Return random configuration instead.") return start_point return res[0]
def setUp(self): self.task = TestTask() kernel = george.kernels.Matern52Kernel(np.ones([self.task.n_dims]) * 0.01, ndim=self.task.n_dims) noise_kernel = george.kernels.WhiteKernel(1e-9, ndim=self.task.n_dims) kernel = 3000 * (kernel + noise_kernel) prior = default_priors.TophatPrior(-2, 2) model = GaussianProcess(kernel, prior=prior) cost_kernel = george.kernels.Matern52Kernel( np.ones([self.task.n_dims]) * 0.01, ndim=self.task.n_dims) cost_noise_kernel = george.kernels.WhiteKernel(1e-9, ndim=self.task.n_dims) cost_kernel = 3000 * (cost_kernel + cost_noise_kernel) prior = default_priors.TophatPrior(-2, 2) cost_model = GaussianProcess(cost_kernel, prior=prior) X = init_random_uniform(self.task.X_lower, self.task.X_upper, 3) Y, C = self.task.evaluate(X) model.train(X, Y, do_optimize=False) cost_model.train(X, C, do_optimize=False) self.acquisition_func = InformationGainPerUnitCost( model, cost_model, self.task.X_lower, self.task.X_upper, self.task.is_env) self.acquisition_func.update(model, cost_model)
def posterior_mean_optimization(model, lower, upper, n_restarts=10, method="scipy", with_gradients=False): """ Estimates the incumbent by minimize the posterior mean of the objective function. Parameters ---------- model : Model object Posterior belief of the objective function. lower : (D) numpy array Specified the lower bound of the input space. Each entry corresponds to one dimension. upper : (D) numpy array Specified the upper bound of the input space. Each entry corresponds to one dimension. n_restarts: int Number of independent restarts of the optimization procedure from random starting points method : {'scipy', 'cma'} Specifies which optimization method is used to minimize the posterior mean. with_gradients : bool Specifies if gradient information are used. Only valid if method == 'scipy'. Returns ------- np.ndarray(D,) best point that was found """ def f(x): return model.predict(x[np.newaxis, :])[0][0] def df(x): dmu = model.predictive_gradients(x[np.newaxis, :])[0] return dmu startpoints = init_random_uniform(lower, upper, n_restarts) x_opt = np.zeros([len(startpoints), lower.shape[0]]) fval = np.zeros([len(startpoints)]) for i, startpoint in enumerate(startpoints): if method == "scipy": if with_gradients: res = optimize.fmin_l_bfgs_b(f, startpoint, df, bounds=list(zip(lower, upper))) x_opt[i] = res[0] fval[i] = res[1] else: res = optimize.minimize(f, startpoint, bounds=list(zip(lower, upper)), method="L-BFGS-B") x_opt[i] = res["x"] fval[i] = res["fun"] elif method == 'cma': res = cma.fmin(f, startpoint, 0.6, options={"bounds": [lower, upper]}) x_opt[i] = res[0] fval[i] = res[1] # Return the point with the lowest function value best = np.argmin(fval) return x_opt[best]
def test(self): task = SinTwo() # Check batch computation n_points = 10 X = init_random_uniform(task.X_lower, task.X_upper, n_points) y = task.evaluate(X) assert len(y.shape) == 2 assert y.shape[0] == n_points assert y.shape[1] == 1 # Check single computation X = init_random_uniform(task.X_lower, task.X_upper, 1) y = task.evaluate(X) assert y.shape[0] == 1
def posterior_mean_plus_std_optimization(model, lower, upper, n_restarts=10, with_gradients=False): """ Estimates the incumbent by minimize the posterior mean + std of the objective function, i.e. the upper bound. Parameters ---------- model : Model object Posterior belief of the objective function. lower : (D) numpy array Specified the lower bound of the input space. Each entry corresponds to one dimension. upper : (D) numpy array Specified the upper bound of the input space. Each entry corresponds to one dimension. n_restarts: int Number of independent restarts of the optimization procedure from random starting points with_gradients : bool Specifies if gradient information are used. Only valid if method == 'scipy'. Returns ------- np.ndarray(D,) best point that was found """ def f(x): mu, var = model.predict(x[np.newaxis, :]) return (mu + np.sqrt(var))[0] def df(x): dmu, dvar = model.predictive_gradients(x[np.newaxis, :]) _, var = model.predict(x[np.newaxis, :]) std = np.sqrt(var) # To get the gradients of the standard deviation # We need to apply chain rule # (s(x)=sqrt[v(x)] => s'(x) = 1/2 * v'(x) / sqrt[v(x)] dstd = 0.5 * dvar / std return dmu[:, :, 0] + dstd startpoints = init_random_uniform(lower, upper, n_restarts) x_opt = np.zeros([len(startpoints), lower.shape[0]]) fval = np.zeros([len(startpoints)]) for i, startpoint in enumerate(startpoints): if with_gradients: res = optimize.fmin_l_bfgs_b(f, startpoint, df, bounds=list(zip(lower, upper))) x_opt[i] = res[0] fval[i] = res[1] else: res = optimize.minimize(f, startpoint, bounds=list(zip(lower, upper)), method="L-BFGS-B") x_opt[i] = res["x"] fval[i] = res["fun"] # Return the point with the lowest function value best = np.argmin(fval) return x_opt[best]
def test_general_interface(self): X_test = init_random_uniform(self.task.X_lower, self.task.X_upper, 1) a = self.acquisition_func(X_test, False) assert len(a.shape) == 2 assert a.shape[0] == X_test.shape[0] assert a.shape[1] == 1
def test_general_interface(self): X_test = init_random_uniform(self.X_lower, self.X_upper, 10) # a, dadx = self.log_ei(X_test, True) a = self.log_ei(X_test) assert len(a.shape) == 2 assert a.shape[0] == X_test.shape[0] assert a.shape[1] == 1
def setUp(self): self.X_lower = np.array([0]) self.X_upper = np.array([1]) self.X = init_random_uniform(self.X_lower, self.X_upper, 10) self.Y = np.sin(self.X) self.kernel = GPy.kern.RBF(input_dim=1) self.model = GPyModel(self.kernel) self.model.train(self.X, self.Y) self.ei = EI(self.model, X_upper=self.X_upper, X_lower=self.X_lower)
def test_general_interface(self): X_test = init_random_uniform(self.X_lower, self.X_upper, 10) # a, dadx = self.log_ei(X_test, True) a = self.log_ei(X_test) assert len(a.shape) == 2 assert a.shape[0] == X_test.shape[0] assert a.shape[1] == 1
def test(self): X_lower = np.array([0]) X_upper = np.array([1]) X = init_random_uniform(X_lower, X_upper, 10) curves = np.zeros(len(X), dtype=object) for i in xrange(len(curves)): curves[i] = np.random.rand(3) model = FreezeThawGP(x_train=X, y_train=curves) model.train() assert len(model.samples.shape)==2 #""" x_test = init_random_uniform(X_lower, X_upper, 3) # Shape matching predict #m, v = model.predict(x_test) #m, v, _ = model.pred_hyper(x_test) m_asympt, v_asympt = model.predict(xprime=x_test, option='asympt') assert len(m_asympt.shape) == 2 assert m_asympt.shape[0] == x_test.shape[0] assert len(v_asympt.shape) == 1 assert v_asympt.shape[0] == x_test.shape[0] #""" m_old,v_old = model.predict(xprime=None, option='old', conf_nr=0, from_step=None, further_steps=1) assert len(m_old.shape) == 2 assert m_old.shape[0] == 1 assert len(v_old.shape) == 1 assert v_old.shape[0] == 1 m_new,v_new = model.predict(xprime=np.array([x_test[0]]), option='new', further_steps=1) assert len(m_new.shape) == 2 assert m_new.shape[0] == np.array([x_test[0]]).shape[0] assert len(v_new.shape) == 1 assert v_new.shape[0] == np.array([x_test[0]]).shape[0]
def extrapolative_initial_design(X_lower, X_upper, is_env, N): # Create grid for the system size idx = is_env == 1 g = np.array([X_upper[idx] / float(i) for i in [4, 8, 16, 32]])[:, 0] X = init_random_uniform(X_lower, X_upper, N) X[:, is_env == 1] = \ np.tile(g, np.ceil(X.shape[0] / 4.))[:X.shape[0], np.newaxis] return X
def test(self): X_lower = np.array([0]) X_upper = np.array([1]) X = init_random_uniform(X_lower, X_upper, 10) curves = np.zeros(len(X), dtype=object) for i in xrange(len(curves)): curves[i] = np.random.rand(3) model = FreezeThawGP(x_train=X, y_train=curves) model.train() x_test = init_random_uniform(X_lower, X_upper, 3) # Shape matching predict m, v = model.predict(x_test) assert len(m.shape) == 1 assert m.shape[0] == x_test.shape[0] assert len(v.shape) == 1 assert v.shape[0] == x_test.shape[0]
def setUp(self): self.X_lower = np.array([0]) self.X_upper = np.array([1]) self.X = init_random_uniform(self.X_lower, self.X_upper, 10) self.Y = np.sin(self.X) self.kernel = GPy.kern.RBF(input_dim=1) self.model = GPyModel(self.kernel) self.model.train(self.X, self.Y) self.pi = PI(self.model, X_upper=self.X_upper, X_lower=self.X_lower)
def posterior_mean_optimization(model, lower, upper, n_restarts=10, with_gradients=False): """ Estimates the incumbent by minimize the posterior mean of the objective function. Parameters ---------- model : Model object Posterior belief of the objective function. lower : (D) numpy array Specified the lower bound of the input space. Each entry corresponds to one dimension. upper : (D) numpy array Specified the upper bound of the input space. Each entry corresponds to one dimension. n_restarts: int Number of independent restarts of the optimization procedure from random starting points with_gradients : bool Specifies if gradient information are used. Only valid if method == 'scipy'. Returns ------- np.ndarray(D,) best point that was found """ def f(x): return model.predict(x[np.newaxis, :])[0][0] def df(x): dmu = model.predictive_gradients(x[np.newaxis, :])[0] return dmu startpoints = init_random_uniform(lower, upper, n_restarts) x_opt = np.zeros([len(startpoints), lower.shape[0]]) fval = np.zeros([len(startpoints)]) for i, startpoint in enumerate(startpoints): if with_gradients: res = optimize.fmin_l_bfgs_b(f, startpoint, df, bounds=list(zip(lower, upper))) x_opt[i] = res[0] fval[i] = res[1] else: res = optimize.minimize(f, startpoint, bounds=list(zip(lower, upper)), method="L-BFGS-B") x_opt[i] = res["x"] fval[i] = res["fun"] # Return the point with the lowest function value best = np.argmin(fval) return x_opt[best]
def test_general_interface(self): X_test = init_random_uniform(self.X_lower, self.X_upper, 10) # Just check if EI is always greater equal than 0 a, dadx = self.ei(X_test, True) assert len(a.shape) == 2 assert a.shape[0] == X_test.shape[0] assert a.shape[1] == 1 assert np.all(a) >= 0.0 assert len(dadx.shape) == 2 assert dadx.shape[0] == X_test.shape[0] assert dadx.shape[1] == X_test.shape[1]
def test_general_interface(self): X_test = init_random_uniform(self.X_lower, self.X_upper, 10) # Just check if PI is always greater equal than 0 a, dadx = self.pi(X_test, True) assert len(a.shape) == 2 assert a.shape[0] == X_test.shape[0] assert a.shape[1] == 1 assert np.all(a) >= 0.0 assert len(dadx.shape) == 2 assert dadx.shape[0] == X_test.shape[0] assert dadx.shape[1] == X_test.shape[1]
def test(self): task = Hartmann3() # Check batch computation n_points = 10 X = init_random_uniform(task.X_lower, task.X_upper, n_points) y = task.evaluate(X) assert len(y.shape) == 2 assert y.shape[0] == n_points assert y.shape[1] == 1 # Check single computation X = init_random_uniform(task.X_lower, task.X_upper, 1) y = task.evaluate(X) assert y.shape[0] == 1 # Check optimas X = task.opt y = task.evaluate(X) assert np.all(np.round(y, 6) == np.array([task.fopt]))
def sample_representer_points(self): self.sampling_acquisition.update(self.model) start_points = init_random_uniform(self.X_lower, self.X_upper, self.Nb) sampler = emcee.EnsembleSampler(self.Nb, self.D, self.sampling_acquisition_wrapper) # zb are the representer points and lmb are their log EI values self.zb, self.lmb, _ = sampler.run_mcmc(start_points, 200) if len(self.zb.shape) == 1: self.zb = self.zb[:, None] if len(self.lmb.shape) == 1: self.lmb = self.lmb[:, None]
def extrapolative_initial_design(X_lower, X_upper, is_env, task, N): # Create grid for the system size idx = is_env == 1 X_upper_re = np.exp(task.retransform(X_upper)) g = np.array([X_upper_re[idx] / float(i) for i in [4, 8, 16, 32]])[:, 0] g = np.true_divide((np.log(g) - task.original_X_lower[idx]), (task.original_X_upper[idx] - task.original_X_lower[idx])) X = init_random_uniform(X_lower, X_upper, N) X[:, is_env == 1] = \ np.tile(g, np.ceil(X.shape[0] / 4.))[:X.shape[0], np.newaxis] return X
def sample_representer_points(self): self.sampling_acquisition.update(self.model) start_points = init_random_uniform(self.X_lower, self.X_upper, self.Nb) sampler = emcee.EnsembleSampler(self.Nb, self.D, self.sampling_acquisition_wrapper) # zb are the representer points and lmb are their log EI values self.zb, self.lmb, _ = sampler.run_mcmc(start_points, 200) if len(self.zb.shape) == 1: self.zb = self.zb[:, None] if len(self.lmb.shape) == 1: self.lmb = self.lmb[:, None]
def extrapolative_initial_design(task, N): # Index of the environmental variable idx = task.is_env == 1 # Upper bound of the dataset size on a linear scale X_upper_re = np.exp(task.retransform(task.X_upper))[idx] # Compute the dataset size on a linear scale for a 1/4, 1/8, 1/16 and 1/32 of the data s = np.array([X_upper_re / float(i) for i in [4, 8, 16, 32]])[:, 0] log_s = np.log(s) # Transform it back to [0, 1] space s = np.true_divide((log_s - task.original_X_lower[idx]), (task.original_X_upper[idx] - task.original_X_lower[idx])) # Draw random points in the configuration space and evaluate them on the predifined data subsets X = init_random_uniform(task.X_lower, task.X_upper, N) X[:, task.is_env == 1] = \ np.tile(s, np.ceil(X.shape[0] / 4.))[:X.shape[0], np.newaxis] return X
def test_rembo(self): class TestTask(REMBO): def __init__(self): X_lower = np.array([-5, 0]) X_upper = np.array([10, 15]) super(TestTask, self).__init__(X_lower, X_upper, d=1) def objective_function(self, x): return x t = TestTask() x = init_random_uniform(t.X_lower, t.X_lower, N=100) projected_scaled_x = t.evaluate(x) assert len(projected_scaled_x.shape) == 2 assert projected_scaled_x.shape[1] == t.d_orig assert np.all(projected_scaled_x >= t.original_X_lower) assert np.all(projected_scaled_x <= t.original_X_upper)
def setUp(self): self.task = SinFunction() kernel = george.kernels.Matern52Kernel(np.ones([self.task.n_dims]) * 0.01, ndim=self.task.n_dims) noise_kernel = george.kernels.WhiteKernel(1e-9, ndim=self.task.n_dims) kernel = 3000 * (kernel + noise_kernel) prior = default_priors.TophatPrior(-2, 2) model = GaussianProcess(kernel, prior=prior) X = init_random_uniform(self.task.X_lower, self.task.X_upper, 3) Y = self.task.evaluate(X) model.train(X, Y, do_optimize=False) self.acquisition_func = InformationGainMC(model, X_upper=self.task.X_upper, X_lower=self.task.X_lower) self.acquisition_func.update(model)
def extrapolative_initial_design(task, N): # Index of the environmental variable idx = task.is_env == 1 # Upper bound of the dataset size on a linear scale X_upper_re = np.exp(task.retransform(task.X_upper))[idx] # Compute the dataset size on a linear scale for a 1/4, 1/8, 1/16 and 1/32 of the data s = np.array([X_upper_re / float(i) for i in [4, 8, 16, 32]])[:, 0] log_s = np.log(s) # Transform it back to [0, 1] space s = np.true_divide( (log_s - task.original_X_lower[idx]), (task.original_X_upper[idx] - task.original_X_lower[idx])) # Draw random points in the configuration space and evaluate them on the predifined data subsets X = init_random_uniform(task.X_lower, task.X_upper, N) X[:, task.is_env == 1] = \ np.tile(s, np.ceil(X.shape[0] / 4.))[:X.shape[0], np.newaxis] return X
def test_rembo(self): class TestTask(REMBO): def __init__(self): X_lower = np.array([-5, 0]) X_upper = np.array([10, 15]) super(TestTask, self).__init__(X_lower, X_upper, d=1) def objective_function(self, x): return x t = TestTask() x = init_random_uniform(t.X_lower, t.X_lower, N=100) projected_scaled_x = t.evaluate(x) assert len(projected_scaled_x.shape) == 2 assert projected_scaled_x.shape[1] == t.d_orig assert np.all(projected_scaled_x >= t.original_X_lower) assert np.all(projected_scaled_x <= t.original_X_upper)
def test(self): X_lower = np.array([0, 0]) X_upper = np.array([1, 1]) is_env = np.array([0, 1]) X = init_random_uniform(X_lower, X_upper, 10) Y = np.sin(X)[:, None, 0] kernel = george.kernels.Matern52Kernel(np.ones([2]), ndim=2) prior = TophatPrior(-2, 2) model = GaussianProcess(kernel, prior=prior) model.train(X, Y) rec = BestProjectedObservation(model, X_lower, X_upper, is_env) inc, inc_val = rec.estimate_incumbent() # Check shapes assert len(inc.shape) == 2 assert inc.shape[0] == 1 assert inc.shape[1] == X_lower.shape[0] assert len(inc_val.shape) == 2 assert inc_val.shape[0] == 1 assert inc_val.shape[1] == 1 # Check if incumbent is in the bounds assert not np.any([np.any(inc[:, i] < X_lower[i]) for i in range(X_lower.shape[0])]) assert not np.any([np.any(inc[:, i] > X_upper[i]) for i in range(X_upper.shape[0])]) # Check if incumbent is the correct point b = np.argmin(Y) x_best = X[b, None, :] assert np.all(inc[:, is_env==0] == x_best[:, is_env==0])
def setUp(self): self.branin = Branin() n_points = 5 rng = np.random.RandomState(42) self.X = init_random_uniform(self.branin.X_lower, self.branin.X_upper, n_points, rng=rng) self.Y = self.branin.evaluate(self.X) kernel = GPy.kern.Matern52(input_dim=self.branin.n_dims) self.model = GPyModel(kernel, optimize=True, noise_variance=1e-4, num_restarts=10) self.model.train(self.X, self.Y) self.acquisition_func = EI(self.model, X_upper=self.branin.X_upper, X_lower=self.branin.X_lower, par=0.1)
def test(self): X_lower = np.array([0, 0]) X_upper = np.array([1, 1]) is_env = np.array([0, 1]) X = init_random_uniform(X_lower, X_upper, 10) Y = np.sin(X)[:, None, 0] kernel = george.kernels.Matern52Kernel(np.ones([2]), ndim=2) prior = TophatPrior(-2, 2) model = GaussianProcess(kernel, prior=prior) model.train(X, Y) rec = BestProjectedObservation(model, X_lower, X_upper, is_env) inc, inc_val = rec.estimate_incumbent() # Check shapes assert len(inc.shape) == 2 assert inc.shape[0] == 1 assert inc.shape[1] == X_lower.shape[0] assert len(inc_val.shape) == 2 assert inc_val.shape[0] == 1 assert inc_val.shape[1] == 1 # Check if incumbent is in the bounds assert not np.any( [np.any(inc[:, i] < X_lower[i]) for i in range(X_lower.shape[0])]) assert not np.any( [np.any(inc[:, i] > X_upper[i]) for i in range(X_upper.shape[0])]) # Check if incumbent is the correct point b = np.argmin(Y) x_best = X[b, None, :] assert np.all(inc[:, is_env == 0] == x_best[:, is_env == 0])
def setUp(self): self.branin = Branin() n_points = 5 rng = np.random.RandomState(42) self.X = init_random_uniform(self.branin.X_lower, self.branin.X_upper, n_points, rng=rng) self.Y = self.branin.evaluate(self.X) kernel = GPy.kern.Matern52(input_dim=self.branin.n_dims) self.model = GPyModel(kernel, optimize=True, noise_variance=1e-4, num_restarts=10) self.model.train(self.X, self.Y) self.acquisition_func = EI(self.model, X_upper=self.branin.X_upper, X_lower=self.branin.X_lower, par=0.1)
def maximize(self): """ Maximizes the given acquisition function. Returns ------- np.ndarray(N,D) Point with the highest acquisition value. """ verbose_level = -9 if self.verbose: verbose_level = 0 start_point = init_random_uniform(self.lower, self.upper, 1, self.rng) def obj_func(x): a = self.objective_func(x[None, :]) return -a[0] res = cma.fmin(obj_func, x0=start_point[0], sigma0=0.6, restarts=self.restarts, options={ "bounds": [self.lower, self.upper], "verbose": verbose_level, "verb_log": sys.maxsize, "maxfevals": self.n_func_evals }) if res[0] is None: logging.error("CMA-ES did not find anything. \ Return random configuration instead.") return start_point return res[0]
def suggest_configuration(self): if self.X is None and self.y is None: # No data points yet to train a model, just return a random configuration instead new_x = init_random_uniform(self.lower, self.upper, n_points=1, rng=self.rng)[0, :] else: # Train the model on all finished runs self.model.train(self.X, self.y) self.acquisition_func.update(self.model) # Maximize the acquisition function new_x = self.maximizer.maximize() # Maps from [0, 1]^D space back to original space next_config = Configuration(self.config_space, vector=new_x) # Transform to sacred configuration result = configspace_config_to_sacred(next_config) return result
def test(self): X_lower = np.array([0]) X_upper = np.array([1]) X = init_random_uniform(X_lower, X_upper, 10) Y = np.sin(X) kernel = GPy.kern.Matern52(input_dim=1) model = GPyModel(kernel) model.train(X, Y) x_test = init_random_uniform(X_lower, X_upper, 3) # Shape matching predict m, v = model.predict(x_test, full_cov=True) assert len(m.shape) == 2 assert m.shape[0] == x_test.shape[0] assert m.shape[1] == 1 assert len(v.shape) == 2 assert v.shape[0] == x_test.shape[0] assert v.shape[1] == x_test.shape[0] # Check gradients dm, dv = model.predictive_gradients(x_test) assert len(dm.shape) == 2 assert dm.shape[0] == x_test.shape[0] assert dm.shape[1] == x_test.shape[1] assert len(dv.shape) == 2 assert dv.shape[0] == x_test.shape[0] assert dv.shape[1] == 1 # Shape matching function sampling x_ = np.linspace(X_lower, X_upper, 10) x_ = x_[:, np.newaxis] funcs = model.sample_functions(x_, n_funcs=2) assert len(funcs.shape) == 2 assert funcs.shape[0] == 2 assert funcs.shape[1] == x_.shape[0] # Shape matching predict variance x_test2 = np.array([np.random.rand(1)]) x_test1 = np.random.rand(10)[:, np.newaxis] var = model.predict_variance(x_test1, x_test2) assert len(var.shape) == 2 assert var.shape[0] == x_test1.shape[0] assert var.shape[1] == 1 # Check compatibility with all acquisition functions acq_func = EI(model, X_upper=X_upper, X_lower=X_lower) acq_func.update(model) acq_func(x_test) acq_func = PI(model, X_upper=X_upper, X_lower=X_lower) acq_func.update(model) acq_func(x_test) acq_func = LCB(model, X_upper=X_upper, X_lower=X_lower) acq_func.update(model) acq_func(x_test) acq_func = InformationGain(model, X_upper=X_upper, X_lower=X_lower) acq_func.update(model) acq_func(x_test) # Check compatibility with all incumbent estimation methods rec = BestObservation(model, X_lower, X_upper) inc, inc_val = rec.estimate_incumbent(None) assert len(inc.shape) == 2 assert inc.shape[0] == 1 assert inc.shape[1] == X_upper.shape[0] assert len(inc_val.shape) == 2 assert inc_val.shape[0] == 1 assert inc_val.shape[1] == 1 rec = PosteriorMeanOptimization(model, X_lower, X_upper) startpoints = init_random_uniform(X_lower, X_upper, 4) inc, inc_val = rec.estimate_incumbent(startpoints) assert len(inc.shape) == 2 assert inc.shape[0] == 1 assert inc.shape[1] == X_upper.shape[0] assert len(inc_val.shape) == 2 assert inc_val.shape[0] == 1 assert inc_val.shape[1] == 1 rec = PosteriorMeanAndStdOptimization(model, X_lower, X_upper) startpoints = init_random_uniform(X_lower, X_upper, 4) inc, inc_val = rec.estimate_incumbent(startpoints) assert len(inc.shape) == 2 assert inc.shape[0] == 1 assert inc.shape[1] == X_upper.shape[0] assert len(inc_val.shape) == 2 assert inc_val.shape[0] == 1 assert inc_val.shape[1] == 1
import numpy as np import matplotlib.pyplot as plt from robo.initial_design.init_random_uniform import init_random_uniform from robo.models.bayesian_linear_regression import BayesianLinearRegression def f(x): return 10 * x - 5 + np.random.randn() * 0.001 rng = np.random.RandomState(42) X = init_random_uniform(np.zeros(1), np.ones(1), 20, rng) y = f(X)[:, 0] model = BayesianLinearRegression() model.train(X, y, do_optimize=True) X_test = np.linspace(0, 1, 100)[:, None] fvals = f(X_test)[:, 0] m, v = model.predict(X_test) plt.plot(X, y, "ro") plt.grid() plt.plot(X_test[:, 0], fvals, "k--") plt.plot(X_test[:, 0], m, "blue")
def test(self): X_lower = np.array([0]) X_upper = np.array([1]) X = init_random_uniform(X_lower, X_upper, 10) Y = np.sin(X) model = RandomForest(types=np.zeros([X_lower.shape[0]])) model.train(X, Y) x_test = init_random_uniform(X_lower, X_upper, 3) # Shape matching predict m, v = model.predict(x_test) assert len(m.shape) == 2 assert m.shape[0] == x_test.shape[0] assert m.shape[1] == 1 assert len(v.shape) == 2 assert v.shape[0] == x_test.shape[0] assert v.shape[1] == 1 # Shape matching function sampling x_ = np.linspace(X_lower, X_upper, 10) x_ = x_[:, np.newaxis] #funcs = model.sample_functions(x_, n_funcs=2) #assert len(funcs.shape) == 2 #assert funcs.shape[0] == 2 #assert funcs.shape[1] == x_.shape[0] # Check compatibility with all acquisition functions acq_func = EI(model, X_upper=X_upper, X_lower=X_lower) acq_func.update(model) acq_func(x_test) acq_func = PI(model, X_upper=X_upper, X_lower=X_lower) acq_func.update(model) acq_func(x_test) acq_func = LCB(model, X_upper=X_upper, X_lower=X_lower) acq_func.update(model) acq_func(x_test) # Check compatibility with all incumbent estimation methods rec = BestObservation(model, X_lower, X_upper) inc, inc_val = rec.estimate_incumbent(None) assert len(inc.shape) == 2 assert inc.shape[0] == 1 assert inc.shape[1] == X_upper.shape[0] assert len(inc_val.shape) == 2 assert inc_val.shape[0] == 1 assert inc_val.shape[1] == 1 rec = PosteriorMeanOptimization(model, X_lower, X_upper) startpoints = init_random_uniform(X_lower, X_upper, 4) inc, inc_val = rec.estimate_incumbent(startpoints) assert len(inc.shape) == 2 assert inc.shape[0] == 1 assert inc.shape[1] == X_upper.shape[0] assert len(inc_val.shape) == 2 assert inc_val.shape[0] == 1 assert inc_val.shape[1] == 1 rec = PosteriorMeanAndStdOptimization(model, X_lower, X_upper) startpoints = init_random_uniform(X_lower, X_upper, 4) inc, inc_val = rec.estimate_incumbent(startpoints) assert len(inc.shape) == 2 assert inc.shape[0] == 1 assert inc.shape[1] == X_upper.shape[0] assert len(inc_val.shape) == 2 assert inc_val.shape[0] == 1 assert inc_val.shape[1] == 1
def run(self, num_iterations=10, X=None, Y=None): """ The main Bayesian optimization loop Parameters ---------- num_iterations: int The number of iterations X: np.ndarray(N,D) Initial points that are already evaluated Y: np.ndarray(N,1) Function values of the already evaluated points Returns ------- np.ndarray(1,D) Incumbent np.ndarray(1,1) (Estimated) function value of the incumbent """ # Save the time where we start the Bayesian optimization procedure self.time_start = time.time() if X is None and Y is None: self.time_func_eval = np.zeros([self.init_points]) self.time_overhead = np.zeros([self.init_points]) self.X = np.zeros([self.init_points, self.task.n_dims]) self.Y = np.zeros([self.init_points, 1]) init = self.initial_design(self.task.X_lower, self.task.X_upper, N=self.init_points) for i, x in enumerate(init): x = x[np.newaxis, :] logger.info("Evaluate: %s" % x) start_time = time.time() y = self.task.evaluate(x) self.X[i] = x[0, :] self.Y[i] = y[0, :] self.time_func_eval[i] = time.time() - start_time self.time_overhead[i] = 0.0 logger.info("Configuration achieved a performance " "of %f in %f seconds" % (self.Y[i], self.time_func_eval[i])) # Use best point seen so far as incumbent best_idx = np.argmin(self.Y) self.incumbent = np.array([self.X[best_idx]]) self.incumbent_value = np.array([self.Y[best_idx]]) self.incumbents.append(self.incumbent) self.incumbent_values.append(self.incumbent_value) self.runtime.append(time.time() - self.start_time) if self.save_dir is not None and (i) % self.num_save == 0: self.save_iteration(i, hyperparameters=None, acquisition_value=0) else: self.X = X self.Y = Y self.time_func_eval = np.zeros([self.X.shape[0]]) self.time_overhead = np.zeros([self.X.shape[0]]) # best = np.argmin(Y) # incumbent = X[best] # incumbent_value = Y[best] # self.incumbents.append(incumbent[np.newaxis, :]) # self.incumbent_values.append(incumbent_value[np.newaxis, :]) # self.runtime.append(time.time() - self.start_time) for it in range(self.init_points, num_iterations): logger.info("Start iteration %d ... ", it) start_time = time.time() # Choose next point to evaluate if it % self.train_intervall == 0: do_optimize = True else: do_optimize = False new_x = self.choose_next(self.X, self.Y, do_optimize) # Estimate current incumbent start_time_inc = time.time() startpoints = init_random_uniform(self.task.X_lower, self.task.X_upper, self.n_restarts) self.incumbent, self.incumbent_value = \ self.estimator.estimate_incumbent(startpoints) self.incumbents.append(self.incumbent) self.incumbent_values.append(self.incumbent_value) logger.info("New incumbent %s found in %f seconds with " "estimated performance %f", str(self.incumbent), time.time() - start_time_inc, self.incumbent_value) time_overhead = time.time() - start_time self.time_overhead = np.append(self.time_overhead, np.array([time_overhead])) logger.info("Optimization overhead was %f seconds" % (self.time_overhead[-1])) logger.info("Evaluate candidate %s" % (str(new_x))) start_time = time.time() new_y = self.task.evaluate(new_x) time_func_eval = time.time() - start_time self.time_func_eval = np.append(self.time_func_eval, np.array([time_func_eval])) logger.info("Configuration achieved a performance of %f " % (new_y[0, 0])) logger.info("Evaluation of this configuration took %f seconds" % (self.time_func_eval[-1])) # Update the data self.X = np.append(self.X, new_x, axis=0) self.Y = np.append(self.Y, new_y, axis=0) self.runtime.append(time.time() - self.start_time) if self.save_dir is not None and (it) % self.num_save == 0: hypers = self.model.hypers self.save_iteration( it, hyperparameters=hypers, acquisition_value=self.acquisition_func(new_x)) # TODO: Retrain model and then return the incumbent logger.info("Return %s as incumbent with predicted performance %f" % (str(self.incumbent), self.incumbent_value)) return self.incumbent, self.incumbent_value
def run(self, num_iterations=10, X=None, Y=None, C=None): """ Runs the main Bayesian optimization loop Parameters ---------- num_iterations : int, optional Specifies the number of iterations. X : (N, D) numpy array, optional Initial points where BO starts from. Y : (N, D) numpy array, optional The function values of the initial points. Make sure the number of points is the same. C : (N, D) numpy array, optional The costs of the initial points. Make sure the number of points is the same. Returns ------- incumbent : (1, D) numpy array The estimated optimum that was found after the specified number of iterations. """ self.time_start = time.time() if X is None and Y is None and C is None: self.time_func_eval = np.zeros([1]) self.time_overhead = np.zeros([1]) self.X = np.zeros([1, self.task.n_dims]) self.Y = np.zeros([1, 1]) self.C = np.zeros([1, 1]) init = extrapolative_initial_design(self.task, N=self.init_points) for i, x in enumerate(init): x = x[np.newaxis, :] start_time = time.time() logger.info("Evaluate: %s" % x) start_time = time.time() y, c = self.task.evaluate(x) # Transform cost to log scale c = np.log(c) if i == 0: self.X[i] = x[0, :] self.Y[i] = y[0, :] self.C[i] = c[0, :] self.time_func_eval[i] = time.time() - start_time self.time_overhead[i] = 0.0 else: self.X = np.append(self.X, x, axis=0) self.Y = np.append(self.Y, y, axis=0) self.C = np.append(self.C, c, axis=0) time_feval = np.array([time.time() - start_time]) self.time_func_eval = np.append(self.time_func_eval, time_feval, axis=0) self.time_overhead = np.append(self.time_overhead, np.array([0]), axis=0) logger.info("Configuration achieved a" "performance of %f and %f costs in %f seconds" % (self.Y[i], self.C[i], self.time_func_eval[i])) # Use best point seen so far as incumbent best_idx = np.argmin(self.Y) best_idx = np.argmin(self.Y) # Copy because we are going to change the system size to smax self.incumbent = np.copy(self.X[best_idx]) self.incumbent_value = self.Y[best_idx] bounds_subspace = self.task.X_upper[self.task.is_env == 1] self.incumbent[self.task.is_env == 1] = bounds_subspace self.incumbent = self.incumbent[np.newaxis, :] self.incumbent_value = self.incumbent_value[np.newaxis, :] self.incumbents.append(self.incumbent) self.incumbent_values.append(self.incumbent_value) self.runtime.append(time.time() - self.start_time) if self.save_dir is not None and (i) % self.num_save == 0: self.save_iteration(i, costs=self.C[-1], hyperparameters=None, acquisition_value=0) else: self.X = X self.Y = Y self.C = C self.time_func_eval = np.zeros([self.X.shape[0]]) self.time_overhead = np.zeros([self.X.shape[0]]) for it in range(0, num_iterations): logger.info("Start iteration %d ... ", it) # Choose a new configuration start_time = time.time() if it % self.train_intervall == 0: do_optimize = True else: do_optimize = False new_x = self.choose_next(self.X, self.Y, self.C, do_optimize) # Estimate current incumbent from the posterior # over the configuration space start_time_inc = time.time() startpoints = init_random_uniform(self.task.X_lower, self.task.X_upper, self.n_restarts) self.incumbent, self.incumbent_value = \ self.estimator.estimate_incumbent(startpoints) self.incumbents.append(self.incumbent) self.incumbent_values.append(self.incumbent_value) logger.info("New incumbent %s found in %f seconds"\ " with predicted performance %f", str(self.incumbent), time.time() - start_time_inc, self.incumbent_value) # Compute the time we needed to pick a new point time_overhead = time.time() - start_time self.time_overhead = np.append(self.time_overhead, np.array([time_overhead])) logger.info("Optimization overhead was " "%f seconds" % (self.time_overhead[-1])) # Evaluate the configuration logger.info("Evaluate candidate %s" % (str(new_x))) start_time = time.time() new_y, new_cost = self.task.evaluate(new_x) time_func_eval = time.time() - start_time # We model the log costs new_cost = np.log(new_cost) self.time_func_eval = np.append(self.time_func_eval, np.array([time_func_eval])) logger.info("Configuration achieved a performance " "of %f in %s seconds" % (new_y[0, 0], new_cost[0])) # Add the new observations to the data self.X = np.append(self.X, new_x, axis=0) self.Y = np.append(self.Y, new_y, axis=0) self.C = np.append(self.C, new_cost, axis=0) self.runtime.append(time.time() - self.start_time) if self.save_dir is not None and (it + self.init_points) % self.num_save == 0: hypers = self.model.hypers self.save_iteration(it + self.init_points, costs=self.C[-1], hyperparameters=hypers, acquisition_value=self.acquisition_func(new_x)) logger.info("Return %s as incumbent" % (str(self.incumbent))) return self.incumbent
def run(self, num_iterations=10, X=None, Y=None): """ Bayesian Optimization iterations Parameters ---------- num_iterations: int How many times the BO loop has to be executed X: np.ndarray(N,D) Initial points that are already evaluated Y: np.ndarray(N,1) Function values of the already evaluated points Returns ------- np.ndarray(1,D) Incumbent np.ndarray(1,1) (Estimated) function value of the incumbent """ init = init_random_uniform(self.task.X_lower, self.task.X_upper, self.init_points) self.basketOld_X = deepcopy(init) val_losses_all = [] nr_epochs = 0 self.create_file_paths() self.time_start = time.time() ys = np.zeros(self.init_points, dtype=object) self.time_func_eval = np.zeros([self.init_points]) self.time_overhead = np.zeros([self.init_points]) ##change: the task function should send the model file_path back and it should be stored here for i in xrange(self.init_points): #ys[i] = self.task.f(np.arange(1, 1 + self.first_steps), x=init[i, :]) ##change: that's not general #ys[i] = self.task.objective(nr_epochs=None, save_file_new=self.basket_files, save_file_old=None) self.task.set_save_modus(is_old=False, file_old=None, file_new=self.basket_files[i]) self.task.set_epochs(self.nr_epochs_inits) #print 'init[i,:]: ', init[i,:] #_, ys[i] = self.task.objective_function(x=init[i,:][np.newaxis,:]) conf_now = init[i,:] logger.info("Evaluate: %s" % conf_now[np.newaxis,:]) start_time = time.time() _, val_losses = self.task.evaluate(x=conf_now[np.newaxis,:]) self.time_func_eval[i] = time.time() - start_time self.time_overhead[i] = 0.0 logger.info("Configuration achieved a performance " "of %f in %f seconds" % (val_losses[-1], self.time_func_eval[i])) #storing configuration, learning curve, activity, index in the basketOld ys[i] = np.asarray(val_losses) self.all_configs[self.total_nr_confs] = [conf_now, ys[i], True, self.total_nr_confs] self.total_nr_confs+=1 val_losses_all = val_losses_all + val_losses nr_epochs+=len(val_losses) if self.save_dir is not None and (i) % self.num_save == 0: self.save_json(i, learning_curve=str(val_losses), information_gain=0., entropy=0, phantasized_entropy=0.) self.save_iteration(i) self.basketOld_Y = deepcopy(ys) Y = np.zeros((len(ys), 1)) for i in xrange(Y.shape[0]): Y[i, :] = ys[i][-1] self.freezeModel.X = self.freezeModel.x_train = self.basketOld_X self.freezeModel.ys = self.freezeModel.y_train = self.basketOld_Y self.freezeModel.Y = Y self.freezeModel.actualize() ###############################From here onwards the iteration with for loop########################################### ####################################################################################################################### for k in range(self.init_points, num_iterations): if self.stop_epochs and nr_epochs >= self.max_epochs: print 'Maximal number of epochs' break logger.info("Start iteration %d ... ", k) print '######################iteration nr: {:d} #################################'.format(k) start_time = time.time() #res = self.choose_next(X=self.basketOld_X, Y=self.basketOld_Y, do_optimize=True) #here just choose the next candidates, which will be stored in basketNew res = self.choose_next_ei(X=self.basketOld_X, Y=self.basketOld_Y, do_optimize=True) #res = res[0] print 'res: {:s}'.format(res) ig = InformationGainMC(model=self.freezeModel, X_lower=self.task.X_lower, X_upper=self.task.X_upper, sampling_acquisition=EI) ig.update(self.freezeModel, calc_repr=True) H = ig.compute() zb = deepcopy(ig.zb) lmb = deepcopy(ig.lmb) print 'H: {}'.format(H) # Fantasize over the old and the new configurations nr_old = self.init_points fant_old = np.zeros(nr_old) for i in xrange(nr_old): fv = self.freezeModel.predict(option='old', conf_nr=i) fant_old[i] = fv[0] nr_new = res.shape[0] fant_new = np.zeros(nr_new) for j in xrange(nr_new): m, v = self.freezeModel.predict(xprime=res[j,:], option='new') fant_new[j] = m Hfant = np.zeros(nr_old + nr_new) for i in xrange(nr_old): freezeModel = deepcopy(self.freezeModel) y_i = freezeModel.ys[i] y_i = np.append(y_i, np.array([fant_old[i]]), axis=0) freezeModel.ys[i] = freezeModel.y_train[i] = y_i freezeModel.Y[i, :] = y_i[-1] ig1 = InformationGainMC(model=freezeModel, X_lower=self.task.X_lower, X_upper=self.task.X_upper, sampling_acquisition=EI) ig1.actualize(zb, lmb) ig1.update(freezeModel) H1 = ig1.compute() Hfant[i] = H1 print 'Hfant: {}'.format(Hfant) print 'freezeModel.X: {}'.format(freezeModel.X) print 'res: {:s}'.format(res) for k in xrange(nr_new): freezeModel = deepcopy(self.freezeModel) freezeModel.X = np.append(freezeModel.X, res[k,:][np.newaxis,:], axis=0) ysNew = np.zeros(len(freezeModel.ys) + 1, dtype=object) for i in xrange(len(freezeModel.ys)): ##improve: do not use loop here, but some expansion ysNew[i] = freezeModel.ys[i] ysNew[-1] = np.array([fant_new[k]]) freezeModel.ys = freezeModel.y_train = ysNew freezeModel.Y = np.append(freezeModel.Y, np.array([[fant_new[k]]]), axis=0) freezeModel.C_samples = np.zeros( (freezeModel.C_samples.shape[0], freezeModel.C_samples.shape[1] + 1, freezeModel.C_samples.shape[2] + 1)) freezeModel.mu_samples = np.zeros( (freezeModel.mu_samples.shape[0], freezeModel.mu_samples.shape[1] + 1, 1)) ig1 = InformationGainMC(model=freezeModel, X_lower=self.task.X_lower, X_upper=self.task.X_upper, sampling_acquisition=EI) ig1.actualize(zb, lmb) ig1.update(freezeModel) H1 = ig1.compute() Hfant[-(nr_new - k)] = H1 #the why of the initial - print 'Hfant: {}'.format(Hfant) # Comparison of the different values infoGain = -(Hfant - H) winner = np.argmax(infoGain) print 'the winner is index: {:d}'.format(winner) time_overhead = time.time() - start_time self.time_overhead = np.append(self.time_overhead, np.array([time_overhead])) logger.info("Optimization overhead was %f seconds" % (self.time_overhead[-1])) #run an old configuration and actualize basket if winner <= ((len(Hfant) - 1) - nr_new): print('###################### run old config ######################') # run corresponding configuration for more one step ##change: the task function should send the model file_path back and it should be stored here #ytplus1 = self.task.f(t=len(self.basketOld_Y[winner]) + 1, x=self.basketOld_X[winner]) #ytplus1 = self.task.f(t=len(self.basketOld_Y[winner]) + 1, x=self.basketOld_X[winner], save_file_old=self.basket_files[winner]) self.task.set_save_modus(is_old=True, file_old=self.basket_files[winner], file_new=None) self.task.set_epochs(self.nr_epochs_further) if self.pkl: with open(self.basket_files[winner],'rb') as f: weights_final = pickle.load(f) W,b = weights_final self.task.set_weights(W=W, b=b) else: self.task.set_weights(self.basket_files[winner]) #ytplus1 = self.task.objective_function(x=self.basketOld_X[winner]) conf_to_run = self.basketOld_X[winner] logger.info("Evaluate candidate %s" % (str(conf_to_run[np.newaxis,:]))) start_time = time.time() _, val_losses= self.task.evaluate(x=conf_to_run[np.newaxis,:]) time_func_eval = time.time() - start_time self.time_func_eval = np.append(self.time_func_eval, np.array([time_func_eval])) logger.info("Configuration achieved a performance of %f " %(val_losses[-1])) logger.info("Evaluation of this configuration took %f seconds" %(self.time_func_eval[-1])) ytplus1 = np.asarray(val_losses) val_losses_all = val_losses_all + val_losses nr_epochs+=len(val_losses) self.basketOld_Y[winner] = np.append(self.basketOld_Y[winner], ytplus1) index_now = self.basket_indices[winner] #self.all_configs[index_now] = [conf_to_run, self.basketOld_Y[winner], True, winner] self.all_configs[index_now][1] = self.basketOld_Y[winner] #else run the new proposed configuration and actualize else: print('###################### run new config ######################') winner = winner - nr_old ##change: the task function should send the model file_path back and it should be saved here #ytplus1 = self.task.f(t=1, x=res[winner]) if self.pkl: file_path = "config_" + str(len(self.basket_files)) + ".pkl" else: file_path = "config_" + str(len(self.basket_files)) file_path = os.path.join(self.directory, file_path) #ytplus1 = self.task.f(t=1, x=res[winner], save_file_new=file_path) self.task.set_save_modus(is_old=False, file_old=None, file_new=file_path) self.task.set_epochs(self.nr_epochs_further) #ytplus1 = self.task.objective_function(x=res[winner]) logger.info("Evaluate candidate %s" % (str(res[winner][np.newaxis,:]))) start_time = time.time() _, val_losses = self.task.evaluate(x=res[winner][np.newaxis,:]) ytplus1 = np.asarray(val_losses) val_losses_all = val_losses_all + val_losses time_func_eval = time.time() - start_time self.time_func_eval = np.append(self.time_func_eval, np.array([time_func_eval])) logger.info("Configuration achieved a performance of %f " %(val_losses[-1])) logger.info("Evaluation of this configuration took %f seconds" %(self.time_func_eval[-1])) nr_epochs+=len(val_losses) replace = get_min_ei(freezeModel, self.basketOld_X, self.basketOld_Y) self.basketOld_X[replace] = res[winner] if type(ytplus1) != np.ndarray: self.basketOld_Y[replace] = np.array([ytplus1]) else: self.basketOld_Y[replace] = ytplus1 self.basket_files[replace] = file_path #deactivate the configuration which is being replaced self.all_configs[self.basket_indices[replace]][2] = False self.all_configs[self.basket_indices[replace]][3] = -1 #actualize the indices table self.total_nr_confs+=1 self.basket_indices[replace]=self.total_nr_confs #add new configuration with learning curve and index conf_to_run = res[winner] self.all_configs[self.total_nr_confs] = [conf_to_run, self.basketOld_Y[winner], True, replace] Y = getY(self.basketOld_Y) self.incumbent, self.incumbent_value = estimate_incumbent(Y, self.basketOld_X) self.incumbents.append(self.incumbent) self.incumbent_values.append(self.incumbent_value) if self.save_dir is not None and (k) % self.num_save == 0: self.save_json(k, learning_curve=str(val_losses), information_gain=str(infoGain), entropy=str(H), phantasized_entropy=str(Hfant)) self.save_iteration(k) with open('val_losses_all.pkl', 'wb') as f: pickle.dump(val_losses_all, f) with open('all_configs.pkl','wb') as c: pickle.dump(self.all_configs, c) if self.save_dir: self.save_json(i) return self.incumbent, self.incumbent_value
def test(self): X_lower = np.array([0]) X_upper = np.array([1]) X = init_random_uniform(X_lower, X_upper, 10) Y = np.sin(X) model = RandomForest(types=np.zeros([X_lower.shape[0]])) model.train(X, Y) x_test = init_random_uniform(X_lower, X_upper, 3) # Shape matching predict m, v = model.predict(x_test) assert len(m.shape) == 2 assert m.shape[0] == x_test.shape[0] assert m.shape[1] == 1 assert len(v.shape) == 2 assert v.shape[0] == x_test.shape[0] assert v.shape[1] == 1 # Shape matching function sampling x_ = np.linspace(X_lower, X_upper, 10) x_ = x_[:, np.newaxis] #funcs = model.sample_functions(x_, n_funcs=2) #assert len(funcs.shape) == 2 #assert funcs.shape[0] == 2 #assert funcs.shape[1] == x_.shape[0] # Check compatibility with all acquisition functions acq_func = EI(model, X_upper=X_upper, X_lower=X_lower) acq_func.update(model) acq_func(x_test) acq_func = PI(model, X_upper=X_upper, X_lower=X_lower) acq_func.update(model) acq_func(x_test) acq_func = LCB(model, X_upper=X_upper, X_lower=X_lower) acq_func.update(model) acq_func(x_test) # Check compatibility with all incumbent estimation methods rec = BestObservation(model, X_lower, X_upper) inc, inc_val = rec.estimate_incumbent(None) assert len(inc.shape) == 2 assert inc.shape[0] == 1 assert inc.shape[1] == X_upper.shape[0] assert len(inc_val.shape) == 2 assert inc_val.shape[0] == 1 assert inc_val.shape[1] == 1 rec = PosteriorMeanOptimization(model, X_lower, X_upper) startpoints = init_random_uniform(X_lower, X_upper, 4) inc, inc_val = rec.estimate_incumbent(startpoints) assert len(inc.shape) == 2 assert inc.shape[0] == 1 assert inc.shape[1] == X_upper.shape[0] assert len(inc_val.shape) == 2 assert inc_val.shape[0] == 1 assert inc_val.shape[1] == 1 rec = PosteriorMeanAndStdOptimization(model, X_lower, X_upper) startpoints = init_random_uniform(X_lower, X_upper, 4) inc, inc_val = rec.estimate_incumbent(startpoints) assert len(inc.shape) == 2 assert inc.shape[0] == 1 assert inc.shape[1] == X_upper.shape[0] assert len(inc_val.shape) == 2 assert inc_val.shape[0] == 1 assert inc_val.shape[1] == 1
import lasagne import numpy as np import matplotlib.pyplot as plt from robo.initial_design.init_random_uniform import init_random_uniform from robo.models.dngo import DNGO from robo.util.normalization import zero_mean_unit_var_normalization, zero_mean_unit_var_unnormalization def f(x): return np.sinc(x * 10 - 5).sum(axis=1)[:, None] rng = np.random.RandomState(42) X = init_random_uniform(np.zeros(1), np.ones(1), 20, rng) y = f(X)[:, 0] model = DNGO() model.train(X, y) predictions = lasagne.layers.get_output(model.network, zero_mean_unit_var_normalization(X, model.X_mean, model.X_std)[0], deterministic=True).eval() predictions = zero_mean_unit_var_unnormalization(predictions, model.y_mean, model.y_std) X_test = np.linspace(0, 1, 100)[:, None] X_test_norm = zero_mean_unit_var_normalization(X_test, model.X_mean, model.X_std)[0]
def run(self, num_iterations=10, X=None, Y=None, C=None): """ Runs the main Bayesian optimization loop Parameters ---------- num_iterations : int, optional Specifies the number of iterations. X : (N, D) numpy array, optional Initial points where BO starts from. Y : (N, D) numpy array, optional The function values of the initial points. Make sure the number of points is the same. C : (N, D) numpy array, optional The costs of the initial points. Make sure the number of points is the same. Returns ------- incumbent : (1, D) numpy array The estimated optimum that was found after the specified number of iterations. """ self.time_start = time.time() if X is None and Y is None and C is None: self.time_func_eval = np.zeros([self.init_points]) self.time_overhead = np.zeros([self.init_points]) self.X = np.zeros([1, self.task.n_dims]) self.Y = np.zeros([1, 1]) self.C = np.zeros([1, 1]) init = self.initial_design(self.task.X_lower, self.task.X_upper, self.init_points) # Evaluate only on cheaper task init[:, -1] = 0 for i, x in enumerate(init): x = x[np.newaxis, :] logger.info("Evaluate: %s" % x) start_time = time.time() y, c = self.task.evaluate(x) # Transform cost to log scale c = np.log(c) if i == 0: self.X[i] = x[0, :] self.Y[i] = y[0, :] self.C[i] = c[0, :] self.time_func_eval[i] = time.time() - start_time self.time_overhead[i] = 0.0 else: self.X = np.append(self.X, x, axis=0) self.Y = np.append(self.Y, y, axis=0) self.C = np.append(self.C, c, axis=0) time_feval = np.array([time.time() - start_time]) self.time_func_eval = np.append(self.time_func_eval, time_feval, axis=0) self.time_overhead = np.append(self.time_overhead, np.array([0]), axis=0) logger.info("Configuration achieved a" "performance of %f and %f costs in %f seconds" % (self.Y[i], self.C[i], self.time_func_eval[i])) # Use best point seen so far as incumbent best_idx = np.argmin(self.Y) best_idx = np.argmin(self.Y) # Copy because we are going to change the system size to smax self.incumbent = np.copy(self.X[best_idx]) self.incumbent_value = self.Y[best_idx] self.runtime.append(time.time() - self.start_time) self.incumbent[-1] = 1 self.incumbent = self.incumbent[np.newaxis, :] self.incumbent_value = self.incumbent_value[np.newaxis, :] self.incumbents.append(self.incumbent) self.incumbent_values.append(self.incumbent_value) if self.save_dir is not None and (i) % self.num_save == 0: self.save_iteration(i, costs=self.C[-1], hyperparameters=None, acquisition_value=0) else: self.X = X self.Y = Y self.C = C self.time_func_eval = np.zeros([self.X.shape[0]]) self.time_overhead = np.zeros([self.X.shape[0]]) for it in range(self.init_points, num_iterations): logger.info("Start iteration %d ... ", it) # Choose a new configuration start_time = time.time() if it % self.train_intervall == 0: do_optimize = True else: do_optimize = False new_x = self.choose_next(self.X, self.Y, self.C, do_optimize) # Estimate current incumbent from the posterior # over the configuration space start_time_inc = time.time() startpoints = init_random_uniform(self.task.X_lower, self.task.X_upper, self.n_restarts) self.incumbent, self.incumbent_value = \ self.estimator.estimate_incumbent(startpoints) self.incumbents.append(self.incumbent) self.incumbent_values.append(self.incumbent_value) logger.info("New incumbent %s found in %f seconds", str(self.incumbent), time.time() - start_time_inc) # Compute the time we needed to pick a new point time_overhead = time.time() - start_time self.time_overhead = np.append(self.time_overhead, np.array([time_overhead])) logger.info("Optimization overhead was " "%f seconds" % (self.time_overhead[-1])) # Evaluate the configuration logger.info("Evaluate candidate %s" % (str(new_x))) start_time = time.time() new_y, new_cost = self.task.evaluate(new_x) time_func_eval = time.time() - start_time # We model the log costs new_cost = np.log(new_cost) self.time_func_eval = np.append(self.time_func_eval, np.array([time_func_eval])) logger.info("Configuration achieved a performance " "of %f in %s seconds" % (new_y[0, 0], new_cost[0])) # Add the new observations to the data self.X = np.append(self.X, new_x, axis=0) self.Y = np.append(self.Y, new_y, axis=0) self.C = np.append(self.C, new_cost, axis=0) self.runtime.append(time.time() - self.start_time) if self.save_dir is not None and (it) % self.num_save == 0: hypers = self.model.hypers self.save_iteration( it, costs=self.C[-1], hyperparameters=hypers, acquisition_value=self.acquisition_func(new_x)) logger.info("Return %s as incumbent" % (str(self.incumbent))) return self.incumbent
import matplotlib.pyplot as plt import robo.models.neural_network as robo_net import robo.models.bagged_networks as bn from robo.initial_design.init_random_uniform import init_random_uniform logging.basicConfig(stream=sys.stdout, level=logging.INFO) def f(x): return np.sinc(x * 10 - 5).sum(axis=1)[:, None] rng = np.random.RandomState(42) X = init_random_uniform(np.zeros(1), np.ones(1), 20, rng).astype(np.float32) Y = f(X) x = np.linspace(0, 1, 512, dtype=np.float32)[:, None] vals = f(x).astype(np.float32) plt.grid() plt.plot(x[:, 0], f(x)[:, 0], label="true", color="green") plt.plot(X[:, 0], Y[:, 0], "ro") model = bn.BaggedNets(robo_net.SGDNet, num_models=16, bootstrap_with_replacement=True, n_epochs=16384, error_threshold=1e-3, n_units=[32, 32, 32],
def run(self, num_iterations=10, X=None, Y=None): """ The main Bayesian optimization loop Parameters ---------- num_iterations: int The number of iterations X: np.ndarray(N,D) Initial points that are already evaluated Y: np.ndarray(N,1) Function values of the already evaluated points Returns ------- np.ndarray(1,D) Incumbent np.ndarray(1,1) (Estimated) function value of the incumbent """ # Save the time where we start the Bayesian optimization procedure self.time_start = time.time() if X is None and Y is None: self.time_func_eval = np.zeros([self.init_points]) self.time_overhead = np.zeros([self.init_points]) self.X = np.zeros([self.init_points, self.task.n_dims]) self.Y = np.zeros([self.init_points, 1]) init = self.initial_design(self.task.X_lower, self.task.X_upper, N=self.init_points) for i, x in enumerate(init): x = x[np.newaxis, :] logger.info("Evaluate: %s" % x) start_time = time.time() y = self.task.evaluate(x) self.X[i] = x[0, :] self.Y[i] = y[0, :] self.time_func_eval[i] = time.time() - start_time self.time_overhead[i] = 0.0 logger.info("Configuration achieved a performance " "of %f in %f seconds" % (self.Y[i], self.time_func_eval[i])) # Use best point seen so far as incumbent best_idx = np.argmin(self.Y) self.incumbent = np.array([self.X[best_idx]]) self.incumbent_value = np.array([self.Y[best_idx]]) self.incumbents.append(self.incumbent) self.incumbent_values.append(self.incumbent_value) self.runtime.append(time.time() - self.start_time) if self.save_dir is not None and (i) % self.num_save == 0: self.save_iteration(i, hyperparameters=None, acquisition_value=0) self.save_json(i) #print self.X #print self.Y else: self.X = X self.Y = Y self.time_func_eval = np.zeros([self.X.shape[0]]) self.time_overhead = np.zeros([self.X.shape[0]]) self.init_points = X.shape[0] print X.shape, Y.shape for i in range(Y.shape[0]): print "Score:", Y[i][0], X[i] # best = np.argmin(Y) # incumbent = X[best] # incumbent_value = Y[best] # self.incumbents.append(incumbent[np.newaxis, :]) # self.incumbent_values.append(incumbent_value[np.newaxis, :]) # self.runtime.append(time.time() - self.start_time) it = self.init_points while it < num_iterations: self.acquisition_func.update_time(it) logger.info("Start iteration %d ... ", it) start_time = time.time() # Choose next point to evaluate if it % self.train_intervall == 0: do_optimize = True else: do_optimize = False try: new_x = self.choose_next(self.X, self.Y, do_optimize) # Estimate current incumbent start_time_inc = time.time() startpoints = init_random_uniform(self.task.X_lower, self.task.X_upper, self.n_restarts) self.incumbent, self.incumbent_value = \ self.estimator.estimate_incumbent(startpoints) self.incumbents.append(self.incumbent) self.incumbent_values.append(self.incumbent_value) logger.info( "New incumbent %s found in %f seconds with " "estimated performance %f", str(self.incumbent), time.time() - start_time_inc, self.incumbent_value) time_overhead = time.time() - start_time self.time_overhead = np.append(self.time_overhead, np.array([time_overhead])) logger.info("Optimization overhead was %f seconds" % (self.time_overhead[-1])) logger.info("Evaluate candidate %s" % (str(new_x))) start_time = time.time() new_y = self.task.evaluate(new_x) time_func_eval = time.time() - start_time self.time_func_eval = np.append(self.time_func_eval, np.array([time_func_eval])) logger.info("Configuration achieved a performance of %f " % (new_y[0, 0])) logger.info( "Evaluation of this configuration took %f seconds" % (self.time_func_eval[-1])) # Update the data self.X = np.append(self.X, new_x, axis=0) self.Y = np.append(self.Y, new_y, axis=0) self.runtime.append(time.time() - self.start_time) if self.save_dir is not None and (it) % self.num_save == 0: hypers = self.model.hypers self.save_iteration( it, hyperparameters=hypers, acquisition_value=self.acquisition_func(new_x)) self.save_json(it) it += 1 except KeyboardInterrupt: raise Exception except: print "experiment failed, retrying" # TODO: Retrain model and then return the incumbent logger.info("Return %s as incumbent with predicted performance %f" % (str(self.incumbent), self.incumbent_value)) return self.incumbent, self.incumbent_value
def test(self): X_lower = np.array([0]) X_upper = np.array([1]) X = init_random_uniform(X_lower, X_upper, 10) Y = np.sin(X) kernel = george.kernels.Matern52Kernel(np.ones([1]), ndim=1) prior = TophatPrior(-2, 2) model = GaussianProcess(kernel, prior=prior) model.train(X, Y) x_test = init_random_uniform(X_lower, X_upper, 3) # Shape matching predict m, v = model.predict(x_test) assert len(m.shape) == 2 assert m.shape[0] == x_test.shape[0] assert m.shape[1] == 1 assert len(v.shape) == 2 assert v.shape[0] == x_test.shape[0] assert v.shape[1] == x_test.shape[0] #TODO: check gradients # Shape matching function sampling x_ = np.linspace(X_lower, X_upper, 10) x_ = x_[:, np.newaxis] funcs = model.sample_functions(x_, n_funcs=2) assert len(funcs.shape) == 2 assert funcs.shape[0] == 2 assert funcs.shape[1] == x_.shape[0] # Shape matching predict variance x_test1 = np.array([np.random.rand(1)]) x_test2 = np.random.rand(10)[:, np.newaxis] var = model.predict_variance(x_test1, x_test2) assert len(var.shape) == 2 assert var.shape[0] == x_test2.shape[0] assert var.shape[1] == 1 # Check compatibility with all acquisition functions acq_func = EI(model, X_upper=X_upper, X_lower=X_lower) acq_func.update(model) acq_func(x_test) acq_func = PI(model, X_upper=X_upper, X_lower=X_lower) acq_func.update(model) acq_func(x_test) acq_func = LCB(model, X_upper=X_upper, X_lower=X_lower) acq_func.update(model) acq_func(x_test) acq_func = InformationGain(model, X_upper=X_upper, X_lower=X_lower) acq_func.update(model) acq_func(x_test) # Check compatibility with all incumbent estimation methods rec = BestObservation(model, X_lower, X_upper) inc, inc_val = rec.estimate_incumbent(None) assert len(inc.shape) == 2 assert inc.shape[0] == 1 assert inc.shape[1] == X_upper.shape[0] assert len(inc_val.shape) == 2 assert inc_val.shape[0] == 1 assert inc_val.shape[1] == 1 rec = PosteriorMeanOptimization(model, X_lower, X_upper) startpoints = init_random_uniform(X_lower, X_upper, 4) inc, inc_val = rec.estimate_incumbent(startpoints) assert len(inc.shape) == 2 assert inc.shape[0] == 1 assert inc.shape[1] == X_upper.shape[0] assert len(inc_val.shape) == 2 assert inc_val.shape[0] == 1 assert inc_val.shape[1] == 1 rec = PosteriorMeanAndStdOptimization(model, X_lower, X_upper) startpoints = init_random_uniform(X_lower, X_upper, 4) inc, inc_val = rec.estimate_incumbent(startpoints) assert len(inc.shape) == 2 assert inc.shape[0] == 1 assert inc.shape[1] == X_upper.shape[0] assert len(inc_val.shape) == 2 assert inc_val.shape[0] == 1 assert inc_val.shape[1] == 1
def test(self): X_lower = np.array([0]) X_upper = np.array([1]) X = init_random_uniform(X_lower, X_upper, 10) Y = np.sin(X) kernel = george.kernels.Matern52Kernel(np.ones([1]), ndim=1) prior = TophatPrior(-2, 2) model = GaussianProcess(kernel, prior=prior) model.train(X, Y) x_test = init_random_uniform(X_lower, X_upper, 3) # Shape matching predict m, v = model.predict(x_test) assert len(m.shape) == 2 assert m.shape[0] == x_test.shape[0] assert m.shape[1] == 1 assert len(v.shape) == 2 assert v.shape[0] == x_test.shape[0] assert v.shape[1] == x_test.shape[0] #TODO: check gradients # Shape matching function sampling x_ = np.linspace(X_lower, X_upper, 10) x_ = x_[:, np.newaxis] funcs = model.sample_functions(x_, n_funcs=2) assert len(funcs.shape) == 2 assert funcs.shape[0] == 2 assert funcs.shape[1] == x_.shape[0] # Shape matching predict variance x_test1 = np.array([np.random.rand(1)]) x_test2 = np.random.rand(10)[:, np.newaxis] var = model.predict_variance(x_test1, x_test2) assert len(var.shape) == 2 assert var.shape[0] == x_test2.shape[0] assert var.shape[1] == 1 # Check compatibility with all acquisition functions acq_func = EI(model, X_upper=X_upper, X_lower=X_lower) acq_func.update(model) acq_func(x_test) acq_func = PI(model, X_upper=X_upper, X_lower=X_lower) acq_func.update(model) acq_func(x_test) acq_func = LCB(model, X_upper=X_upper, X_lower=X_lower) acq_func.update(model) acq_func(x_test) acq_func = InformationGain(model, X_upper=X_upper, X_lower=X_lower) acq_func.update(model) acq_func(x_test) # Check compatibility with all incumbent estimation methods rec = BestObservation(model, X_lower, X_upper) inc, inc_val = rec.estimate_incumbent(None) assert len(inc.shape) == 2 assert inc.shape[0] == 1 assert inc.shape[1] == X_upper.shape[0] assert len(inc_val.shape) == 2 assert inc_val.shape[0] == 1 assert inc_val.shape[1] == 1 rec = PosteriorMeanOptimization(model, X_lower, X_upper) startpoints = init_random_uniform(X_lower, X_upper, 4) inc, inc_val = rec.estimate_incumbent(startpoints) assert len(inc.shape) == 2 assert inc.shape[0] == 1 assert inc.shape[1] == X_upper.shape[0] assert len(inc_val.shape) == 2 assert inc_val.shape[0] == 1 assert inc_val.shape[1] == 1 rec = PosteriorMeanAndStdOptimization(model, X_lower, X_upper) startpoints = init_random_uniform(X_lower, X_upper, 4) inc, inc_val = rec.estimate_incumbent(startpoints) assert len(inc.shape) == 2 assert inc.shape[0] == 1 assert inc.shape[1] == X_upper.shape[0] assert len(inc_val.shape) == 2 assert inc_val.shape[0] == 1 assert inc_val.shape[1] == 1
import numpy as np import matplotlib.pyplot as plt import robo.models.neural_network as robo_net import robo.models.bagged_networks as bn from robo.initial_design.init_random_uniform import init_random_uniform logging.basicConfig(stream=sys.stdout, level=logging.INFO) def f(x): return np.sinc(x * 10 - 5).sum(axis=1)[:, None] rng = np.random.RandomState(42) X = init_random_uniform(np.zeros(1), np.ones(1), 20, rng).astype(np.float32) Y = f(X) x = np.linspace(0, 1, 512, dtype=np.float32)[:, None] vals = f(x).astype(np.float32) plt.grid() plt.plot(x[:, 0], f(x)[:, 0], label="true", color="green") plt.plot(X[:, 0], Y[:, 0], "ro") model = bn.BaggedNets(robo_net.SGDNet, num_models=16, bootstrap_with_replacement=True, n_epochs=16384, error_threshold=1e-3, n_units=[32, 32, 32], dropout=0, batch_size=10, learning_rate=1e-3, shuffle_batches=True)