def test_predict_test(): np.random.seed(42) dim_X = 2 num_X = 5 num_X_test = 20 X = np.random.randn(num_X, dim_X) Y = np.random.randn(num_X, 1) X_test = np.random.randn(num_X_test, dim_X) prior_mu = None cov_X_X, inv_cov_X_X, hyps = gp.get_optimized_kernel(X, Y, prior_mu, 'se') with pytest.raises(AssertionError) as error: gp.predict_test(X, Y, X_test, hyps, str_cov='se', prior_mu='abc') with pytest.raises(AssertionError) as error: gp.predict_test(X, Y, X_test, hyps, str_cov=1, prior_mu=prior_mu) with pytest.raises(AssertionError) as error: gp.predict_test(X, Y, X_test, 1, str_cov='se', prior_mu=prior_mu) with pytest.raises(AssertionError) as error: gp.predict_test(X, Y, 1, hyps, str_cov='se', prior_mu=prior_mu) with pytest.raises(AssertionError) as error: gp.predict_test(X, 1, X_test, hyps, str_cov='se', prior_mu=prior_mu) with pytest.raises(AssertionError) as error: gp.predict_test(1, Y, X_test, hyps, str_cov='se', prior_mu=prior_mu) with pytest.raises(AssertionError) as error: gp.predict_test(np.random.randn(num_X, 1), Y, X_test, hyps, str_cov='se', prior_mu=prior_mu) with pytest.raises(AssertionError) as error: gp.predict_test(np.random.randn(10, dim_X), Y, X_test, hyps, str_cov='se', prior_mu=prior_mu) with pytest.raises(AssertionError) as error: gp.predict_test(X, np.random.randn(10, 1), X_test, hyps, str_cov='se', prior_mu=prior_mu) mu_Xs, sigma_Xs = gp.predict_test(X, Y, X_test, hyps, str_cov='se', prior_mu=prior_mu) print(mu_Xs) print(sigma_Xs)
def optimize( self, X_train, Y_train, str_initial_method=constants.STR_AO_INITIALIZATION, int_samples=constants.NUM_ACQ_SAMPLES, is_normalized=constants.IS_NORMALIZED_RESPONSE, str_mlm_method=constants.STR_MLM_METHOD, str_modelselection_method=constants.STR_MODELSELECTION_METHOD): # TODO: is_normalized cases assert isinstance(X_train, np.ndarray) assert isinstance(Y_train, np.ndarray) assert isinstance(str_initial_method, str) assert isinstance(int_samples, int) assert isinstance(is_normalized, bool) assert isinstance(str_mlm_method, str) assert isinstance(str_modelselection_method, str) assert len(X_train.shape) == 2 assert len(Y_train.shape) == 2 assert Y_train.shape[1] == 1 assert X_train.shape[0] == Y_train.shape[0] assert X_train.shape[1] == self.num_dim assert int_samples > 0 assert str_initial_method in constants.ALLOWED_INITIALIZATIONS_AO assert str_mlm_method in constants.ALLOWED_MLM_METHOD assert str_modelselection_method in constants.ALLOWED_MODELSELECTION_METHOD time_start = time.time() if is_normalized: Y_train = (Y_train - np.min(Y_train)) / ( np.max(Y_train) - np.min(Y_train)) * constants.MULTIPLIER_RESPONSE if str_mlm_method == 'regular': cov_X_X, inv_cov_X_X, hyps = gp.get_optimized_kernel( X_train, Y_train, self.prior_mu, self.str_cov, str_optimizer_method=self.str_optimizer_method_gp, str_modelselection_method=str_modelselection_method, debug=self.debug) elif str_mlm_method == 'converged': if self.is_optimize_hyps: cov_X_X, inv_cov_X_X, hyps = gp.get_optimized_kernel( X_train, Y_train, self.prior_mu, self.str_cov, str_optimizer_method=self.str_optimizer_method_gp, str_modelselection_method=str_modelselection_method, debug=self.debug) self.is_optimize_hyps = not _check_hyps_convergence( self.historical_hyps, hyps, self.str_cov, constants.IS_FIXED_GP_NOISE) # TODO: Can we test this else statement? else: # pragma: no cover print('[DEBUG] optimize in bo.py: hyps are converged.') hyps = self.historical_hyps[-1] cov_X_X, inv_cov_X_X = gp.get_kernel_inverse(X_train, hyps, self.str_cov, debug=self.debug) elif str_mlm_method == 'probabilistic': # pragma: no cover raise NotImplementedError('optimize: it will be added.') else: # pragma: no cover raise ValueError('optimize: missing condition for str_mlm_method.') self.historical_hyps.append(hyps) fun_acquisition = _choose_fun_acquisition(self.str_acq, hyps) fun_negative_acquisition = lambda X_test: -1.0 * constants.MULTIPLIER_ACQ * self._optimize_objective( fun_acquisition, X_train, Y_train, X_test, cov_X_X, inv_cov_X_X, hyps) next_point, next_points = self._optimize( fun_negative_acquisition, str_initial_method=str_initial_method, int_samples=int_samples) acquisitions = fun_negative_acquisition(next_points) time_end = time.time() if self.debug: print('[DEBUG] optimize in bo.py: time consumed', time_end - time_start, 'sec.') return next_point, next_points, acquisitions, cov_X_X, inv_cov_X_X, hyps
def oracle(args, model): seed = 42 num_all = 100 num_iter = 50 num_init = args.bo_num_init str_cov = 'se' list_dict = [] if args.bo_kernel == 'rbf': kernel = RBFKernel() elif args.bo_kernel == 'matern': kernel = Matern52Kernel() elif args.bo_kernel == 'periodic': kernel = PeriodicKernel() else: raise ValueError(f'Invalid kernel {args.bo_kernel}') for ind_seed in range(1, num_all + 1): seed_ = seed * ind_seed if seed_ is not None: torch.manual_seed(seed_) torch.cuda.manual_seed(seed_) if os.path.exists( get_str_file('./results', args.bo_kernel, 'oracle', args.t_noise, seed=ind_seed)): dict_exp = np.load(get_str_file('./results', args.bo_kernel, 'oracle', args.t_noise, seed=ind_seed), allow_pickle=True) dict_exp = dict_exp[()] list_dict.append(dict_exp) print(dict_exp) print(dict_exp['global']) print(np.array2string(dict_exp['minima'], separator=',')) print(np.array2string(dict_exp['regrets'], separator=',')) continue sampler = GPPriorSampler(kernel, t_noise=args.t_noise) xp = torch.linspace(-2, 2, 1000).cuda() xp_ = xp.unsqueeze(0).unsqueeze(2) yp = sampler.sample(xp_) min_yp = yp.min() print(min_yp.cpu().numpy()) model.eval() batch = AttrDict() indices_permuted = torch.randperm(yp.shape[1]) batch.x = xp_[:, indices_permuted[:2 * num_init], :] batch.y = yp[:, indices_permuted[:2 * num_init], :] batch.xc = xp_[:, indices_permuted[:num_init], :] batch.yc = yp[:, indices_permuted[:num_init], :] batch.xt = xp_[:, indices_permuted[num_init:2 * num_init], :] batch.yt = yp[:, indices_permuted[num_init:2 * num_init], :] X_train = batch.xc.squeeze(0).cpu().numpy() Y_train = batch.yc.squeeze(0).cpu().numpy() X_test = xp_.squeeze(0).cpu().numpy() list_min = [] list_min.append(batch.yc.min().cpu().numpy()) for ind_iter in range(0, num_iter): print('ind_seed {} seed {} iter {}'.format(ind_seed, seed_, ind_iter + 1)) cov_X_X, inv_cov_X_X, hyps = bayesogp.get_optimized_kernel( X_train, Y_train, None, str_cov, is_fixed_noise=False, debug=False) prior_mu_train = bayesogp.get_prior_mu(None, X_train) prior_mu_test = bayesogp.get_prior_mu(None, X_test) cov_X_Xs = covariance.cov_main(str_cov, X_train, X_test, hyps, False) cov_Xs_Xs = covariance.cov_main(str_cov, X_test, X_test, hyps, True) cov_Xs_Xs = (cov_Xs_Xs + cov_Xs_Xs.T) / 2.0 mu_ = np.dot(np.dot(cov_X_Xs.T, inv_cov_X_X), Y_train - prior_mu_train) + prior_mu_test Sigma_ = cov_Xs_Xs - np.dot(np.dot(cov_X_Xs.T, inv_cov_X_X), cov_X_Xs) sigma_ = np.expand_dims(np.sqrt(np.maximum(np.diag(Sigma_), 0.0)), axis=1) acq_vals = -1.0 * acquisition.ei(np.ravel(mu_), np.ravel(sigma_), Y_train) ind_ = np.argmin(acq_vals) x_new = xp[ind_, None, None, None] y_new = yp[:, ind_, None, :] batch.x = torch.cat([batch.x, x_new], axis=1) batch.y = torch.cat([batch.y, y_new], axis=1) batch.xc = torch.cat([batch.xc, x_new], axis=1) batch.yc = torch.cat([batch.yc, y_new], axis=1) X_train = batch.xc.squeeze(0).cpu().numpy() Y_train = batch.yc.squeeze(0).cpu().numpy() min_cur = batch.yc.min() list_min.append(min_cur.cpu().numpy()) print(min_yp.cpu().numpy()) print(np.array2string(np.array(list_min), separator=',')) print( np.array2string(np.array(list_min) - min_yp.cpu().numpy(), separator=',')) dict_exp = { 'seed': seed_, 'str_cov': str_cov, 'global': min_yp.cpu().numpy(), 'minima': np.array(list_min), 'regrets': np.array(list_min) - min_yp.cpu().numpy(), 'xc': X_train, 'yc': Y_train, 'model': 'oracle', } np.save( get_str_file('./results', args.bo_kernel, 'oracle', args.t_noise, seed=ind_seed), dict_exp) list_dict.append(dict_exp) np.save( get_str_file('./figures/results', args.bo_kernel, 'oracle', args.t_noise), list_dict)
def optimize( self, X_train, Y_train, str_initial_method_ao=constants.STR_AO_INITIALIZATION, int_samples=constants.NUM_ACQ_SAMPLES, str_mlm_method=constants.STR_MLM_METHOD, ): """ It computes acquired example, candidates of acquired examples, acquisition function values for the candidates, covariance matrix, inverse matrix of the covariance matrix, hyperparameters optimized, and execution times. :param X_train: inputs. Shape: (n, d) or (n, m, d). :type X_train: numpy.ndarray :param Y_train: outputs. Shape: (n, 1). :type Y_train: numpy.ndarray :param str_initial_method_ao: the name of initialization method for acquisition function optimization. :type str_initial_method_ao: str., optional :param int_samples: the number of samples. :type int_samples: int., optional :param str_mlm_method: the name of marginal likelihood maximization method for Gaussian process regression. :type str_mlm_method: str., optional :returns: acquired example, candidates of acquired examples, acquisition function values over the candidates, covariance matrix by `hyps`, inverse matrix of the covariance matrix, hyperparameters optimized, and execution times. Shape: ((d, ), (`int_samples`, d), (`int_samples`, ), (n, n), (n, n), dict., dict.). :rtype: (numpy.ndarray, numpy.ndarray, numpy.ndarray, numpy.ndarray, numpy.ndarray, dict., dict.) :raises: AssertionError """ assert isinstance(X_train, np.ndarray) assert isinstance(Y_train, np.ndarray) assert isinstance(str_initial_method_ao, str) assert isinstance(int_samples, int) assert isinstance(str_mlm_method, str) assert len(X_train.shape) == 2 assert len(Y_train.shape) == 2 assert Y_train.shape[1] == 1 assert X_train.shape[0] == Y_train.shape[0] assert X_train.shape[1] == self.num_dim assert int_samples > 0 assert str_initial_method_ao in constants.ALLOWED_INITIALIZATIONS_AO assert str_mlm_method in constants.ALLOWED_MLM_METHOD time_start = time.time() if self.is_normalized: Y_train = (Y_train - np.min(Y_train)) / ( np.max(Y_train) - np.min(Y_train)) * constants.MULTIPLIER_RESPONSE time_start_gp = time.time() if str_mlm_method == 'regular': cov_X_X, inv_cov_X_X, hyps = gp.get_optimized_kernel( X_train, Y_train, self.prior_mu, self.str_cov, str_optimizer_method=self.str_optimizer_method_gp, str_modelselection_method=self.str_modelselection_method, debug=self.debug) elif str_mlm_method == 'converged': is_fixed_noise = constants.IS_FIXED_GP_NOISE if self.is_optimize_hyps: cov_X_X, inv_cov_X_X, hyps = gp.get_optimized_kernel( X_train, Y_train, self.prior_mu, self.str_cov, str_optimizer_method=self.str_optimizer_method_gp, str_modelselection_method=self.str_modelselection_method, debug=self.debug) self.is_optimize_hyps = not _check_hyps_convergence( self.historical_hyps, hyps, self.str_cov, is_fixed_noise) else: # pragma: no cover print('[DEBUG] optimize in bo.py: hyps are converged.') hyps = self.historical_hyps[-1] cov_X_X, inv_cov_X_X, _ = gp.get_kernel_inverse( X_train, hyps, self.str_cov, is_fixed_noise=is_fixed_noise, debug=self.debug) elif str_mlm_method == 'probabilistic': # pragma: no cover raise NotImplementedError('optimize: it will be added.') else: # pragma: no cover raise ValueError('optimize: missing condition for str_mlm_method.') time_end_gp = time.time() self.historical_hyps.append(hyps) fun_acquisition = _choose_fun_acquisition(self.str_acq, hyps) time_start_acq = time.time() fun_negative_acquisition = lambda X_test: -1.0 * constants.MULTIPLIER_ACQ * self._optimize_objective( fun_acquisition, X_train, Y_train, X_test, cov_X_X, inv_cov_X_X, hyps) next_point, next_points = self._optimize( fun_negative_acquisition, str_initial_method=str_initial_method_ao, int_samples=int_samples) time_end_acq = time.time() acquisitions = fun_negative_acquisition(next_points) time_end = time.time() times = { 'overall': time_end - time_start, 'gp': time_end_gp - time_start_gp, 'acq': time_end_acq - time_start_acq, } if self.debug: print('[DEBUG] optimize in bo.py: time consumed', time_end - time_start, 'sec.') return next_point, next_points, acquisitions, cov_X_X, inv_cov_X_X, hyps, times
def test_get_optimized_kernel(): np.random.seed(42) dim_X = 3 num_X = 10 num_instances = 5 X = np.random.randn(num_X, dim_X) X_set = np.random.randn(num_X, num_instances, dim_X) Y = np.random.randn(num_X, 1) prior_mu = None with pytest.raises(AssertionError) as error: gp.get_optimized_kernel(X, Y, prior_mu, 1) with pytest.raises(AssertionError) as error: gp.get_optimized_kernel(X, Y, 1, 'se') with pytest.raises(AssertionError) as error: gp.get_optimized_kernel(X, 1, prior_mu, 'se') with pytest.raises(AssertionError) as error: gp.get_optimized_kernel(1, Y, prior_mu, 'se') with pytest.raises(AssertionError) as error: gp.get_optimized_kernel(np.ones(num_X), Y, prior_mu, 'se') with pytest.raises(AssertionError) as error: gp.get_optimized_kernel(X, np.ones(num_X), prior_mu, 'se') with pytest.raises(AssertionError) as error: gp.get_optimized_kernel(np.ones((50, 3)), Y, prior_mu, 'se') with pytest.raises(AssertionError) as error: gp.get_optimized_kernel(X, np.ones((50, 1)), prior_mu, 'se') with pytest.raises(ValueError) as error: gp.get_optimized_kernel(X, Y, prior_mu, 'abc') with pytest.raises(AssertionError) as error: gp.get_optimized_kernel(X, Y, prior_mu, 'se', str_optimizer_method=1) with pytest.raises(AssertionError) as error: gp.get_optimized_kernel(X, Y, prior_mu, 'se', str_modelselection_method=1) with pytest.raises(AssertionError) as error: gp.get_optimized_kernel(X, Y, prior_mu, 'se', is_fixed_noise=1) with pytest.raises(AssertionError) as error: gp.get_optimized_kernel(X, Y, prior_mu, 'se', debug=1) # INFO: tests for set inputs with pytest.raises(AssertionError) as error: gp.get_optimized_kernel(X_set, Y, prior_mu, 'se') with pytest.raises(AssertionError) as error: gp.get_optimized_kernel(X, Y, prior_mu, 'set_se') with pytest.raises(AssertionError) as error: gp.get_optimized_kernel(X_set, Y, prior_mu, 'set_se', debug=1) gp.get_optimized_kernel(X, Y, prior_mu, 'se') gp.get_optimized_kernel(X, Y, prior_mu, 'se', str_optimizer_method='L-BFGS-B') gp.get_optimized_kernel(X, Y, prior_mu, 'se', str_modelselection_method='loocv') gp.get_optimized_kernel(X_set, Y, prior_mu, 'set_se') gp.get_optimized_kernel(X_set, Y, prior_mu, 'set_se', str_optimizer_method='L-BFGS-B') gp.get_optimized_kernel(X_set, Y, prior_mu, 'set_se', str_modelselection_method='loocv')