def predict_with_acq(self, x): from entmoot.utils import is_2Dlistlike if is_2Dlistlike(x): next_x = np.asarray(self.space.transform(x)) else: next_x = np.asarray(self.space.transform([x])[0]).reshape(1, -1) if self.models: temp_val = _gaussian_acquisition( X=next_x, model=self.models[-1], y_opt=np.min(self.yi), acq_func=self.acq_func, acq_func_kwargs=self.acq_func_kwargs) else: est = self.base_estimator_ est.fit(self.space.transform(self.Xi), self.yi) temp_val = _gaussian_acquisition( X=next_x, model=est, y_opt=np.min(self.yi), acq_func=self.acq_func, acq_func_kwargs=self.acq_func_kwargs) if is_2Dlistlike(x): return temp_val else: return temp_val[0]
def predict_with_est(self, x, return_std=True): from entmoot.utils import is_2Dlistlike if is_2Dlistlike(x): next_x = np.asarray(self.space.transform(x)) else: next_x = np.asarray(self.space.transform([x])[0]).reshape(1, -1) est = self.base_estimator_ est.fit(self.space.transform(self.Xi), self.yi) temp_mu, temp_std = \ est.predict( X=next_x, return_std=True) if is_2Dlistlike(x): if return_std: return temp_mu, temp_std else: return temp_mu else: if return_std: return temp_mu[0], temp_std[0] else: return temp_mu[0]
def _tell(self, x, y): """ Adds the new data points to the data set. :param x: list, locations of new data points :param y: list, target value of new data points """ from entmoot.utils import is_listlike from entmoot.utils import is_2Dlistlike # if y isn't a scalar it means we have been handed a batch of points if is_listlike(y) and is_2Dlistlike(x): self.Xi.extend(x) self.yi.extend(y) self._n_initial_points -= len(y) elif is_listlike(x): self.Xi.append(x) self.yi.append(y) self._n_initial_points -= 1 else: raise ValueError("Type of arguments `x` (%s) and `y` (%s) " "not compatible." % (type(x), type(y))) # optimizer learned something new - discard cache self.cache_ = {} # set self._next_x to None to indicate that the solver has learned something new self._next_x = None
def _check_y_is_valid(self, x, y): """check if the shape and types of x and y are consistent.""" from entmoot.utils import is_listlike from entmoot.utils import is_2Dlistlike # single objective checks for scalar values if self.num_obj == 1: # if y isn't a scalar it means we have been handed a batch of points if is_listlike(y) and is_2Dlistlike(x): for y_value in y: if not isinstance(y_value, Number): raise ValueError("expected y to be a list of scalars") elif is_listlike(x): if not isinstance(y, Number): raise ValueError("`func` should return a scalar") else: raise ValueError("Type of arguments `x` (%s) and `y` (%s) " "not compatible." % (type(x), type(y))) else: # if y isn't a scalar it means we have been handed a batch of points if is_listlike(y[0]) and is_2Dlistlike(x): for y_value in y: if len(y_value) != self.num_obj: raise ValueError( f"expected y to be of size {self.num_obj}") for yi in y_value: if not isinstance(yi, Number): raise ValueError( f"expected y to be a list of list-like items of length {self.num_obj}" ) elif is_listlike(x): if len(y) != self.num_obj: raise ValueError( f"`func` should return a list-like item of length {self.num_obj}" ) for yi in y: if not isinstance(yi, Number): raise ValueError( f"`func` should return a list-like item of length {self.num_obj}" ) else: raise ValueError("Type of arguments `x` (%s) and `y` (%s) " "not compatible." % (type(x), type(y)))
def __call__(self, X): # check if multiple points are given if not is_2Dlistlike(X): X = [X] res = [] for x in X: res.append(self._eval_point(x)) if len(res) == 1: res = res[0] return res