def DotsCoordinates(n, k, a, method): ''' n - dimension of space k - number of points a - length of hypercube's edge method - method of dots generation: 'Uniform', 'Halton' or 'Sobol' Returns matrix in which coordinates of point are saved line-by-line ''' if method == 'Uniform': result = np.zeros((k, n)) for i in range(k): result[i] = np.random.uniform(0, a, n) elif method == 'Halton': np.random.seed() i = np.random.randint(1, 1000, size=1)[0] temp = ht.halton(n, i + k) * a result = temp[i:] np.random.seed(102) elif method == 'Sobol': np.random.seed() i = np.random.randint(1, 1000, size=1)[0] result = sl.i4_sobol_generate(n, k, i) result = result.T * a np.random.seed(102) return result
def __init__(self, gp, cost_gp, num_of_hal_vals=21, num_of_samples=500, num_of_rep_points=20, chain_length=20, transformation=8): self._gp = gp self._cost_gp = cost_gp self._num_of_hallucinated_vals = num_of_hal_vals self._num_of_samples = num_of_samples self._num_of_representer_points = num_of_rep_points if transformation not in range(1, 11): raise NotImplementedError("There exists no transformation with number " + str(transformation)) self._transformation = transformation comp = gp.getPoints() vals = gp.getValues() points = i4_sobol_generate(self._gp.getPoints().shape[1], 100, 1) points[0, :] = 1 #Evaluate EI of a sobel gird to find a good starting point to sample the representer points ei_vals = np.zeros([points.shape[1]]) for i in xrange(0, points.shape[1]): ei_vals[i] = compute_expected_improvement(points[:, i], self._gp) idx = np.argmax(ei_vals) starting_point = points[:, idx] starting_point = starting_point[1:] #representers = sample_from_proposal_measure(starting_point, self._sample_measure, num_of_rep_points - 1, chain_length) representers = sample_representer_points(starting_point, self._sample_measure, num_of_rep_points - 1, chain_length) self._representer_points = np.empty([num_of_rep_points, comp.shape[1]]) self._log_proposal_vals = np.zeros(num_of_rep_points) for i in range(0, num_of_rep_points - 1): self._log_proposal_vals[i] = self._sample_measure(representers[i]) #set first value to one self._representer_points[i] = np.insert(representers[i], 0, 1) incumbent = comp[np.argmin(vals)][1:] self._representer_points[-1] = np.insert(incumbent, 0, 1) self._log_proposal_vals[-1] = self._sample_measure(incumbent) #as fortran array Omega.T is C-contiguous which speeds up dot product computation self._Omega = np.asfortranarray(np.random.normal(0, 1, (self._num_of_samples, self._num_of_representer_points))) self._hallucinated_vals = norm.ppf(np.linspace(1. / (self._num_of_hallucinated_vals + 1), 1 - 1. / (self._num_of_hallucinated_vals + 1), self._num_of_hallucinated_vals)) #TODO: Change it to vector computation self._pmin_old = self._compute_pmin_old(self._gp) entropy_pmin_old = -np.dot(self._pmin_old, np.log(self._pmin_old + 1e-50)) log_proposal_old = np.dot(self._log_proposal_vals, self._pmin_old) self._kl_divergence_old = -(entropy_pmin_old - log_proposal_old) self._idx = np.arange(0, self._num_of_samples)
def Unif_3d_cart(n, skip=None): ''' generates a uniform distribution in 3D within [0,1] ''' global DEB if DEB == 1: print('>>>> Unif_2d_cart') if skip == None: skip = np.random.random_integers(0, 1000) out = sob.i4_sobol_generate(3, n, skip) return (out)
def __init__(self, search_space, grid_size, gp_priors, optimist): '''GPSingle constructor Parameters: ----------- search_space: list of subspaces representing conditional or independent hyperparameters. For now only one level of subspaces is supported. grid_size: total number of points to allow for the discretization of the search space. Budget is spread evenly across subspaces to the ratio of their dimensionalities. gp_priors: priors to apply for the optimization of each GP's hyperparameters. optimist: if True, the GP will fix the prior mean to the maximum possible value (e.g., 1 for 100% accuracy) ''' self.__name__ = "gpsingle" self.flat_search_space = [{"name": "learner_choice", "min": 0, "max": len(search_space)-1, "type": "int"}] self.learner_param_indexes = [] self.search_space = search_space if optimist: mu = 1 gp_priors["mu"] = None else: mu = 0 n_params = 1 for i in range(len(search_space)): ss_i = search_space[i] # list of dicts self.flat_search_space.extend(ss_i) cur_indexes = [] for j in range(len(ss_i)): cur_indexes.append(n_params) n_params += 1 self.learner_param_indexes.append(cur_indexes) # build grid dims = len(self.flat_search_space) self.map = grid.GridMap(self.flat_search_space) self.space = np.transpose(sobol_lib.i4_sobol_generate(dims, grid_size, 9001)) self.gp = pygp.BasicGP(sn=1, sf=1, ell=np.ones(dims), mu=mu, kernel="matern5") self.gp_priors = gp_priors
def __init__(self, n_variable, grid_size=None, shuffle=True): self.grid = i4_sobol_generate(n_variable, grid_size, 1).T if shuffle: idx = np.arange(grid_size) np.random.shuffle(idx) self.grid = self.grid[idx, :] self.status = np.zeros(grid_size, dtype=int) + CANDIDATE_STATE self.values = np.zeros(grid_size) + np.nan self.durations = np.zeros(grid_size) + np.nan
def Unif_1d(n, skip=None): ''' generate a 1D uniform distribution in [0,1] ''' global DEB if DEB == 1: print('>>>> Unif_1d') if skip == None: skip = np.random.random_integers(0, 1000) out = sob.i4_sobol_generate(1, n, skip) return (out.flatten())
def __init__(self, n_variable, grid_size=None, shuffle=True ): self.grid = i4_sobol_generate(n_variable,grid_size,1).T if shuffle: idx = np.arange(grid_size) np.random.shuffle(idx) self.grid = self.grid[idx,:] self.status = np.zeros(grid_size, dtype=int) + CANDIDATE_STATE self.values = np.zeros(grid_size) + np.nan self.durations = np.zeros(grid_size) + np.nan
def __init__(self, gp, num_of_hal_vals=21, num_of_samples=500, num_of_rep_points=20, chain_length=20): #Number of samples for the current candidate self._num_of_hallucinated_vals = num_of_hal_vals #Number of function drawn from the gp self._num_of_samples = num_of_samples #Number of point where pmin is evaluated self._num_of_representer_points = num_of_rep_points self._gp = gp self._idx = np.arange(0, self._num_of_samples) #as fortran array Omega.T is C-contiguous which speeds up dot computation self._Omega = np.asfortranarray(np.random.normal(0, 1, (self._num_of_samples, self._num_of_representer_points))) self._hallucinated_vals = norm.ppf(np.linspace(1. / (self._num_of_hallucinated_vals + 1), 1 - 1. / (self._num_of_hallucinated_vals + 1), self._num_of_hallucinated_vals)) comp = gp.getPoints() vals = gp.getValues() incumbent = comp[np.argmin(vals)] points = i4_sobol_generate(self._gp.getPoints().shape[1], 100, 1) #Evaluate EI of a sobel gird to find a good starting point to sample the representer points ei_vals = np.zeros([points.shape[1]]) for i in xrange(0, points.shape[1]): ei_vals[i] = compute_expected_improvement(points[:, i], self._gp) idx = np.argmax(ei_vals) start_point = points[:, idx] self._representer_points = sample_from_proposal_measure(start_point, self._log_proposal_measure, num_of_rep_points - 1, chain_length) #Add the incumbent to the representer points self._representer_points = np.vstack((self._representer_points, incumbent)) self._log_proposal_vals = np.zeros(self._num_of_representer_points) for i in range(0, self._num_of_representer_points): self._log_proposal_vals[i] = self._log_proposal_measure(self._representer_points[i])
def DotsCoordinates(n, k, a, method): ''' n - dimension of space k - number of points a - length of hypercube's edge method - method of dots generation: 'Uniform', 'Halton' or 'Sobol' Returns matrix in which coordinates of point are saved line-by-line ''' if method == 'Uniform': result = np.zeros((k, n)) for i in range(k): result[i] = np.random.uniform(0, a, n) elif method == 'Sobol': result = sl.i4_sobol_generate(n, k, 10) result = result.T * a return result
def _general_initialization(self, comp, vals, gp, cost_gp=None): ''' Is called in __init__ and performs initialization that should also apply to children of this class. ''' self._gp = gp self._cost_gp = cost_gp self._ei = ExpectedImprovement(comp, vals, gp, cost_gp) #we need this as an iterator for the computation of Pmin self._idx = np.arange(0, NUMBER_OF_PMIN_SAMPLES) #samples for the reprensenter points to compute Pmin #self._Omega = np.random.normal(0, 1, (NUMBER_OF_PMIN_SAMPLES, # NUMBER_OF_REPRESENTER_POINTS)) self._Omega = norm.ppf(i4_sobol_generate(NUMBER_OF_REPRESENTER_POINTS, NUMBER_OF_PMIN_SAMPLES+1, 1)[:,1:]).T #we skip the first entry since it will yield 0 and ppf(0)=-infty #samples for the candidates #self._omega_cands = np.random.normal(0, 1, NUMBER_OF_CANDIDATE_SAMPLES) #we use stratified sampling for the candidates self._omega_cands = norm.ppf(np.linspace(1./(NUMBER_OF_CANDIDATE_SAMPLES+1), 1-1./(NUMBER_OF_CANDIDATE_SAMPLES+1), NUMBER_OF_CANDIDATE_SAMPLES))
def __init__(self, search_space, grid_size, gp_priors, optimist): """GPFamily constructor Parameters: ----------- search_space: list of subspaces representing conditional or independent hyperparameters. For now only one level of subspaces is supported. grid_size: total number of points to allow for the discretization of the search space. Budget is spread evenly across subspaces to the ratio of their dimensionalities. gp_priors: priors to apply for the optimization of each GP's hyperparameters. optimist: if True, the GP will fix the prior mean to the maximum possible value (e.g., 1 for 100% accuracy) """ self.__name__ = "gpfamily" self.n_members = len(search_space) dims = [len(ss) for ss in search_space] dims_total = np.sum(dims) self.dims = dims self.dims_total = dims_total if optimist: # optimisim in face of uncertainty # set mean (not optimized) of GPs self.init_mu = 1 gp_priors["mu"] = None else: # set initial optimization mean of GPs self.init_mu = 0 # build gridsss self.maps = [] self.grids = [] self.members = [] for d, subspace in zip(dims, search_space): # Create grids and gridmaps for subspaces if d > 0: subspace_map = grid.GridMap(subspace) subspace_grid = np.transpose(sobol_lib.i4_sobol_generate(d, int(grid_size * (d / dims_total)), 9001)) else: # This fake parameter will be used to get a value for # the acquisition function in this subspace with no # hyperparameters. fake_params = [{"name": "placeholder", "min": 0, "max": 0, "size": 1, "type": "int"}] subspace_map = grid.GridMap(fake_params) subspace_grid = np.array([[0.0]]) member = pygp.BasicGP(sn=1, sf=1, ell=np.ones(d), mu=self.init_mu, kernel="matern5") self.maps.append(subspace_map) self.grids.append(subspace_grid) self.members.append(member) self.gp_priors = gp_priors # Scores of trained classifiers, all spaces mixed self.scores = []
def init_grid(self,n_dims): self.n_dims = n_dims self.grid = i4_sobol_generate( self.n_dims, self._grid_size,1).T self._shuffle()
def rand_sobol(N, seed): x = sobol.i4_sobol_generate(12, N, seed) x = x.sum(axis=0) - 6 seed = seed + N return (x, seed)
def hypercube_grid(self, size, seed): # Generate from a sobol sequence sobol_grid = np.transpose(i4_sobol_generate(self.cardinality,size,seed)) return sobol_grid
def _hypercube_grid(self, dims, size): # Generate from a sobol sequence sobol_grid = np.transpose(i4_sobol_generate(dims,size,self.seed)) return sobol_grid