def test_RBF_times_ConstantKernel(self): n = 3 m = 4 n_dim = 12 X = np.random.randn(n, n_dim) Y = np.random.randn(m, n_dim) factor = 10.0 prod_kernel = Product(RBFKernel(), ConstantKernel(constant=factor)) ref_kernel = RBFKernel() K_prod, dK_prod = prod_kernel( X, Y, dx=True, dy=True, eval_gradient=True) K_ref, dK_ref1 = ref_kernel(X, Y, dx=True, dy=True, eval_gradient=True) # Derivative with respect to the second hyperparameter: dK_ref2 = np.zeros((n*(1+n_dim), m*(1+n_dim), 1)) dK_ref2[:, :, 0] = K_ref K_ref *= factor dK_ref1 *= factor np.testing.assert_allclose(K_prod, K_ref) np.testing.assert_allclose(dK_prod[:, :, :-1], dK_ref1) np.testing.assert_allclose(dK_prod[:, :, -1:], dK_ref2)
def test_comparison2product(self): X = np.random.randn(3, 4) Y = np.random.randn(3, 4) kernel1 = Exponentiation(RBFKernel(length_scale=1.23), exponent=2) kernel2 = Product(RBFKernel(length_scale=1.23), RBFKernel(length_scale=1.23)) K1 = kernel1(X, Y, dx=True, dy=True) K2 = kernel2(X, Y, dx=True, dy=True) np.testing.assert_allclose(K1, K2)
def test_versus_anisotropic_rbf(self): X = np.random.randn(8, 2) Y = np.random.randn(8, 2) kernel = Product(Masking(RBFKernel(length_scale=1.5), slice(0, 1)), Masking(RBFKernel(length_scale=0.5), slice(1, 2))) ref_kernel = RBFKernel(length_scale=np.array([1.5, 0.5])) K, dK = kernel(X, Y, dx=True, dy=True, eval_gradient=True) K_ref, dK_ref = ref_kernel(X, Y, dx=True, dy=True, eval_gradient=True) np.testing.assert_allclose(K, K_ref) np.testing.assert_allclose(dK, dK_ref)
def test_2times2is4(self): n = 3 m = 4 n_dim = 12 X = np.random.randn(n, n_dim) Y = np.random.randn(m, n_dim) kernel1 = Product(DotProductKernel(), DotProductKernel()) kernel2 = DotProductKernel(exponent=4) K1 = kernel1(X, Y, dx=True, dy=True) K2 = kernel2(X, Y, dx=True, dy=True) np.testing.assert_allclose(K1, K2)
def test_comparison2product(self): n = 3 m = 4 n_dim = 12 X = np.random.randn(n, n_dim) Y = np.random.randn(m, n_dim) factor = 10.0 kernel1 = Rescaling(RBFKernel(), factor) kernel2 = Product(ConstantKernel(constant=factor), RBFKernel()) K1, dK1 = kernel1(X, Y, dx=True, dy=True, eval_gradient=True) K2, dK2 = kernel2(X, Y, dx=True, dy=True, eval_gradient=True) np.testing.assert_allclose(K1, K2) np.testing.assert_allclose(dK1, dK2)
class DottimeDotTest(KernelTest.KernelTest): kernel = Product(DotProductKernel(exponent=1, sigma0=0.1), DotProductKernel(sigma0=0.1)) def test_2times2is4(self): n = 3 m = 4 n_dim = 12 X = np.random.randn(n, n_dim) Y = np.random.randn(m, n_dim) kernel1 = Product(DotProductKernel(), DotProductKernel()) kernel2 = DotProductKernel(exponent=4) K1 = kernel1(X, Y, dx=True, dy=True) K2 = kernel2(X, Y, dx=True, dy=True) np.testing.assert_allclose(K1, K2)
class DottimesConstantTest(KernelTest.KernelTest): kernel = Product(DotProductKernel(exponent=2), ConstantKernel(constant=10.0))
class RBFtimesRBFTest(KernelTest.KernelTest): kernel = Product(RBFKernel(length_scale=0.8), RBFKernel(length_scale=1.2))