| 94 | // ---------------------------------------------------------------------------------------- |
| 95 | |
| 96 | void test_normal_no_bias() |
| 97 | { |
| 98 | typedef matrix<double,10,1> sample_type; |
| 99 | |
| 100 | |
| 101 | typedef linear_kernel<sample_type> kernel_type; |
| 102 | |
| 103 | |
| 104 | |
| 105 | svm_c_linear_trainer<kernel_type> linear_trainer_cpa; |
| 106 | svm_c_linear_dcd_trainer<kernel_type> linear_trainer; |
| 107 | |
| 108 | svm_c_linear_dcd_trainer<kernel_type>::optimizer_state state; |
| 109 | |
| 110 | const double C = 1.0; |
| 111 | linear_trainer.set_epsilon(1e-10); |
| 112 | linear_trainer_cpa.set_epsilon(1e-10); |
| 113 | linear_trainer_cpa.set_relative_epsilon(1e-10); |
| 114 | |
| 115 | linear_trainer.include_bias(false); |
| 116 | |
| 117 | |
| 118 | std::vector<sample_type> samples, samples_explict_bias; |
| 119 | std::vector<double> labels; |
| 120 | |
| 121 | // make an instance of a sample vector so we can use it below |
| 122 | sample_type sample; |
| 123 | |
| 124 | decision_function<kernel_type> df, df2, df3; |
| 125 | |
| 126 | dlib::rand rnd; |
| 127 | // Now lets go into a loop and randomly generate 10000 samples. |
| 128 | double label = +1; |
| 129 | for (int i = 0; i < 100; ++i) |
| 130 | { |
| 131 | // flip this flag |
| 132 | label *= -1; |
| 133 | |
| 134 | sample = 0; |
| 135 | |
| 136 | // now make a random sparse sample with at most 10 non-zero elements |
| 137 | for (int j = 0; j < 5; ++j) |
| 138 | { |
| 139 | int idx = rnd.get_random_32bit_number()%9; |
| 140 | double value = rnd.get_random_double(); |
| 141 | |
| 142 | sample(idx) = label*value; |
| 143 | } |
| 144 | |
| 145 | // Also save the samples we are generating so we can let the svm_c_linear_trainer |
| 146 | // learn from them below. |
| 147 | samples.push_back(sample); |
| 148 | labels.push_back(label); |
| 149 | |
| 150 | sample(9) = -1; |
| 151 | samples_explict_bias.push_back(sample); |
| 152 | |
| 153 | if (samples.size() > 1) |
no test coverage detected