This function puts it all together. In here we use the three_class_classifier_problem along with dlib's oca cutting plane solver to find the optimal weights given our training data.
| 368 | // along with dlib's oca cutting plane solver to find the optimal weights given our |
| 369 | // training data. |
| 370 | column_vector train_three_class_classifier ( |
| 371 | const std::vector<sample_type>& samples, |
| 372 | const std::vector<int>& labels |
| 373 | ) |
| 374 | { |
| 375 | const unsigned long num_threads = 4; |
| 376 | three_class_classifier_problem problem(samples, labels, num_threads); |
| 377 | |
| 378 | // Before we run the solver we set up some general parameters. First, |
| 379 | // you can set the C parameter of the structural SVM by calling set_c(). |
| 380 | problem.set_c(1); |
| 381 | |
| 382 | // The epsilon parameter controls the stopping tolerance. The optimizer will run until |
| 383 | // R(w) is within epsilon of its optimal value. If you don't set this then it defaults |
| 384 | // to 0.001. |
| 385 | problem.set_epsilon(0.0001); |
| 386 | |
| 387 | // Uncomment this and the optimizer will print its progress to standard out. You will |
| 388 | // be able to see things like the current risk gap. The optimizer continues until the |
| 389 | // risk gap is below epsilon. |
| 390 | //problem.be_verbose(); |
| 391 | |
| 392 | // The optimizer uses an internal cache to avoid unnecessary calls to your |
| 393 | // separation_oracle() routine. This parameter controls the size of that cache. |
| 394 | // Bigger values use more RAM and might make the optimizer run faster. You can also |
| 395 | // disable it by setting it to 0 which is good to do when your separation_oracle is |
| 396 | // very fast. If you don't call this function it defaults to a value of 5. |
| 397 | //problem.set_max_cache_size(20); |
| 398 | |
| 399 | |
| 400 | column_vector weights; |
| 401 | // Finally, we create the solver and then run it. |
| 402 | oca solver; |
| 403 | solver(problem, weights); |
| 404 | |
| 405 | // Alternatively, if you wanted to require that the learned weights are all |
| 406 | // non-negative then you can call the solver as follows and it will put a constraint on |
| 407 | // the optimization problem which causes all elements of weights to be >= 0. |
| 408 | //solver(problem, weights, problem.get_num_dimensions()); |
| 409 | |
| 410 | return weights; |
| 411 | } |
| 412 | |
| 413 | // ---------------------------------------------------------------------------------------- |
| 414 |
no test coverage detected