Beispiel #1
0
 def setUp(self):
     self.se0 = Covariance(RBF(1, active_dims=[0]))
     self.se1 = Covariance(RBF(1, active_dims=[1]))
     self.se2 = Covariance(RBF(1, active_dims=[2]))
     self.rq0 = Covariance(RationalQuadratic(1, active_dims=[0]))
     self.rq1 = Covariance(RationalQuadratic(1, active_dims=[1]))
     self.rq2 = Covariance(RationalQuadratic(1, active_dims=[2]))
     self.lin0 = Covariance(LinScaleShift(1, active_dims=[0]))
Beispiel #2
0
    def test_encode_gp_models(self):
        gp_models = [GPModel(Covariance(RBF(1))), GPModel(Covariance(RationalQuadratic(1)))]
        result = encode_gp_models(gp_models)
        self.assertIsInstance(result, np.ndarray)
        self.assertEqual(result.shape, (len(gp_models), 1))
        self.assertListEqual(result[0][0], [encode_kernel(gp_models[0].covariance.raw_kernel), [None]])
        self.assertListEqual(result[1][0], [encode_kernel(gp_models[1].covariance.raw_kernel), [None]])

        gp_models = [GPModel(Covariance(RBF(1) * RBF(1))), GPModel(Covariance(RationalQuadratic(1)))]
        result = encode_gp_models(gp_models)
        self.assertIsInstance(result, np.ndarray)
        self.assertEqual(result.shape, (len(gp_models), 1))
        self.assertListEqual(result[0][0], [encode_kernel(gp_models[0].covariance.raw_kernel), [None]])
        self.assertListEqual(result[1][0], [encode_kernel(gp_models[1].covariance.raw_kernel), [None]])
Beispiel #3
0
    def setUp(self) -> None:
        self.x = np.array([[1, 2], [4, 5], [6, 7], [8, 9], [10, 11]])

        self.noise_prior = Gaussian(mu=np.log(0.01), sigma=1)
        cov_1 = Covariance(RBF(1))
        p1 = LogGaussian(20, 1)
        p2 = LogGaussian(0, 1.1)
        cov_1.raw_kernel.variance.set_prior(p1, warning=False)
        cov_1.raw_kernel.lengthscale.set_prior(p2, warning=False)

        cov_2 = Covariance(RBF(1))
        p3 = LogGaussian(11, 1)
        p4 = LogGaussian(1, 1.21)
        cov_2.raw_kernel.variance.set_prior(p3, warning=False)
        cov_2.raw_kernel.lengthscale.set_prior(p4, warning=False)

        cov_3 = Covariance(RationalQuadratic(1))
        p5 = LogGaussian(4, 1)
        p6 = LogGaussian(1.2, 1.21)
        p7 = LogGaussian(13, 1.21)
        cov_3.raw_kernel.variance.set_prior(p5, warning=False)
        cov_3.raw_kernel.lengthscale.set_prior(p6, warning=False)
        cov_3.raw_kernel.power.set_prior(p7, warning=False)
        models = [GPModel(cov_1), GPModel(cov_2), GPModel(cov_3)]
        self.active_models = ActiveSet(max_n_models=3)
        self.active_models.models = models
        self.ind_init = [0, 2]
Beispiel #4
0
 def test_tokens_to_kernel_symbols(self):
     k1 = RBF(1)
     k2 = RationalQuadratic(1)
     kernel_tokens = [k1, '+', k2]
     actual = tokens_to_kernel_symbols(kernel_tokens)
     expected = [KernelSymbol('SE_1', k1), '+', KernelSymbol('RQ_1', k2)]
     self.assertListEqual(expected, actual)
Beispiel #5
0
    def test_hellinger_distance(self):
        cov_i = Covariance(RBF(1))
        p1 = LogGaussian(20, 1)
        p2 = LogGaussian(0, 1.1)
        cov_i.raw_kernel.variance.set_prior(p1, warning=False)
        cov_i.raw_kernel.lengthscale.set_prior(p2, warning=False)

        cov_j = Covariance(RationalQuadratic(1))
        p3 = LogGaussian(11, 2)
        p4 = LogGaussian(2, 1.12)
        cov_j.raw_kernel.variance.set_prior(p3, warning=False)
        cov_j.raw_kernel.lengthscale.set_prior(p4, warning=False)
        cov_j.raw_kernel.power.set_prior(p2, warning=False)

        x = np.array([[1, 2], [4, 5], [6, 7], [8, 9], [10, 11]])
        noise_prior = Gaussian(mu=np.log(0.01), sigma=1)
        builder = HellingerDistanceBuilder(noise_prior,
                                           num_samples=20,
                                           max_num_hyperparameters=40,
                                           max_num_kernels=10,
                                           active_models=MagicMock(),
                                           initial_model_indices=MagicMock(),
                                           data_X=x)
        log_det_i, mini_gram_matrices_i = builder.create_precomputed_info(
            cov_i, x)
        log_det_j, mini_gram_matrices_j = builder.create_precomputed_info(
            cov_j, x)
        result = HellingerDistanceBuilder.hellinger_distance(
            log_det_i, mini_gram_matrices_i, log_det_j, mini_gram_matrices_j)
        self.assertIsInstance(result, float)
