Skip to content

test_svgp_model

TestSVGP

Bases: unittest.TestCase

Unit tests for SVGP class.

Source code in uncertaintyplayground/tests/test_svgp_model.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class TestSVGP(unittest.TestCase):
    """
    Unit tests for SVGP class.
    """

    def setUp(self):
        """
        Test fixture setup method.
        """
        self.inducing_points = torch.rand((10, 2))
        self.dtype = torch.float32
        self.device = None
        self.svgp = SVGP(self.inducing_points, dtype = self.dtype,  device = self.device)

    def test_init(self):
        """
        Test case for SVGP initialization.
        """
        self.assertIsInstance(self.svgp, SVGP)
        self.assertEqual(self.svgp.mean_module.constant.item(), 0.)

        # Expect default initial lengthscale to be ~0.693
        expected_lengthscale = torch.log(torch.tensor(2.0)).item()
        self.assertAlmostEqual(self.svgp.covar_module.base_kernel.lengthscale.item(), expected_lengthscale, places=5)

    def test_forward(self):
        """
        Test case for forward method in SVGP.
        """
        x = torch.rand((5, 2))
        output = self.svgp.forward(x)
        self.assertIsInstance(output, gpytorch.distributions.MultivariateNormal)
        self.assertEqual(output.loc.shape, torch.Size([x.shape[0]]))

setUp()

Test fixture setup method.

Source code in uncertaintyplayground/tests/test_svgp_model.py
11
12
13
14
15
16
17
18
def setUp(self):
    """
    Test fixture setup method.
    """
    self.inducing_points = torch.rand((10, 2))
    self.dtype = torch.float32
    self.device = None
    self.svgp = SVGP(self.inducing_points, dtype = self.dtype,  device = self.device)

test_forward()

Test case for forward method in SVGP.

Source code in uncertaintyplayground/tests/test_svgp_model.py
31
32
33
34
35
36
37
38
def test_forward(self):
    """
    Test case for forward method in SVGP.
    """
    x = torch.rand((5, 2))
    output = self.svgp.forward(x)
    self.assertIsInstance(output, gpytorch.distributions.MultivariateNormal)
    self.assertEqual(output.loc.shape, torch.Size([x.shape[0]]))

test_init()

Test case for SVGP initialization.

Source code in uncertaintyplayground/tests/test_svgp_model.py
20
21
22
23
24
25
26
27
28
29
def test_init(self):
    """
    Test case for SVGP initialization.
    """
    self.assertIsInstance(self.svgp, SVGP)
    self.assertEqual(self.svgp.mean_module.constant.item(), 0.)

    # Expect default initial lengthscale to be ~0.693
    expected_lengthscale = torch.log(torch.tensor(2.0)).item()
    self.assertAlmostEqual(self.svgp.covar_module.base_kernel.lengthscale.item(), expected_lengthscale, places=5)