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)
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]
def expand_full_kernel(self, covariance: Covariance) -> List[Covariance]: """Expand full kernel. :param covariance: Covariance to expand. :return: """ result = self.expand_single_kernel(covariance) if covariance is None: pass elif not covariance.is_base(): for i, operand in enumerate(covariance.raw_kernel.parts): covariance_operand = Covariance(operand) for e in self.expand_full_kernel(covariance_operand): new_operands = covariance.raw_kernel.parts[:i] + [e.raw_kernel] \ + covariance.raw_kernel.parts[i + 1:] new_operands = [op.copy() for op in new_operands] if covariance.is_prod(): prod_kern = new_operands[0] for part in new_operands[1:]: prod_kern *= part result.append(Covariance(prod_kern)) elif covariance.is_sum(): prod_kern = new_operands[0] for part in new_operands[1:]: prod_kern += part result.append(Covariance(prod_kern)) else: raise TypeError( f'Unknown combination kernel class {covariance.__class__.__name__}' ) return result
def test_is_base(self): kern = self.se_0 cov = Covariance(kern) self.assertTrue(cov.is_base()) kern = self.se_0 + self.se_0 cov = Covariance(kern) self.assertFalse(cov.is_base())
def test_multiply(self): kern_1 = self.se_0 kern_2 = self.se_0 cov_1 = Covariance(kern_1) cov_2 = Covariance(kern_2) cov = cov_1 * cov_2 self.assertEqual('SE_1 * SE_1', cov.infix)
def test_index_same_candidate_expression(self): candidates = [GPModel(Covariance(RBF(1)))] self.active_set.update(candidates) new_model = GPModel(Covariance(RBF(1))) actual = self.active_set.index(new_model) self.assertIsInstance(actual, int) expected_ind = 0 self.assertEqual(expected_ind, actual)
def test_add(self): kern_1 = self.se_0 kern_2 = self.se_0 cov_1 = Covariance(kern_1) cov_2 = Covariance(kern_2) cov = cov_1 + cov_2 self.assertEqual('SE_1 + SE_1', cov.infix)
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_with_duplicates(self): candidates = [GPModel(Covariance(RBF(1))), GPModel(Covariance(RBF(1)))] expected_candidates_ind = [0] new_candidates_ind = self.active_set.update(candidates) self.assertEqual(expected_candidates_ind, new_candidates_ind) expected_models = [candidates[0], None, None, None, None] self.assertListEqual(expected_models, self.active_set.models) expected_next_ind = 1 self.assertEqual(expected_next_ind, self.active_set.get_index_to_insert())
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]])
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())
def test_get_index_to_insert_one_item(self): # add one model self.active_set.add_model(GPModel(Covariance(RBF(1)))) expected_index = 1 actual_index = self.active_set.get_index_to_insert() self.assertEqual(expected_index, actual_index)
def test_get_same_candidate_with_default(self): candidates = [GPModel(Covariance(RBF(1)))] self.active_set.update(candidates) actual = self.active_set.get(candidates[0], -1) self.assertIsInstance(actual, int) expected_ind = 0 self.assertEqual(expected_ind, actual)
def test_create_precomputed_info(self): num_samples = 20 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, max_num_hyperparameters=40, max_num_kernels=10, active_models=MagicMock(), initial_model_indices=MagicMock(), data_X=x) cov = Covariance(RBF(1)) p1 = LogGaussian(20, 1) p2 = LogGaussian(0, 1.1) cov.raw_kernel.variance.set_prior(p1, warning=False) cov.raw_kernel.lengthscale.set_prior(p2, warning=False) result = builder.create_precomputed_info(cov, x) self.assertIsInstance(result, tuple) self.assertEqual(len(result), 2) self.assertIsInstance(result[0], np.ndarray) self.assertIsInstance(result[1], np.ndarray) self.assertEqual(result[0].shape, (num_samples, )) self.assertEqual(result[1].shape, (x.shape[0], x.shape[0], num_samples))
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)
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)
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)
def test_to_dict(self): kern = self.se_0 cov = Covariance(kern) actual = cov.to_dict() self.assertIsInstance(actual, dict) self.assertIn('kernel', actual) self.assertEqual(kern.to_dict(), actual['kernel'])
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)
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]))
def test_from_dict(self): test_cases = (Covariance, Serializable) for cls in test_cases: with self.subTest(name=cls.__name__): kern = self.se_0 cov = Covariance(kern) actual = cls.from_dict(cov.to_dict()) self.assertIsInstance(actual, Covariance) self.assertEqual(cov.infix, actual.infix)
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)
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])
def build(self, n_dims: int): """Set the base kernels.""" if self.base_kernel_names is None: self.base_kernel_names = self._get_default_base_kernel_names( n_dims) self.n_dims = n_dims raw_kernels = get_all_1d_kernels( self.base_kernel_names, self.n_dims, hyperpriors=self.hyperpriors.prior_map) self.base_kernels = [Covariance(kernel) for kernel in raw_kernels] self.built = True
def test_add_model_empty(self): candidate = GPModel(Covariance(RBF(1))) expected_ind, expected_status = 0, True actual_ind, actual_status = self.active_set.add_model(candidate) self.assertEqual(expected_ind, actual_ind) self.assertEqual(expected_status, actual_status) expected_models = [candidate, None, None, None, None] self.assertListEqual(expected_models, self.active_set.models) expected_next_ind = 1 self.assertEqual(expected_next_ind, self.active_set.get_index_to_insert())
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)
def test_create_one_d(self): kern = self.se_0 cov = Covariance(kern) self.assertEqual(kern, cov.raw_kernel) self.assertListEqual([kern], cov.infix_tokens) self.assertIsInstance(cov.infix, str) self.assertIsInstance(cov.infix_full, str) self.assertEqual('SE_1', cov.infix) self.assertEqual('SE_1', cov.postfix) self.assertListEqual([kern], cov.postfix_tokens) self.assertIsInstance(cov.symbolic_expr, KernelSymbol) self.assertEqual(kern, cov.symbolic_expr.kernel_one_d) self.assertIsInstance(cov.symbolic_expr_expanded, KernelSymbol) self.assertEqual(kern, cov.symbolic_expr_expanded.kernel_one_d)