Beispiel #6
0
    def test_propose_new_models(self, mock_get_candidates):
        expected = [
            Covariance(RBF(1)),
            Covariance(RationalQuadratic(1)),
            Covariance(RBF(1) + RationalQuadratic(1)),
            Covariance(RBF(1) * RationalQuadratic(1))
        ]
        mock_get_candidates.return_value = expected

        pop = ActiveModelPopulation()
        pop.update(self.gp_models)
        actual = self.model_selector._propose_new_models(pop,
                                                         callbacks=MagicMock())
        self.assertIsInstance(actual, list)
        self.assertEqual(len(expected), len(actual))
        for expected_cov, actual_cov in zip(expected, actual):
            self.assertEqual(expected_cov.infix, actual_cov.covariance.infix)
Beispiel #7
0
    def test_save(self):
        kernel = Covariance(RBF(1) * RBF(1) + RationalQuadratic(1))
        gp_model = GPModel(kernel)

        file_name = "test_save"
        out_fname = gp_model.save(file_name)

        self.addCleanup(os.remove, out_fname)
Beispiel #8
0
 def test_shd_metric(self):
     gp_models = [
         GPModel(Covariance(RBF(1) + RationalQuadratic(1))),
         GPModel(Covariance(RBF(1)))
     ]
     data = encode_gp_models(gp_models)
     u, v = data[0], data[1]
     result = shd_metric(u, v)
     self.assertEqual(result, 1)
    def test_get_index_to_insert_full_no_priority(self):
        # add five models
        self.active_set.add_model(GPModel(Covariance(RBF(1))))
        self.active_set.add_model(GPModel(Covariance(RationalQuadratic(1))))
        self.active_set.add_model(GPModel(Covariance(StandardPeriodic(1))))
        self.active_set.add_model(GPModel(Covariance(LinScaleShift(1))))
        self.active_set.add_model(GPModel(Covariance(RBF(1) + RBF(1))))

        self.assertRaises(ValueError, self.active_set.get_index_to_insert)
 def test_get_new_candidate_with_default(self):
     candidates = [GPModel(Covariance(RBF(1)))]
     self.active_set.update(candidates)
     new_model = GPModel(Covariance(RationalQuadratic(1)))
     default = 2
     actual = self.active_set.get(new_model, default)
     self.assertIsInstance(actual, int)
     expected_ind = default
     self.assertEqual(expected_ind, actual)
 def test_update_exceed_max_no_remove(self):
     candidates = [
         GPModel(Covariance(RBF(1))),
         GPModel(Covariance(RationalQuadratic(1))),
         GPModel(Covariance(LinScaleShift(1))),
         GPModel(Covariance(StandardPeriodic(1))),
         GPModel(Covariance(RBF(1) + RBF(1))),
         GPModel(Covariance(RBF(1) * RBF(1)))
     ]
     self.assertRaises(ValueError, self.active_set.update, candidates)
Beispiel #12
0
    def test_from_dict(self):
        test_cases = (GPModel, Serializable)
        for cls in test_cases:
            with self.subTest(name=cls.__name__):
                kernel = Covariance(RBF(1) * RBF(1) + RationalQuadratic(1))
                gp_model = GPModel(kernel)
                actual = cls.from_dict(gp_model.to_dict())

                self.assertIsInstance(actual, GPModel)
                self.assertEqual(gp_model.likelihood, actual.likelihood)
                self.assertEqual(gp_model.covariance.infix, actual.covariance.infix)
Beispiel #13
0
    def test_load(self):
        kernel = Covariance(RBF(1) * RBF(1) + RationalQuadratic(1))
        gp_model = GPModel(kernel)

        file_name = "test_save"
        out_file_name = gp_model.save(file_name)

        self.addCleanup(os.remove, out_file_name)

        new_gp_model = GPModel.load(out_file_name)

        self.assertIsInstance(new_gp_model, GPModel)
        self.assertEqual(gp_model.covariance.infix, new_gp_model.covariance.infix)
