From 3b0f4ff135a2ab24b86b568d9424ad9048495a02 Mon Sep 17 00:00:00 2001 From: Davis King Date: Thu, 3 Jan 2013 22:17:10 -0500 Subject: [PATCH] Added more unit tests for the forces_last_weight_to_1 stuff. --- dlib/test/ranking.cpp | 36 +++++++++++++++++++++++++--------- dlib/test/svm_c_linear_dcd.cpp | 16 ++++++++++++++- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/dlib/test/ranking.cpp b/dlib/test/ranking.cpp index 66314885d..07a5b4d59 100644 --- a/dlib/test/ranking.cpp +++ b/dlib/test/ranking.cpp @@ -223,7 +223,7 @@ namespace // ---------------------------------------------------------------------------------------- - template + template class simple_rank_trainer { public: @@ -250,18 +250,35 @@ namespace } } - svm_c_linear_dcd_trainer trainer; - trainer.set_c(1.0/samples.size()); - trainer.set_epsilon(1e-10); - trainer.force_last_weight_to_1(true); - //trainer.be_verbose(); - return trainer.train(samples, labels); + if (use_dcd_trainer) + { + svm_c_linear_dcd_trainer trainer; + trainer.set_c(1.0/samples.size()); + trainer.set_epsilon(1e-10); + trainer.force_last_weight_to_1(true); + //trainer.be_verbose(); + return trainer.train(samples, labels); + } + else + { + svm_c_linear_trainer trainer; + trainer.set_c(1.0); + trainer.set_epsilon(1e-13); + trainer.force_last_weight_to_1(true); + //trainer.be_verbose(); + decision_function df = trainer.train(samples, labels); + DLIB_TEST_MSG(df.b == 0, df.b); + return df; + } } }; + template void test_svmrank_weight_force_dense() { print_spinner(); + dlog << LINFO << "use_dcd_trainer: "<< use_dcd_trainer; + typedef matrix sample_type; typedef linear_kernel kernel_type; @@ -291,7 +308,7 @@ namespace dlog << LINFO << "ranking accuracy: " << acc1; DLIB_TEST(std::abs(acc1 - 1) == 0); - simple_rank_trainer strainer; + simple_rank_trainer strainer; decision_function df2; df2 = strainer.train(pair); dlog << LINFO << "weights: "<< trans(df2.basis_vectors(0)); @@ -325,7 +342,8 @@ namespace test_count_ranking_inversions(); dotest1(); dotest_sparse_vectors(); - test_svmrank_weight_force_dense(); + test_svmrank_weight_force_dense(); + test_svmrank_weight_force_dense(); } } a; diff --git a/dlib/test/svm_c_linear_dcd.cpp b/dlib/test/svm_c_linear_dcd.cpp index f69e9145a..b9bc9974c 100644 --- a/dlib/test/svm_c_linear_dcd.cpp +++ b/dlib/test/svm_c_linear_dcd.cpp @@ -250,6 +250,7 @@ namespace typedef linear_kernel kernel_type; + svm_c_linear_trainer linear_trainer_cpa; svm_c_linear_dcd_trainer linear_trainer; @@ -257,7 +258,9 @@ namespace const double C = 1; linear_trainer.set_epsilon(1e-10); + linear_trainer_cpa.set_epsilon(1e-11); + linear_trainer_cpa.force_last_weight_to_1(force_weight); linear_trainer.force_last_weight_to_1(force_weight); linear_trainer.include_bias(have_bias); @@ -268,7 +271,7 @@ namespace // make an instance of a sample vector so we can use it below sample_type sample; - decision_function df; + decision_function df, df2; running_stats rs; @@ -299,11 +302,22 @@ namespace labels.push_back(label); linear_trainer.set_c(C); + linear_trainer_cpa.set_c(C*samples.size()); df = linear_trainer.train(samples, labels, state); if (force_weight) + { DLIB_TEST(std::abs(df.basis_vectors(0)(9) - 1) < 1e-8); + DLIB_TEST(std::abs(df.b) < 1e-8); + + if (samples.size() > 1) + { + df2 = linear_trainer_cpa.train(samples, labels); + DLIB_TEST_MSG( max(abs(df.basis_vectors(0) - df2.basis_vectors(0))) < 1e-7, max(abs(df.basis_vectors(0) - df2.basis_vectors(0)))); + DLIB_TEST( std::abs(df.b - df2.b) < 1e-7); + } + } if (!have_bias) DLIB_TEST(std::abs(df.b) < 1e-8);