Beispiel #1
0
    def testValuesAreCorrectVectorTransform(self, feature_ndims, dims):
        amplitude = self.dtype(5.)
        length_scale = self.dtype(0.2)
        kernel = tfpk.ExponentiatedQuadratic(amplitude, length_scale,
                                             feature_ndims)
        input_shape = [dims] * feature_ndims

        scale_diag = np.random.uniform(-1, 1, size=(dims, )).astype(self.dtype)
        bij = bijectors.Affine(scale_diag=scale_diag)

        # Scaling the last dimension.
        def vector_transform(x, feature_ndims, param_expansion_ndims):
            del feature_ndims, param_expansion_ndims
            return bij.forward(x)

        vector_transformed_kernel = tfpk.FeatureTransformed(
            kernel, transformation_fn=vector_transform)

        x = np.random.uniform(-1, 1, size=input_shape).astype(self.dtype)
        y = np.random.uniform(-1, 1, size=input_shape).astype(self.dtype)
        self.assertAllClose(
            _numpy_exp_quad(amplitude,
                            length_scale,
                            scale_diag * x,
                            scale_diag * y,
                            feature_ndims=feature_ndims),
            self.evaluate(vector_transformed_kernel.apply(x, y)))
 def testMean(self):
     mean_fn = lambda x: x[:, 0]**2
     kernel = psd_kernels.ExponentiatedQuadratic()
     index_points = np.expand_dims(np.random.uniform(-1., 1., 10), -1)
     gp = tfd.GaussianProcess(kernel, index_points, mean_fn=mean_fn)
     expected_mean = mean_fn(index_points)
     self.assertAllClose(expected_mean, self.evaluate(gp.mean()))
    def testRetrieveIdentityTransform(self, feature_ndims, dims):
        amplitude = np.random.uniform(low=1., high=10.,
                                      size=[10, 2]).astype(self.dtype)
        inner_length_scale = self.dtype(1.)
        kernel = tfpk.ExponentiatedQuadratic(amplitude, inner_length_scale,
                                             feature_ndims)
        input_shape = [dims] * feature_ndims

        # This is the identity transform.
        concentration1 = self.dtype(1.)
        concentration0 = self.dtype(1.)

        kum_kernel = tfpk.KumaraswamyTransformed(kernel, concentration1,
                                                 concentration0)

        x = np.random.uniform(size=input_shape).astype(self.dtype)
        y = np.random.uniform(size=input_shape).astype(self.dtype)

        self.assertAllClose(
            _numpy_exp_quad(amplitude,
                            inner_length_scale,
                            x,
                            y,
                            feature_ndims=feature_ndims),
            self.evaluate(kum_kernel.apply(x, y)))

        z = np.random.uniform(size=[10] + input_shape).astype(self.dtype)

        self.assertAllClose(
            _numpy_exp_quad_matrix(amplitude[..., None, None],
                                   inner_length_scale,
                                   z,
                                   feature_ndims=feature_ndims),
            self.evaluate(kum_kernel.matrix(z, z)))
Beispiel #4
0
  def testVarianceAndCovarianceMatrix(self):
    amp = np.float64(.5)
    len_scale = np.float64(.2)
    jitter = np.float64(1e-4)
    observation_noise_variance = np.float64(3e-3)

    kernel = psd_kernels.ExponentiatedQuadratic(amp, len_scale)

    index_points = np.expand_dims(np.random.uniform(-1., 1., 10), -1)

    gp = tfd.GaussianProcess(
        kernel,
        index_points,
        observation_noise_variance=observation_noise_variance,
        jitter=jitter)

    def _kernel_fn(x, y):
      return amp * np.exp(-.5 * (np.squeeze((x - y)**2)) / (len_scale**2))

    expected_covariance = (
        _kernel_fn(np.expand_dims(index_points, 0),
                   np.expand_dims(index_points, 1)) +
        (observation_noise_variance + jitter) * np.eye(10))

    self.assertAllClose(expected_covariance,
                        self.evaluate(gp.covariance()))
    self.assertAllClose(np.diag(expected_covariance),
                        self.evaluate(gp.variance()))