Beispiel #14
0
 def test_euclidean_metric(self):
     x_train = np.array([[1, 2], [3, 4]])
     gp_models = [
         GPModel(Covariance(RBF(1) + RationalQuadratic(1))),
         GPModel(Covariance(RBF(1)))
     ]
     data = encode_gp_models(gp_models)
     u, v = data[0], data[1]
     result = euclidean_metric(u, v, get_x_train=lambda: x_train)
     self.assertIsInstance(result, float)
     self.assertAlmostEqual(
         result,
         np.linalg.norm(
             gp_models[0].covariance.raw_kernel.K(x_train, x_train) -
             gp_models[1].covariance.raw_kernel.K(x_train, x_train)))
    def test_update_empty(self):
        candidates = [
            GPModel(Covariance(RBF(1))),
            GPModel(Covariance(RationalQuadratic(1)))
        ]
        expected_candidates_ind = [0, 1]
        new_candidates_ind = self.active_set.update(candidates)
        self.assertEqual(expected_candidates_ind, new_candidates_ind)

        expected_models = [candidates[0], candidates[1], None, None, None]
        self.assertListEqual(expected_models, self.active_set.models)

        expected_next_ind = 2
        self.assertEqual(expected_next_ind,
                         self.active_set.get_index_to_insert())
Beispiel #16
0
    def setUp(self):
        self.gp_models = [
            GPModel(Covariance(RationalQuadratic(1))),
            GPModel(Covariance(RBF(1) + RBF(1))),
            GPModel(Covariance(RBF(1)))
        ]

        grammar = GeometricRandomGrammar()
        grammar.build(n_dims=1)

        fitness_fn = 'nbic'
        self.x_train = np.array([[1, 2, 3], [4, 5, 6]])
        self.y_train = np.array([[5], [10]])
        self.x_test = np.array([[10, 20, 30], [40, 50, 60]])
        self.y_test = np.array([[2], [1]])
        self.model_selector = ModelSelector(grammar, fitness_fn)
Beispiel #17
0
    def setUp(self):
        self.gp_models = [
            GPModel(Covariance(RationalQuadratic(1))),
            GPModel(Covariance(RBF(1) + RBF(1))),
            GPModel(Covariance(RBF(1)))
        ]

        grammar = MagicMock()
        kernel_selector = MagicMock()
        objective = MagicMock()
        self.x_train = np.array([[1, 2, 3], [4, 5, 6]])
        self.y_train = np.array([[5], [10]])
        self.x_test = np.array([[10, 20, 30], [40, 50, 60]])
        self.y_test = np.array([[2], [1]])
        self.model_selector = BomsModelSelector(grammar, kernel_selector,
                                                objective)
Beispiel #18
0
    def test_get_priors(self):
        # Test assertRaises ValueError if not all parameters have priors set
        c = RBF(1)
        self.assertRaises(ValueError, get_priors, c)

        c = RBF(1) * RationalQuadratic(1)
        p1 = LogGaussian(2, 2.21)
        p2 = LogGaussian(1, 2.1)
        p3 = LogGaussian(1, 2)
        c.parameters[0].variance.set_prior(p1, warning=False)
        c.parameters[1].lengthscale.set_prior(p2, warning=False)
        c.parameters[0].lengthscale.set_prior(p3, warning=False)
        c.parameters[1].power.set_prior(p2, warning=False)
        c.parameters[1].variance.set_prior(p1, warning=False)
        result = get_priors(c)
        self.assertIsInstance(result, np.ndarray)
        self.assertEqual(result.shape, (5, ))
        np.testing.assert_array_equal(result, [p1, p3, p1, p2, p2])
