| 21 | // ---------------------------------------------------------------------------------------- |
| 22 | |
| 23 | void test_sparse() |
| 24 | { |
| 25 | typedef std::map<unsigned long,double> sample_type; |
| 26 | |
| 27 | |
| 28 | typedef sparse_linear_kernel<sample_type> kernel_type; |
| 29 | |
| 30 | |
| 31 | |
| 32 | svm_c_linear_trainer<kernel_type> linear_trainer_cpa; |
| 33 | svm_c_linear_dcd_trainer<kernel_type> linear_trainer; |
| 34 | |
| 35 | svm_c_linear_dcd_trainer<kernel_type>::optimizer_state state; |
| 36 | |
| 37 | const double C = 0.2; |
| 38 | linear_trainer.set_epsilon(1e-10); |
| 39 | linear_trainer_cpa.set_epsilon(1e-10); |
| 40 | linear_trainer_cpa.set_relative_epsilon(1e-10); |
| 41 | |
| 42 | |
| 43 | std::vector<sample_type> samples; |
| 44 | std::vector<double> labels; |
| 45 | |
| 46 | // make an instance of a sample vector so we can use it below |
| 47 | sample_type sample; |
| 48 | |
| 49 | decision_function<kernel_type> df, df2, df3; |
| 50 | |
| 51 | dlib::rand rnd; |
| 52 | // Now lets go into a loop and randomly generate 10000 samples. |
| 53 | double label = +1; |
| 54 | for (int i = 0; i < 100; ++i) |
| 55 | { |
| 56 | // flip this flag |
| 57 | label *= -1; |
| 58 | |
| 59 | sample.clear(); |
| 60 | |
| 61 | // now make a random sparse sample with at most 10 non-zero elements |
| 62 | for (int j = 0; j < 5; ++j) |
| 63 | { |
| 64 | int idx = rnd.get_random_32bit_number()%10; |
| 65 | double value = rnd.get_random_double(); |
| 66 | |
| 67 | sample[idx] = label*value; |
| 68 | } |
| 69 | |
| 70 | // Also save the samples we are generating so we can let the svm_c_linear_trainer |
| 71 | // learn from them below. |
| 72 | samples.push_back(sample); |
| 73 | labels.push_back(label); |
| 74 | |
| 75 | if (samples.size() > 1) |
| 76 | { |
| 77 | linear_trainer_cpa.set_c_class1(C); |
| 78 | linear_trainer_cpa.set_c_class2(1.5*C); |
| 79 | linear_trainer.set_c_class1(C/samples.size()); |
| 80 | linear_trainer.set_c_class2(1.5*C/samples.size()); |
no test coverage detected