Esempio n. 1
0
 def testNoneShapes(self):
   k = tfpk.ExponentiatedQuadratic(
       amplitude=np.reshape(np.arange(12.), [2, 3, 2]))
   self.assertEqual([2, 3, 2], k.batch_shape.as_list())
Esempio n. 2
0
 def kernel(self):
     kernel = tfk.ExponentiatedQuadratic(self._amplitude,
                                         self._length_scale)
     return kernel
Esempio n. 3
0
 def testMismatchedFloatTypesAreBad(self):
   tfpk.ExponentiatedQuadratic(1, 1)  # Should be OK (float32 fallback).
   tfpk.ExponentiatedQuadratic(np.float32(1.), 1.)  # Should be OK.
   with self.assertRaises(TypeError):
     tfpk.ExponentiatedQuadratic(np.float32(1.), np.float64(1.))
Esempio n. 4
0
    def testShapes(self):
        # 5x5 grid of index points in R^2 and flatten to 25x2
        index_points = np.linspace(-4., 4., 5, dtype=np.float64)
        index_points = np.stack(np.meshgrid(index_points, index_points),
                                axis=-1)
        index_points = np.reshape(index_points, [-1, 2])
        # ==> shape = [25, 2]
        batched_index_points = np.expand_dims(np.stack([index_points] * 6), -3)
        # ==> shape = [6, 1, 25, 2]

        # 9 inducing index points in R^2
        inducing_index_points = np.linspace(-4., 4., 3, dtype=np.float64)
        inducing_index_points = np.stack(np.meshgrid(inducing_index_points,
                                                     inducing_index_points),
                                         axis=-1)
        inducing_index_points = np.reshape(inducing_index_points, [-1, 2])
        # ==> shape = [9, 2]

        variational_inducing_observations_loc = np.zeros([3, 9],
                                                         dtype=np.float64)
        variational_inducing_observations_scale = np.eye(9, dtype=np.float64)

        # Kernel with batch_shape [2, 4, 1, 1]
        amplitude = np.array([1., 2.], np.float64).reshape([2, 1, 1, 1])
        length_scale = np.array([.1, .2, .3, .4],
                                np.float64).reshape([1, 4, 1, 1])

        jitter = np.float64(1e-6)
        observation_noise_variance = np.float64(1e-2)

        if not self.is_static:
            amplitude = tf.compat.v1.placeholder_with_default(amplitude,
                                                              shape=None)
            length_scale = tf.compat.v1.placeholder_with_default(length_scale,
                                                                 shape=None)
            batched_index_points = tf.compat.v1.placeholder_with_default(
                batched_index_points, shape=None)

            inducing_index_points = tf.compat.v1.placeholder_with_default(
                inducing_index_points, shape=None)
            variational_inducing_observations_loc = tf.compat.v1.placeholder_with_default(
                variational_inducing_observations_loc, shape=None)
            variational_inducing_observations_scale = tf.compat.v1.placeholder_with_default(
                variational_inducing_observations_scale, shape=None)

        kernel = psd_kernels.ExponentiatedQuadratic(amplitude, length_scale)

        vgp = tfd.VariationalGaussianProcess(
            kernel=kernel,
            index_points=batched_index_points,
            inducing_index_points=inducing_index_points,
            variational_inducing_observations_loc=(
                variational_inducing_observations_loc),
            variational_inducing_observations_scale=(
                variational_inducing_observations_scale),
            observation_noise_variance=observation_noise_variance,
            jitter=jitter)

        batch_shape = [2, 4, 6, 3]
        event_shape = [25]
        sample_shape = [9, 3]

        samples = vgp.sample(sample_shape)

        if self.is_static or tf.executing_eagerly():
            self.assertAllEqual(vgp.batch_shape_tensor(), batch_shape)
            self.assertAllEqual(vgp.event_shape_tensor(), event_shape)
            self.assertAllEqual(samples.shape,
                                sample_shape + batch_shape + event_shape)
            self.assertAllEqual(vgp.batch_shape, batch_shape)
            self.assertAllEqual(vgp.event_shape, event_shape)
            self.assertAllEqual(samples.shape,
                                sample_shape + batch_shape + event_shape)
        else:
            self.assertAllEqual(self.evaluate(vgp.batch_shape_tensor()),
                                batch_shape)
            self.assertAllEqual(self.evaluate(vgp.event_shape_tensor()),
                                event_shape)
            self.assertAllEqual(
                self.evaluate(samples).shape,
                sample_shape + batch_shape + event_shape)
            self.assertIsNone(tensorshape_util.rank(samples.shape))
            self.assertIsNone(tensorshape_util.rank(vgp.batch_shape))
            self.assertEqual(tensorshape_util.rank(vgp.event_shape), 1)
            self.assertIsNone(
                tf.compat.dimension_value(
                    tensorshape_util.dims(vgp.event_shape)[0]))