Beispiel #5
0
    def testValuesAreCorrectScalarTransform(self, feature_ndims, dims):
        amplitude = self.dtype(5.)
        length_scale = self.dtype(0.2)
        kernel = tfpk.ExponentiatedQuadratic(amplitude, length_scale,
                                             feature_ndims)
        input_shape = [dims] * feature_ndims

        bij = bijectors.AffineScalar(self.dtype(0.), self.dtype(2.))

        # Flat multiplication by 2.
        def scale_transform(x, feature_ndims, param_expansion_ndims):
            del feature_ndims, param_expansion_ndims
            return bij.forward(x)

        scale_transformed_kernel = tfpk.FeatureTransformed(
            kernel, transformation_fn=scale_transform)

        x = np.random.uniform(-1, 1, size=input_shape).astype(self.dtype)
        y = np.random.uniform(-1, 1, size=input_shape).astype(self.dtype)
        self.assertAllClose(
            _numpy_exp_quad(amplitude,
                            length_scale,
                            2. * x,
                            2. * y,
                            feature_ndims=feature_ndims),
            self.evaluate(scale_transformed_kernel.apply(x, y)))
  def testVarianceAndCovarianceMatrix(self):
    df = np.float64(4.)
    amp = np.float64(.5)
    len_scale = np.float64(.2)
    jitter = np.float64(1e-4)

    kernel = psd_kernels.ExponentiatedQuadratic(amp, len_scale)

    index_points = np.expand_dims(np.random.uniform(-1., 1., 10), -1)

    tp = tfd.StudentTProcess(
        df=df,
        kernel=kernel,
        index_points=index_points,
        jitter=jitter)

    def _kernel_fn(x, y):
      return amp ** 2 * np.exp(-.5 * (np.squeeze((x - y)**2)) / (len_scale**2))

    expected_covariance = (
        _kernel_fn(np.expand_dims(index_points, 0),
                   np.expand_dims(index_points, 1)) +
        jitter * np.eye(10))

    self.assertAllClose(expected_covariance,
                        self.evaluate(tp.covariance()))
    self.assertAllClose(np.diag(expected_covariance),
                        self.evaluate(tp.variance()))
  def testShapes(self):
    # 5x5 grid of index points in R^2 and flatten to 25x2
    index_points = np.linspace(-4., 4., 5, dtype=np.float32)
    index_points = np.stack(np.meshgrid(index_points, index_points), axis=-1)
    index_points = np.reshape(index_points, [-1, 2])
    # ==> shape = [25, 2]

    # Kernel with batch_shape [2, 4, 3, 1]
    amplitude = np.array([1., 2.], np.float32).reshape([2, 1, 1, 1])
    length_scale = np.array([1., 2., 3., 4.], np.float32).reshape([1, 4, 1, 1])
    observation_noise_variance = np.array(
        [1e-5, 1e-6, 1e-5], np.float32).reshape([1, 1, 3, 1])
    batched_index_points = np.stack([index_points]*6)
    # ==> shape = [6, 25, 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)
    kernel = psd_kernels.ExponentiatedQuadratic(amplitude, length_scale)
    gp = tfd.GaussianProcess(
        kernel,
        batched_index_points,
        observation_noise_variance=observation_noise_variance,
        jitter=1e-5)

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

    samples = gp.sample(sample_shape)

    if self.is_static or tf.executing_eagerly():
      self.assertAllEqual(gp.batch_shape_tensor(), batch_shape)
      self.assertAllEqual(gp.event_shape_tensor(), event_shape)
      self.assertAllEqual(samples.shape,
                          sample_shape + batch_shape + event_shape)
      self.assertAllEqual(gp.batch_shape, batch_shape)
      self.assertAllEqual(gp.event_shape, event_shape)
      self.assertAllEqual(samples.shape,
                          sample_shape + batch_shape + event_shape)
      self.assertAllEqual(gp.mean().shape, batch_shape + event_shape)
      self.assertAllEqual(gp.variance().shape, batch_shape + event_shape)
    else:
      self.assertAllEqual(self.evaluate(gp.batch_shape_tensor()), batch_shape)
      self.assertAllEqual(self.evaluate(gp.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(gp.batch_shape))
      self.assertEqual(tensorshape_util.rank(gp.event_shape), 1)
      self.assertIsNone(
          tf.compat.dimension_value(tensorshape_util.dims(gp.event_shape)[0]))
      self.assertAllEqual(
          self.evaluate(tf.shape(input=gp.mean())), batch_shape + event_shape)
      self.assertAllEqual(self.evaluate(
          tf.shape(input=gp.variance())), batch_shape + event_shape)
    def testCopy(self):
        # 5 random index points in R^2
        index_points_1 = np.random.uniform(-4., 4., (5, 2)).astype(np.float32)
        # 10 random index points in R^2
        index_points_2 = np.random.uniform(-4., 4., (10, 2)).astype(np.float32)

        # ==> shape = [6, 25, 2]
        if not self.is_static:
            index_points_1 = tf1.placeholder_with_default(index_points_1,
                                                          shape=None)
            index_points_2 = tf1.placeholder_with_default(index_points_2,
                                                          shape=None)

        mean_fn = lambda x: np.array([0.], np.float32)
        kernel_1 = psd_kernels.ExponentiatedQuadratic()
        kernel_2 = psd_kernels.ExpSinSquared()

        tp1 = tfd.StudentTProcess(df=3.,
                                  kernel=kernel_1,
                                  index_points=index_points_1,
                                  mean_fn=mean_fn,
                                  jitter=1e-5,
                                  validate_args=True)
        tp2 = tp1.copy(df=4., index_points=index_points_2, kernel=kernel_2)

        event_shape_1 = [5]
        event_shape_2 = [10]

        self.assertEqual(tp1.mean_fn, tp2.mean_fn)
        self.assertIsInstance(tp1.kernel, psd_kernels.ExponentiatedQuadratic)
        self.assertIsInstance(tp2.kernel, psd_kernels.ExpSinSquared)

        if self.is_static or tf.executing_eagerly():
            self.assertAllEqual(tp1.batch_shape, tp2.batch_shape)
            self.assertAllEqual(tp1.event_shape, event_shape_1)
            self.assertAllEqual(tp2.event_shape, event_shape_2)
            self.assertEqual(self.evaluate(tp1.df), 3.)
            self.assertEqual(self.evaluate(tp2.df), 4.)
            self.assertAllEqual(tp2.index_points, index_points_2)
            self.assertAllEqual(tp1.index_points, index_points_1)
            self.assertAllEqual(tp2.index_points, index_points_2)
            self.assertAllEqual(tf.get_static_value(tp1.jitter),
                                tf.get_static_value(tp2.jitter))
        else:
            self.assertAllEqual(self.evaluate(tp1.batch_shape_tensor()),
                                self.evaluate(tp2.batch_shape_tensor()))
            self.assertAllEqual(self.evaluate(tp1.event_shape_tensor()),
                                event_shape_1)
            self.assertAllEqual(self.evaluate(tp2.event_shape_tensor()),
                                event_shape_2)
            self.assertEqual(self.evaluate(tp1.jitter),
                             self.evaluate(tp2.jitter))
            self.assertEqual(self.evaluate(tp1.df), 3.)
            self.assertEqual(self.evaluate(tp2.df), 4.)
            self.assertAllEqual(self.evaluate(tp1.index_points),
                                index_points_1)
            self.assertAllEqual(self.evaluate(tp2.index_points),
                                index_points_2)
Beispiel #9
0
    def testKernelParametersBroadcast(self, feature_ndims, dims):
        # Batch shape [10, 2]
        amplitude = np.random.uniform(low=1., high=10.,
                                      size=[10, 2]).astype(self.dtype)
        length_scale = np.random.uniform(low=1., high=10.,
                                         size=[1, 2]).astype(self.dtype)
        kernel = tfpk.ExponentiatedQuadratic(amplitude, length_scale,
                                             feature_ndims)
        input_shape = [dims] * feature_ndims

        # Batch shape [3, 1, 2].
        scale_diag = np.random.uniform(-1, 1, size=(
            3,
            1,
            2,
            dims,
        )).astype(self.dtype)

        # Scaling the last dimension.
        def vector_transform(x, feature_ndims, param_expansion_ndims):
            diag = util.pad_shape_with_ones(scale_diag,
                                            param_expansion_ndims +
                                            feature_ndims - 1,
                                            start=-2)
            return diag * x

        vector_transformed_kernel = tfpk.FeatureTransformed(
            kernel, transformation_fn=vector_transform)

        x = np.random.uniform(-1, 1, size=input_shape).astype(self.dtype)
        y = np.random.uniform(-1, 1, size=input_shape).astype(self.dtype)

        # Pad for each feature dimension.
        expanded_scale_diag = scale_diag
        for _ in range(feature_ndims - 1):
            expanded_scale_diag = expanded_scale_diag[..., None, :]

        self.assertAllClose(
            _numpy_exp_quad(amplitude,
                            length_scale,
                            expanded_scale_diag * x,
                            expanded_scale_diag * y,
                            feature_ndims=feature_ndims),
            self.evaluate(vector_transformed_kernel.apply(x, y)))

        z = np.random.uniform(-1, 1,
                              size=[10] + input_shape).astype(self.dtype)

        self.assertAllClose(
            _numpy_exp_quad_matrix(
                # We need to take in to account the event dimension.
                amplitude[..., None, None],
                length_scale[..., None, None],
                # Extra dimension for the event dimension.
                expanded_scale_diag[..., None, :] * z,
                feature_ndims=feature_ndims),
            self.evaluate(vector_transformed_kernel.matrix(z, z)))
Beispiel #10
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.float32)
    index_points = np.stack(np.meshgrid(index_points, index_points), axis=-1)
    index_points = np.reshape(index_points, [-1, 2])
    # ==> shape = [25, 2]

    # Kernel with batch_shape [2, 4, 1]
    df = np.array(
        [[3., 4., 5., 4.], [7.5, 8, 5., 5.]],
        dtype=np.float32).reshape([2, 4, 1])
    amplitude = np.array([1., 2.], np.float32).reshape([2, 1, 1])
    length_scale = np.array([1., 2., 3., 4.], np.float32).reshape([1, 4, 1])
    batched_index_points = np.stack([index_points]*6)
    # ==> shape = [6, 25, 2]
    if not self.is_static:
      df = tf1.placeholder_with_default(df, shape=None)
      amplitude = tf1.placeholder_with_default(amplitude, shape=None)
      length_scale = tf1.placeholder_with_default(length_scale, shape=None)
      batched_index_points = tf1.placeholder_with_default(
          batched_index_points, shape=None)
    kernel = psd_kernels.ExponentiatedQuadratic(amplitude, length_scale)
    tp = tfd.StudentTProcess(
        df, kernel, batched_index_points, jitter=1e-5, validate_args=True)

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

    samples = tp.sample(sample_shape)

    if self.is_static or tf.executing_eagerly():
      self.assertAllEqual(tp.batch_shape_tensor(), batch_shape)
      self.assertAllEqual(tp.event_shape_tensor(), event_shape)
      self.assertAllEqual(samples.shape,
                          sample_shape + batch_shape + event_shape)
      self.assertAllEqual(tp.batch_shape, batch_shape)
      self.assertAllEqual(tp.event_shape, event_shape)
      self.assertAllEqual(samples.shape,
                          sample_shape + batch_shape + event_shape)
      self.assertAllEqual(tp.mean().shape, batch_shape + event_shape)
      self.assertAllEqual(tp.variance().shape, batch_shape + event_shape)
    else:
      self.assertAllEqual(self.evaluate(tp.batch_shape_tensor()), batch_shape)
      self.assertAllEqual(self.evaluate(tp.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(tp.batch_shape))
      self.assertEqual(tensorshape_util.rank(tp.event_shape), 1)
      self.assertIsNone(
          tf.compat.dimension_value(tensorshape_util.dims(tp.event_shape)[0]))
      self.assertAllEqual(
          self.evaluate(tf.shape(tp.mean())), batch_shape + event_shape)
      self.assertAllEqual(self.evaluate(
          tf.shape(tp.variance())), batch_shape + event_shape)
Beispiel #11
0
    def testInitParameterVariations(self, noise_kwargs, implied_values):
        num_test_points = 3
        num_obs_points = 4
        kernel = psd_kernels.ExponentiatedQuadratic()
        index_points = np.random.uniform(-1., 1., (num_test_points, 1))
        observation_index_points = np.random.uniform(-1., 1.,
                                                     (num_obs_points, 1))
        observations = np.random.uniform(-1., 1., num_obs_points)
        jitter = 1e-6

        gprm = tfd.GaussianProcessRegressionModel(
            kernel=kernel,
            index_points=index_points,
            observation_index_points=observation_index_points,
            observations=observations,
            jitter=jitter,
            validate_args=True,
            **noise_kwargs)

        # 'Property' means what was passed to CTOR. 'Parameter' is the effective
        # value by which the distribution is parameterized.
        implied_onv_param = implied_values[
            'observation_noise_variance_parameter']
        implied_pnv_param = implied_values[
            'predictive_noise_variance_parameter']

        # k_xx - k_xn @ (k_nn + ONV) @ k_nx + PNV
        k = lambda x, y: _np_kernel_matrix_fn(1., 1., x, y)
        k_tt_ = k(index_points, index_points)
        k_tx_ = k(index_points, observation_index_points)
        k_xx_plus_noise_ = (
            k(observation_index_points, observation_index_points) +
            (jitter + implied_onv_param) * np.eye(num_obs_points))

        expected_predictive_covariance = (
            k_tt_ - np.dot(k_tx_, np.linalg.solve(k_xx_plus_noise_, k_tx_.T)) +
            implied_pnv_param * np.eye(num_test_points))

        # Assertion 1: predictive covariance is correct.
        self.assertAllClose(self.evaluate(gprm.covariance()),
                            expected_predictive_covariance)

        # Assertion 2: predictive_noise_variance property is correct
        self.assertIsInstance(gprm.predictive_noise_variance, tf.Tensor)
        self.assertAllClose(self.evaluate(gprm.predictive_noise_variance),
                            implied_pnv_param)

        # Assertion 3: observation_noise_variance property is correct.
        self.assertIsInstance(gprm.observation_noise_variance, tf.Tensor)
        self.assertAllClose(
            self.evaluate(gprm.observation_noise_variance),
            # Note that this is, somewhat unintuitively, expceted to equal the
            # predictive_noise_variance. This is because of 1) the inheritance
            # structure of GPRM as a subclass of GaussianProcess and 2) the poor
            # choice of name of the GaussianProcess noise parameter. The latter
            # issue is being cleaned up in cl/256413439.
            implied_pnv_param)
    def testMarginalHasCorrectTypes(self):
        gp = tfd.GaussianProcess(kernel=psd_kernels.ExponentiatedQuadratic())

        self.assertIsInstance(
            gp.get_marginal_distribution(
                index_points=np.ones([1, 1], dtype=np.float32)), tfd.Normal)

        self.assertIsInstance(
            gp.get_marginal_distribution(
                index_points=np.ones([10, 1], dtype=np.float32)),
            tfd.MultivariateNormalLinearOperator)
 def testMean(self):
     mean_fn = lambda x: x[:, 0]**2
     kernel = psd_kernels.ExponentiatedQuadratic()
     index_points = np.expand_dims(np.random.uniform(-1., 1., 10), -1)
     tp = tfd.StudentTProcess(df=3.,
                              kernel=kernel,
                              index_points=index_points,
                              mean_fn=mean_fn,
                              validate_args=True)
     expected_mean = mean_fn(index_points)
     self.assertAllClose(expected_mean, self.evaluate(tp.mean()))
Beispiel #14
0
    def testMarginalHasCorrectTypes(self):
        tp = tfd.StudentTProcess(df=3.,
                                 kernel=psd_kernels.ExponentiatedQuadratic())

        self.assertIsInstance(
            tp.get_marginal_distribution(
                index_points=np.ones([1, 1], dtype=np.float32)), tfd.StudentT)

        self.assertIsInstance(
            tp.get_marginal_distribution(
                index_points=np.ones([10, 1], dtype=np.float32)),
            tfd.MultivariateStudentTLinearOperator)
 def testBatchShape(self):
   # Batch shape [10, 2]
   amplitude = np.random.uniform(
       low=1., high=10., size=[10, 2]).astype(self.dtype)
   inner_length_scale = self.dtype(1.)
   # Use 3 feature_ndims.
   kernel = tfpk.ExponentiatedQuadratic(
       amplitude, inner_length_scale, feature_ndims=3)
   scale_diag = tf.ones([20, 1, 2, 1, 1, 1])
   ard_kernel = tfpk.FeatureScaled(kernel, scale_diag=scale_diag)
   self.assertAllEqual([20, 10, 2], ard_kernel.batch_shape)
   self.assertAllEqual(
       [20, 10, 2], self.evaluate(ard_kernel.batch_shape_tensor()))
    def testShapes(self):
        # 5x5 grid of index points in R^2 and flatten to 25x2
        index_points = np.linspace(-4., 4., 5, dtype=np.float32)
        index_points = np.stack(np.meshgrid(index_points, index_points),
                                axis=-1)
        index_points = np.reshape(index_points, [-1, 2])
        # ==> shape = [25, 2]

        # Kernel with batch_shape [2, 4, 1]
        amplitude = np.array([1., 2.], np.float32).reshape([2, 1, 1])
        length_scale = np.array([1., 2., 3., 4.],
                                np.float32).reshape([1, 4, 1])
        batched_index_points = np.stack([index_points] * 6)
        # ==> shape = [6, 25, 2]
        if not self.is_static:
            amplitude = tf.placeholder_with_default(amplitude, shape=None)
            length_scale = tf.placeholder_with_default(length_scale,
                                                       shape=None)
            batched_index_points = tf.placeholder_with_default(
                batched_index_points, shape=None)
        kernel = psd_kernels.ExponentiatedQuadratic(amplitude, length_scale)
        gp = tfd.GaussianProcess(kernel, batched_index_points, jitter=1e-5)

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

        samples = gp.sample(sample_shape)

        with self.test_session():
            if self.is_static or tf.executing_eagerly():
                self.assertAllEqual(gp.batch_shape_tensor(), batch_shape)
                self.assertAllEqual(gp.event_shape_tensor(), event_shape)
                self.assertAllEqual(samples.shape,
                                    sample_shape + batch_shape + event_shape)
                self.assertAllEqual(gp.batch_shape, batch_shape)
                self.assertAllEqual(gp.event_shape, event_shape)
                self.assertAllEqual(samples.shape,
                                    sample_shape + batch_shape + event_shape)
            else:
                self.assertAllEqual(gp.batch_shape_tensor().eval(),
                                    batch_shape)
                self.assertAllEqual(gp.event_shape_tensor().eval(),
                                    event_shape)
                self.assertAllEqual(samples.eval().shape,
                                    sample_shape + batch_shape + event_shape)
                self.assertIsNone(samples.shape.ndims)
                self.assertIsNone(gp.batch_shape.ndims)
                self.assertEqual(gp.event_shape.ndims, 1)
                self.assertIsNone(gp.event_shape.dims[0].value)
    def testCopy(self):
        # 5 random index points in R^2
        index_points_1 = np.random.uniform(-4., 4., (5, 2)).astype(np.float32)
        # 10 random index points in R^2
        index_points_2 = np.random.uniform(-4., 4., (10, 2)).astype(np.float32)

        # ==> shape = [6, 25, 2]
        if not self.is_static:
            index_points_1 = tf.placeholder_with_default(index_points_1,
                                                         shape=None)
            index_points_2 = tf.placeholder_with_default(index_points_2,
                                                         shape=None)

        mean_fn = lambda x: np.array([0.], np.float32)
        kernel_1 = psd_kernels.ExponentiatedQuadratic()
        kernel_2 = psd_kernels.ExpSinSquared()

        gp1 = tfd.GaussianProcess(kernel_1,
                                  index_points_1,
                                  mean_fn,
                                  jitter=1e-5)
        gp2 = gp1.copy(index_points=index_points_2, kernel=kernel_2)

        event_shape_1 = [5]
        event_shape_2 = [10]

        with self.test_session():
            self.assertEqual(gp1.mean_fn, gp2.mean_fn)
            self.assertIsInstance(gp1.kernel,
                                  psd_kernels.ExponentiatedQuadratic)
            self.assertIsInstance(gp2.kernel, psd_kernels.ExpSinSquared)

            if self.is_static or tf.executing_eagerly():
                self.assertAllEqual(gp1.batch_shape, gp2.batch_shape)
                self.assertAllEqual(gp1.event_shape, event_shape_1)
                self.assertAllEqual(gp2.event_shape, event_shape_2)
                self.assertAllEqual(gp1.index_points, index_points_1)
                self.assertAllEqual(gp2.index_points, index_points_2)
                self.assertAllEqual(tensor_util.constant_value(gp1.jitter),
                                    tensor_util.constant_value(gp2.jitter))
            else:
                self.assertAllEqual(gp1.batch_shape_tensor().eval(),
                                    gp2.batch_shape_tensor().eval())
                self.assertAllEqual(gp1.event_shape_tensor().eval(),
                                    event_shape_1)
                self.assertAllEqual(gp2.event_shape_tensor().eval(),
                                    event_shape_2)
                self.assertEqual(gp1.jitter.eval(), gp2.jitter.eval())
                self.assertAllEqual(gp1.index_points.eval(), index_points_1)
                self.assertAllEqual(gp2.index_points.eval(), index_points_2)
    def testKernelParametersBroadcast(self, feature_ndims, dims):
        # Batch shape [10, 2]
        amplitude = np.random.uniform(low=1., high=10.,
                                      size=[10, 2]).astype(self.dtype)
        inner_length_scale = self.dtype(1.)
        kernel = tfpk.ExponentiatedQuadratic(amplitude, inner_length_scale,
                                             feature_ndims)
        input_shape = [dims] * feature_ndims

        # Batch shape [3, 1, 2].
        concentration1 = np.random.uniform(
            2, 5, size=([3, 1, 2] + input_shape)).astype(self.dtype)

        concentration0 = np.random.uniform(
            2, 5, size=([3, 1, 2] + input_shape)).astype(self.dtype)

        kum_kernel = tfpk.KumaraswamyTransformed(kernel, concentration1,
                                                 concentration0)

        x = np.random.uniform(size=input_shape).astype(self.dtype)
        y = np.random.uniform(size=input_shape).astype(self.dtype)

        self.assertAllClose(_numpy_exp_quad(
            amplitude,
            inner_length_scale,
            _kumaraswamy_warp(x, concentration1, concentration0),
            _kumaraswamy_warp(y, concentration1, concentration0),
            feature_ndims=feature_ndims),
                            self.evaluate(kum_kernel.apply(x, y)),
                            rtol=1e-4,
                            atol=1e-4)

        z = np.random.uniform(size=[10] + input_shape).astype(self.dtype)

        expanded_c1 = np.expand_dims(concentration1, -(feature_ndims + 1))
        expanded_c0 = np.expand_dims(concentration0, -(feature_ndims + 1))

        self.assertAllClose(_numpy_exp_quad_matrix(
            amplitude[..., None, None],
            inner_length_scale,
            _kumaraswamy_warp(z, expanded_c1, expanded_c0),
            feature_ndims=feature_ndims),
                            self.evaluate(kum_kernel.matrix(z, z)),
                            atol=1e-4,
                            rtol=1e-4)
Beispiel #19
0
    def testErrorCases(self):
        kernel = psd_kernels.ExponentiatedQuadratic()
        index_points = np.random.uniform(-1., 1., (10, 1)).astype(np.float64)
        observation_index_points = (np.random.uniform(-1., 1., (5, 1)).astype(
            np.float64))
        observations = np.random.uniform(-1., 1., 3).astype(np.float64)

        # Both or neither of `observation_index_points` and `observations` must be
        # specified.
        with self.assertRaises(ValueError):
            tfd.GaussianProcessRegressionModel(kernel,
                                               index_points,
                                               observation_index_points=None,
                                               observations=observations)
        with self.assertRaises(ValueError):
            tfd.GaussianProcessRegressionModel(kernel,
                                               index_points,
                                               observation_index_points,
                                               observations=None)

        # If specified, mean_fn must be a callable.
        with self.assertRaises(ValueError):
            tfd.GaussianProcessRegressionModel(kernel,
                                               index_points,
                                               mean_fn=0.)

        # Observation index point and observation counts must be broadcastable.
        if self.is_static or tf.executing_eagerly:
            with self.assertRaises(ValueError):
                tfd.GaussianProcessRegressionModel(
                    kernel,
                    index_points,
                    observation_index_points=np.ones([2, 2, 2]),
                    observations=np.ones([5, 5]))
        else:
            gprm = tfd.GaussianProcessRegressionModel(
                kernel,
                index_points,
                observation_index_points=tf.placeholder_with_default(
                    np.ones([2, 2, 2]), shape=None),
                observations=tf.placeholder_with_default(np.ones([5, 5]),
                                                         shape=None))
            with self.assertRaises(ValueError):
                self.evaluate(gprm.event_shape_tensor())
Beispiel #20
0
 def testValuesAreCorrectIdentity(self, feature_ndims, dims):
     amplitude = self.dtype(5.)
     length_scale = self.dtype(0.2)
     kernel = tfpk.ExponentiatedQuadratic(amplitude, length_scale,
                                          feature_ndims)
     input_shape = [dims] * feature_ndims
     identity_transformed_kernel = tfpk.FeatureTransformed(
         kernel,
         transformation_fn=lambda x, feature_ndims, param_expansion_ndims: x
     )
     x = np.random.uniform(-1, 1, size=input_shape).astype(self.dtype)
     y = np.random.uniform(-1, 1, size=input_shape).astype(self.dtype)
     self.assertAllClose(
         _numpy_exp_quad(amplitude,
                         length_scale,
                         x,
                         y,
                         feature_ndims=feature_ndims),
         self.evaluate(identity_transformed_kernel.apply(x, y)))
Beispiel #21
0
    def testEmptyDataMatchesGPPrior(self):
        amp = np.float64(.5)
        len_scale = np.float64(.2)
        jitter = np.float64(1e-4)
        index_points = np.random.uniform(-1., 1., (10, 1)).astype(np.float64)

        # k_xx - k_xn @ (k_nn + sigma^2) @ k_nx + sigma^2
        mean_fn = lambda x: x[:, 0]**2

        kernel = psd_kernels.ExponentiatedQuadratic(amp, len_scale)
        gp = tfd.GaussianProcess(kernel,
                                 index_points,
                                 mean_fn=mean_fn,
                                 jitter=jitter,
                                 validate_args=True)

        gprm_nones = tfd.GaussianProcessRegressionModel(kernel,
                                                        index_points,
                                                        mean_fn=mean_fn,
                                                        jitter=jitter,
                                                        validate_args=True)

        gprm_zero_shapes = tfd.GaussianProcessRegressionModel(
            kernel,
            index_points,
            observation_index_points=tf.ones([5, 0, 1], tf.float64),
            observations=tf.ones([5, 0], tf.float64),
            mean_fn=mean_fn,
            jitter=jitter,
            validate_args=True)

        for gprm in [gprm_nones, gprm_zero_shapes]:
            self.assertAllClose(self.evaluate(gp.mean()),
                                self.evaluate(gprm.mean()))
            self.assertAllClose(self.evaluate(gp.covariance()),
                                self.evaluate(gprm.covariance()))
            self.assertAllClose(self.evaluate(gp.variance()),
                                self.evaluate(gprm.variance()))

            observations = np.random.uniform(-1., 1., 10).astype(np.float64)
            self.assertAllClose(self.evaluate(gp.log_prob(observations)),
                                self.evaluate(gprm.log_prob(observations)))
Beispiel #22
0
    def testLateBindingIndexPoints(self):
        amp = np.float64(.5)
        len_scale = np.float64(.2)
        kernel = psd_kernels.ExponentiatedQuadratic(amp, len_scale)
        mean_fn = lambda x: x[:, 0]**2
        jitter = np.float64(1e-4)
        observation_noise_variance = np.float64(3e-3)

        gp = tfd.GaussianProcess(
            kernel=kernel,
            mean_fn=mean_fn,
            observation_noise_variance=observation_noise_variance,
            jitter=jitter,
            validate_args=True)

        index_points = np.random.uniform(-1., 1., [10, 1])

        expected_mean = mean_fn(index_points)
        self.assertAllClose(expected_mean,
                            self.evaluate(gp.mean(index_points=index_points)))

        def _kernel_fn(x, y):
            return amp**2 * np.exp(-.5 * (np.squeeze(
                (x - y)**2)) / (len_scale**2))

        expected_covariance = (_kernel_fn(np.expand_dims(index_points, -3),
                                          np.expand_dims(index_points, -2)) +
                               observation_noise_variance * np.eye(10))

        self.assertAllClose(
            expected_covariance,
            self.evaluate(gp.covariance(index_points=index_points)))
        self.assertAllClose(
            np.diag(expected_covariance),
            self.evaluate(gp.variance(index_points=index_points)))
        self.assertAllClose(
            np.sqrt(np.diag(expected_covariance)),
            self.evaluate(gp.stddev(index_points=index_points)))

        # Calling mean with no index_points should raise an Error
        with self.assertRaises(ValueError):
            gp.mean()
  def testKernelParametersBroadcast(self, feature_ndims, dims):
    # Batch shape [10, 2]
    amplitude = np.random.uniform(
        low=1., high=10., size=[10, 2]).astype(self.dtype)
    inner_length_scale = self.dtype(1.)
    kernel = tfpk.ExponentiatedQuadratic(
        amplitude, inner_length_scale, feature_ndims)
    input_shape = [dims] * feature_ndims

    # Batch shape [3, 1, 2].
    length_scale = np.random.uniform(
        2, 5, size=([3, 1, 2] + input_shape)).astype(self.dtype)

    ard_kernel = tfpk.FeatureScaled(kernel, scale_diag=length_scale)

    x = np.random.uniform(-1, 1, size=input_shape).astype(self.dtype)
    y = np.random.uniform(-1, 1, size=input_shape).astype(self.dtype)

    self.assertAllClose(
        _numpy_exp_quad(
            amplitude,
            inner_length_scale,
            x / length_scale,
            y / length_scale,
            feature_ndims=feature_ndims
        ),
        self.evaluate(ard_kernel.apply(x, y)))

    z = np.random.uniform(-1, 1, size=[10] + input_shape).astype(self.dtype)

    expanded_length_scale = np.expand_dims(length_scale, -(feature_ndims + 1))

    self.assertAllClose(
        _numpy_exp_quad_matrix(
            amplitude[..., None, None],
            inner_length_scale,
            z / expanded_length_scale,
            feature_ndims=feature_ndims
        ),
        self.evaluate(ard_kernel.matrix(z, z)))
Beispiel #24
0
    def testErrorCases(self):
        kernel = psd_kernels.ExponentiatedQuadratic()
        index_points = np.random.uniform(-1., 1., (10, 1)).astype(np.float64)
        observation_index_points = (np.random.uniform(-1., 1., (5, 1)).astype(
            np.float64))
        observations = np.random.uniform(-1., 1., 3).astype(np.float64)

        # Both or neither of `observation_index_points` and `observations` must be
        # specified.
        with self.assertRaises(ValueError):
            tfd.GaussianProcessRegressionModel(kernel,
                                               index_points,
                                               observation_index_points=None,
                                               observations=observations,
                                               validate_args=True)
        with self.assertRaises(ValueError):
            tfd.GaussianProcessRegressionModel(kernel,
                                               index_points,
                                               observation_index_points,
                                               observations=None,
                                               validate_args=True)

        # If specified, mean_fn must be a callable.
        with self.assertRaises(ValueError):
            tfd.GaussianProcessRegressionModel(kernel,
                                               index_points,
                                               mean_fn=0.,
                                               validate_args=True)

        # Observation index point and observation counts must be broadcastable.
        # Errors based on conditions of dynamic shape in graph mode cannot be
        # caught, so we only check this error case in static shape or eager mode.
        if self.is_static or tf.executing_eagerly():
            with self.assertRaises(ValueError):
                tfd.GaussianProcessRegressionModel(
                    kernel,
                    index_points,
                    observation_index_points=np.ones([2, 2, 2]),
                    observations=np.ones([5, 5]),
                    validate_args=True)
Beispiel #25
0
    def testLateBindingIndexPoints(self):
        amp = np.float64(.5)
        len_scale = np.float64(.2)
        kernel = psd_kernels.ExponentiatedQuadratic(amp, len_scale)
        mean_fn = lambda x: x[:, 0]**2
        jitter = np.float64(1e-4)

        tp = tfd.StudentTProcess(df=np.float64(3.),
                                 kernel=kernel,
                                 mean_fn=mean_fn,
                                 jitter=jitter)

        index_points = np.random.uniform(-1., 1., [10, 1]).astype(np.float64)

        expected_mean = mean_fn(index_points)
        self.assertAllClose(expected_mean,
                            self.evaluate(tp.mean(index_points=index_points)))

        def _kernel_fn(x, y):
            return amp**2 * np.exp(-.5 * (np.squeeze(
                (x - y)**2)) / (len_scale**2))

        expected_covariance = _kernel_fn(np.expand_dims(index_points, -3),
                                         np.expand_dims(index_points, -2))

        self.assertAllClose(
            expected_covariance,
            self.evaluate(tp.covariance(index_points=index_points)))
        self.assertAllClose(
            np.diag(expected_covariance),
            self.evaluate(tp.variance(index_points=index_points)))
        self.assertAllClose(
            np.sqrt(np.diag(expected_covariance)),
            self.evaluate(tp.stddev(index_points=index_points)))

        # Calling mean with no index_points should raise an Error
        with self.assertRaises(ValueError):
            tp.mean()
  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]

    # 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)
    observation_index_points = (
        np.random.uniform(-1., 1., (3, 7, 2)).astype(np.float64))
    observations = np.random.uniform(-1., 1., (3, 7)).astype(np.float64)

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

      observation_index_points = tf.placeholder_with_default(
          observation_index_points, shape=None)
      observations = tf.placeholder_with_default(observations, shape=None)

    kernel = psd_kernels.ExponentiatedQuadratic(amplitude, length_scale)

    gprm = tfd.GaussianProcessRegressionModel(
        kernel,
        batched_index_points,
        observation_index_points,
        observations,
        observation_noise_variance,
        jitter=jitter)

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

    samples = gprm.sample(sample_shape)

    if self.is_static or tf.executing_eagerly():
      self.assertAllEqual(gprm.batch_shape_tensor(), batch_shape)
      self.assertAllEqual(gprm.event_shape_tensor(), event_shape)
      self.assertAllEqual(samples.shape,
                          sample_shape + batch_shape + event_shape)
      self.assertAllEqual(gprm.batch_shape, batch_shape)
      self.assertAllEqual(gprm.event_shape, event_shape)
      self.assertAllEqual(samples.shape,
                          sample_shape + batch_shape + event_shape)
    else:
      self.assertAllEqual(self.evaluate(gprm.batch_shape_tensor()), batch_shape)
      self.assertAllEqual(self.evaluate(gprm.event_shape_tensor()), event_shape)
      self.assertAllEqual(self.evaluate(samples).shape,
                          sample_shape + batch_shape + event_shape)
      self.assertIsNone(samples.shape.ndims)
      self.assertIsNone(gprm.batch_shape.ndims)
      self.assertEqual(gprm.event_shape.ndims, 1)
      self.assertIsNone(gprm.event_shape.dims[0].value)
  def testMeanVarianceAndCovariance(self):
    amp = np.float64(.5)
    len_scale = np.float64(.2)
    observation_noise_variance = np.float64(1e-3)
    jitter = np.float64(1e-4)
    num_test = 10
    num_obs = 3
    index_points = np.random.uniform(-1., 1., (num_test, 1)).astype(np.float64)
    observation_index_points = (
        np.random.uniform(-1., 1., (num_obs, 1)).astype(np.float64))
    observations = np.random.uniform(-1., 1., 3).astype(np.float64)

    # k_xx - k_xn @ (k_nn + sigma^2) @ k_nx + sigma^2
    k = lambda x, y: _np_kernel_matrix_fn(amp, len_scale, x, y)
    k_xx_ = k(index_points, index_points)
    k_xn_ = k(index_points, observation_index_points)
    k_nn_plus_noise_ = (
        k(observation_index_points, observation_index_points) +
        (jitter + observation_noise_variance) * np.eye(num_obs))

    expected_predictive_covariance_no_noise = (
        k_xx_ - np.dot(k_xn_, np.linalg.solve(k_nn_plus_noise_, k_xn_.T)) +
        np.eye(num_test) * jitter)

    expected_predictive_covariance_with_noise = (
        expected_predictive_covariance_no_noise +
        np.eye(num_test) * observation_noise_variance)

    mean_fn = lambda x: x[:, 0]**2
    prior_mean = mean_fn(observation_index_points)
    expected_mean = np.dot(
        k_xn_, np.linalg.solve(k_nn_plus_noise_, observations - prior_mean))

    kernel = psd_kernels.ExponentiatedQuadratic(amp, len_scale)
    gprm = tfd.GaussianProcessRegressionModel(
        kernel=kernel,
        index_points=index_points,
        observation_index_points=observation_index_points,
        observations=observations,
        observation_noise_variance=observation_noise_variance,
        mean_fn=mean_fn,
        jitter=jitter)

    self.assertAllClose(expected_predictive_covariance_with_noise,
                        self.evaluate(gprm.covariance()))
    self.assertAllClose(np.diag(expected_predictive_covariance_with_noise),
                        self.evaluate(gprm.variance()))
    self.assertAllClose(expected_mean,
                        self.evaluate(gprm.mean()))

    gprm_no_predictive_noise = tfd.GaussianProcessRegressionModel(
        kernel=kernel,
        index_points=index_points,
        observation_index_points=observation_index_points,
        observations=observations,
        observation_noise_variance=observation_noise_variance,
        predictive_noise_variance=0.,
        mean_fn=mean_fn,
        jitter=jitter)

    self.assertAllClose(expected_predictive_covariance_no_noise,
                        self.evaluate(gprm_no_predictive_noise.covariance()))
    self.assertAllClose(np.diag(expected_predictive_covariance_no_noise),
                        self.evaluate(gprm_no_predictive_noise.variance()))
    self.assertAllClose(expected_mean,
                        self.evaluate(gprm_no_predictive_noise.mean()))
