| 71 | // ---------------------------------------------------------------------------------------- |
| 72 | |
| 73 | void test_sparse() |
| 74 | { |
| 75 | std::vector<sparse_type> x; |
| 76 | std::vector<double> y; |
| 77 | make_problem(x, y); |
| 78 | |
| 79 | const auto df = auto_train_linear_classifier(x, y, false); |
| 80 | DLIB_TEST(df(sparse_type{{0, 3.0}}) > 0.9); |
| 81 | DLIB_TEST(df(sparse_type{{0, -3.0}}) < 0.1); |
| 82 | |
| 83 | // The SVM inside is a class-balanced SVM trained on all the samples with a power |
| 84 | // of 10 C, here below the initial 0.1, 1 and 10. |
| 85 | const double c = find_c(df.decision_funct, x, y); |
| 86 | dlog << LINFO << "chosen C: " << c; |
| 87 | DLIB_TEST(c != 0 && c < 0.1); |
| 88 | |
| 89 | // The same data gives the same result. |
| 90 | const auto df2 = auto_train_linear_classifier(x, y, false); |
| 91 | DLIB_TEST(df2.alpha == df.alpha && df2.beta == df.beta); |
| 92 | DLIB_TEST(df2.decision_funct.b == df.decision_funct.b); |
| 93 | |
| 94 | // A view of selected samples gives the same result as a copy of them. |
| 95 | std::vector<long> rows; |
| 96 | std::vector<sparse_type> copy_x; |
| 97 | std::vector<double> copy_y; |
| 98 | for (long i = 0; i < 100; i += 2) |
| 99 | { |
| 100 | rows.push_back(i); |
| 101 | copy_x.push_back(x[i]); |
| 102 | copy_y.push_back(y[i]); |
| 103 | } |
| 104 | const auto df_view = auto_train_linear_classifier(rowm(mat(x), mat(rows)), rowm(mat(y), mat(rows)), false); |
| 105 | const auto df_copy = auto_train_linear_classifier(copy_x, copy_y, false); |
| 106 | DLIB_TEST(df_view.alpha == df_copy.alpha && df_view.beta == df_copy.beta); |
| 107 | DLIB_TEST(df_view.decision_funct.b == df_copy.decision_funct.b); |
| 108 | } |
| 109 | |
| 110 | // ---------------------------------------------------------------------------------------- |
| 111 |
no test coverage detected