Beispiel #19
0
    def test_additive_part_to_vec(self):
        base_kernels = ['SE', 'RQ']
        n_dims = 2
        k = RBF(1) + RBF(1)
        self.assertRaises(TypeError, additive_part_to_vec, k, base_kernels,
                          n_dims)

        k = RBF(1)
        result = additive_part_to_vec(k, base_kernels=base_kernels, n_dims=2)
        np.testing.assert_array_equal(result, np.array([1, 0, 0, 0]))

        k = RBF(1) * RBF(1) * RBF(1, active_dims=[1]) * RationalQuadratic(
            1, active_dims=[1])
        result = additive_part_to_vec(k, base_kernels=base_kernels, n_dims=2)
        np.testing.assert_array_equal(result, np.array([2, 1, 0, 1]))

        k = RBF(1) * (RBF(1) + RBF(1))
        self.assertRaises(TypeError, additive_part_to_vec, k, base_kernels,
                          n_dims)
    def test_get_index_to_insert_full_with_priority(self):
        # add five models
        self.active_set.add_model(GPModel(Covariance(RBF(1))))
        self.active_set.add_model(GPModel(Covariance(RationalQuadratic(1))))
        self.active_set.add_model(GPModel(Covariance(StandardPeriodic(1))))
        self.active_set.add_model(GPModel(Covariance(LinScaleShift(1))))
        self.active_set.add_model(GPModel(Covariance(RBF(1) + RBF(1))))

        remove_priority = [2]
        self.active_set.remove_priority = remove_priority
        actual = self.active_set.get_index_to_insert()
        expected = 2
        self.assertEqual(expected, actual)
        self.assertEqual(self.active_set.remove_priority, [])

        remove_priority = [0, 2, 3]
        self.active_set.remove_priority = remove_priority
        actual = self.active_set.get_index_to_insert()
        expected = 0
        self.assertEqual(expected, actual)
        self.assertEqual(self.active_set.remove_priority, [2, 3])
Beispiel #21
0
    def test_to_dict(self):
        test_cases = (
            (None, 'None score'),
            (10., 'Positive score')
        )
        for score, description in test_cases:
            with self.subTest(description=description):
                kernel = Covariance(RBF(1) * RBF(1) + RationalQuadratic(1))
                gp_model = GPModel(kernel)
                gp_model.score = score

                actual = gp_model.to_dict()

                self.assertIsInstance(actual, dict)

                self.assertIn('likelihood', actual)
                self.assertIn('covariance', actual)
                self.assertIn('score', actual)

                self.assertEqual(None, actual['likelihood'])
                self.assertEqual(gp_model.covariance.to_dict(), actual['covariance'])
                self.assertEqual(gp_model.score, actual['score'])
    def test_hd_kern_nodes(self):
        node_1 = KernelNode(RBF(1, active_dims=[0]).to_dict())
        node_2 = KernelNode(RBF(1, active_dims=[0]).to_dict())
        result = hd_kern_nodes(node_1, node_2)
        self.assertEqual(result, 0)

        node_1 = KernelNode(RBF(1, active_dims=[0]).to_dict())
        node_2 = KernelNode(RationalQuadratic(1, active_dims=[0]).to_dict())
        result = hd_kern_nodes(node_1, node_2)
        self.assertEqual(result, 1)

        node_1 = KernelNode(RBF(1, active_dims=[0]).to_dict())
        node_2 = KernelNode(RBF(1, active_dims=[1]).to_dict())
        result = hd_kern_nodes(node_1, node_2)
        self.assertEqual(result, 1)

        node_1 = KernelNode(RBF(1, active_dims=[0]).to_dict())
        node_1.add_left('U')
        node_1.add_right('V')
        node_2 = KernelNode(RBF(1, active_dims=[0]).to_dict())
        result = hd_kern_nodes(node_1, node_2)
        self.assertEqual(result, 1)
    def test_update_exceed_max_remove_set(self):
        candidates = [
            GPModel(Covariance(RBF(1))),
            GPModel(Covariance(RationalQuadratic(1))),
            GPModel(Covariance(LinScaleShift(1))),
            GPModel(Covariance(StandardPeriodic(1))),
            GPModel(Covariance(RBF(1) + RBF(1))),
            GPModel(Covariance(RBF(1) * RBF(1)))
        ]
        self.active_set.remove_priority = [0, 1, 2, 3, 4]
        actual_new_candidates_ind = self.active_set.update(candidates)
        expected_new_candidates_ind = [1, 2, 3, 4, 0]
        self.assertListEqual(expected_new_candidates_ind,
                             actual_new_candidates_ind)

        expected_models = [
            candidates[5], candidates[1], candidates[2], candidates[3],
            candidates[4]
        ]
        self.assertListEqual(expected_models, self.active_set.models)

        expected_next_ind = 1
        self.assertEqual(expected_next_ind,
                         self.active_set.get_index_to_insert())
