def test_sample_conditional_mixedkernel(): q_mu = tf.random.uniform((Data.M, Data.L), dtype=tf.float64) # M x L q_sqrt = tf.convert_to_tensor( [np.tril(tf.random.uniform((Data.M, Data.M), dtype=tf.float64)) for _ in range(Data.L)] ) # L x M x M Z = Data.X[: Data.M, ...] # M x D N = int(10e5) Xs = np.ones((N, Data.D), dtype=float_type) # Path 1: mixed kernel: most efficient route W = np.random.randn(Data.P, Data.L) mixed_kernel = mk.LinearCoregionalization([SquaredExponential() for _ in range(Data.L)], W) optimal_inducing_variable = mf.SharedIndependentInducingVariables(InducingPoints(Z)) value, mean, var = sample_conditional( Xs, optimal_inducing_variable, mixed_kernel, q_mu, q_sqrt=q_sqrt, white=True ) # Path 2: independent kernels, mixed later separate_kernel = mk.SeparateIndependent([SquaredExponential() for _ in range(Data.L)]) fallback_inducing_variable = mf.SharedIndependentInducingVariables(InducingPoints(Z)) value2, mean2, var2 = sample_conditional( Xs, fallback_inducing_variable, separate_kernel, q_mu, q_sqrt=q_sqrt, white=True ) value2 = np.matmul(value2, W.T) # check if mean and covariance of samples are similar np.testing.assert_array_almost_equal(np.mean(value, axis=0), np.mean(value2, axis=0), decimal=1) np.testing.assert_array_almost_equal( np.cov(value, rowvar=False), np.cov(value2, rowvar=False), decimal=1 )
def test_sample_conditional_mixedkernel(session_tf): q_mu = np.random.randn(Data.M , Data.L) # M x L q_sqrt = np.array([np.tril(np.random.randn(Data.M, Data.M)) for _ in range(Data.L)]) # L x M x M Z = Data.X[:Data.M,...] # M x D N = int(10e5) Xs = np.ones((N, Data.D), dtype=float_type) values = {"Xnew": Xs, "q_mu": q_mu, "q_sqrt": q_sqrt} placeholders = _create_placeholder_dict(values) feed_dict = _create_feed_dict(placeholders, values) # Path 1: mixed kernel: most efficient route W = np.random.randn(Data.P, Data.L) mixed_kernel = mk.SeparateMixedMok([RBF(Data.D) for _ in range(Data.L)], W) mixed_feature = mf.MixedKernelSharedMof(InducingPoints(Z.copy())) sample = sample_conditional(placeholders["Xnew"], mixed_feature, mixed_kernel, placeholders["q_mu"], q_sqrt=placeholders["q_sqrt"], white=True) value, mean, var = session_tf.run(sample, feed_dict=feed_dict) # Path 2: independent kernels, mixed later separate_kernel = mk.SeparateIndependentMok([RBF(Data.D) for _ in range(Data.L)]) shared_feature = mf.SharedIndependentMof(InducingPoints(Z.copy())) sample2 = sample_conditional(placeholders["Xnew"], shared_feature, separate_kernel, placeholders["q_mu"], q_sqrt=placeholders["q_sqrt"], white=True) value2, mean2, var2 = session_tf.run(sample2, feed_dict=feed_dict) value2 = np.matmul(value2, W.T) # check if mean and covariance of samples are similar np.testing.assert_array_almost_equal(np.mean(value, axis=0), np.mean(value2, axis=0), decimal=1) np.testing.assert_array_almost_equal(np.cov(value, rowvar=False), np.cov(value2, rowvar=False), decimal=1)
def test_sample_conditional(whiten, full_cov, full_output_cov): if full_cov and full_output_cov: return q_mu = tf.random.uniform((Data.M, Data.P), dtype=tf.float64) # [M, P] q_sqrt = tf.convert_to_tensor([ np.tril(tf.random.uniform((Data.M, Data.M), dtype=tf.float64)) for _ in range(Data.P) ]) # [P, M, M] Z = Data.X[:Data.M, ...] # [M, D] Xs = np.ones((Data.N, Data.D), dtype=float_type) inducing_variable = InducingPoints(Z) kernel = SquaredExponential() # Path 1 value_f, mean_f, var_f = sample_conditional( Xs, inducing_variable, kernel, q_mu, q_sqrt=q_sqrt, white=whiten, full_cov=full_cov, full_output_cov=full_output_cov, num_samples=int(1e5), ) value_f = value_f.numpy().reshape((-1, ) + value_f.numpy().shape[2:]) # Path 2 if full_output_cov: pytest.skip( "sample_conditional with X instead of inducing_variable does not support full_output_cov" ) value_x, mean_x, var_x = sample_conditional( Xs, Z, kernel, q_mu, q_sqrt=q_sqrt, white=whiten, full_cov=full_cov, full_output_cov=full_output_cov, num_samples=int(1e5), ) value_x = value_x.numpy().reshape((-1, ) + value_x.numpy().shape[2:]) # check if mean and covariance of samples are similar np.testing.assert_array_almost_equal(np.mean(value_x, axis=0), np.mean(value_f, axis=0), decimal=1) np.testing.assert_array_almost_equal(np.cov(value_x, rowvar=False), np.cov(value_f, rowvar=False), decimal=1) np.testing.assert_allclose(mean_x, mean_f) np.testing.assert_allclose(var_x, var_f)
def test_sample_conditional(session_tf, whiten, full_cov, full_output_cov): q_mu = np.random.randn(Data.M, Data.P) # M x P q_sqrt = np.array([ np.tril(np.random.randn(Data.M, Data.M)) for _ in range(Data.P) ]) # P x M x M Z = Data.X[:Data.M, ...] # M x D Xs = np.ones((Data.N, Data.D), dtype=float_type) feature = InducingPoints(Z.copy()) kernel = RBF(Data.D) values = {"Z": Z, "Xnew": Xs, "q_mu": q_mu, "q_sqrt": q_sqrt} placeholders = _create_placeholder_dict(values) feed_dict = _create_feed_dict(placeholders, values) # Path 1 sample_f = sample_conditional(placeholders["Xnew"], feature, kernel, placeholders["q_mu"], q_sqrt=placeholders["q_sqrt"], white=whiten, full_cov=full_cov, full_output_cov=full_output_cov, num_samples=int(1e5)) value_f, mean_f, var_f = session_tf.run(sample_f, feed_dict=feed_dict) value_f = value_f.reshape((-1, ) + value_f.shape[2:]) # Path 2 if full_output_cov: pytest.skip( "sample_conditional with X instead of feature does not support full_output_cov" ) sample_x = sample_conditional(placeholders["Xnew"], placeholders["Z"], kernel, placeholders["q_mu"], q_sqrt=placeholders["q_sqrt"], white=whiten, full_cov=full_cov, full_output_cov=full_output_cov, num_samples=int(1e5)) value_x, mean_x, var_x = session_tf.run(sample_x, feed_dict=feed_dict) value_x = value_x.reshape((-1, ) + value_x.shape[2:]) # check if mean and covariance of samples are similar np.testing.assert_array_almost_equal(np.mean(value_x, axis=0), np.mean(value_f, axis=0), decimal=1) np.testing.assert_array_almost_equal(np.cov(value_x, rowvar=False), np.cov(value_f, rowvar=False), decimal=1) np.testing.assert_allclose(mean_x, mean_f) np.testing.assert_allclose(var_x, var_f)
def test_sample_conditional_mixedkernel(session_tf): q_mu = np.random.randn(Data.M, Data.L) # M x L q_sqrt = np.array([ np.tril(np.random.randn(Data.M, Data.M)) for _ in range(Data.L) ]) # L x M x M Z = Data.X[:Data.M, ...] # M x D N = int(10e5) Xs = np.ones((N, Data.D), dtype=float_type) values = {"Xnew": Xs, "q_mu": q_mu, "q_sqrt": q_sqrt} placeholders = _create_placeholder_dict(values) feed_dict = _create_feed_dict(placeholders, values) # Path 1: mixed kernel: most efficient route W = np.random.randn(Data.P, Data.L) mixed_kernel = mk.SeparateMixedMok([RBF(Data.D) for _ in range(Data.L)], W) mixed_feature = mf.MixedKernelSharedMof(InducingPoints(Z.copy())) sample = sample_conditional(placeholders["Xnew"], mixed_feature, mixed_kernel, placeholders["q_mu"], q_sqrt=placeholders["q_sqrt"], white=True) value = session_tf.run(sample, feed_dict=feed_dict) # Path 2: independent kernels, mixed later separate_kernel = mk.SeparateIndependentMok( [RBF(Data.D) for _ in range(Data.L)]) shared_feature = mf.SharedIndependentMof(InducingPoints(Z.copy())) sample2 = sample_conditional(placeholders["Xnew"], shared_feature, separate_kernel, placeholders["q_mu"], q_sqrt=placeholders["q_sqrt"], white=True) value2 = session_tf.run(sample2, feed_dict=feed_dict) value2 = np.matmul(value2, W.T) # check if mean and covariance of samples are similar np.testing.assert_array_almost_equal(np.mean(value, axis=0), np.mean(value2, axis=0), decimal=1) np.testing.assert_array_almost_equal(np.cov(value, rowvar=False), np.cov(value2, rowvar=False), decimal=1)
def sample_conditional_fn(X): return sample_conditional(X, inducing_variable, kernel, tf.convert_to_tensor(q_mu), q_sqrt=tf.convert_to_tensor(q_sqrt), white=white, full_cov=full_cov, num_samples=num_samples)
def multisample_sample_conditional(Xnew: tf.Tensor, feat: InducingPoints, kern: Kernel, f: tf.Tensor, *, full_cov=False, full_output_cov=False, q_sqrt=None, white=False): if isinstance(kern, SharedMixedMok) and isinstance(feat, MixedKernelSharedMof): if Xnew.get_shape().ndims == 3: sample, gmean, gvar = independent_multisample_sample_conditional(Xnew, feat.feat, kern.kernel, f, white=white, q_sqrt=q_sqrt, full_output_cov=False, full_cov=False) # N x L, N x L o = tf.ones(([tf.shape(Xnew)[0], 1, 1]), dtype=settings.float_type) else: sample, gmean, gvar = sample_conditional(Xnew, feat.feat, kern.kernel, f, white=white, q_sqrt=q_sqrt, full_output_cov=False, full_cov=False) # N x L, N x L o = 1. with params_as_tensors_for(kern): f_sample = tf.matmul(sample, o * kern.W, transpose_b=True) f_mu = tf.matmul(gmean, o * kern.W, transpose_b=True) f_var = tf.matmul(gvar, o * kern.W ** 2, transpose_b=True) return f_sample, f_mu, f_var else: assert not isinstance(kern, Mok) if Xnew.get_shape().ndims == 3: return independent_multisample_sample_conditional(Xnew, feat, kern, f, full_cov=full_cov, full_output_cov=full_output_cov, q_sqrt=q_sqrt, white=white) else: return sample_conditional(Xnew, feat, kern, f, full_cov=full_cov, full_output_cov=full_output_cov, q_sqrt=q_sqrt, white=white)
def test_sample_conditional(session_tf, whiten): q_mu = np.random.randn(Data.M, Data.P) # M x P q_sqrt = np.array([ np.tril(np.random.randn(Data.M, Data.M)) for _ in range(Data.P) ]) # P x M x M Z = Data.X[:Data.M, ...] # M x D Xs = np.ones((int(10e5), Data.D), dtype=float_type) feature = InducingPoints(Z.copy()) kernel = RBF(Data.D) values = {"Z": Z, "Xnew": Xs, "q_mu": q_mu, "q_sqrt": q_sqrt} placeholders = _create_placeholder_dict(values) feed_dict = _create_feed_dict(placeholders, values) # Path 1 sample = sample_conditional(placeholders["Xnew"], placeholders["Z"], kernel, placeholders["q_mu"], q_sqrt=placeholders["q_sqrt"], white=whiten) value = session_tf.run(sample, feed_dict=feed_dict) # Path 2 sample2 = sample_conditional(placeholders["Xnew"], feature, kernel, placeholders["q_mu"], q_sqrt=placeholders["q_sqrt"], white=whiten) value2 = session_tf.run(sample2, feed_dict=feed_dict) # check if mean and covariance of samples are similar np.testing.assert_array_almost_equal(np.mean(value, axis=0), np.mean(value2, axis=0), decimal=1) np.testing.assert_array_almost_equal(np.cov(value, rowvar=False), np.cov(value2, rowvar=False), decimal=1)
def test_sample_conditional(session_tf, whiten, full_cov, full_output_cov): q_mu = np.random.randn(Data.M , Data.P) # M x P q_sqrt = np.array([np.tril(np.random.randn(Data.M, Data.M)) for _ in range(Data.P)]) # P x M x M Z = Data.X[:Data.M, ...] # M x D Xs = np.ones((Data.N, Data.D), dtype=float_type) feature = InducingPoints(Z.copy()) kernel = RBF(Data.D) values = {"Z": Z, "Xnew": Xs, "q_mu": q_mu, "q_sqrt": q_sqrt} placeholders = _create_placeholder_dict(values) feed_dict = _create_feed_dict(placeholders, values) # Path 1 sample_f = sample_conditional(placeholders["Xnew"], feature, kernel, placeholders["q_mu"], q_sqrt=placeholders["q_sqrt"], white=whiten, full_cov=full_cov, full_output_cov=full_output_cov, num_samples=int(1e5)) value_f, mean_f, var_f = session_tf.run(sample_f, feed_dict=feed_dict) value_f = value_f.reshape((-1,) + value_f.shape[2:]) # Path 2 if full_output_cov: pytest.skip("sample_conditional with X instead of feature does not support full_output_cov") sample_x = sample_conditional(placeholders["Xnew"], placeholders["Z"], kernel, placeholders["q_mu"], q_sqrt=placeholders["q_sqrt"], white=whiten, full_cov=full_cov, full_output_cov=full_output_cov, num_samples=int(1e5)) value_x, mean_x, var_x = session_tf.run(sample_x, feed_dict=feed_dict) value_x = value_x.reshape((-1,) + value_x.shape[2:]) # check if mean and covariance of samples are similar np.testing.assert_array_almost_equal(np.mean(value_x, axis=0), np.mean(value_f, axis=0), decimal=1) np.testing.assert_array_almost_equal(np.cov(value_x, rowvar=False), np.cov(value_f, rowvar=False), decimal=1) np.testing.assert_allclose(mean_x, mean_f) np.testing.assert_allclose(var_x, var_f)
def test_conditional_broadcasting(session_tf, full_cov, white, conditional_type): """ Test that the `conditional` and `sample_conditional` broadcasts correctly over leading dimensions of Xnew. Xnew can be shape [..., N, D], and conditional should broadcast over the [...]. """ X_ = tf.placeholder(tf.float64, [None, None]) q_mu = np.random.randn(Data.M, Data.Dy) q_sqrt = np.tril(np.random.randn(Data.Dy, Data.M, Data.M), -1) if conditional_type == "Z": feat = Data.Z kern = gpflow.kernels.Matern52(Data.Dx, lengthscales=0.5) elif conditional_type == "inducing_points": feat = gpflow.features.InducingPoints(Data.Z) kern = gpflow.kernels.Matern52(Data.Dx, lengthscales=0.5) elif conditional_type == "mixing": # variational params have different output dim in this case q_mu = np.random.randn(Data.M, Data.L) q_sqrt = np.tril(np.random.randn(Data.L, Data.M, Data.M), -1) feat = mf.MixedKernelSharedMof(gpflow.features.InducingPoints(Data.Z)) kern = mk.SeparateMixedMok(kernels=[ gpflow.kernels.Matern52(Data.Dx, lengthscales=0.5) for _ in range(Data.L) ], W=Data.W) if conditional_type == "mixing" and full_cov: pytest.skip("combination is not implemented") num_samples = 5 sample_tf, mean_tf, cov_tf = sample_conditional( X_, feat, kern, tf.convert_to_tensor(q_mu), q_sqrt=tf.convert_to_tensor(q_sqrt), white=white, full_cov=full_cov, num_samples=num_samples) ss, ms, vs = [], [], [] for X in Data.SX: s, m, v = session_tf.run([sample_tf, mean_tf, cov_tf], {X_: X}) ms.append(m) vs.append(v) ss.append(s) ms = np.array(ms) vs = np.array(vs) ss = np.array(ss) ss_S12, ms_S12, vs_S12 = session_tf.run( sample_conditional(Data.SX, feat, kern, tf.convert_to_tensor(q_mu), q_sqrt=tf.convert_to_tensor(q_sqrt), white=white, full_cov=full_cov, num_samples=num_samples)) ss_S1_S2, ms_S1_S2, vs_S1_S2 = session_tf.run( sample_conditional(Data.S1_S2_X, feat, kern, tf.convert_to_tensor(q_mu), q_sqrt=tf.convert_to_tensor(q_sqrt), white=white, full_cov=full_cov, num_samples=num_samples)) assert_allclose(ss_S12.shape, ss.shape) assert_allclose(ms_S12, ms) assert_allclose(vs_S12, vs) assert_allclose(ms_S1_S2.reshape(Data.S1 * Data.S2, Data.N, Data.Dy), ms) assert_allclose(ss_S1_S2.shape, [Data.S1, Data.S2, num_samples, Data.N, Data.Dy]) if full_cov: assert_allclose( vs_S1_S2.reshape(Data.S1 * Data.S2, Data.Dy, Data.N, Data.N), vs) else: assert_allclose(vs_S1_S2.reshape(Data.S1 * Data.S2, Data.N, Data.Dy), vs)
def test_conditional_broadcasting(full_cov, white, conditional_type): """ Test that the `conditional` and `sample_conditional` broadcasts correctly over leading dimensions of Xnew. Xnew can be shape [..., N, D], and conditional should broadcast over the [...]. """ q_mu = np.random.randn(Data.M, Data.Dy) q_sqrt = np.tril(np.random.randn(Data.Dy, Data.M, Data.M), -1) if conditional_type == "Z": inducing_variable = Data.Z kernel = gpflow.kernels.Matern52(lengthscales=0.5) elif conditional_type == "inducing_points": inducing_variable = gpflow.inducing_variables.InducingPoints(Data.Z) kernel = gpflow.kernels.Matern52(lengthscales=0.5) elif conditional_type == "mixing": # variational params have different output dim in this case q_mu = np.random.randn(Data.M, Data.L) q_sqrt = np.tril(np.random.randn(Data.L, Data.M, Data.M), -1) inducing_variable = mf.SharedIndependentInducingVariables( gpflow.inducing_variables.InducingPoints(Data.Z) ) kernel = mk.LinearCoregionalization( kernels=[gpflow.kernels.Matern52(lengthscales=0.5) for _ in range(Data.L)], W=Data.W, ) else: raise NotImplementedError if conditional_type == "mixing" and full_cov: pytest.skip("combination is not implemented") num_samples = 5 def sample_conditional_fn(X): return sample_conditional( X, inducing_variable, kernel, tf.convert_to_tensor(q_mu), q_sqrt=tf.convert_to_tensor(q_sqrt), white=white, full_cov=full_cov, num_samples=num_samples, ) samples = np.array([sample_conditional_fn(X)[0] for X in Data.SX]) means = np.array([sample_conditional_fn(X)[1] for X in Data.SX]) variables = np.array([sample_conditional_fn(X)[2] for X in Data.SX]) samples_S12, means_S12, vars_S12 = sample_conditional( Data.SX, inducing_variable, kernel, tf.convert_to_tensor(q_mu), q_sqrt=tf.convert_to_tensor(q_sqrt), white=white, full_cov=full_cov, num_samples=num_samples, ) samples_S1_S2, means_S1_S2, vars_S1_S2 = sample_conditional( Data.S1_S2_X, inducing_variable, kernel, tf.convert_to_tensor(q_mu), q_sqrt=tf.convert_to_tensor(q_sqrt), white=white, full_cov=full_cov, num_samples=num_samples, ) assert_allclose(samples_S12.shape, samples.shape) assert_allclose(samples_S1_S2.shape, [Data.S1, Data.S2, num_samples, Data.N, Data.Dy]) assert_allclose(means_S12, means) assert_allclose(vars_S12, variables) assert_allclose(means_S1_S2.numpy().reshape(Data.S1 * Data.S2, Data.N, Data.Dy), means) if full_cov: vars_s1_s2 = vars_S1_S2.numpy().reshape(Data.S1 * Data.S2, Data.Dy, Data.N, Data.N) assert_allclose(vars_s1_s2, variables) else: vars_s1_s2 = vars_S1_S2.numpy().reshape(Data.S1 * Data.S2, Data.N, Data.Dy) assert_allclose(vars_s1_s2, variables)