Esempio n. 5
0
    def testVariationalLossShapes(self):
        # 2x2 grid of index points in R^2 and flatten to 4x2
        index_points = np.linspace(-4., 4., 2, dtype=np.float64)
        index_points = np.stack(np.meshgrid(index_points, index_points),
                                axis=-1)
        index_points = np.reshape(index_points, [-1, 2])
        # ==> shape = [4, 2]
        batched_index_points = np.expand_dims(np.stack([index_points] * 6), -3)
        # ==> shape = [6, 1, 4, 2]

        # 3x3 grid of index points in R^2 and flatten to 9x2
        observation_index_points = np.linspace(-4., 4., 3, dtype=np.float64)
        observation_index_points = np.stack(np.meshgrid(
            observation_index_points, observation_index_points),
                                            axis=-1)
        observation_index_points = np.reshape(observation_index_points,
                                              [-1, 2])
        # ==> shape = [9, 2]
        observation_index_points = np.expand_dims(
            np.stack([observation_index_points] * 6), -3)
        # ==> shape = [6, 1, 9, 2]
        observations = np.sin(observation_index_points[..., 0])
        # ==> shape = [6, 1, 9]

        # 9 inducing index points in R^2
        inducing_index_points = np.linspace(-4., 4., 3, dtype=np.float64)
        inducing_index_points = np.stack(np.meshgrid(inducing_index_points,
                                                     inducing_index_points),
                                         axis=-1)
        inducing_index_points = np.reshape(inducing_index_points, [-1, 2])
        # ==> shape = [9, 2]

        variational_inducing_observations_loc = np.zeros([3, 9],
                                                         dtype=np.float64)
        variational_inducing_observations_scale = np.eye(9, dtype=np.float64)

        # Kernel with batch_shape [2, 4, 1, 1]
        amplitude = np.array([1., 2.], np.float64).reshape([2, 1, 1, 1])
        length_scale = np.array([.1, .2, .3, .4],
                                np.float64).reshape([1, 4, 1, 1])

        jitter = np.float64(1e-6)
        observation_noise_variance = np.float64(1e-2)

        if not self.is_static:
            amplitude = tf.compat.v1.placeholder_with_default(amplitude,
                                                              shape=None)
            length_scale = tf.compat.v1.placeholder_with_default(length_scale,
                                                                 shape=None)
            batched_index_points = tf.compat.v1.placeholder_with_default(
                batched_index_points, shape=None)

            observations = tf.compat.v1.placeholder_with_default(observations,
                                                                 shape=None)
            observation_index_points = tf.compat.v1.placeholder_with_default(
                observation_index_points, shape=None)
            inducing_index_points = tf.compat.v1.placeholder_with_default(
                inducing_index_points, shape=None)
            variational_inducing_observations_loc = tf.compat.v1.placeholder_with_default(
                variational_inducing_observations_loc, shape=None)
            variational_inducing_observations_scale = tf.compat.v1.placeholder_with_default(
                variational_inducing_observations_scale, shape=None)

        kernel = psd_kernels.ExponentiatedQuadratic(amplitude, length_scale)

        vgp = tfd.VariationalGaussianProcess(
            kernel=kernel,
            index_points=batched_index_points,
            inducing_index_points=inducing_index_points,
            variational_inducing_observations_loc=(
                variational_inducing_observations_loc),
            variational_inducing_observations_scale=(
                variational_inducing_observations_scale),
            observation_noise_variance=observation_noise_variance,
            jitter=jitter)

        loss = vgp.variational_loss(
            observations=observations,
            observation_index_points=observation_index_points)
        # Expect a scalar loss.
        self.assertAllClose([], tf.shape(input=loss))
    length_scale_assign = invert_softplus(len_p, len_var)

observation_noise_variance = (np.finfo(np.float64).tiny + tf.nn.softplus(
    tf.Variable(initial_value=INIT_OBSNOISEVAR,
                name='observation_noise_variance',
                dtype=np.float64)))
#####################################

# Create the covariance kernel, which will be shared between the prior (which we
# use for maximum likelihood training) and the posterior (which we use for
# posterior predictive sampling)
assert amplitude.shape == AMPLITUDE_INIT.shape
assert length_scale.shape == LENGTHSCALE_INIT.shape
assert amplitude.shape == length_scale.shape

kernel = tfk.ExponentiatedQuadratic(amplitude, length_scale)


def kernel_test():
    k = None
    # select two points: X1, X2; k = kernel(X1, X2)
    for i in range(observation_index_points_.shape[0]):
        for j in range(observation_index_points_.shape[0]):
            if i != j:
                k = kernel._apply(observation_index_points_[i, :],
                                  observation_index_points_[j, :])
    pass


# kernel_test()
#####################################
Esempio n. 7
0
def cov_kernel(amplitude_, length_scale_):
    return tfk.ExponentiatedQuadratic(amplitude_, length_scale_)