Last active
February 1, 2018 13:58
-
-
Save iaroslav-ai/42bf1088c129b497dc097ee518d23d84 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from skopt import gp_minimize | |
from skopt.space import Real | |
from skopt.plots import plot_convergence | |
from skopt.plots import plot_objective, plot_evaluations | |
import numpy as np | |
np.random.seed(2) | |
# a dummy objective with 6 dimensions | |
dims = [Real(-1.0, 1.0, name=name) for name in ['a', 'b', 'c', 'd', 'e', 'f']] | |
N = len(dims) | |
# weights of nn that serves as objective function | |
a = 2.0 | |
M = np.random.randn(N, N) * a | |
b = np.random.randn(N) * a | |
s = np.random.randn(N) * a | |
def fnc(x): | |
# correlate the inputs | |
h = np.dot(M, x) + b | |
# apply non - linear function | |
h = np.tanh(h) | |
# final output | |
h = np.dot(h, s) | |
return h | |
# run the minimization | |
search_result = gp_minimize(fnc, dims, n_calls=200) | |
# get the search space and best x, y, model | |
space = search_result.space | |
model = search_result.models[-1] | |
best_x = search_result.x | |
best_y = search_result.fun | |
# sample points from search space | |
random_samples = np.array(space.rvs(n_samples=1000000)) | |
best_x_sample = np.array([best_x]) | |
# make estimations with surrogate | |
y_random = model.predict(space.transform(random_samples)) | |
best_y_estimated = model.predict(space.transform(best_x_sample))[0] | |
# print best found actual objective value | |
print("Best objective value found:") | |
print(search_result.fun) | |
# print the best estimated objective | |
print("Surrogate value at best x:") | |
print(best_y_estimated) | |
# print the minimum objective found by random sampling from parameter space | |
print("Best objective value found by sampling 1000000 random points:") | |
print(np.min(y_random)) | |
# visualize plots for comparison | |
import matplotlib.pyplot as plt | |
plot_objective(result=search_result) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment