SheffieldML / GPyOpt

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

cost_withGradients='evaluation_time' problem #64

Open 1Reinier opened 7 years ago

1Reinier commented 7 years ago

Hi, When setting cost_withGradients='evaluation_time' I persistently run into the same error: AttributeError: 'NoneType' object has no attribute 'predict'. Without it it works fine.Do you know a solution to this issue? Here's a stack trace:

...
/anaconda/lib/python3.5/site-packages/GPyOpt/methods/bayesian_optimization.py in __init__(self, f, domain, constrains, cost_withGradients, model_type, X, Y, initial_design_numdata, initial_design_type, acquisition_type, normalize_Y, exact_feval, acquisition_optimizer_type, model_update_interval, evaluator_type, batch_size, num_cores, verbosity, verbosity_model, bounds, maximize, **kwargs)
    242 
    243         # --- Initialize everything
--> 244         self.run_optimization(max_iter=0,verbosity=self.verbosity)
    245 
    246     def _model_chooser(self):

/anaconda/lib/python3.5/site-packages/GPyOpt/methods/bayesian_optimization.py in run_optimization(self, max_iter, max_time, eps, verbosity, save_models_parameters, report_file, evaluations_file, models_file, **kwargs)
    456                 self.acquisition_optimizer.optimizer ='CMA'
    457             print('WARNING: "acqu_optimize_method" will be deprecated in the next version!')
--> 458         super(BayesianOptimization, self).run_optimization(max_iter = max_iter, max_time = max_time,  eps = eps, verbosity=verbosity, save_models_parameters = save_models_parameters, report_file = report_file, evaluations_file= evaluations_file, models_file=models_file)
    459 
    460     def _sign(self,f):

/anaconda/lib/python3.5/site-packages/GPyOpt/core/bo.py in run_optimization(self, max_iter, max_time, eps, verbosity, save_models_parameters, report_file, evaluations_file, models_file)
    106 
    107             # --- Update and optimize acquisition and compute the exploration level in the next evaluation
--> 108             self.suggested_sample = self._compute_next_evaluations()
    109 
    110             if not ((self.num_acquisitions < self.max_iter) and (self._distance_last_evaluations() > self.eps)):

/anaconda/lib/python3.5/site-packages/GPyOpt/core/bo.py in _compute_next_evaluations(self)
    184         Computes the location of the new evaluation (optimizes the acquisition in the standard case).
    185         """
--> 186         return self.evaluator.compute_batch()
    187 
    188     def _update_model(self):

/anaconda/lib/python3.5/site-packages/GPyOpt/core/evaluators/sequential.py in compute_batch(self)
     19         Selects the new location to evaluate the objective.
     20         """
---> 21         return self.acquisition.optimize()
     22 
     23 

/anaconda/lib/python3.5/site-packages/GPyOpt/acquisitions/base.py in optimize(self)
     57             out = self.optimizer.optimize(f=self.acquisition_function)[0]
     58         else:
---> 59             out = self.optimizer.optimize(f=self.acquisition_function, f_df=self.acquisition_function_withGradients)[0]
     60         return out
     61 

/anaconda/lib/python3.5/site-packages/GPyOpt/optimization/acquisition_optimizer.py in optimize(self, f, df, f_df)
    281         for i in range(num_discrete):
    282             self.mixed_optimizer.fix_dimensions(dims=self.discrete_dims, values=self.discrete_values[i,:])
--> 283             partial_x_min[i,:] , partial_f_min[i,:] = self.mixed_optimizer.optimize(f, df, f_df)
    284 
    285         return np.atleast_2d(partial_x_min[np.argmin(partial_f_min)]), np.atleast_2d(min(partial_f_min))

/anaconda/lib/python3.5/site-packages/GPyOpt/optimization/acquisition_optimizer.py in optimize(self, f, df, f_df)
    171         ## --- Fast method: only runs a local optimizer at the best found evaluation
    172         if self.fast:
--> 173             pred_fp = fp(self.samples)
    174             x0 =  self.samples[np.argmin(pred_fp)]
    175             if self.search:

/anaconda/lib/python3.5/site-packages/GPyOpt/optimization/acquisition_optimizer.py in fp(x)
    154                 return self.f(xx)[0]
    155             else:
--> 156                 return self.f(xx)
    157 
    158         def fp_dfp(x):

/anaconda/lib/python3.5/site-packages/GPyOpt/acquisitions/base.py in acquisition_function(self, x)
     36         """
     37         f_acqu = self._compute_acq(x)
---> 38         cost_x, _ = self.cost_withGradients(x)
     39         return -(f_acqu*self.space.indicator_constraints(x))/cost_x
     40 

/anaconda/lib/python3.5/site-packages/GPyOpt/core/task/cost.py in _cost_gp_withGradients(self, x)
     50         Predicts the time cost and its gradient of evaluating the function at x.
     51         """
---> 52         m, _, dmdx, _= self.cost_model.predict_withGradients(x)
     53         return np.exp(m), np.exp(m)*dmdx
     54 

/anaconda/lib/python3.5/site-packages/GPyOpt/models/gpmodel.py in predict_withGradients(self, X)
    109         """
    110         if X.ndim==1: X = X[None,:]
--> 111         m, v = self.model.predict(X)
    112         v = np.clip(v, 1e-10, np.inf)
    113         dmdx, dvdx = self.model.predictive_gradients(X)

AttributeError: 'NoneType' object has no attribute 'predict'
javiergonzalezh commented 7 years ago

This part of the code hasn't been properly tested in cases in which you want to do things in parallel. Does it work for at least you if you try to collect the evaluations sequentially?

Javier

sdrobert commented 5 years ago

Here's a serial example using GPyOpt 1.2.5:

def foo(x):
    return x[0, 0]

domain = [{'name': 'x', 'type': 'continuous', 'domain': (0, 3)}]

import GPyOpt
bo = GPyOpt.methods.BayesianOptimization(f=foo, domain=domain, cost_withGradients='evaluation_time')
bo.run_optimization(1)

which gives us

D:\conda-envs\tlf\lib\site-packages\GPyOpt\models\gpmodel.py in predict_withGradients(self, X)
    132         """
    133         if X.ndim==1: X = X[None,:]
--> 134         m, v = self.model.predict(X)
    135         v = np.clip(v, 1e-10, np.inf)
    136         dmdx, dvdx = self.model.predictive_gradients(X)

AttributeError: 'NoneType' object has no attribute 'predict'