def __init__( self, weight_dims, context_dims, kernel="rbf", gamma=None, coef0=1.5, n_components=20, covariance_scale=1.0, alpha=0.0, bias=True, normalize=True, random_state=None, ): self.weight_dims = weight_dims self.context_dims = context_dims self.kernel = kernel self.gamma = gamma self.coef0 = coef0 self.n_components = n_components self.alpha = alpha self.bias = bias self.normalize = normalize self.Sigma = np.eye(weight_dims) * covariance_scale self.random_state = check_random_state(random_state)
def __init__(self, boundaries, bo_type="bo", acquisition_function="UCB", optimizer="direct+lbfgs", acq_fct_kwargs={}, gp_kwargs={}, value_transform=lambda x: x, random_state=None, **kwargs): if not hasattr(boundaries, "__len__"): raise ValueError("Boundaries must be passed as an array-like of " "tuples (pairs).") self.boundaries = boundaries self.bo_type = bo_type self.acquisition_function = acquisition_function self.value_transform = value_transform if isinstance(self.value_transform, basestring): self.value_transform = eval(self.value_transform) self.optimizer = optimizer self.acq_fct_kwargs = acq_fct_kwargs self.gp_kwargs = gp_kwargs self.rng = check_random_state(random_state) self.kwargs = kwargs
def __init__(self, weight_dims, context_dims, kernel="rbf", gamma=None, coef0=1.5, n_components=20, covariance_scale=1.0, alpha=0.0, bias=True, normalize=True, random_state=None): self.weight_dims = weight_dims self.context_dims = context_dims self.kernel = kernel self.gamma = gamma self.coef0 = coef0 self.n_components = n_components self.alpha = alpha self.bias = bias self.normalize = normalize self.Sigma = np.eye(weight_dims) * covariance_scale self.random_state = check_random_state(random_state)
def __init__(self, boundaries, bo_type="bo", acquisition_function="UCB", policy=None, optimizer="direct+lbfgs", acq_fct_kwargs={}, gp_kwargs={}, value_transform=lambda x: x, random_state=None, *args, **kwargs): if not isinstance(boundaries, list): raise ValueError("Boundaries must be passed as a list of tuples " "(pairs).") self.boundaries = boundaries self.bo_type = bo_type self.value_transform = value_transform if isinstance(self.value_transform, str): self.value_transform = eval(self.value_transform) self.optimizer = optimizer self.acquisition_function = acquisition_function self.acq_fct_kwargs = acq_fct_kwargs self.gp_kwargs = gp_kwargs self.policy = policy self.rng = check_random_state(random_state) self.kwargs = kwargs
def test_ranking_dist_40d(): random_state = check_random_state(0) n_dims = 40 n_train = int(70 * np.sqrt(n_dims)) n_test = 500 n_iter = int(50000 * np.sqrt(n_dims)) epsilon = 1.0 def generate_data(n_samples, n_dims, random_state): """Rank data by distance to 0.5.""" X = random_state.rand(n_samples, n_dims) distances = np.sum((0.5 - X) ** 2, axis=1) X = X[np.argsort(distances)] return X, np.sort(distances) X_train, dist_train = generate_data(n_train, n_dims, random_state) X_test, dist_test = generate_data(n_test, n_dims, random_state) ranking_svm = RankingSVM(n_iter, epsilon, random_state=random_state) ranking_svm.fit(X_train) y_train = ranking_svm.predict(X_train) y_test = ranking_svm.predict(X_test) # Ranking is 80% correct between samples of rank distance 100 assert_greater(np.count_nonzero(y_train[100:] < y_train[:-100]), 0.8 * (n_train - 100)) assert_greater(np.count_nonzero(y_test[100:] < y_test[:-100]), 0.8 * (n_test - 100))
def init(self): self.random_state = check_random_state(self.random_state) if hasattr(self.segments, "__iter__"): self.segments = np.asarray(self.segments, dtype=np.float) else: self.segments = self._generate_segments(self.segments) self._compute_surface() # Remember the maximum feedback obtainable in a context (the baseline) self.max_feedback_cache = {} self.params = np.zeros(2) self.context = np.array([0.5])
def test_ranking_linear_1d(): random_state = check_random_state(0) n_dims = 1 n_train = int(30 * np.sqrt(n_dims)) n_test = 500 n_iter = int(50000 * np.sqrt(n_dims)) epsilon = 1.0 X_train = np.linspace(0, 1, n_train)[:, np.newaxis] X_test = np.linspace(0, 1, n_test)[:, np.newaxis] ranking_svm = RankingSVM(n_iter, epsilon, random_state=random_state) ranking_svm.fit(X_train) y_train = ranking_svm.predict(X_train) y_test = ranking_svm.predict(X_test) assert_true(np.all(y_train[1:] < y_train[:-1])) assert_true(np.all(y_test[1:] < y_test[:-1]))
def init(self, n_params): """Initialize the behavior search. Parameters ---------- n_params : int dimension of the parameter vector """ self.random_state = check_random_state(self.random_state) self.n_params = n_params self.it = 0 if self.initial_params is None: self.initial_params = np.zeros(n_params) else: self.initial_params = np.asarray(self.initial_params).astype( np.float64, copy=True) if n_params != len(self.initial_params): raise ValueError("Number of dimensions (%d) does not match " "number of initial parameters (%d)." % (n_params, len(self.initial_params))) if self.elite_frac <= 0 or self.elite_frac >= 1: raise ValueError( "Elite fraction should be positive and in range (0.0,1.0)") if self.covariance is None: self.covariance = np.eye(self.n_params) else: self.covariance = np.asarray(self.covariance).copy() if self.covariance.ndim == 1: self.covariance = np.diag(self.covariance) self.best_fitness = np.inf self.best_fitness_it = self.it self.best_params = self.initial_params.copy() self.initial_it = self.it if self.n_samples_per_update is None: self.n_samples_per_update = 4 + int(3 * np.log(self.n_params)) if self.bounds is not None: self.bounds = np.asarray(self.bounds) self.mean = self.initial_params.copy() self.cov = self.covariance.copy() self.samples = self._sample(self.n_samples_per_update) self.fitness = np.empty(self.n_samples_per_update)
def __init__(self, boundaries, bo_type="bo", acquisition_function="UCB", policy=None, optimizer="direct+lbfgs", acq_fct_kwargs={}, gp_kwargs={}, value_transform=lambda x: x, random_state=None, *args, **kwargs): if not isinstance(boundaries, list): raise ValueError("Boundaries must be passed as a list of tuples " "(pairs).") self.boundaries = boundaries self.bo_type = bo_type self.value_transform = value_transform if isinstance(self.value_transform, basestring): self.value_transform = eval(self.value_transform) self.optimizer = optimizer self.acquisition_function = acquisition_function self.acq_fct_kwargs = acq_fct_kwargs self.gp_kwargs = gp_kwargs self.policy = policy self.rng = check_random_state(random_state) self.kwargs = kwargs