Beispiel #28
0
  def testCopy(self):
    # 5 random index points in R^2
    index_points_1 = np.random.uniform(-4., 4., (5, 2)).astype(np.float32)
    # 10 random index points in R^2
    index_points_2 = np.random.uniform(-4., 4., (10, 2)).astype(np.float32)

    observation_index_points_1 = (
        np.random.uniform(-4., 4., (7, 2)).astype(np.float32))
    observation_index_points_2 = (
        np.random.uniform(-4., 4., (9, 2)).astype(np.float32))

    observations_1 = np.random.uniform(-1., 1., 7).astype(np.float32)
    observations_2 = np.random.uniform(-1., 1., 9).astype(np.float32)

    # ==> shape = [6, 25, 2]
    if not self.is_static:
      index_points_1 = tf.placeholder_with_default(index_points_1, shape=None)
      index_points_2 = tf.placeholder_with_default(index_points_2, shape=None)
      observation_index_points_1 = tf.placeholder_with_default(
          observation_index_points_1, shape=None)
      observation_index_points_2 = tf.placeholder_with_default(
          observation_index_points_2, shape=None)
      observations_1 = tf.placeholder_with_default(observations_1, shape=None)
      observations_2 = tf.placeholder_with_default(observations_2, shape=None)

    mean_fn = lambda x: np.array([0.], np.float32)
    kernel_1 = psd_kernels.ExponentiatedQuadratic()
    kernel_2 = psd_kernels.ExpSinSquared()

    gprm1 = tfd.GaussianProcessRegressionModel(
        kernel=kernel_1,
        index_points=index_points_1,
        observation_index_points=observation_index_points_1,
        observations=observations_1,
        mean_fn=mean_fn,
        jitter=1e-5)
    gprm2 = gprm1.copy(
        kernel=kernel_2,
        index_points=index_points_2,
        observation_index_points=observation_index_points_2,
        observations=observations_2)

    event_shape_1 = [5]
    event_shape_2 = [10]

    self.assertEqual(gprm1.mean_fn, gprm2.mean_fn)
    self.assertIsInstance(gprm1.kernel, psd_kernels.ExponentiatedQuadratic)
    self.assertIsInstance(gprm2.kernel, psd_kernels.ExpSinSquared)

    if self.is_static or tf.executing_eagerly():
      self.assertAllEqual(gprm1.batch_shape, gprm2.batch_shape)
      self.assertAllEqual(gprm1.event_shape, event_shape_1)
      self.assertAllEqual(gprm2.event_shape, event_shape_2)
      self.assertAllEqual(gprm1.index_points, index_points_1)
      self.assertAllEqual(gprm2.index_points, index_points_2)
      self.assertAllEqual(tf.contrib.util.constant_value(gprm1.jitter),
                          tf.contrib.util.constant_value(gprm2.jitter))
    else:
      self.assertAllEqual(self.evaluate(gprm1.batch_shape_tensor()),
                          self.evaluate(gprm2.batch_shape_tensor()))
      self.assertAllEqual(self.evaluate(gprm1.event_shape_tensor()),
                          event_shape_1)
      self.assertAllEqual(self.evaluate(gprm2.event_shape_tensor()),
                          event_shape_2)
      self.assertEqual(self.evaluate(gprm1.jitter), self.evaluate(gprm2.jitter))
      self.assertAllEqual(self.evaluate(gprm1.index_points), index_points_1)
      self.assertAllEqual(self.evaluate(gprm2.index_points), index_points_2)