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(config): assert config is not None, ValueError tf.random.set_seed(config.seed) gpflow_config.set_default_float(config.floatx) gpflow_config.set_default_jitter(config.jitter) X = tf.random.uniform([config.num_cond, config.input_dims], dtype=floatx()) Xnew = tf.random.uniform([config.num_test, config.input_dims], dtype=floatx()) for cls in SupportedBaseKernels: minval = config.rel_lengthscales_min * (config.input_dims**0.5) maxval = config.rel_lengthscales_max * (config.input_dims**0.5) lenscales = tf.random.uniform(shape=[config.input_dims], minval=minval, maxval=maxval, dtype=floatx()) kern = cls(lengthscales=lenscales, variance=config.kernel_variance) const = tf.random.normal([1], dtype=floatx()) K = kern(X, full_cov=True) K = tf.linalg.set_diag( K, tf.linalg.diag_part(K) + config.noise_variance) L = tf.linalg.cholesky(K) y = L @ tf.random.normal([L.shape[-1], 1], dtype=floatx()) + const model = GPR(kernel=kern, noise_variance=config.noise_variance, data=(X, y), mean_function=mean_functions.Constant(c=const)) mf, Sff = subroutine(config, model, Xnew) mg, Sgg = model.predict_f(Xnew, full_cov=True) tol = config.error_tol assert allclose(mf, mg, tol, tol) assert allclose(Sff, Sgg, tol, tol)
def compute_analytic_GP_predictions(X, y, kernel, noise_variance, X_star): """ Identify the mean and covariance of an analytic GPR posterior for test point locations. :param X: The train point locations, with a shape of [N x D]. :param y: The train targets, with a shape of [N x 1]. :param kernel: The kernel object. :param noise_variance: The variance of the observation model. :param X_star: The test point locations, with a shape of [N* x D]. :return: The mean and covariance of the noise-free predictions, with a shape of [N*] and [N* x N*] respectively. """ gpr_model = GPR(data=(X, y), kernel=kernel, noise_variance=noise_variance) f_mean, f_var = gpr_model.predict_f(X_star, full_cov=True) f_mean, f_var = f_mean[..., 0], f_var[0] assert f_mean.shape == (X_star.shape[0], ) assert f_var.shape == (X_star.shape[0], X_star.shape[0]) return f_mean, f_var