Skip to content

test_mdn_trainer

TestMDNTrainer

Bases: unittest.TestCase

This test class provides unit tests for the MDNTrainer class.

Source code in uncertaintyplayground/tests/test_mdn_trainer.py
 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class TestMDNTrainer(unittest.TestCase):
    """
    This test class provides unit tests for the MDNTrainer class.

    """

    def setUp(self):
        """
        Sets up the testing environment for each test method.
        """
        self.modes = [
            {'mean': -3.0, 'std_dev': 0.5, 'weight': 0.3},
            {'mean': 0.0, 'std_dev': 1.0, 'weight': 0.4},
            {'mean': 3.0, 'std_dev': 0.7, 'weight': 0.3}
        ]

        torch.manual_seed(1)
        np.random.seed(42)
        self.num_samples = 100
        self.X = np.random.rand(self.num_samples, 20)
        self.y = generate_multi_modal_data(self.num_samples, self.modes)

    def test_train(self):
        """
        Tests the train method of the MDNTrainer class.
        """
        trainer = MDNTrainer(self.X, self.y, num_epochs=3, lr=0.01, dense1_units=5, n_gaussians=3, dtype = torch.float32)
        trainer.train()

    def test_predict_with_uncertainty(self):
        """
        Tests the predict_with_uncertainty method of the MDNTrainer class.
        """
        trainer = MDNTrainer(self.X, self.y, num_epochs=2, lr=0.01, dense1_units=5, n_gaussians=3)
        trainer.train()

        # Generate a test instance
        test_instance = np.random.rand(1, 20)
        test_instance = test_instance.astype(np.float32)

        pi, mu, sigma, sample = trainer.predict_with_uncertainty(test_instance)
        # Add assertions based on the expected behavior of the predict_with_uncertainty function
        self.assertEqual(pi.shape, (1, 3))
        self.assertEqual(mu.shape, (1, 3))
        self.assertEqual(sigma.shape, (1, 3))
        self.assertIsInstance(sample, np.ndarray)

setUp()

Sets up the testing environment for each test method.

Source code in uncertaintyplayground/tests/test_mdn_trainer.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def setUp(self):
    """
    Sets up the testing environment for each test method.
    """
    self.modes = [
        {'mean': -3.0, 'std_dev': 0.5, 'weight': 0.3},
        {'mean': 0.0, 'std_dev': 1.0, 'weight': 0.4},
        {'mean': 3.0, 'std_dev': 0.7, 'weight': 0.3}
    ]

    torch.manual_seed(1)
    np.random.seed(42)
    self.num_samples = 100
    self.X = np.random.rand(self.num_samples, 20)
    self.y = generate_multi_modal_data(self.num_samples, self.modes)

test_predict_with_uncertainty()

Tests the predict_with_uncertainty method of the MDNTrainer class.

Source code in uncertaintyplayground/tests/test_mdn_trainer.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def test_predict_with_uncertainty(self):
    """
    Tests the predict_with_uncertainty method of the MDNTrainer class.
    """
    trainer = MDNTrainer(self.X, self.y, num_epochs=2, lr=0.01, dense1_units=5, n_gaussians=3)
    trainer.train()

    # Generate a test instance
    test_instance = np.random.rand(1, 20)
    test_instance = test_instance.astype(np.float32)

    pi, mu, sigma, sample = trainer.predict_with_uncertainty(test_instance)
    # Add assertions based on the expected behavior of the predict_with_uncertainty function
    self.assertEqual(pi.shape, (1, 3))
    self.assertEqual(mu.shape, (1, 3))
    self.assertEqual(sigma.shape, (1, 3))
    self.assertIsInstance(sample, np.ndarray)

test_train()

Tests the train method of the MDNTrainer class.

Source code in uncertaintyplayground/tests/test_mdn_trainer.py
29
30
31
32
33
34
def test_train(self):
    """
    Tests the train method of the MDNTrainer class.
    """
    trainer = MDNTrainer(self.X, self.y, num_epochs=3, lr=0.01, dense1_units=5, n_gaussians=3, dtype = torch.float32)
    trainer.train()