MCPcopy Create free account
hub / github.com/davisking/dlib / test_sparse

Function test_sparse

dlib/test/svm_c_linear.cpp:174–242  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

172// ----------------------------------------------------------------------------------------
173
174 void test_sparse (
175 )
176 {
177 print_spinner();
178 dlog << LINFO << "test with sparse vectors";
179 std::vector<sparse_sample_type> samples;
180 std::vector<double> labels;
181
182 sample_type samp;
183
184 get_simple_points_sparse(samples,labels);
185
186 svm_c_linear_trainer<sparse_linear_kernel<sparse_sample_type> > trainer;
187 trainer.set_c(1e4);
188 //trainer.be_verbose();
189 trainer.set_epsilon(1e-11);
190
191
192 double obj;
193 decision_function<sparse_linear_kernel<sparse_sample_type> > df = trainer.train(samples, labels, obj);
194 dlog << LDEBUG << "obj: "<< obj;
195 DLIB_TEST_MSG(abs(obj - 0.72222222222) < 1e-7, obj);
196
197 DLIB_TEST(abs(df(samples[0]) - (-1)) < 1e-6);
198 DLIB_TEST(abs(df(samples[1]) - (-1)) < 1e-6);
199 DLIB_TEST(abs(df(samples[2]) - (1)) < 1e-6);
200 DLIB_TEST(abs(df(samples[3]) - (1)) < 1e-6);
201
202
203 // While we are at it, make sure the krr_trainer works with sparse samples
204 krr_trainer<sparse_linear_kernel<sparse_sample_type> > krr;
205
206 df = krr.train(samples, labels);
207 DLIB_TEST(abs(df(samples[0]) - (-1)) < 1e-6);
208 DLIB_TEST(abs(df(samples[1]) - (-1)) < 1e-6);
209 DLIB_TEST(abs(df(samples[2]) - (1)) < 1e-6);
210 DLIB_TEST(abs(df(samples[3]) - (1)) < 1e-6);
211
212
213 // Now test some of the sparse helper functions
214 DLIB_TEST(max_index_plus_one(samples) == 2);
215 DLIB_TEST(max_index_plus_one(samples[0]) == 2);
216
217 matrix<double,3,1> m;
218 m = 1;
219 add_to(m, samples[3]);
220 DLIB_TEST(m(0) == 1 + samples[3][0].second);
221 DLIB_TEST(m(1) == 1 + samples[3][1].second);
222 DLIB_TEST(m(2) == 1);
223
224 m = 1;
225 subtract_from(m, samples[3]);
226 DLIB_TEST(m(0) == 1 - samples[3][0].second);
227 DLIB_TEST(m(1) == 1 - samples[3][1].second);
228 DLIB_TEST(m(2) == 1);
229
230 m = 1;
231 add_to(m, samples[3], 2);

Callers 1

perform_testMethod · 0.70

Calls 9

print_spinnerFunction · 0.85
get_simple_points_sparseFunction · 0.85
absFunction · 0.85
add_toFunction · 0.85
subtract_fromFunction · 0.85
max_index_plus_oneFunction · 0.50
set_cMethod · 0.45
set_epsilonMethod · 0.45
trainMethod · 0.45

Tested by

no test coverage detected