def compare_to_single_layer(self, Y, Ys, lik, L, num_outputs=None): kern = Matern52(self.X.shape[1], lengthscales=0.1) m_svgp = SVGP(self.X, Y, kern, lik, Z=self.X, num_latent=num_outputs) m_svgp.q_mu = self.q_mu m_svgp.q_sqrt = self.q_sqrt L_svgp = m_svgp.compute_log_likelihood() mean_svgp, var_svgp = m_svgp.predict_y(self.Xs) test_lik_svgp = m_svgp.predict_density(self.Xs, Ys) pred_m_svgp, pred_v_svgp = m_svgp.predict_f(self.Xs) pred_mfull_svgp, pred_vfull_svgp = m_svgp.predict_f_full_cov(self.Xs) kerns = [] for _ in range(L - 1): kerns.append( Matern52(self.X.shape[1], lengthscales=0.1, variance=2e-6)) kerns.append(Matern52(self.X.shape[1], lengthscales=0.1)) m_dgp = DGP(self.X, Y, self.X, kerns, lik, num_samples=2, num_outputs=num_outputs) m_dgp.layers[-1].q_mu = self.q_mu m_dgp.layers[-1].q_sqrt = self.q_sqrt L_dgp = m_dgp.compute_log_likelihood() mean_dgp, var_dgp = m_dgp.predict_y(self.Xs, 1) test_lik_dgp = m_dgp.predict_density(self.Xs, Ys, 1) pred_m_dgp, pred_v_dgp = m_dgp.predict_f(self.Xs, 1) pred_mfull_dgp, pred_vfull_dgp = m_dgp.predict_f_full_cov(self.Xs, 1) if L == 1: # these should all be exactly the same atol = 1e-7 rtol = 1e-7 else: # jitter makes these not exactly equal atol = 1e-1 rtol = 1e-2 assert_allclose(L_svgp, L_dgp, rtol=rtol, atol=atol) assert_allclose(mean_svgp, mean_dgp[0], rtol=rtol, atol=atol) assert_allclose(var_svgp, var_dgp[0], rtol=rtol, atol=atol) assert_allclose(test_lik_svgp, test_lik_dgp, rtol=rtol, atol=atol) assert_allclose(pred_m_dgp[0], pred_m_svgp, rtol=rtol, atol=atol) assert_allclose(pred_v_dgp[0], pred_v_svgp, rtol=rtol, atol=atol) assert_allclose(pred_mfull_dgp[0], pred_mfull_svgp, rtol=rtol, atol=atol) assert_allclose(pred_vfull_dgp[0], pred_vfull_svgp, rtol=rtol, atol=atol)
def make_kernels(num_latent_k): return [ construct_basic_kernel([Matern52() for _ in range(num_latent_k)]), construct_basic_kernel(Matern52(), output_dim=num_latent_k, share_hyperparams=False), construct_basic_kernel(Matern52(), output_dim=num_latent_k, share_hyperparams=True), ]
def test_construct_kernel_separate_independent_custom_list(): kernel_list = [SquaredExponential(), Matern52()] mok = construct_basic_kernel(kernel_list) assert isinstance(mok, MultioutputKernel) assert isinstance(mok, SeparateIndependent) assert mok.kernels == kernel_list
def init_model(self, Model, X, Y): """ Initialize the model TODO: Currently I'm coding in the choice of having purely a combo of Matern and Linear. We can make this flexible. """ Dx = X.shape[1] kern = Matern52(input_dim=len(self.matern_dims), active_dims=self.matern_dims, lengthscales=SETTINGS.lengthscales * Dx ** 0.5) + \ Linear(input_dim=len(self.linear_dims), active_dims=self.linear_dims) lik = Gaussian() lik.variance = SETTINGS.likelihood_variance gamma = kmeans2(X, self.M_gamma, minit='points')[ 0] if self.M_gamma > 0 else np.empty((0, Dx)) beta = kmeans2(X, self.M_beta, minit='points')[0] if self.M_gamma > 0: gamma_minibatch_size = SETTINGS.gamma_minibatch_size else gamma_minibatch_size = None self.model = Model(X, Y, kern, lik, gamma, beta, minibatch_size=SETTINGS.minibatch_size, gamma_minibatch_size=gamma_minibatch_size) self.sess = self.model.enquire_session()
def test_if_trackable_layer_workaround_still_required(): """ With the release of TensorFlow 2.5, our TrackableLayer workaround is no longer needed. Remove trackable_layer module, tests and references to it. See https://github.com/Prowler-io/gpflux/issues/189 """ layer = UntrackableCompositeLayer([Matern52()]) assert len(layer.trainable_variables) == 0
def test_vs_single_layer(self): lik = Gaussian() lik_var = 0.01 lik.variance = lik_var N, Ns, D_Y, D_X = self.X.shape[0], self.Xs.shape[ 0], self.D_Y, self.X.shape[1] Y = np.random.randn(N, D_Y) Ys = np.random.randn(Ns, D_Y) kern = Matern52(self.X.shape[1], lengthscales=0.5) # mf = Linear(A=np.random.randn(D_X, D_Y), b=np.random.randn(D_Y)) mf = Zero() m_gpr = GPR(self.X, Y, kern, mean_function=mf) m_gpr.likelihood.variance = lik_var mean_gpr, var_gpr = m_gpr.predict_y(self.Xs) test_lik_gpr = m_gpr.predict_density(self.Xs, Ys) pred_m_gpr, pred_v_gpr = m_gpr.predict_f(self.Xs) pred_mfull_gpr, pred_vfull_gpr = m_gpr.predict_f_full_cov(self.Xs) kerns = [] kerns.append( Matern52(self.X.shape[1], lengthscales=0.5, variance=1e-1)) kerns.append(kern) layer0 = GPMC_Layer(kerns[0], self.X.copy(), D_X, Identity()) layer1 = GPR_Layer(kerns[1], mf, D_Y) m_dgp = DGP_Heinonen(self.X, Y, lik, [layer0, layer1]) mean_dgp, var_dgp = m_dgp.predict_y(self.Xs, 1) test_lik_dgp = m_dgp.predict_density(self.Xs, Ys, 1) pred_m_dgp, pred_v_dgp = m_dgp.predict_f(self.Xs, 1) pred_mfull_dgp, pred_vfull_dgp = m_dgp.predict_f_full_cov( self.Xs, 1) tol = 1e-4 assert_allclose(mean_dgp[0], mean_gpr, atol=tol, rtol=tol) assert_allclose(test_lik_dgp, test_lik_gpr, atol=tol, rtol=tol) assert_allclose(pred_m_dgp[0], pred_m_gpr, atol=tol, rtol=tol) assert_allclose(pred_mfull_dgp[0], pred_mfull_gpr, atol=tol, rtol=tol) assert_allclose(pred_vfull_dgp[0], pred_vfull_gpr, atol=tol, rtol=tol)
def main(): dataname = "1DGP_MaternCombo1" ptr = "data/toy_data/" + dataname + '/' n_functions = 1000 lengthscales = 1.0 kernel = Matern52(variance=1.0, lengthscales=2.0) + Matern32( variance=2.0, lengthscales=1.0) data_generator = GPDataGenerator(kernel=kernel) x_trains = [] y_trains = [] x_tests = [] y_tests = [] #Generate n_functions sets of values of x in the range [min_x, max_x], to be used for training and testing plt.figure() for i in range(n_functions): if i % (n_functions // 5) == 0: n_train = np.random.randint(low=5, high=10) n_test = 20 else: n_train = np.random.randint(low=25, high=100) n_test = int(0.2 * n_train) x_train, y_train, x_test, y_test = data_generator.sample( train_size=n_train, test_size=n_test, x_min=-3, x_max=3) x_trains.append(x_train) y_trains.append(y_train) x_tests.append(x_test) y_tests.append(y_test) if i == 0: plt.scatter(x_train, y_train, c='r', s=1, label="train") plt.scatter(x_test, y_test, c="magenta", s=1, label="test") elif i == 1: plt.scatter(x_train, y_train, c='black', s=1, label="train") plt.scatter(x_test, y_test, c="yellow", s=1, label="test") elif i == 2: plt.scatter(x_train, y_train, c='b', s=1, label="train") plt.scatter(x_test, y_test, c="g", s=1, label="test") plt.legend() plt.xlabel("x") plt.xticks([]) plt.ylabel('f(x)') plt.yticks([]) plt.show() x_trains = np.array(x_trains) y_trains = np.array(y_trains) x_tests = np.array(x_tests) y_tests = np.array(y_tests) np.save(ptr + dataname + "_X_trains.npy", x_trains) np.save(ptr + dataname + "_y_trains.npy", y_trains) np.save(ptr + dataname + "_X_tests.npy", x_tests) np.save(ptr + dataname + "_y_tests.npy", y_tests)
def test_verify_compatibility_type_errors(): valid_inducing_variable = construct_basic_inducing_variables([35], input_dim=40) valid_kernel = construct_basic_kernel([Matern52()]) valid_mean_function = Zero( ) # all gpflow mean functions are currently valid with pytest.raises(GPLayerIncompatibilityException ): # gpflow kernels must be MultioutputKernels verify_compatibility(Matern52(), valid_mean_function, valid_inducing_variable) Z = valid_inducing_variable.inducing_variable_list[0].Z inducing_variable = InducingPoints(Z) with pytest.raises( GPLayerIncompatibilityException ): # gpflow inducing_variables must be MultioutputInducingVariables verify_compatibility(valid_kernel, valid_mean_function, inducing_variable)
def _build_kernel(self, w, mu, v, kern_var, Fs = np.array([np.sqrt(45)/2.5, np.sqrt(45)/2.5, 1./8.]), **priors): kern_var = 1. if kern_var == 0. else kern_var # ## # # time # kern_time = SpectralMixture(1, w, v[:,None], mu[:,None], active_dims=slice(2,3,1)) # kern_time.w.transform = RescaleVec(w)(gp.transforms.positive) # kern_time.v.transform = RescaleVec(v[:,None])(gp.transforms.positive) # kern_time.mu.transform = gp.transforms.Logistic(-1e-6, Fs[2])(RescaleVec(mu[:,None])) # kern_time.w.trainable = False # kern_time.mu.trainable = False # kern_time.v.trainable = False kern_dir = Matern52(2,lengthscales=1., variance=1.,active_dims=slice(0,2,1)) kern_dir.lengthscales.trainable = False kern_dir.variance.trainable = False kern_time = Matern52(1,lengthscales=50., variance=1.,active_dims=slice(2,3,1)) kern_time.lengthscales.trainable = False kern_time.lengthscales.transpose = gp.transforms.Rescale(50.)(gp.transforms.positive) kern_time.variance.trainable = False # kernels = [] # for p in range(w.shape[1]): # kernels.append(SpectralMixture(1, w[:,p], v[:,p:p+1], mu[:,p:p+1], active_dims=slice(p,p+1,1))) # kernels[-1].w.transform = RescaleVec(w[:,p])(gp.transforms.positive) # kernels[-1].v.transform = RescaleVec(v[:,p:p+1])(gp.transforms.positive) # kernels[-1].mu.transform = gp.transforms.Logistic(-1e-6, Fs[p])(RescaleVec(mu[:,p:p+1])) # kernels[-1].w.trainable = False # kernels[-1].mu.trainable = False # kernels[-1].v.trainable = False kern_bias = Bias(3) # kern_var = log_normal_solve_fwhm(kern_var*0.75, kern_var*1.5, D=0.1) kern_bias.variance = 1.#kern_var # kern_bias.variance.prior = LogNormal(kern_var[0], kern_var[1]**2) kern_bias.variance.trainable = False kern_bias.variance.transform = gp.transforms.positiveRescale(kern_var) return kern_time*kern_bias*kern_dir
def test_construct_kernel_shared_independent_duplicates(): kernel = Matern52(variance=5) output_dim = 3 mok = construct_basic_kernel(kernel, output_dim=output_dim, share_hyperparams=True) assert isinstance(mok, MultioutputKernel) assert isinstance(mok, SharedIndependent) assert isinstance(mok.kernel, Matern52) assert mok.kernel is kernel
def make_data(input_dim: int, output_dim: int, num_data: int): lim = [0, 20] sigma = 0.1 X = np.random.random(size=(num_data, input_dim)) * lim[1] cov = Matern52()(X) + np.eye(num_data) * sigma**2 Y = [ np.random.multivariate_normal(np.zeros(num_data), cov)[:, None] for _ in range(output_dim) ] Y = np.hstack(Y) return X, Y # TODO: improve this test; for most of the likelihoods, Y won't actually be valid
def setup_layer_modules_variables(): variables = [ tf.Variable(4.0, dtype=default_float()), tf.Variable(5.0, dtype=default_float(), trainable=False), ] modules = [ RBF(), CompositeModule(attributes=[Matern12()]), UntrackableCompositeLayer(attributes=[ tf.Variable(6.0, dtype=default_float()), tf.Variable(7.0, dtype=default_float(), trainable=False), ]), [ CompositeModule(attributes=[Matern52()]), CompositeModule(attributes=[Matern52()]), ], ] modules_variables = [ modules[0].variance.unconstrained_variable, modules[0].lengthscales.unconstrained_variable, modules[1].var_0.variance.unconstrained_variable, modules[1].var_0.lengthscales.unconstrained_variable, modules[2].var_0, modules[2].var_1, modules[3][0].var_0.variance.unconstrained_variable, modules[3][0].var_0.lengthscales.unconstrained_variable, modules[3][1].var_0.variance.unconstrained_variable, modules[3][1].var_0.lengthscales.unconstrained_variable, ] attributes = variables + modules trackable_layer = TrackableCompositeLayer(attributes=attributes) flat_modules = modules[:3] + modules[3] return trackable_layer, variables, flat_modules, modules_variables
def test_construct_kernel_separate_independent_duplicates(): kernel = Matern52(variance=5) output_dim = 3 mok = construct_basic_kernel(kernel, output_dim=output_dim, share_hyperparams=False) assert isinstance(mok, MultioutputKernel) assert isinstance(mok, SeparateIndependent) # check all kernels same type assert all([isinstance(k, Matern52) for k in mok.kernels]) # check kernels have same hyperparameter values but are independent assert mok.kernels[0] is not mok.kernels[-1] assert mok.kernels[0].variance.numpy() == mok.kernels[-1].variance.numpy() assert mok.kernels[0].lengthscales.numpy( ) == mok.kernels[-1].lengthscales.numpy()
def setup_gp_layer_and_data(num_inducing: int, **gp_layer_kwargs): input_dim = 5 output_dim = 3 num_data = 13 data = make_data(input_dim, output_dim, num_data=num_data) kernel = construct_basic_kernel(Matern52(), output_dim) inducing_vars = construct_basic_inducing_variables(num_inducing, input_dim, output_dim) mean_function = Zero(output_dim) gp_layer = GPLayer(kernel, inducing_vars, num_data, mean_function=mean_function, **gp_layer_kwargs) return gp_layer, data
def init_kern(num_pitches, energy, frequency): """Initialize kernels for activations and components""" k_act, k_com = [], [] k_com_a, k_com_b = [], [] for i in range(num_pitches): k_act.append(Matern32(1, lengthscales=0.25, variance=3.5)) k_com_a.append(Matern52(1, lengthscales=0.25, variance=1.0)) k_com_a[i].variance.fixed = True k_com_a[i].lengthscales.transform = gpflow.transforms.Logistic(0., 0.5) k_com_b.append( MercerCosMix(input_dim=1, energy=energy[i].copy(), frequency=frequency[i].copy(), variance=0.25, features_as_params=False)) k_com_b[i].fixed = True k_com.append(k_com_a[i] * k_com_b[i]) kern = [k_act, k_com] return kern
def run_gp_optim(target_column: str, split_perc: float, imputation: str, featureset: str): """ Run whole GPR optimization loop :param target_column: target variable for predictions :param split_perc: percentage of samples to use for train set :param imputation: imputation method for missing values :param featureset: featureset to use """ config = configparser.ConfigParser() config.read('Configs/dataset_specific_config.ini') # get optim parameters base_dir, seasonal_periods, split_perc, init_train_len, test_len, resample_weekly = \ TrainHelper.get_optimization_run_parameters(config=config, target_column=target_column, split_perc=split_perc) # load datasets datasets = TrainHelper.load_datasets(config=config, target_column=target_column) # prepare parameter grid kernels = [] base_kernels = [ SquaredExponential(), Matern52(), White(), RationalQuadratic(), Polynomial() ] for kern in base_kernels: if isinstance(kern, IsotropicStationary): base_kernels.append(Periodic(kern, period=seasonal_periods)) TrainHelper.extend_kernel_combinations(kernels=kernels, base_kernels=base_kernels) param_grid = { 'dataset': datasets, 'imputation': [imputation], 'featureset': [featureset], 'dim_reduction': ['None', 'pca'], 'kernel': kernels, 'mean_function': [None, gpflow.mean_functions.Constant()], 'noise_variance': [0.01, 1, 10, 100], 'optimizer': [gpflow.optimizers.Scipy()], 'standardize_x': [False, True], 'standardize_y': [False, True], 'osa': [True] } # random sample from parameter grid params_lst = TrainHelper.random_sample_parameter_grid( param_grid=param_grid, sample_share=0.2) doc_results = None best_rmse = 5000000.0 best_mape = 5000000.0 best_smape = 5000000.0 dataset_last_name = 'Dummy' imputation_last = 'Dummy' dim_reduction_last = 'Dummy' featureset_last = 'Dummy' for i in tqdm(range(len(params_lst))): warnings.simplefilter('ignore') dataset = params_lst[i]['dataset'] imputation = params_lst[i]['imputation'] featureset = params_lst[i]['featureset'] dim_reduction = None if params_lst[i][ 'dim_reduction'] == 'None' else params_lst[i]['dim_reduction'] # deepcopy to prevent impact of previous optimizations kernel = gpflow.utilities.deepcopy(params_lst[i]['kernel']) mean_fct = gpflow.utilities.deepcopy(params_lst[i]['mean_function']) noise_var = params_lst[i]['noise_variance'] optimizer = gpflow.utilities.deepcopy(params_lst[i]['optimizer']) stand_x = params_lst[i]['standardize_x'] stand_y = params_lst[i]['standardize_y'] one_step_ahead = params_lst[i]['osa'] # dim_reduction only done without NaNs if imputation is None and dim_reduction is not None: continue # dim_reduction does not make sense for few features if featureset == 'none' and dim_reduction is not None: continue if not ((dataset.name == dataset_last_name) and (imputation == imputation_last) and (dim_reduction == dim_reduction_last) and (featureset == featureset_last)): if resample_weekly and 'weekly' not in dataset.name: dataset.name = dataset.name + '_weekly' print(dataset.name + ' ' + str('None' if imputation is None else imputation) + ' ' + str('None' if dim_reduction is None else dim_reduction) + ' ' + featureset + ' ' + target_column) train_test_list = TrainHelper.get_ready_train_test_lst( dataset=dataset, config=config, init_train_len=init_train_len, test_len=test_len, split_perc=split_perc, imputation=imputation, target_column=target_column, dimensionality_reduction=dim_reduction, featureset=featureset) if dataset.name != dataset_last_name: best_rmse = 5000000.0 best_mape = 5000000.0 best_smape = 5000000.0 dataset_last_name = dataset.name imputation_last = imputation dim_reduction_last = dim_reduction featureset_last = featureset kernel_string, mean_fct_string, optimizer_string = get_docresults_strings( kernel=kernel, mean_function=mean_fct, optimizer=optimizer) sum_dict = None try: for train, test in train_test_list: model = ModelsGPR.GaussianProcessRegressionGPFlow( target_column=target_column, seasonal_periods=seasonal_periods, kernel=kernel, mean_function=mean_fct, noise_variance=noise_var, optimizer=optimizer, standardize_x=stand_x, standardize_y=stand_y, one_step_ahead=one_step_ahead) cross_val_dict = model.train(train=train, cross_val_call=False) eval_dict = model.evaluate(train=train, test=test) eval_dict.update(cross_val_dict) if sum_dict is None: sum_dict = eval_dict else: for k, v in eval_dict.items(): sum_dict[k] += v evaluation_dict = { k: v / len(train_test_list) for k, v in sum_dict.items() } params_dict = { 'dataset': dataset.name, 'featureset': featureset, 'imputation': str('None' if imputation is None else imputation), 'dim_reduction': str('None' if dim_reduction is None else dim_reduction), 'init_train_len': init_train_len, 'test_len': test_len, 'split_perc': split_perc, 'kernel': kernel_string, 'mean_function': mean_fct_string, 'noise_variance': noise_var, 'optimizer': optimizer_string, 'standardize_x': stand_x, 'standardize_y': stand_y, 'one_step_ahead': one_step_ahead, 'optim_mod_params': model.model.parameters } save_dict = params_dict.copy() save_dict.update(evaluation_dict) if doc_results is None: doc_results = pd.DataFrame(columns=save_dict.keys()) doc_results = doc_results.append(save_dict, ignore_index=True) best_rmse, best_mape, best_smape = TrainHelper.print_best_vals( evaluation_dict=evaluation_dict, best_rmse=best_rmse, best_mape=best_mape, best_smape=best_smape, run_number=i) except KeyboardInterrupt: print('Got interrupted') break except Exception as exc: # print(exc) params_dict = { 'dataset': 'Failure', 'featureset': featureset, 'imputation': str('None' if imputation is None else imputation), 'dim_reduction': str('None' if dim_reduction is None else dim_reduction), 'init_train_len': init_train_len, 'test_len': test_len, 'split_perc': split_perc, 'kernel': kernel_string, 'mean_function': mean_fct_string, 'noise_variance': noise_var, 'optimizer': optimizer_string, 'standardize_x': stand_x, 'standardize_y': stand_y, 'one_step_ahead': one_step_ahead, 'optim_mod_params': 'failed' } save_dict = params_dict.copy() save_dict.update(TrainHelper.get_failure_eval_dict()) if doc_results is None: doc_results = pd.DataFrame(columns=save_dict.keys()) doc_results = doc_results.append(save_dict, ignore_index=True) TrainHelper.save_csv_results(doc_results=doc_results, save_dir=base_dir + 'OptimResults/', company_model_desc='gpr', target_column=target_column, seasonal_periods=seasonal_periods, datasets=datasets, featuresets=param_grid['featureset'], imputations=param_grid['imputation'], split_perc=split_perc) print('Optimization Done. Saved Results.')
def compare_to_single_layer(self, Y, Ys, lik, L, white, num_outputs=None): kern = Matern52(self.X.shape[1], lengthscales=0.5) m_svgp = SVGP(self.X, Y, kern, lik, Z=self.X, whiten=white, num_latent=num_outputs) m_svgp.q_mu = self.q_mu m_svgp.q_sqrt = self.q_sqrt L_svgp = m_svgp.compute_log_likelihood() mean_svgp, var_svgp = m_svgp.predict_y(self.Xs) test_lik_svgp = m_svgp.predict_density(self.Xs, Ys) pred_m_svgp, pred_v_svgp = m_svgp.predict_f(self.Xs) pred_mfull_svgp, pred_vfull_svgp = m_svgp.predict_f_full_cov( self.Xs) kerns = [] for _ in range(L - 1): class NoTransformMatern52(Matern52): def __init__(self, *args, variance=1., **kwargs): Matern52.__init__(self, *args, **kwargs) del self.variance self.variance = Parameter(variance) kerns.append( NoTransformMatern52(self.X.shape[1], variance=1e-24, lengthscales=0.5)) kerns.append(kern) m_dgp = DGP(self.X, Y, self.X, kerns, lik, white=white, num_samples=2, num_outputs=num_outputs) m_dgp.layers[-1].q_mu = self.q_mu m_dgp.layers[-1].q_sqrt = self.q_sqrt L_dgp = m_dgp.compute_log_likelihood() mean_dgp, var_dgp = m_dgp.predict_y(self.Xs, 1) test_lik_dgp = m_dgp.predict_density(self.Xs, Ys, 1) pred_m_dgp, pred_v_dgp = m_dgp.predict_f(self.Xs, 1) pred_mfull_dgp, pred_vfull_dgp = m_dgp.predict_f_full_cov( self.Xs, 1) if L == 1: # these should all be exactly the same atol = 1e-7 rtol = 1e-7 else: # jitter makes these not exactly equal atol = 1e-6 rtol = 1e-6 assert_allclose(L_svgp, L_dgp, rtol=rtol, atol=atol) assert_allclose(mean_svgp, mean_dgp[0], rtol=rtol, atol=atol) assert_allclose(var_svgp, var_dgp[0], rtol=rtol, atol=atol) assert_allclose(test_lik_svgp, test_lik_dgp, rtol=rtol, atol=atol) assert_allclose(pred_m_dgp[0], pred_m_svgp, rtol=rtol, atol=atol) assert_allclose(pred_v_dgp[0], pred_v_svgp, rtol=rtol, atol=atol) assert_allclose(pred_mfull_dgp[0], pred_mfull_svgp, rtol=rtol, atol=atol) assert_allclose(pred_vfull_dgp[0], pred_vfull_svgp, rtol=rtol, atol=atol)
def test_tensorflow_classes_trackable(composite_class): composite_object = composite_class([Matern52()]) assert len(composite_object.trainable_variables) == 2
def test_vs_DGP2(self): lik = Gaussian() lik_var = 0.1 lik.variance = lik_var N, Ns, D_Y, D_X = self.X.shape[0], self.Xs.shape[ 0], self.D_Y, self.X.shape[1] q_mu = np.random.randn(N, D_X) Y = np.random.randn(N, D_Y) Ys = np.random.randn(Ns, D_Y) kern1 = Matern52(self.X.shape[1], lengthscales=0.5) kern2 = Matern52(self.X.shape[1], lengthscales=0.5) kerns = [kern1, kern2] # mf = Linear(A=np.random.randn(D_X, D_Y), b=np.random.randn(D_Y)) mf = Zero() m_dgp = DGP(self.X, Y, self.X, kerns, lik, mean_function=mf, white=True) m_dgp.layers[0].q_mu = q_mu m_dgp.layers[0].q_sqrt = m_dgp.layers[0].q_sqrt.read_value( ) * 1e-24 Fs, ms, vs = m_dgp.predict_all_layers(self.Xs, 1) Z = self.X.copy() Z[:len(self.Xs)] = ms[0][0] m_dgp.layers[ 1].feature.Z = Z # need to put the inducing points in the right place var_list = [[m_dgp.layers[1].q_mu, m_dgp.layers[1].q_sqrt]] NatGradOptimizer(gamma=1).minimize(m_dgp, var_list=var_list, maxiter=1) mean_dgp, var_dgp = m_dgp.predict_y(self.Xs, 1) test_lik_dgp = m_dgp.predict_density(self.Xs, Ys, 1) pred_m_dgp, pred_v_gpr = m_dgp.predict_f(self.Xs, 1) pred_mfull_dgp, pred_vfull_dgp = m_dgp.predict_f_full_cov( self.Xs, 1) # mean_functions = [Identity(), mf] layer0 = GPMC_Layer(kerns[0], self.X.copy(), D_X, Identity()) layer1 = GPR_Layer(kerns[1], mf, D_Y) m_heinonen = DGP_Heinonen(self.X, Y, lik, [layer0, layer1]) m_heinonen.layers[0].q_mu = q_mu mean_heinonen, var_heinonen = m_heinonen.predict_y(self.Xs, 1) test_lik_heinonen = m_heinonen.predict_density(self.Xs, Ys, 1) pred_m_heinonen, pred_v_heinonen = m_heinonen.predict_f(self.Xs, 1) pred_mfull_heinonen, pred_vfull_heinonen = m_heinonen.predict_f_full_cov( self.Xs, 1) tol = 1e-4 assert_allclose(mean_dgp, mean_heinonen, atol=tol, rtol=tol) assert_allclose(test_lik_dgp, test_lik_heinonen, atol=tol, rtol=tol) assert_allclose(pred_m_dgp, pred_m_heinonen, atol=tol, rtol=tol) assert_allclose(pred_mfull_dgp, pred_mfull_heinonen, atol=tol, rtol=tol) assert_allclose(pred_vfull_dgp, pred_vfull_heinonen, atol=tol, rtol=tol)
def __init__(self, *args, variance=1., **kwargs): Matern52.__init__(self, *args, **kwargs) del self.variance self.variance = Parameter(variance)