Exemplo n.º 1
0
    def test_define_posterior_predictive_2d_vector_lengthscale(self):
        with self.test_session():
            N, D = 3, 2
            sigma_signal = 3.3
            sigma_noise = 1.5
            lengthscale = np.array([0.8, 2.3], np.float32)

            X_ = tf.placeholder(tf.float32, [N, D])
            K_ = tf.placeholder(tf.float32, [N, N])
            f_ = tf.placeholder(tf.float32, [N])

            X = np.arange(N * D).reshape(N, D)
            K = compute_rbf(X,
                            sigma_signal=sigma_signal,
                            sigma_noise=sigma_noise,
                            lengthscale=lengthscale)
            f = np.zeros(N)

            x_, mu, var = define_posterior_predictive(X_, K_, f_, sigma_signal,
                                                      sigma_noise, lengthscale)

            base_dict = {
                X_: X,
                K_: K,
                f_: f,
            }

            # single query point
            x = np.array([[0.5, 2]])

            mu_true, var_true = compute_posterior_predictive(
                X,
                x=x,
                f=f,
                sigma_signal=sigma_signal,
                sigma_noise=sigma_noise,
                lengthscale=lengthscale)

            feed_dict = {x_: x}
            feed_dict.update(base_dict)

            self.assertAllClose(var.eval(feed_dict=feed_dict), var_true)
            self.assertAllClose(mu.eval(feed_dict=feed_dict), mu_true)

            # multiple query points
            x = np.array([[0, 1.5], [9.2, -3], [17, 2]])

            mu_true, var_true = compute_posterior_predictive(
                X,
                x=x,
                f=f,
                sigma_signal=sigma_signal,
                sigma_noise=sigma_noise,
                lengthscale=lengthscale)

            feed_dict = {x_: x}
            feed_dict.update(base_dict)

            self.assertAllClose(var.eval(feed_dict=feed_dict), var_true)
            self.assertAllClose(mu.eval(feed_dict=feed_dict), mu_true)
Exemplo n.º 2
0
    def test_define_posterior_predictive_1d_simple(self):
        with self.test_session():
            N, D = 3, 1
            sigma_signal = 0.7
            sigma_noise = 1.5
            lengthscale = 1.2

            X_ = tf.placeholder(tf.float32, [N, D])
            K_ = tf.placeholder(tf.float32, [N, N])
            f_ = tf.placeholder(tf.float32, [N])

            X = np.arange(N)[:, np.newaxis]
            K = compute_rbf(X,
                            sigma_signal=sigma_signal,
                            sigma_noise=sigma_noise,
                            lengthscale=lengthscale)
            f = np.zeros(N)

            x_, mu, var = define_posterior_predictive(X_, K_, f_, sigma_signal,
                                                      sigma_noise, lengthscale)

            base_dict = {
                X_: X,
                K_: K,
                f_: f,
            }

            # single query point
            x = np.array([0.5])[:, np.newaxis]

            mu_true, var_true = compute_posterior_predictive(
                X,
                x=x,
                f=f,
                sigma_signal=sigma_signal,
                sigma_noise=sigma_noise,
                lengthscale=lengthscale)

            feed_dict = {x_: x}
            feed_dict.update(base_dict)

            self.assertAllClose(var.eval(feed_dict=feed_dict), var_true)
            self.assertAllClose(mu.eval(feed_dict=feed_dict), mu_true)

            # multiple query points
            x = np.array([0, 1.5, 9])[:, np.newaxis]

            mu_true, var_true = compute_posterior_predictive(
                X,
                x=x,
                f=f,
                sigma_signal=sigma_signal,
                sigma_noise=sigma_noise,
                lengthscale=lengthscale)

            feed_dict = {x_: x}
            feed_dict.update(base_dict)

            self.assertAllClose(var.eval(feed_dict=feed_dict), var_true)
            self.assertAllClose(mu.eval(feed_dict=feed_dict), mu_true)