def run(): # Defining the bounds and dimensions of the input space X_lower = np.array([0]) X_upper = np.array([6]) dims = 1 # Set the method that we will use to optimize the acquisition function maximizer = stochastic_local_search # Defining the method to model the objective function kernel = GPy.kern.Matern52(input_dim=dims) model = GPyModel(kernel, optimize=True, noise_variance=1e-4, num_restarts=10) # The acquisition function that we optimize in order to pick a new x acquisition_func = EI( model, X_upper=X_upper, X_lower=X_lower, compute_incumbent=compute_incumbent, par=0.1 ) # par is the minimum improvement that a point has to obtain # Draw one random point and evaluate it to initialize BO X = np.array([np.random.uniform(X_lower, X_upper, dims)]) Y = objective_function(X) # Fit the model on the data we observed so far model.train(X, Y) # Update the acquisition function model with the retrained model acquisition_func.update(model) # Optimize the acquisition function to obtain a new point new_x = maximizer(acquisition_func, X_lower, X_upper) # Evaluate the point and add the new observation to our set of previous seen points new_y = objective_function(np.array(new_x)) X = np.append(X, new_x, axis=0) Y = np.append(Y, new_y, axis=0) # Visualize the objective function, model and the acquisition function fig = plt.figure() # Sub plot for the model and the objective function ax1 = fig.add_subplot(2, 1, 1) # Sub plot for the acquisition function ax2 = fig.add_subplot(2, 1, 2) resolution = 0.1 # Call plot_model function ax1 = plotting.plot_model(model, X_lower, X_upper, ax1, resolution, "b", "blue", "Prosterior Mean", 3, True) # Call plot_objective_function ax1 = plotting.plot_objective_function( objective_function, X_lower, X_upper, X, Y, ax1, resolution, "black", "ObjectiveFunction", True ) ax1.set_title("Model + Objective Function") # Call plot_acquisition_function ax2 = plotting.plot_acquisition_function( acquisition_func, X_lower, X_upper, X, ax2, resolution, "AcquisitionFunction", True ) plt.savefig("test2.png") os.system("eog test2.png&")
def test(self): X_upper = np.array([2.1]) X_lower = np.array([-2.1]) best = np.argmin(self.y) incumbent = self.x[best] ei_par = EI(self.model, X_upper=X_upper, X_lower=X_lower, compute_incumbent=compute_incumbent, par=0.0) out0 = ei_par(incumbent[:, np.newaxis], derivative=True) value0 = out0[0] derivative0 = out0[1] assert (value0[0] <= 1e-5) x_value = incumbent + np.random.random_integers(1, 10) / 1000. out1 = ei_par(x_value[:, np.newaxis], derivative=True) value1 = out1[0] derivative1 = out1[1] assert (np.all(value0 < value1)) assert (np.all(np.abs(derivative0) < np.abs(derivative1)))
from robo import BayesianOptimization # def objective_function(x): # return np.sin(x) + 0.1 * np.cos(10 * x) def objective_function(x): return np.sin(3 * x) * 4 * (x - 1) * (x + 2) X_lower = np.array([0]) X_upper = np.array([6]) dims = 1 kernel = GPy.kern.Matern52(input_dim=dims, lengthscale=0.01) model = GPyModel(kernel, optimize=True, noise_variance=1e-8, num_restarts=10) acquisition_func = EI(model, X_upper=X_upper, X_lower=X_lower, compute_incumbent=compute_incumbent, par=0.1) maximizer = DIRECT bo = BayesianOptimization(acquisition_fkt=acquisition_func, model=model, maximize_fkt=maximizer, X_lower=X_lower, X_upper=X_upper, dims=dims, objective_fkt=objective_function) bo.run(num_iterations=5) X, Y = bo.get_observations() X = X[:-1] Y = Y[:-1]
## print dims # Set the method that we will use to optimize the acquisition function maximizer = stochastic_local_search # Defining the method to model the objective function kernel = GPy.kern.Exponential(input_dim=dims) model = GPyModel(kernel, optimize=True, noise_variance=1e-5, num_restarts=10) acqf = 'ucb' # The acquisition function that we optimize in order to pick a new x if acqf == 'ei': acquisition_func = EI( model, X_upper=X_upper, X_lower=X_lower, compute_incumbent=compute_incumbent, par=0.05) # par is the minimum improvement that a point has to obtain else: acquisition_func = UCB(model, X_upper=X_upper, X_lower=X_lower, compute_incumbent=compute_incumbent, par=5) xv = np.array([np.random.uniform(X_lower, X_upper, dims)]) xv = np.array([[-10.46778014, -7.01362049]]) print xv dimfold = 1 slowness = X.shape[0] / dimfold cvfolds = X.shape[0] / dimfold
def objective_function(x): return np.sin(3 * x) * 4 * (x - 1) * (x + 2) X_lower = np.array([0]) X_upper = np.array([6]) dims = 1 kernel = GPy.kern.Matern52(input_dim=dims) model = GPyModel(kernel, optimize=True, noise_variance=1e-4, num_restarts=10) proposal_measurement = EI(model, X_upper=X_upper, X_lower=X_lower, compute_incumbent=compute_incumbent, par=0.1) acquisition_func = Entropy(model, X_lower, X_upper, sampling_acquisition=proposal_measurement) maximizer = grid_search bo = BayesianOptimization(acquisition_fkt=acquisition_func, model=model, maximize_fkt=maximizer, X_lower=X_lower, X_upper=X_upper, dims=dims, objective_fkt=objective_function)
return np.sin(3 * x) * 4 * (x - 1) * (x + 2) # Defining the bounds and dimensions of the input space X_lower = np.array([0]) X_upper = np.array([6]) dims = 1 # Set the method that we will use to optimize the acquisition function maximizer = stochastic_local_search # Defining the method to model the objective function kernel = GPy.kern.Matern52(input_dim=dims) model = GPyModel(kernel, optimize=True, noise_variance=1e-4, num_restarts=10) # The acquisition function that we optimize in order to pick a new x acquisition_func = EI(model, X_upper=X_upper, X_lower=X_lower, compute_incumbent=compute_incumbent, par=0.1) # par is the minimum improvement that a point has to obtain # Draw one random point and evaluate it to initialize BO X = np.array([np.random.uniform(X_lower, X_upper, dims)]) Y = objective_function(X) # This is the main Bayesian optimization loop for i in xrange(10): # Fit the model on the data we observed so far model.train(X, Y) # Update the acquisition function model with the retrained model acquisition_func.update(model) # Optimize the acquisition function to obtain a new point new_x = maximizer(acquisition_func, X_lower, X_upper)
# Defining the bounds and dimensions of the input space X_lower = np.array([0]) X_upper = np.array([6]) dims = 1 # Set the method that we will use to optimize the acquisition function maximizer = stochastic_local_search # Defining the method to model the objective function kernel = GPy.kern.Matern52(input_dim=dims) model = GPyModel(kernel, optimize=True, noise_variance=1e-4, num_restarts=10) # The acquisition function that we optimize in order to pick a new x acquisition_func = EI( model, X_upper=X_upper, X_lower=X_lower, compute_incumbent=compute_incumbent, par=0.1) # par is the minimum improvement that a point has to obtain # Draw one random point and evaluate it to initialize BO X = np.array([np.random.uniform(X_lower, X_upper, dims)]) Y = objective_function(X) # This is the main Bayesian optimization loop for i in xrange(10): # Fit the model on the data we observed so far model.train(X, Y) # Update the acquisition function model with the retrained model acquisition_func.update(model)
def __init__(self): self.X_lower = np.array([0]) self.X_upper = np.array([7]) self.n_dims = 1 def objective_function(self, x): return np.sin(3 * x) * 4 * (x - 1) * (x + 2) task = ExampleTask() # Defining the method to model the objective function kernel = GPy.kern.Matern52(input_dim=task.n_dims) model = GPyModel(kernel, optimize=True, noise_variance=1e-4, num_restarts=10) # The acquisition function that we optimize in order to pick a new x acquisition_func = EI(model, X_upper=task.X_upper, X_lower=task.X_lower, compute_incumbent=compute_incumbent, par=0.1) # par is the minimum improvement that a point has to obtain # Set the method that we will use to optimize the acquisition function maximizer = GridSearch(acquisition_func, task.X_lower, task.X_upper) # Draw one random point and evaluate it to initialize BO X = np.array([np.random.uniform(task.X_lower, task.X_upper, task.n_dims)]) Y = task.objective_function(X) # This is the main Bayesian optimization loop for i in range(10): # Fit the model on the data we observed so far model.train(X, Y)