| 101 | // ---------------------------------------------------------------------------------------- |
| 102 | |
| 103 | normalized_function<multiclass_linear_decision_function<linear_kernel<matrix<double,0,1>>, unsigned long>> |
| 104 | auto_train_multiclass_svm_linear_classifier ( |
| 105 | std::vector<matrix<double,0,1>> x, |
| 106 | std::vector<unsigned long> y, |
| 107 | const std::chrono::nanoseconds max_runtime, |
| 108 | bool be_verbose |
| 109 | ) |
| 110 | { |
| 111 | const auto labels = select_all_distinct_labels(y); |
| 112 | for (const auto label : labels) |
| 113 | { |
| 114 | const auto num_samples = sum(mat(y) == label); |
| 115 | DLIB_CASSERT(num_samples >= 3, |
| 116 | "You must provide at least 3 examples of each class to this training routine, however, label " |
| 117 | << label << " has only " << num_samples << " examples."); |
| 118 | } |
| 119 | DLIB_ASSERT(is_learning_problem(x,y) == true); |
| 120 | |
| 121 | |
| 122 | randomize_samples(x, y); |
| 123 | |
| 124 | using kernel_type = linear_kernel<matrix<double,0,1>>; |
| 125 | normalized_function<multiclass_linear_decision_function<kernel_type, unsigned long>> df; |
| 126 | // let the normalizer learn the mean and standard deviation of the samples |
| 127 | df.normalizer.train(x); |
| 128 | for (auto& samp : x) |
| 129 | samp = df.normalizer(samp); |
| 130 | |
| 131 | |
| 132 | auto cross_validation_score = [&](const double c) |
| 133 | { |
| 134 | svm_multiclass_linear_trainer<kernel_type, unsigned long> trainer; |
| 135 | trainer.set_c(c); |
| 136 | trainer.set_epsilon(0.01); |
| 137 | trainer.set_max_iterations(100); |
| 138 | trainer.set_num_threads(std::thread::hardware_concurrency()); |
| 139 | |
| 140 | // Finally, perform 3-fold cross validation and then print and return the confusion matrix. |
| 141 | const auto cm = cross_validate_multiclass_trainer(trainer, x, y, 3); |
| 142 | const double accuracy = sum(diag(cm)) / sum(cm); |
| 143 | if (be_verbose) |
| 144 | { |
| 145 | std::cout << "C: " << c << " cross validation accuracy: " << accuracy << '\n'; |
| 146 | std::cout << cm << std::endl; |
| 147 | } |
| 148 | return accuracy; |
| 149 | }; |
| 150 | |
| 151 | if (be_verbose) |
| 152 | std::cout << "Searching for best Multiclass linear SVM training parameters..." << std::endl; |
| 153 | const auto result = find_max_global(cross_validation_score, 1e-3, 1000, max_runtime); |
| 154 | |
| 155 | const double best_c = result.x(0); |
| 156 | |
| 157 | if (be_verbose) |
| 158 | { |
| 159 | std::cout << " best cross-validation score: " << result.y << std::endl; |
| 160 | std::cout << " best C: " << best_c << std::endl; |
no test coverage detected