Beispiel #24
0
    def test_remove_duplicate_gp_models(self):
        k1 = GPModel(Covariance(RBF(1)))
        k1.score = 10

        k2 = GPModel(Covariance(RBF(1)))
        k2.score = 9

        k3 = GPModel(Covariance(RBF(1)))

        k4 = GPModel(Covariance(RBF(1)))

        k5 = GPModel(Covariance(RBF(1)))

        k6 = GPModel(Covariance(RBF(1, lengthscale=0.5)))

        k7 = GPModel(Covariance(RationalQuadratic(1)))

        # Always keep k1 then k2 then k3 etc.
        result = remove_duplicate_gp_models([k1, k2, k3, k4, k5, k6, k7])
        self.assertListEqual(result, [k1, k7])

        result = remove_duplicate_gp_models([k1, k2, k3, k4, k5, k7])
        self.assertListEqual(result, [k1, k7])

        result = remove_duplicate_gp_models([k1, k2, k3, k4, k7])
        self.assertListEqual(result, [k1, k7])

        result = remove_duplicate_gp_models([k1, k2, k3, k7])
        self.assertListEqual(result, [k1, k7])

        result = remove_duplicate_gp_models([k1, k2, k7])
        self.assertListEqual(result, [k1, k7])

        result = remove_duplicate_gp_models([k1, k7])
        self.assertListEqual(result, [k1, k7])

        result = remove_duplicate_gp_models([k2, k3, k4, k5, k6, k7])
        self.assertListEqual(result, [k2, k7])

        result = remove_duplicate_gp_models([k2, k3, k4, k5, k7])
        self.assertListEqual(result, [k2, k7])

        result = remove_duplicate_gp_models([k2, k3, k4, k7])
        self.assertListEqual(result, [k2, k7])

        result = remove_duplicate_gp_models([k2, k3, k7])
        self.assertListEqual(result, [k2, k7])

        result = remove_duplicate_gp_models([k2, k7])
        self.assertListEqual(result, [k2, k7])

        result = remove_duplicate_gp_models([k3, k4, k5, k6, k7])
        self.assertTrue(result == [k3, k7] or result == [k4, k7])

        result = remove_duplicate_gp_models([k3, k4, k5, k7])
        self.assertTrue(result == [k3, k7] or result == [k4, k7])

        result = remove_duplicate_gp_models([k4, k3, k5, k6, k7])
        self.assertTrue(result == [k3, k7] or result == [k4, k7])

        result = remove_duplicate_gp_models([k4, k3, k5, k7])
        self.assertTrue(result == [k3, k7] or result == [k4, k7])

        result = remove_duplicate_gp_models([k3, k4, k5, k7])
        self.assertTrue(result == [k3, k7] or result == [k4, k7])

        result = remove_duplicate_gp_models([k3, k4, k7])
        self.assertTrue(result == [k3, k7] or result == [k4, k7])

        result = remove_duplicate_gp_models([k4, k3, k7])
        self.assertTrue(result == [k3, k7] or result == [k4, k7])

        result = remove_duplicate_gp_models([k3, k7])
        self.assertListEqual(result, [k3, k7])

        result = remove_duplicate_gp_models([k4, k7])
        self.assertListEqual(result, [k4, k7])

        result = remove_duplicate_gp_models([k5, k6, k7])
        self.assertTrue(result == [k5, k7] or result == [k6, k7])

        result = remove_duplicate_gp_models([k6, k5, k7])
        self.assertTrue(result == [k5, k7] or result == [k6, k7])
 def test_index_new_model(self):
     candidates = [GPModel(Covariance(RBF(1)))]
     self.active_set.update(candidates)
     new_model = GPModel(Covariance(RationalQuadratic(1)))
     self.assertRaises(KeyError, self.active_set.index, new_model)
Beispiel #26
0
 def setUp(self):
     self.se0 = Covariance(RBF(1, active_dims=[0]))
     self.se1 = Covariance(RBF(1, active_dims=[1]))
     self.rq0 = Covariance(RationalQuadratic(1, active_dims=[0]))
     self.rq1 = Covariance(RationalQuadratic(1, active_dims=[1]))
Beispiel #27
0
 def test_to_binary_tree(self):
     kernel = Covariance(RBF(1) * RBF(1) + RationalQuadratic(1))
     gp_model = GPModel(kernel)
     result = gp_model.covariance.to_binary_tree()
     self.assertIsInstance(result, KernelTree)
     self.assertCountEqual(result.postfix_tokens(), ['SE_1', 'SE_1', '*', 'RQ_1', '+'])
Beispiel #28
0
 def setUp(self):
     self.se0 = RBF(1, active_dims=[0])
     self.se1 = RBF(1, active_dims=[1])
     self.rq0 = RationalQuadratic(1, active_dims=[0])
     self.rq1 = RationalQuadratic(1, active_dims=[1])