| 405 | typename in_scalar_vector_type |
| 406 | > |
| 407 | const matrix<double, 1, 2, typename trainer_type::mem_manager_type> |
| 408 | cross_validate_trainer_impl ( |
| 409 | const trainer_type& trainer, |
| 410 | const in_sample_vector_type& x, |
| 411 | const in_scalar_vector_type& y, |
| 412 | const long folds |
| 413 | ) |
| 414 | { |
| 415 | typedef typename in_scalar_vector_type::value_type scalar_type; |
| 416 | typedef typename trainer_type::mem_manager_type mem_manager_type; |
| 417 | typedef matrix<scalar_type,0,1,mem_manager_type> scalar_vector_type; |
| 418 | |
| 419 | // make sure requires clause is not broken |
| 420 | DLIB_CASSERT(is_binary_classification_problem(x,y) == true && |
| 421 | 1 < folds && folds <= std::min(sum(y>0),sum(y<0)), |
| 422 | "\tmatrix cross_validate_trainer()" |
| 423 | << "\n\t invalid inputs were given to this function" |
| 424 | << "\n\t std::min(sum(y>0),sum(y<0)): " << std::min(sum(y>0),sum(y<0)) |
| 425 | << "\n\t folds: " << folds |
| 426 | << "\n\t is_binary_classification_problem(x,y): " << ((is_binary_classification_problem(x,y))? "true":"false") |
| 427 | ); |
| 428 | |
| 429 | |
| 430 | // count the number of positive and negative examples |
| 431 | long num_pos = 0; |
| 432 | long num_neg = 0; |
| 433 | for (long r = 0; r < y.nr(); ++r) |
| 434 | { |
| 435 | if (y(r) == +1.0) |
| 436 | ++num_pos; |
| 437 | else |
| 438 | ++num_neg; |
| 439 | } |
| 440 | |
| 441 | // figure out how many positive and negative examples we will have in each fold |
| 442 | const long num_pos_test_samples = num_pos/folds; |
| 443 | const long num_pos_train_samples = num_pos - num_pos_test_samples; |
| 444 | const long num_neg_test_samples = num_neg/folds; |
| 445 | const long num_neg_train_samples = num_neg - num_neg_test_samples; |
| 446 | |
| 447 | |
| 448 | matrix<long,0,1> x_test, x_train; |
| 449 | scalar_vector_type y_test, y_train; |
| 450 | x_test.set_size (num_pos_test_samples + num_neg_test_samples); |
| 451 | y_test.set_size (num_pos_test_samples + num_neg_test_samples); |
| 452 | x_train.set_size(num_pos_train_samples + num_neg_train_samples); |
| 453 | y_train.set_size(num_pos_train_samples + num_neg_train_samples); |
| 454 | |
| 455 | long pos_idx = 0; |
| 456 | long neg_idx = 0; |
| 457 | |
| 458 | matrix<double, 1, 2, mem_manager_type> res; |
| 459 | set_all_elements(res,0); |
| 460 | |
| 461 | for (long i = 0; i < folds; ++i) |
| 462 | { |
| 463 | long cur = 0; |
| 464 |
no test coverage detected