diff --git a/src/shogun/kernel/CombinedKernel.cpp b/src/shogun/kernel/CombinedKernel.cpp index 7b36c3a891b..ca2569aad95 100644 --- a/src/shogun/kernel/CombinedKernel.cpp +++ b/src/shogun/kernel/CombinedKernel.cpp @@ -66,17 +66,13 @@ bool CCombinedKernel::init_with_extracted_subsets( SGVector rhs_subset) { + auto l_combined = dynamic_cast(l); + auto r_combined = dynamic_cast(r); + + if (!l_combined || !r_combined) + SG_ERROR("Cast failed - unsupported features passed") + CKernel::init(l, r); - REQUIRE( - l->get_feature_class() == C_COMBINED, - "%s::init(): LHS features are" - " of class %s but need to be combined features!\n", - get_name(), l->get_name()); - REQUIRE( - r->get_feature_class() == C_COMBINED, - "%s::init(): RHS features are" - " of class %s but need to be combined features!\n", - get_name(), r->get_name()); ASSERT(l->get_feature_type() == F_UNKNOWN) ASSERT(r->get_feature_type() == F_UNKNOWN) @@ -84,9 +80,6 @@ bool CCombinedKernel::init_with_extracted_subsets( CFeatures* rf = NULL; CKernel* k = NULL; - auto l_combined = dynamic_cast(l); - auto r_combined = dynamic_cast(r); - bool result = true; index_t f_idx = 0; @@ -138,6 +131,8 @@ bool CCombinedKernel::init_with_extracted_subsets( "No kernel matrix was assigned to this Custom kernel\n") auto k_custom = dynamic_cast(k); + if (!k_custom) + SG_ERROR("Dynamic cast to custom kernel failed") // clear all previous subsets k_custom->remove_all_row_subsets(); @@ -193,21 +188,18 @@ bool CCombinedKernel::init(CFeatures* l, CFeatures* r) { init_subkernel_weights(); } - /* - * The two subsets, we will be passing those to init_with_extracted_subsets - */ + + if (!l) + SG_ERROR("LHS features are NULL"); + if (!r) + SG_ERROR("RHS features are NULL"); + SGVector lhs_subset; SGVector rhs_subset; - /* - * We will be passing these features to init_with_extracted_subsets - */ CCombinedFeatures* combined_l; CCombinedFeatures* combined_r; - /* - * Extract the subsets so that we can pass them on - */ auto l_subset_stack = l->get_subset_stack(); auto r_subset_stack = r->get_subset_stack(); @@ -258,9 +250,6 @@ bool CCombinedKernel::init(CFeatures* l, CFeatures* r) } else { - /* - * Otherwise, we just pass l & r straight on - */ combined_l = (CCombinedFeatures*)l; combined_r = (CCombinedFeatures*)r; } diff --git a/tests/unit/kernel/CombinedKernel_unittest.cc b/tests/unit/kernel/CombinedKernel_unittest.cc index ac013ad4cb2..2ea57517452 100644 --- a/tests/unit/kernel/CombinedKernel_unittest.cc +++ b/tests/unit/kernel/CombinedKernel_unittest.cc @@ -45,10 +45,12 @@ TEST(CombinedKernelTest,test_array_operations) TEST(CombinedKernelTest, test_subset_mixed) { - CMeanShiftDataGenerator* gen = new CMeanShiftDataGenerator(0, 2); - CFeatures* feats = gen->get_streamed_features(10); + int n_runs = 10; - CCombinedFeatures* cf = new CCombinedFeatures(); + auto gen = new CMeanShiftDataGenerator(0, 2); + CFeatures* feats = gen->get_streamed_features(n_runs); + + CCombinedFeatures* feats_combined = new CCombinedFeatures(); CCombinedKernel* combined = new CCombinedKernel(); @@ -64,50 +66,48 @@ TEST(CombinedKernelTest, test_subset_mixed) combined->append_kernel(custom_1); combined->append_kernel(gaus_1); - cf->append_feature_obj(feats); + feats_combined->append_feature_obj(feats); combined->append_kernel(custom_2); combined->append_kernel(gaus_2); - cf->append_feature_obj(feats); + feats_combined->append_feature_obj(feats); SGVector inds(10); inds.range_fill(); - for (index_t i = 0; i < 10; ++i) + for (index_t i = 0; i < n_runs; ++i) { CMath::permute(inds); - cf->add_subset(inds); - combined->init(cf, cf); + feats_combined->add_subset(inds); + combined->init(feats_combined, feats_combined); - CKernel* k_g = combined->get_kernel(1); - CKernel* k_0 = combined->get_kernel(0); - CKernel* k_3 = combined->get_kernel(2); + CKernel* ground_truth_kernel = combined->get_kernel(1); + CKernel* custom_kernel_1 = combined->get_kernel(0); + CKernel* custom_kernel_2 = combined->get_kernel(2); - SGMatrix gauss_matrix = k_g->get_kernel_matrix(); - SGMatrix custom_matrix_1 = k_0->get_kernel_matrix(); - SGMatrix custom_matrix_2 = k_3->get_kernel_matrix(); + SGMatrix gauss_matrix = + ground_truth_kernel->get_kernel_matrix(); + SGMatrix custom_matrix_1 = + custom_kernel_1->get_kernel_matrix(); + SGMatrix custom_matrix_2 = + custom_kernel_2->get_kernel_matrix(); - for (index_t j = 0; j < 10; ++j) + for (index_t j = 0; j < n_runs; ++j) { - for (index_t k = 0; k < 10; ++k) + for (index_t k = 0; k < n_runs; ++k) { - EXPECT_LE( - CMath::abs(gauss_matrix(k, j) - custom_matrix_1(k, j)), - 1e-6); - EXPECT_LE( - CMath::abs(gauss_matrix(k, j) - custom_matrix_2(k, j)), - 1e-6); + EXPECT_NEAR(gauss_matrix(j, k), custom_matrix_1(j, k), 1e-6); + EXPECT_NEAR(gauss_matrix(j, k), custom_matrix_1(j, k), 1e-6); } } - cf->remove_subset(); - SG_UNREF(k_g); - SG_UNREF(k_0); - SG_UNREF(k_3); + feats_combined->remove_subset(); + SG_UNREF(ground_truth_kernel); + SG_UNREF(custom_kernel_1); + SG_UNREF(custom_kernel_2); } - SG_UNREF(gen); SG_UNREF(gaus_ck); SG_UNREF(combined); } @@ -115,8 +115,10 @@ TEST(CombinedKernelTest, test_subset_mixed) TEST(CombinedKernelTest, test_subset_combined_only) { - CMeanShiftDataGenerator* gen = new CMeanShiftDataGenerator(0, 2); - CFeatures* feats = gen->get_streamed_features(10); + int n_runs = 10; + + auto gen = new CMeanShiftDataGenerator(0, 2); + CFeatures* feats = gen->get_streamed_features(n_runs); CCombinedKernel* combined = new CCombinedKernel(); @@ -125,15 +127,14 @@ TEST(CombinedKernelTest, test_subset_combined_only) CCustomKernel* custom_1 = new CCustomKernel(gaus_ck); CCustomKernel* custom_2 = new CCustomKernel(gaus_ck); - ; combined->append_kernel(custom_1); combined->append_kernel(custom_2); - SGVector inds(10); + SGVector inds(n_runs); inds.range_fill(); - for (index_t i = 0; i < 10; ++i) + for (index_t i = 0; i < n_runs; ++i) { CMath::permute(inds); @@ -141,32 +142,29 @@ TEST(CombinedKernelTest, test_subset_combined_only) combined->init(feats, feats); gaus_ck->init(feats, feats); - CKernel* k_0 = combined->get_kernel(0); - CKernel* k_1 = combined->get_kernel(1); + CKernel* custom_kernel_1 = combined->get_kernel(0); + CKernel* custom_kernel_2 = combined->get_kernel(1); SGMatrix gauss_matrix = gaus_ck->get_kernel_matrix(); - SGMatrix custom_matrix_1 = k_0->get_kernel_matrix(); - SGMatrix custom_matrix_2 = k_1->get_kernel_matrix(); + SGMatrix custom_matrix_1 = + custom_kernel_1->get_kernel_matrix(); + SGMatrix custom_matrix_2 = + custom_kernel_2->get_kernel_matrix(); - for (index_t j = 0; j < 10; ++j) + for (index_t j = 0; j < n_runs; ++j) { - for (index_t k = 0; k < 10; ++k) + for (index_t k = 0; k < n_runs; ++k) { - EXPECT_LE( - CMath::abs(gauss_matrix(k, j) - custom_matrix_1(k, j)), - 1e-6); - EXPECT_LE( - CMath::abs(gauss_matrix(k, j) - custom_matrix_2(k, j)), - 1e-6); + EXPECT_NEAR(gauss_matrix(j, k), custom_matrix_1(j, k), 1e-6); + EXPECT_NEAR(gauss_matrix(j, k), custom_matrix_1(j, k), 1e-6); } } feats->remove_subset(); - SG_UNREF(k_0); - SG_UNREF(k_1); + SG_UNREF(custom_kernel_1); + SG_UNREF(custom_kernel_2); } - SG_UNREF(gen); SG_UNREF(gaus_ck); SG_UNREF(combined); }