From d7f207f2f7e7e29f8807a09ec0b2809eeb24c1f5 Mon Sep 17 00:00:00 2001 From: Davis King Date: Fri, 23 May 2014 18:35:49 -0400 Subject: [PATCH] Added unit tests for the svm_multiclass_linear with sparse priors --- dlib/test/svm_multiclass_linear.cpp | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/dlib/test/svm_multiclass_linear.cpp b/dlib/test/svm_multiclass_linear.cpp index a5cb699c5..e01d48892 100644 --- a/dlib/test/svm_multiclass_linear.cpp +++ b/dlib/test/svm_multiclass_linear.cpp @@ -6,6 +6,7 @@ #include #include "create_iris_datafile.h" #include +#include #include namespace @@ -92,6 +93,62 @@ namespace DLIB_TEST((unsigned int)sum(diag(res))==samples.size()); } + void test_prior_sparse () + { + print_spinner(); + typedef std::map sample_type; + typedef sparse_linear_kernel kernel_type; + + std::vector samples; + std::vector labels; + + for (int i = 0; i < 4; ++i) + { + if (i==2) + ++i; + for (int iter = 0; iter < 5; ++iter) + { + sample_type samp; + samp[i] = 1; + samples.push_back(samp); + labels.push_back(i); + } + } + + + svm_multiclass_linear_trainer trainer; + + multiclass_linear_decision_function df = trainer.train(samples, labels); + + //cout << "test: \n" << test_multiclass_decision_function(df, samples, labels) << endl; + //cout << df.weights << endl; + //cout << df.b << endl; + + std::vector samples2; + std::vector labels2; + int i = 2; + for (int iter = 0; iter < 5; ++iter) + { + sample_type samp; + samp[i] = 1; + samp[i+10] = 1; + samples2.push_back(samp); + labels2.push_back(i); + samples.push_back(samp); + labels.push_back(i); + } + + trainer.set_prior(df); + trainer.set_c(0.1); + df = trainer.train(samples2, labels2); + + matrix res = test_multiclass_decision_function(df, samples, labels); + dlog << LINFO << "test: \n" << res; + dlog << LINFO << df.weights; + dlog << LINFO << df.b; + DLIB_TEST((unsigned int)sum(diag(res))==samples.size()); + } + template void run_test() { @@ -158,6 +215,7 @@ namespace run_test > >(); test_prior(); + test_prior_sparse(); } };