def __init__(self, latent_dim, Z, mean_function=None, kern=None, likelihood=None, name=None): super().__init__(name=name) self.latent_dim = latent_dim self.obs_dim = 1 self.n_ind_pts = Z.shape[0] self.mean_function = mean_function or mean_fns.Zero( output_dim=self.obs_dim) self.kern = kern or gp.kernels.RBF(self.latent_dim, ARD=True) self.likelihood = likelihood or gp.likelihoods.Gaussian() self.Z = gp.features.InducingPoints(Z) self.Umu = gp.Param(np.zeros( (self.n_ind_pts, self.latent_dim))) # (Lm^-1)(Umu - m(Z)) self.Ucov_chol = gp.Param(np.tile( np.eye(self.n_ind_pts)[None, ...], [self.obs_dim, 1, 1]), transform=gp.transforms.LowerTriangular( self.n_ind_pts, num_matrices=self.obs_dim, squeeze=False)) # (Lm^-1)Lu
def zero_mean(): return mean_functions.Zero(output_dim=Data.D_out)
def markov_gauss(): cov_params = rng.randn(num_data + 1, D_in, 2 * D_in) / 2.0 # (N+1)xDx2D Xcov = cov_params @ np.transpose(cov_params, (0, 2, 1)) # (N+1)xDxD Xcross = cov_params[:-1] @ np.transpose(cov_params[1:], (0, 2, 1)) # NxDxD Xcross = np.concatenate((Xcross, np.zeros((1, D_in, D_in))), 0) # (N+1)xDxD Xcov = np.stack([Xcov, Xcross]) # 2x(N+1)xDxD return MarkovGaussian(Xmu_markov, ctt(Xcov)) _means = { "lin": mf.Linear(A=rng.randn(D_in, D_out), b=rng.randn(D_out)), "identity": mf.Identity(input_dim=D_in), "const": mf.Constant(c=rng.randn(D_out)), "zero": mf.Zero(output_dim=D_out), } _distrs = { "gauss": Gaussian(Xmu, Xcov), "dirac_gauss": Gaussian(Xmu, np.zeros((num_data, D_in, D_in))), "gauss_diag": DiagonalGaussian(Xmu, rng.rand(num_data, D_in)), "dirac_diag": DiagonalGaussian(Xmu, np.zeros((num_data, D_in))), "dirac_markov_gauss": MarkovGaussian(Xmu_markov, np.zeros((2, num_data + 1, D_in, D_in))), "markov_gauss": markov_gauss(),
def markov_gauss(): cov_params = rng.randn(num_data + 1, D_in, 2 * D_in) / 2. # (N+1)xDx2D Xcov = cov_params @ np.transpose(cov_params, (0, 2, 1)) # (N+1)xDxD Xcross = cov_params[:-1] @ np.transpose(cov_params[1:], (0, 2, 1)) # NxDxD Xcross = np.concatenate((Xcross, np.zeros((1, D_in, D_in))), 0) # (N+1)xDxD Xcov = np.stack([Xcov, Xcross]) # 2x(N+1)xDxD return MarkovGaussian(Xmu_markov, ctt(Xcov)) _means = { 'lin': mf.Linear(A=rng.randn(D_in, D_out), b=rng.randn(D_out)), 'identity': mf.Identity(input_dim=D_in), 'const': mf.Constant(c=rng.randn(D_out)), 'zero': mf.Zero(output_dim=D_out) } _distrs = { 'gauss': Gaussian(Xmu, Xcov), 'dirac_gauss': Gaussian(Xmu, np.zeros((num_data, D_in, D_in))), 'gauss_diag': DiagonalGaussian(Xmu, rng.rand(num_data, D_in)), 'dirac_diag': DiagonalGaussian(Xmu, np.zeros((num_data, D_in))), 'dirac_markov_gauss': MarkovGaussian(Xmu_markov, np.zeros((2, num_data + 1, D_in, D_in))), 'markov_gauss': markov_gauss()
class Data: rng = np.random.RandomState(1) num_data = 5 num_ind = 4 D_in = 2 D_out = 2 Xmu = rng.randn(num_data, D_in) L = gen_L(rng, num_data, D_in, D_in) Xvar = np.array([l @ l.T for l in L]) Z = rng.randn(num_ind, D_in) # distributions don't need to be compiled (No Parameter objects) # but the members should be Tensors created in the same graph graph = tf.Graph() with test_util.session_context(graph) as sess: gauss = Gaussian(tf.constant(Xmu), tf.constant(Xvar)) dirac = Gaussian(tf.constant(Xmu), tf.constant(np.zeros((num_data, D_in, D_in)))) gauss_diag = DiagonalGaussian(tf.constant(Xmu), tf.constant(rng.rand(num_data, D_in))) dirac_diag = DiagonalGaussian(tf.constant(Xmu), tf.constant(np.zeros((num_data, D_in)))) dirac_markov_gauss = MarkovGaussian( tf.constant(Xmu), tf.constant(np.zeros((2, num_data, D_in, D_in)))) # create the covariance for the pairwise markov-gaussian dummy_gen = lambda rng, n, *shape: np.array( [rng.randn(*shape) for _ in range(n)]) L_mg = dummy_gen(rng, num_data, D_in, 2 * D_in) # N+1 x D x 2D LL = np.concatenate((L_mg[:-1], L_mg[1:]), 1) # N x 2D x 2D Xcov = LL @ np.transpose(LL, (0, 2, 1)) Xc = np.concatenate((Xcov[:, :D_in, :D_in], Xcov[-1:, D_in:, D_in:]), 0) # N+1 x D x D Xcross = np.concatenate( (Xcov[:, :D_in, D_in:], np.zeros( (1, D_in, D_in))), 0) # N+1 x D x D Xcc = np.stack([Xc, Xcross]) # 2 x N+1 x D x D markov_gauss = MarkovGaussian(Xmu, Xcc) with gpflow.decors.defer_build(): # features ip = features.InducingPoints(Z) # kernels rbf_prod_seperate_dims = kernels.Product([ kernels.RBF(1, variance=rng.rand(), lengthscales=rng.rand(), active_dims=[0]), kernels.RBF(1, variance=rng.rand(), lengthscales=rng.rand(), active_dims=[1]) ]) rbf_lin_sum = kernels.Sum([ kernels.RBF(D_in, variance=rng.rand(), lengthscales=rng.rand()), kernels.RBF(D_in, variance=rng.rand(), lengthscales=rng.rand()), kernels.Linear(D_in, variance=rng.rand()) ]) rbf = kernels.RBF(D_in, variance=rng.rand(), lengthscales=rng.rand()) lin_kern = kernels.Linear(D_in, variance=rng.rand()) # mean functions lin = mean_functions.Linear(rng.rand(D_in, D_out), rng.rand(D_out)) iden = mean_functions.Identity( D_in) # Note: Identity can only be used if Din == Dout zero = mean_functions.Zero(output_dim=D_out) const = mean_functions.Constant(rng.rand(D_out))