interpolation='bicubic', origin='lower', cmap=plt.get_cmap('hot'), norm=matplotlib.colors.LogNorm(), aspect='auto') resampledLimits = (resampled.min(), resampled.max()) plt.colorbar(implt, ax=ax1) # Add optimum ax1.plot(xactions, yactions_exact) # Create predicted contour plot x = np.linspace(0, 1, num=50) y = np.linspace(0, 1, num=50) X, Y = np.meshgrid(x, y) # Predict mean mean, var = model.predict( np.transpose(np.array((X.flatten(order='C'), Y.flatten(order='C'))))) Z = np.reshape(mean, X.shape, order='C') CS = ax2.contour(X, Y, np.maximum(Z, 1e-3), levels=[ 1e-3, 3e-3, 1e-2, 3e-2, 1e-1, 3e-1, 1e0, 3e0, 1e1, 3e1, 1e2, 3e2, 1e3, 3e3, 1e4 ], norm=matplotlib.colors.LogNorm()) ax2.clabel(CS, inline=1, fontsize=10) ax2.set_title('predicted function (payoff)') ax2.set_xlabel('context') ax2.set_ylabel('action') ax2.set_ylim((0, 1))
# 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) # 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() ax1 = fig.add_subplot(1, 1, 1) plotting_range = np.linspace(X_lower[0], X_upper[0], num=1000) ax1.plot(plotting_range, objective_function(plotting_range[:, np.newaxis]), color='b', linestyle="--") _min_y1, _max_y1 = ax1.get_ylim() model.visualize(ax1, X_lower[0], X_upper[0]) _min_y2, _max_y2 = ax1.get_ylim() ax1.set_ylim(min(_min_y1, _min_y2), max(_max_y1, _max_y2)) mu, var = model.predict(new_x) ax1.plot(new_x[0], mu[0], "r.", markeredgewidth=5.0) ax2 = acquisition_func.plot(fig, X_lower[0], X_upper[0], plot_attr={"color": "red"}, resolution=1000) plt.show(block=True)
ax1.set_ylim((0, 1)) imx, imy = np.mgrid[0:1:100j, 0:1:100j] resampled = griddata((X.flatten(), Y.flatten()), Z.flatten(), (imx, imy)) implt = ax1.imshow(resampled.T, extent=(0, 1, 0, 1), interpolation='bicubic', origin='lower', cmap=plt.get_cmap('hot'), norm=matplotlib.colors.LogNorm(), aspect='auto') resampledLimits = (resampled.min(), resampled.max()) plt.colorbar(implt, ax=ax1) # Add optimum ax1.plot(xactions, yactions_exact) # Create predicted contour plot x = np.linspace(0, 1, num=50) y = np.linspace(0, 1, num=50) X, Y = np.meshgrid(x, y) # Predict mean mean, var = model.predict(np.transpose(np.array((X.flatten(order='C'), Y.flatten(order='C'))))) Z = np.reshape(mean, X.shape, order='C') CS = ax2.contour(X, Y, np.maximum(Z, 1e-3), levels=[1e-3, 3e-3, 1e-2, 3e-2, 1e-1, 3e-1, 1e0, 3e0, 1e1, 3e1, 1e2, 3e2, 1e3, 3e3, 1e4], norm=matplotlib.colors.LogNorm()) ax2.clabel(CS, inline=1, fontsize=10) ax2.set_title('predicted function (payoff)') ax2.set_xlabel('context') ax2.set_ylabel('action') ax2.set_ylim((0, 1)) imx, imy = np.mgrid[0:1:100j, 0:1:100j] resampled = griddata((X.flatten(), Y.flatten()), Z.flatten(), (imx, imy)) implt = ax2.imshow(resampled.T, extent=(0, 1, 0, 1), interpolation='bicubic', origin='lower', cmap=plt.get_cmap('hot'), norm=matplotlib.colors.LogNorm(), aspect='auto', vmin=resampledLimits[0], vmax=resampledLimits[1]) plt.colorbar(implt, ax=ax2) ax2.plot(bo.X[:, 0], bo.X[:, 1], 'x')
# 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() ax1 = fig.add_subplot(1, 1, 1) plotting_range = np.linspace(X_lower[0], X_upper[0], num=1000) ax1.plot(plotting_range, objective_function(plotting_range[:, np.newaxis]), color='b', linestyle="--") _min_y1, _max_y1 = ax1.get_ylim() model.visualize(ax1, X_lower[0], X_upper[0]) _min_y2, _max_y2 = ax1.get_ylim() ax1.set_ylim(min(_min_y1, _min_y2), max(_max_y1, _max_y2)) mu, var = model.predict(new_x) ax1.plot(new_x[0], mu[0], "r.", markeredgewidth=5.0) ax2 = acquisition_func.plot(fig, X_lower[0], X_upper[0], plot_attr={"color": "red"}, resolution=1000) plt.show(block=True)