def testValidateArgs(self): with self.assertRaises(tf.errors.InvalidArgumentError): k = psd_kernels.RationalQuadratic(amplitude=-1., length_scale=1., scale_mixture_rate=1., validate_args=True) self.evaluate(k.amplitude) with self.assertRaises(tf.errors.InvalidArgumentError): k = psd_kernels.RationalQuadratic(amplitude=1., length_scale=-1., scale_mixture_rate=1., validate_args=True) self.evaluate(k.length_scale) with self.assertRaises(tf.errors.InvalidArgumentError): k = psd_kernels.RationalQuadratic(amplitude=1., length_scale=1., scale_mixture_rate=-1., validate_args=True) self.evaluate(k.scale_mixture_rate) # But `None`'s are ok k = psd_kernels.RationalQuadratic(amplitude=None, length_scale=None, scale_mixture_rate=None, validate_args=True) self.evaluate(k.apply([1.], [1.]))
def testShapesAreCorrect(self): k = psd_kernels.RationalQuadratic(amplitude=1., length_scale=1.) x = np.ones([4, 3], np.float32) y = np.ones([5, 3], np.float32) self.assertAllEqual([4, 5], k.matrix(x, y).shape) self.assertAllEqual([2, 4, 5], k.matrix(tf.stack([x] * 2), tf.stack([y] * 2)).shape) k = psd_kernels.RationalQuadratic( amplitude=np.ones([2, 1, 1], np.float32), length_scale=np.ones([1, 3, 1], np.float32), scale_mixture_rate=np.ones([2, 1, 1, 1], np.float32)) self.assertAllEqual( [2, 2, 3, 2, 4, 5], #`-----' | `--' # | | `- matrix shape # | `- from input batch shapes # `- from broadcasting kernel params k.matrix( tf.stack([x] * 2), # shape [2, 4, 3] tf.stack([y] * 2) # shape [2, 5, 3] ).shape)
def testBatchShape(self): amplitude = np.random.uniform(2, 3., size=[3, 1, 2]).astype(np.float32) length_scale = np.random.uniform(2, 3., size=[1, 3, 1]).astype(np.float32) scale_mixture_rate = np.random.uniform( 2, 3., size=[3, 1, 1]).astype(np.float32) k = tfpk.RationalQuadratic( amplitude, length_scale, scale_mixture_rate) self.assertAllEqual(tf.TensorShape([3, 3, 2]), k.batch_shape) self.assertAllEqual([3, 3, 2], self.evaluate(k.batch_shape_tensor()))
def testShapesAreCorrect(self): k = psd_kernels.RationalQuadratic(amplitude=1., length_scale=1.) x = np.ones([4, 3], np.float32) y = np.ones([5, 3], np.float32) self.assertAllEqual(k.matrix(x, y).shape, [4, 5]) self.assertAllEqual( k.matrix(tf.stack([x] * 2), tf.stack([y] * 2)).shape, [2, 4, 5]) k = psd_kernels.RationalQuadratic( amplitude=np.ones([2, 1, 1], np.float32), length_scale=np.ones([1, 3, 1], np.float32), scale_mixture_rate=np.ones([2, 1, 1, 1], np.float32)) self.assertAllEqual( k.matrix( tf.stack([x] * 2), # shape [2, 4, 3] tf.stack([y] * 2) # shape [2, 5, 3] ).shape, [2, 2, 3, 2, 4, 5])
def testValuesAreCorrect(self, feature_ndims, dtype, dims): amplitude = np.array(5., dtype=dtype) length_scale = np.array(.2, dtype=dtype) scale_mixture_rate = np.array(3., dtype=dtype) np.random.seed(42) k = psd_kernels.RationalQuadratic(amplitude, length_scale, scale_mixture_rate, feature_ndims) shape = [dims] * feature_ndims for _ in range(5): x = np.random.uniform(-1, 1, size=shape).astype(dtype) y = np.random.uniform(-1, 1, size=shape).astype(dtype) self.assertAllClose( self._rational_quadratic(amplitude, length_scale, scale_mixture_rate, x, y), self.evaluate(k.apply(x, y)))
def testNoneScaleMixture(self): amplitude = 5. length_scale = .2 np.random.seed(42) k = tfpk.RationalQuadratic( amplitude=amplitude, length_scale=length_scale, scale_mixture_rate=None) x = np.random.uniform(-1, 1, size=[5]).astype(np.float32) y = np.random.uniform(-1, 1, size=[5]).astype(np.float32) self.assertAllClose( # Ensure that a None value for scale_mixture_rate has the same semantics # as scale_mixture_rate=1. self._rational_quadratic( amplitude=amplitude, length_scale=length_scale, scale_mixture_rate=1., x=x, y=y), self.evaluate(k.apply(x, y)))
def testMismatchedFloatTypesAreBad(self): with self.assertRaises(TypeError): psd_kernels.RationalQuadratic(np.float32(1.), np.float64(1.))