Example #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)))
Example #2
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)))
Example #3
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)))
Example #4
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)))