def optimize(self, num_iter=10): if not self.initialised: self._init_model(num_initial_evaluations=self.num_initial_evaluations) logger.info("Optimising %d iterations" % num_iter) for _ in range(num_iter): index = policies.EI(self.model, self.bounds, self.X) xnext, _ = solvers.solve_lbfgs(index, self.bounds) # observe and update model ynext = self._eval(xnext) self.model.add_data(xnext, ynext) self.X.append(xnext) self.Y.append(ynext) # best point so far self.xbest = recommenders.best_incumbent(self.model, self.bounds, self.X) return self._search_domain_to_param_dict(self.xbest)
def _init_model(self, num_initial_evaluations, previous_model=None): logger.info("Initial fitting using %d points" % num_initial_evaluations) # get initial data and some test points. self.X = list(inits.init_latin(self.bounds, num_initial_evaluations)) self.Y = [self._eval(x) for x in self.X] # initial values for kernel parameters, taken from pybo code sn2 = 1e-6 rho = np.max(self.Y) - np.min(self.Y) rho = 1. if (rho < 1e-1) else rho ell = 0.25 * (self.bounds[:, 1] - self.bounds[:, 0]) if previous_model is None: # use data mean as GP mean bias = np.mean(self.Y) self.model = reggie.make_gp(sn2, rho, ell, bias) # define priors if gp was created from scratch self.model.params['mean.bias'].set_prior('normal', bias, rho) self.model.params['like.sn2'].set_prior('horseshoe', 0.1) self.model.params['kern.rho'].set_prior('lognormal', np.log(rho), 1.) self.model.params['kern.ell'].set_prior('uniform', ell / 100, ell * 10) else: # if there has been a previous model, use it as mean like = previous_model._like kern = previous_model._kern mean = GPMean(previous_model) self.model = reggie.GP(like, kern, mean) # initialize the MCMC inference meta-model and add data self.model.add_data(self.X, self.Y) self.model = reggie.MCMC(self.model, n=10, burn=100) # best point so far self.xbest = recommenders.best_incumbent(self.model, self.bounds, self.X) self.initialised = True
def main(): """Run the demo.""" # define the bounds over which we'll optimize, the optimal x for comparison, # and a sequence of test points bounds = np.array([[-5, 10.], [0, 15]]) xopt = np.array([np.pi, 2.275]) x1, x2 = np.meshgrid(np.linspace(*bounds[0], num=100), np.linspace(*bounds[1], num=100)) xx = np.c_[x1.flatten(), x2.flatten()] # get initial data and some test points. X = list(inits.init_latin(bounds, 6)) Y = [f(x_) for x_ in X] F = list() # initialize the model model = make_gp(0.01, 10, [1., 1.], 0) model.add_data(X, Y) # set a prior on the parameters model.params['like.sn2'].set_prior('uniform', 0.005, 0.015) model.params['kern.rho'].set_prior('lognormal', 0, 3) model.params['kern.ell'].set_prior('lognormal', 0, 3) model.params['mean.bias'].set_prior('normal', 0, 20) # make a model which samples parameters model = MCMC(model, n=10, rng=None) # create a new figure fig = figure(figsize=(10, 6)) while True: # get index to solve it and plot it index = policies.EI(model, bounds, X, xi=0.1) # get the recommendation and the next query xbest = recommenders.best_incumbent(model, bounds, X) xnext, _ = solvers.solve_lbfgs(index, bounds) # observe and update model ynext = f(xnext) model.add_data(xnext, ynext) # evaluate the posterior and the acquisition function mu, s2 = model.predict(xx) # record our data and update the model X.append(xnext) Y.append(ynext) F.append(f(xbest)) fig.clear() ax1 = fig.add_subplotspec((2, 2), (0, 0), hidex=True) ax2 = fig.add_subplotspec((2, 2), (1, 0), hidey=True, sharex=ax1) ax3 = fig.add_subplotspec((2, 2), (0, 1), rowspan=2) # plot the posterior and data ax1.contourf(x1, x2, mu.reshape(x1.shape), alpha=0.4) X_ = np.array(X) ax1.scatter(X_[:-1, 0], X_[:-1, 1], marker='.') ax1.scatter(xbest[0], xbest[1], linewidths=3, marker='o', color='r') ax1.scatter(xnext[0], xnext[1], linewidths=3, marker='o', color='g') ax1.set_xlim(*bounds[0]) ax1.set_ylim(*bounds[1]) ax1.set_title('current model (xbest and xnext)') # plot the acquisition function ax2.contourf(x1, x2, index(xx).reshape(x1.shape), alpha=0.5) ax2.scatter(xbest[0], xbest[1], linewidths=3, marker='o', color='r') ax2.scatter(xnext[0], xnext[1], linewidths=3, marker='o', color='g') ax2.set_xlim(*bounds[0]) ax2.set_ylim(*bounds[1]) ax2.set_title('current policy (xnext)') # plot the latent function at recomended points ax3.axhline(f(xopt)) ax3.plot(F) ax3.set_ylim(-1., 0.) ax3.set_title('value of recommendation') # draw fig.canvas.draw() show(block=False)
def main(): """Run the demo.""" # define the bounds over which we'll optimize, the optimal x for # comparison, and a sequence of test points bounds = np.array([[0.5, 2.5]]) xopt = 0.54856343 fopt = f(xopt) x = np.linspace(bounds[0][0], bounds[0][1], 500) # get initial data and some test points. X = list(inits.init_latin(bounds, 3)) Y = [f(x_) for x_ in X] F = [] # initialize the model model = make_gp(0.01, 1.9, 0.1, 0) model.add_data(X, Y) # set a prior on the parameters model.params['like.sn2'].set_prior('uniform', 0.005, 0.015) model.params['kern.rho'].set_prior('lognormal', 0, 100) model.params['kern.ell'].set_prior('lognormal', 0, 10) model.params['mean.bias'].set_prior('normal', 0, 20) # make a model which samples parameters model = MCMC(model, n=20, rng=None) # create a new figure fig = figure(figsize=(10, 6)) while True: # get acquisition function (or index) index = policies.EI(model, bounds, X, xi=0.1) # get the recommendation and the next query xbest = recommenders.best_incumbent(model, bounds, X) xnext, _ = solvers.solve_lbfgs(index, bounds) ynext = f(xnext) # evaluate the posterior before updating the model for plotting mu, s2 = model.predict(x[:, None]) # record our data and update the model X.append(xnext) Y.append(ynext) F.append(f(xbest)) model.add_data(xnext, ynext) # PLOT EVERYTHING fig.clear() ax1 = fig.add_subplotspec((2, 2), (0, 0), hidex=True) ax2 = fig.add_subplotspec((2, 2), (1, 0), hidey=True, sharex=ax1) ax3 = fig.add_subplotspec((2, 2), (0, 1), rowspan=2) # plot the posterior and data ax1.plot_banded(x, mu, 2*np.sqrt(s2)) ax1.scatter(np.ravel(X), Y) ax1.axvline(xbest) ax1.axvline(xnext, color='g') ax1.set_ylim(-6, 3) ax1.set_title('current model (xbest and xnext)') # plot the acquisition function ax2.plot_banded(x, index(x[:, None])) ax2.axvline(xnext, color='g') ax2.set_xlim(*bounds) ax2.set_title('current policy (xnext)') # plot the latent function at recomended points ax3.plot(F) ax3.axhline(fopt) ax3.set_ylim(0.4, 0.9) ax3.set_title('value of recommendation') # draw fig.canvas.draw() show(block=False)