| 532 | |
| 533 | template <typename kernel_type> |
| 534 | void test_kcentroid_with_offset_sparse_linear_kernel( |
| 535 | ) |
| 536 | /*! |
| 537 | requires |
| 538 | - kernel_type::sample_type == a std::map<unsigned long,double> |
| 539 | - kernel_type == a kernel that just computes a dot product |
| 540 | between its inputs + some constant. I.e. a linear kernel |
| 541 | wrapped by offset_kernel |
| 542 | ensures |
| 543 | - tests the kcentroid object with the given kernel |
| 544 | !*/ |
| 545 | { |
| 546 | // Here we declare that our samples will be 2 dimensional column vectors. |
| 547 | typedef typename kernel_type::sample_type sample_type; |
| 548 | |
| 549 | kernel_type k; |
| 550 | kcentroid<kernel_type> test(k,0.001,20); |
| 551 | |
| 552 | sample_type temp, temp2, temp3; |
| 553 | |
| 554 | std::map<unsigned long,double> val, val2; |
| 555 | |
| 556 | const double b = std::sqrt(k.offset); |
| 557 | |
| 558 | temp.clear(); |
| 559 | temp[0] = 2; |
| 560 | temp2.clear(); |
| 561 | val.clear(); |
| 562 | DLIB_TEST(approx_equal(test(temp), dist(k,temp,val))); |
| 563 | DLIB_TEST(approx_equal(test(temp2), dist(k,temp2,val))); |
| 564 | DLIB_TEST(approx_equal(test.squared_norm(), length_squared(val))); |
| 565 | |
| 566 | |
| 567 | temp2.clear(); |
| 568 | |
| 569 | // make test store the point(0,0,0,0,b) |
| 570 | val.clear(); |
| 571 | val[4] = b; |
| 572 | test.train(temp2, 0,1); |
| 573 | |
| 574 | temp.clear(); |
| 575 | temp[0] = 2; |
| 576 | dlog << LDEBUG << test(temp) ; |
| 577 | dlog << LDEBUG << "squared_norm(): " << test.squared_norm() ; |
| 578 | |
| 579 | DLIB_TEST(approx_equal(test(temp), dist(k,temp,val))); |
| 580 | DLIB_TEST(approx_equal(test(temp2), dist(k,temp2,val))); |
| 581 | DLIB_TEST_MSG(approx_equal(test.get_distance_function()(temp2), dist(k,temp2,val), 1e-7), |
| 582 | test.get_distance_function()(temp2) - dist(k,temp2,val) |
| 583 | ); |
| 584 | DLIB_TEST(approx_equal(test.squared_norm(), length_squared(val))); |
| 585 | DLIB_TEST(approx_equal(test(test), 0)); |
| 586 | DLIB_TEST(approx_equal(test.get_distance_function()(test.get_distance_function()), 0, 1e-6)); |
| 587 | |
| 588 | // make test store the point(0,0,0,0,0) |
| 589 | val.clear(); |
| 590 | test.train(temp2, 1,-1); |
| 591 |
nothing calls this directly
no test coverage detected