def setUp(self): f = lambda x: np.sin(2 * np.pi * 10. * x) + np.sin(2 * np.pi * 13. * x) Ts = 1 / 150.0 self.x = np.atleast_2d(np.arange(0, 1, Ts)).T self.xt = np.atleast_2d(np.arange(0, 2, Ts)).T self.y = np.atleast_2d(f(self.x).ravel()).T self.yt = np.atleast_2d(f(self.xt).ravel()).T self.hyps = { 'mean': np.array([]), 'lik': [np.log(0.5)], 'cov': np.log([ 2.54846959e-01, 5.24927544e-01, 2.15436686e-01, 2.86117061e-01, 1.81531436e-01, 1.44032999e-01, 1.29493734e+01, 9.92190150e+00, 1.85152636e+02, 7.49453588e+01, 1.81532400e+00, 2.28497943e+01, 1.03092807e-01, 4.88398945e-02, 1.94985263e+01, 3.19829864e+01, 2.12244665e+00, 8.39227875e-01 ]) } self.hypGP = gp.GaussianProcess(hyp=self.hyps, inf=gp.inferences.exact, mean=gp.means.zero, cov=gp.kernels.spectral_mixture, lik=gp.likelihoods.gaussian, xtrain=self.x, ytrain=self.y, xtest=self.xt, ytest=self.yt)
l1Optimizer = 'COBYLA' l1Options = {'maxiter': 100} l2Optimizer = 'L-BFGS-B' l2Options = {'maxiter': 1000} # Noise std. deviation sn = 1.0 # Initialize hyperparams initArgs = {'Q': Q, 'x': x, 'y': y, 'samplingFreq': 1, 'nPeaks': Q, 'sn': sn} hypGuess = gp.core.initSMParamsFourier(**initArgs) # Initialize GP object hypGP = gp.GaussianProcess(hyp=hypGuess, inf=infFunc, mean=meanFunc, cov=covFunc, lik=likFunc, xtrain=x, ytrain=y, xtest=xt) # Random starts for itr in range(nItr): # Start over hypGuess = gp.core.initSMParamsFourier(**initArgs) hypGP.hyp = hypGuess # Optimize the guessed hyperparams try: hypGP.train(l1Optimizer, l1Options) except Exception as e: print "Iteration: ", itr, "FAILED" print "\t", e continue
import torch import numpy as np import kernel as k import gaussian_process as gp import bayes_opt as bo def ackley(x): exp1 = -0.2*(0.5*x.pow(2)).sum().pow(0.5) exp2 = 0.5*torch.cos(2*np.pi*x).sum() y = np.e + 20 - 20*torch.exp(exp1) - torch.exp(exp2) return y n_dimensions = 2 bounds = torch.tensor([[-10.0, 10.0]]).expand(n_dimensions, 2) iters = 50 kernel = k.Matern32Kernel(1.0, 1.0) model = gp.GaussianProcess(kernel, 0.001) acq_func = bo.Acquisition(1.0) optimiser = bo.BayesOptimiser(model, acq_func) x_min, y_min, x_hist, y_hist = optimiser.minimise(ackley, bounds, iters, iter_loops=10)
mu_grad = gp.mu_grad(x) sigma_grad = gp.sigma_grad(x) grad = mu_grad - self.beta * sigma_grad return grad def ackley(x): x = torch.from_numpy(x) x = x.view(-1, 1) exp1 = -0.2 * (0.5 * x.pow(2).sum(1)).pow(0.5) exp2 = 0.5 * torch.cos(2 * np.pi * x).sum(1) y = np.e + 20 - 20 * torch.exp(exp1) - torch.exp(exp2) return y.data.numpy() bounds = torch.tensor([[-8.0, 8.0]]).expand(1, 2) iters = 20 kernel = krn.Matern32Kernel(1.0, 1.0, 'cpu') * \ krn.Matern52Kernel(1.0, 1.0, 'cpu') gp = gaussp.GaussianProcess(kernel, alpha=0.0001) acq_func = Acquisition(1.0) optimiser = BayesOptimiser(gp, acq_func) x_min, y_min, xp, yp = optimiser.minimise(ackley, bounds, iters) filenames = glob.glob('ackley_frame*.png') filenames = sorted(filenames) gif_images = [] for filename in filenames: gif_images.append(imageio.imread(filename)) imageio.mimsave('ackley.gif', gif_images, duration=1 / 2)