In [ ]:
%load_ext autoreload
%autoreload 2
In [ ]:
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
import matplotlib.pyplot as plt
import seaborn as sns; sns.set() # prettify matplotlib
import numpy as np
import sklearn.gaussian_process as gp
import GPy
In [ ]:
# local modules
import turbo as tb
import turbo.modules as tm
import turbo.plotting as tp
import turbo.gui.jupyter as tg
In [ ]:
# make deterministic
np.random.seed(42)
Function to optimize:
In [ ]:
#f = lambda x: 1 * x * np.cos(x)
f = lambda x: 100 * np.sin(x**2/5) * np.cos(x*1.5) + 100 + \
np.random.normal(0, 6, size=None if isinstance(x, float) else x.shape)
xmin, xmax = 0, 12
xs = np.linspace(xmin, xmax, num=200)
ys = f(xs)
best_y = np.min(ys)
best_x = xs[np.argmin(ys)]
In [ ]:
plt.figure(figsize=(12, 4))
plt.plot(xs, ys, 'g-', label='objective')
plt.plot(best_x, best_y, 'bo', label='optima')
plt.legend(loc='upper left')
plt.margins(0.01, 0.1)
plt.xlabel(r'$x$')
plt.ylabel(r'$f(x)$')
plt.show()
In [ ]:
pre_phase = 6
max_trials = 35
op = tb.Optimiser(f, 'min', bounds=[('x', xmin, xmax)], pre_phase_trials=pre_phase, settings_preset='default')
#op.surrogate_factory = tm.SciKitGPSurrogate(model_params=dict(kernel=1.0*gp.kernels.RBF()+gp.kernels.WhiteKernel(), normalize_y=True))
kernel = GPy.kern.RBF(input_dim=1) + GPy.kern.White(input_dim=1)
#kernel.white.variance.set_prior(GPy.priors.Gamma.from_EV(0.8, 0.4))
op.surrogate = tm.GPySurrogate(model_params=dict(kernel=kernel, noise_var=1e-10, normalizer=True),
training_iterations=lambda trial_num: [8,4,2][(trial_num-pre_phase)%3])
def beta(trial_num):
if trial_num < 15:
return 1.5
else:
m = 0.10
return m*trial_num - m*15+1.5
op.acquisition = tm.UCB(beta=beta)
rec = tb.Recorder(op)
In [ ]:
tp.plot_acquisition_parameter_function(op.acquisition.beta, pre_phase, max_trials)
In [ ]:
tg.OptimiserProgressBar(op)
op.run(max_trials=max_trials)
In [ ]:
op.get_incumbent()
In [ ]:
tp.plot_overview(rec)
In [ ]:
tp.plot_error(rec, true_best=best_y, log_scale=False, fig_ax=plt.subplots(figsize=(8, 3)));
In [ ]:
tp.interactive_plot_trial_1D(rec, param='x', true_objective=f, n_sigma='beta', ylim=(-10, 220))
In [ ]:
#figs = (tp.plot_trial_1D(rec, param='x', trial_num=n, true_objective=f, n_sigma='beta', ylim=(-10, 220))
# for n, _ in rec.get_sorted_trials())
#tp.save_animation(figs, 'animation.mp4')
In [ ]:
ro = tb.Optimiser(f, 'min', bounds=[('x', xmin, xmax)], pre_phase_trials=float('inf'), settings_preset='random_search')
rrec = tb.Recorder(ro)
In [ ]:
tg.OptimiserProgressBar(ro)
ro.run(max_trials=100)
In [ ]:
ro.get_incumbent()
In [ ]:
tp.plot_error(rrec, true_best=best_y, log_scale=False, fig_ax=plt.subplots(figsize=(8, 3)));
In [ ]:
tp.interactive_plot_trial_1D(rrec, param='x', true_objective=f)
In [ ]: