SheffieldML / GPyOpt

Gaussian Process Optimization using GPy
BSD 3-Clause "New" or "Revised" License
929 stars 261 forks source link

What policy is used when evaluating external functions? #340

Open lewisscola opened 4 years ago

lewisscola commented 4 years ago

Hi, I see the example that can do Bayesian Optimization on external functions. The example is in this website:https://nbviewer.jupyter.org/github/SheffieldML/GPyOpt/blob/devel/manual/GPyOpt_external_objective_evaluation.ipynb

I'm just wondering which policy is used in the BO, EI, PI or LCB?

Thank you for your help!

ekalosak commented 4 years ago

You should feel free to look at the source code associated with some of these objects. Specifically, if you look at the source for GPyOpt.methods.BayesianOptimization, you'll see:

class BayesianOptimization(BO):
    ...
    def __init__(self, f, domain = None, constraints = None, cost_withGradients = None, model_type = 'GP', X = None, Y = None,
        initial_design_numdata = 5, initial_design_type='random', acquisition_type ='EI', normalize_Y = True,
        exact_feval = False, acquisition_optimizer_type = 'lbfgs', model_update_interval=1, evaluator_type = 'sequential',
        batch_size = 1, num_cores = 1, verbosity=False, verbosity_model = False, maximize=False, de_duplication=False, **kwargs):
    ...
lewisscola commented 4 years ago

Thank you for your reply! When I evaluate the above external function, I want to know the current optimal point of the model. But I encounter some errors. image Could you tell me how to know the optimal point?

apaleyes commented 4 years ago

This needs more code to troubleshoot. I suspect optimization hasn't run yet, which is why x_opt and fx_opt aren't initialized

lewisscola commented 4 years ago

Yes, it is because optimization hasn't run. However, I want to know the optimal value of the external function, which has no function formula. And it can not run optimization without objective function. image image

Is this means we can not get the current optiaml point when evaluating external functions?

ekalosak commented 4 years ago

Please post the entire code you're using (not in a screenshot, try using the GitHub code chunk format with ```) so it's easier for us to see what's going on.

Quickly, looking at your screenshot, you appear to be trying to optimize f = None, which is precisely what the Exception is complaining about. If you want to do external function optimization, I'd highly recommend using ModularBayesianOptimization. As a quick-and-dirty patch, you might try f = lambda x: return sum(x) or some arbitrary placeholder function, but this is definitely dirty and not recommended.

lewisscola commented 4 years ago

Thank you so much for your reply! yes, I'm trying to optimize the external function. Then I'm just curious how GpyOpt to do Bayesian Optimization on a black-box function(have no exact function formula), which I think is equal to an external function. Do you mean I have to use ModularBaysianOptimization to do optimize a black-box function?

Another thing I don't understand is that when I change max_iter to 1, why the convergence plot will display 5 iterations?

myBopt = GPyOpt.methods.BayesianOptimization(f=g,
domain=bounds2d1, acquisition_type='EI', exact_feval = True)

max_iter = 1 # evaluation budget max_time = 60 # time budget eps = 10e-6 # Minimum allows distance between the las two observations```

myBopt.run_optimization(max_iter, max_time, eps)
myBopt.plot_convergence()

image I think when max_iter=1, it should be just 1 iteration in the convergence plot, could you help me explain it ?

apaleyes commented 4 years ago

Have you read the tutorials on GPyOpt? This one explains how to run optimization without excplicitly specifying the objective fuction pretty clearly:

https://nbviewer.jupyter.org/github/SheffieldML/GPyOpt/blob/master/manual/GPyOpt_external_objective_evaluation.ipynb

I suspect you are not providing initial values to the bayes opt object. Either initial values or explicit funciton to evaluate are required.

lewisscola commented 4 years ago

Hi, Yes, I have read the tutorials. For the external function BO, I have initial values. but the problem is that when I trying to optimize f = None, I can not run BO as Eric said in the above discussion.

Could you help me explain my second question above that the convergence plot shows 5 iterations when the max_iter=1?

Have you read the tutorials on GPyOpt? This one explains how to run optimization without excplicitly specifying the objective fuction pretty clearly:

https://nbviewer.jupyter.org/github/SheffieldML/GPyOpt/blob/master/manual/GPyOpt_external_objective_evaluation.ipynb

I suspect you are not providing initial values to the bayes opt object. Either initial values or explicit funciton to evaluate are required.

apaleyes commented 4 years ago

Again, please post your full code for the case when f=None for us to troubleshoot. Preferably not as a screenshot this time.

You are getting 6 evaluations on the plot because this plot displays all known evaluations. This includes initial values too. If you provide 5 initial values, or have GPyOpt generate 5 (default) initial values for you, as is the case on your last post, plus one iteration of optimization, gives 6.

Note that the first plot is showing distance between two consecutive points, so it has 5 datapoints on it, not 6.

lewisscola commented 4 years ago

Thank you so much for your explanation! My code is almost the same as the tutorial, which is in the websitehttps://nbviewer.jupyter.org/github/SheffieldML/GPyOpt/blob/devel/manual/GPyOpt_external_objective_evaluation.ipynb

%pylab inline
import GPyOpt
from numpy.random import seed

func = GPyOpt.objective_examples.experiments1d.forrester() 
domain =[{'name': 'var1', 'type': 'continuous', 'domain': (0,1)}]
X_init = np.array([[0.0],[0.5],[1.0]])
Y_init = func.f(X_init)

iter_count = 10
current_iter = 0
X_step = X_init
Y_step = Y_init

while current_iter < iter_count:
    bo_step = GPyOpt.methods.BayesianOptimization(f = None, domain = domain, X = X_step, Y = Y_step)
    x_next = bo_step.suggest_next_locations()
    y_next = func.f(x_next)

    X_step = np.vstack((X_step, x_next))
    Y_step = np.vstack((Y_step, y_next))

    current_iter += 1

x = np.arange(0.0, 1.0, 0.01)
y = func.f(x)

plt.figure()
plt.plot(x, y)
for i, (xs, ys) in enumerate(zip(X_step, Y_step)):
    plt.plot(xs, ys, 'rD', markersize=10 + 20 * (i+1)/len(X_step))

bo_step.run_optimization(max_iter=10)
print(bo_step.x_opt,bo_step.fx_opt)

The error are: image

My understanding is an eternal function here is like a black-box function, which has no formula. But I can not get optimal of the eternal function through BO. So, for a black-box function, Do you mean I have to use ModularBaysianOptimization to do optimize a black-box function as Eric said before?

Again, please post your full code for the case when f=None for us to troubleshoot. Preferably not as a screenshot this time.

You are getting 6 evaluations on the plot because this plot displays all known evaluations. This includes initial values too. If you provide 5 initial values, or have GPyOpt generate 5 (default) initial values for you, as is the case on your last post, plus one iteration of optimization, gives 6.

Note that the first plot is showing distance between two consecutive points, so it has 5 datapoints on it, not 6.

apaleyes commented 4 years ago

Right, now it's very clear once we see the full code block. Calling run_optimization makes no sense in this setting, because objective is not provided explicitly. The while loop above is exactly what you are supposed to do when f=None, because in that case all GPyOpt can do is suggest next location, the task of retriving y for this location and giving is back to GPyOpt is on the user.

If you need GPyOpt to handle the whole optimization, you have to give it access to the objective.

xmaww commented 1 year ago

@lewisscola
Could you please tell me more details about ModularBaysianOptimization?What is it for?Where can I find this module or tutorial? Many thanks!