| 445 | // ---------------------------------------------------------------------------------------- |
| 446 | |
| 447 | void test_l2_version () |
| 448 | { |
| 449 | typedef std::map<unsigned long,double> sample_type; |
| 450 | typedef sparse_linear_kernel<sample_type> kernel_type; |
| 451 | |
| 452 | svm_c_linear_dcd_trainer<kernel_type> linear_trainer; |
| 453 | linear_trainer.set_c(10); |
| 454 | linear_trainer.set_epsilon(1e-5); |
| 455 | |
| 456 | std::vector<sample_type> samples; |
| 457 | std::vector<double> labels; |
| 458 | |
| 459 | // make an instance of a sample vector so we can use it below |
| 460 | sample_type sample; |
| 461 | |
| 462 | |
| 463 | // Now let's go into a loop and randomly generate 10000 samples. |
| 464 | double label = +1; |
| 465 | for (int i = 0; i < 1000; ++i) |
| 466 | { |
| 467 | // flip this flag |
| 468 | label *= -1; |
| 469 | |
| 470 | sample.clear(); |
| 471 | |
| 472 | // now make a random sparse sample with at most 10 non-zero elements |
| 473 | for (int j = 0; j < 10; ++j) |
| 474 | { |
| 475 | int idx = std::rand()%100; |
| 476 | double value = static_cast<double>(std::rand())/RAND_MAX; |
| 477 | |
| 478 | sample[idx] = label*value; |
| 479 | } |
| 480 | |
| 481 | // Also save the samples we are generating so we can let the svm_c_linear_trainer |
| 482 | // learn from them below. |
| 483 | samples.push_back(sample); |
| 484 | labels.push_back(label); |
| 485 | } |
| 486 | |
| 487 | decision_function<kernel_type> df = linear_trainer.train(samples, labels); |
| 488 | |
| 489 | sample.clear(); |
| 490 | sample[4] = 0.3; |
| 491 | sample[10] = 0.9; |
| 492 | DLIB_TEST(df(sample) > 0); |
| 493 | |
| 494 | sample.clear(); |
| 495 | sample[83] = -0.3; |
| 496 | sample[26] = -0.9; |
| 497 | sample[58] = -0.7; |
| 498 | DLIB_TEST(df(sample) < 0); |
| 499 | |
| 500 | sample.clear(); |
| 501 | sample[0] = -0.2; |
| 502 | sample[9] = -0.8; |
| 503 | DLIB_TEST(df(sample) < 0); |
| 504 | } |
no test coverage detected