| 36 | |
| 37 | template <typename scalar_type> |
| 38 | void test1() |
| 39 | { |
| 40 | typedef matrix<scalar_type,0,1> sample_type; |
| 41 | |
| 42 | typedef radial_basis_kernel<sample_type> kernel_type; |
| 43 | |
| 44 | print_spinner(); |
| 45 | |
| 46 | std::vector<sample_type> samples; |
| 47 | std::vector<scalar_type> targets; |
| 48 | |
| 49 | // The first thing we do is pick a few training points from the sinc() function. |
| 50 | sample_type m(1); |
| 51 | for (scalar_type x = -10; x <= 4; x += 1) |
| 52 | { |
| 53 | m(0) = x; |
| 54 | |
| 55 | samples.push_back(m); |
| 56 | targets.push_back(sinc(x)+1.1); |
| 57 | } |
| 58 | |
| 59 | randomize_samples(samples, targets); |
| 60 | |
| 61 | empirical_kernel_map<kernel_type> ekm; |
| 62 | ekm.load(kernel_type(0.1), samples); |
| 63 | |
| 64 | for (unsigned long i = 0; i < samples.size(); ++i) |
| 65 | samples[i] = ekm.project(samples[i]); |
| 66 | |
| 67 | svr_linear_trainer<linear_kernel<sample_type> > linear_trainer; |
| 68 | linear_trainer.set_epsilon(0.0001); |
| 69 | linear_trainer.set_c(30); |
| 70 | linear_trainer.set_epsilon_insensitivity(0.001); |
| 71 | |
| 72 | matrix<double> res = cross_validate_regression_trainer(linear_trainer, samples, targets, 5); |
| 73 | dlog << LINFO << "MSE and R-Squared: "<< res; |
| 74 | DLIB_TEST(res(0) < 1e-4); |
| 75 | DLIB_TEST(res(1) > 0.99); |
| 76 | |
| 77 | dlib::rand rnd; |
| 78 | |
| 79 | samples.clear(); |
| 80 | targets.clear(); |
| 81 | std::vector<scalar_type> noisefree_targets; |
| 82 | for (scalar_type x = 0; x <= 5; x += 0.1) |
| 83 | { |
| 84 | m(0) = x; |
| 85 | samples.push_back(matrix_cast<scalar_type>(linpiece(m, linspace(0,5,20)))); |
| 86 | targets.push_back(x*x + rnd.get_random_gaussian()); |
| 87 | noisefree_targets.push_back(x*x); |
| 88 | } |
| 89 | linear_trainer.set_learns_nonnegative_weights(true); |
| 90 | linear_trainer.set_epsilon_insensitivity(1.0); |
| 91 | decision_function<linear_kernel<sample_type> > df2 = linear_trainer.train(samples, targets); |
| 92 | |
| 93 | print_spinner(); |
| 94 | res = test_regression_function(df2, samples, noisefree_targets); |
| 95 | dlog << LINFO << "MSE and R-Squared: "<< res; |
nothing calls this directly
no test coverage detected