|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import itertools |
| 4 | +import unittest |
| 5 | + |
| 6 | +import torch |
| 7 | + |
| 8 | +from torch import Tensor |
| 9 | + |
| 10 | +from gpytorch.kernels import AdditiveKernel, ConstantKernel, MaternKernel, ProductKernel, ScaleKernel |
| 11 | +from gpytorch.lazy import LazyEvaluatedKernelTensor |
| 12 | +from gpytorch.priors.torch_priors import GammaPrior |
| 13 | +from gpytorch.test.base_kernel_test_case import BaseKernelTestCase |
| 14 | + |
| 15 | + |
| 16 | +class TestConstantKernel(unittest.TestCase, BaseKernelTestCase): |
| 17 | + def create_kernel_no_ard(self, **kwargs): |
| 18 | + return ConstantKernel(**kwargs) |
| 19 | + |
| 20 | + def test_constant_kernel(self): |
| 21 | + with self.subTest(device="cpu"): |
| 22 | + self._test_constant_kernel(torch.device("cpu")) |
| 23 | + |
| 24 | + if torch.cuda.is_available(): |
| 25 | + with self.subTest(device="cuda"): |
| 26 | + self._test_constant_kernel(torch.device("cuda")) |
| 27 | + |
| 28 | + def _test_constant_kernel(self, device: torch.device): |
| 29 | + n, d = 3, 5 |
| 30 | + dtypes = [torch.float, torch.double] |
| 31 | + batch_shapes = [(), (2,), (7, 2)] |
| 32 | + torch.manual_seed(123) |
| 33 | + for dtype, batch_shape in itertools.product(dtypes, batch_shapes): |
| 34 | + tkwargs = {"dtype": dtype, "device": device} |
| 35 | + places = 6 if dtype == torch.float else 12 |
| 36 | + X = torch.rand(*batch_shape, n, d, **tkwargs) |
| 37 | + |
| 38 | + constant_kernel = ConstantKernel(batch_shape=batch_shape) |
| 39 | + KL = constant_kernel(X) |
| 40 | + self.assertIsInstance(KL, LazyEvaluatedKernelTensor) |
| 41 | + KM = KL.to_dense() |
| 42 | + self.assertIsInstance(KM, Tensor) |
| 43 | + self.assertEqual(KM.shape, (*batch_shape, n, n)) |
| 44 | + self.assertEqual(KM.dtype, dtype) |
| 45 | + self.assertEqual(KM.device.type, device.type) |
| 46 | + # standard deviation is zero iff KM is constant |
| 47 | + self.assertAlmostEqual(KM.std().item(), 0, places=places) |
| 48 | + |
| 49 | + # testing last_dim_is_batch |
| 50 | + with self.subTest(last_dim_is_batch=True): |
| 51 | + KD = constant_kernel(X, last_dim_is_batch=True).to(device=device) |
| 52 | + self.assertIsInstance(KD, LazyEvaluatedKernelTensor) |
| 53 | + KM = KD.to_dense() |
| 54 | + self.assertIsInstance(KM, Tensor) |
| 55 | + self.assertEqual(KM.shape, (*batch_shape, d, n, n)) |
| 56 | + self.assertAlmostEqual(KM.std().item(), 0, places=places) |
| 57 | + self.assertEqual(KM.dtype, dtype) |
| 58 | + self.assertEqual(KM.device.type, device.type) |
| 59 | + |
| 60 | + # testing diag |
| 61 | + with self.subTest(diag=True): |
| 62 | + KD = constant_kernel(X, diag=True) |
| 63 | + self.assertIsInstance(KD, Tensor) |
| 64 | + self.assertEqual(KD.shape, (*batch_shape, n)) |
| 65 | + self.assertAlmostEqual(KD.std().item(), 0, places=places) |
| 66 | + self.assertEqual(KD.dtype, dtype) |
| 67 | + self.assertEqual(KD.device.type, device.type) |
| 68 | + |
| 69 | + # testing diag and last_dim_is_batch |
| 70 | + with self.subTest(diag=True, last_dim_is_batch=True): |
| 71 | + KD = constant_kernel(X, diag=True, last_dim_is_batch=True) |
| 72 | + self.assertIsInstance(KD, Tensor) |
| 73 | + self.assertEqual(KD.shape, (*batch_shape, d, n)) |
| 74 | + self.assertAlmostEqual(KD.std().item(), 0, places=places) |
| 75 | + self.assertEqual(KD.dtype, dtype) |
| 76 | + self.assertEqual(KD.device.type, device.type) |
| 77 | + |
| 78 | + # testing AD |
| 79 | + with self.subTest(requires_grad=True): |
| 80 | + X.requires_grad = True |
| 81 | + constant_kernel(X).to_dense().sum().backward() |
| 82 | + self.assertIsNone(X.grad) # constant kernel is not dependent on X |
| 83 | + |
| 84 | + # testing algebraic combinations with another kernel |
| 85 | + base_kernel = MaternKernel().to(device=device) |
| 86 | + |
| 87 | + with self.subTest(additive=True): |
| 88 | + sum_kernel = base_kernel + constant_kernel |
| 89 | + self.assertIsInstance(sum_kernel, AdditiveKernel) |
| 90 | + self.assertAllClose( |
| 91 | + sum_kernel(X).to_dense(), |
| 92 | + base_kernel(X).to_dense() + constant_kernel.constant.unsqueeze(-1), |
| 93 | + ) |
| 94 | + |
| 95 | + # product with constant is equivalent to scale kernel |
| 96 | + with self.subTest(product=True): |
| 97 | + product_kernel = base_kernel * constant_kernel |
| 98 | + self.assertIsInstance(product_kernel, ProductKernel) |
| 99 | + |
| 100 | + scale_kernel = ScaleKernel(base_kernel, batch_shape=batch_shape) |
| 101 | + scale_kernel.to(device=device) |
| 102 | + self.assertAllClose(scale_kernel(X).to_dense(), product_kernel(X).to_dense()) |
| 103 | + |
| 104 | + # setting constant |
| 105 | + pies = torch.full_like(constant_kernel.constant, torch.pi) |
| 106 | + constant_kernel.constant = pies |
| 107 | + self.assertAllClose(constant_kernel.constant, pies) |
| 108 | + |
| 109 | + # specifying prior |
| 110 | + constant_kernel = ConstantKernel(constant_prior=GammaPrior(concentration=2.4, rate=2.7)) |
| 111 | + |
| 112 | + with self.assertRaisesRegex(TypeError, "Expected gpytorch.priors.Prior but got"): |
| 113 | + ConstantKernel(constant_prior=1) |
0 commit comments