| 24 | } |
| 25 | |
| 26 | int main() |
| 27 | { |
| 28 | // Here we declare that our samples will be 1 dimensional column vectors. |
| 29 | typedef matrix<double,1,1> sample_type; |
| 30 | |
| 31 | // Now sample some points from the sinc() function |
| 32 | sample_type m; |
| 33 | std::vector<sample_type> samples; |
| 34 | std::vector<double> labels; |
| 35 | for (double x = -10; x <= 4; x += 1) |
| 36 | { |
| 37 | m(0) = x; |
| 38 | samples.push_back(m); |
| 39 | labels.push_back(sinc(x)); |
| 40 | } |
| 41 | |
| 42 | // Now we are making a typedef for the kind of kernel we want to use. I picked the |
| 43 | // radial basis kernel because it only has one parameter and generally gives good |
| 44 | // results without much fiddling. |
| 45 | typedef radial_basis_kernel<sample_type> kernel_type; |
| 46 | |
| 47 | // Here we declare an instance of the krr_trainer object. This is the |
| 48 | // object that we will later use to do the training. |
| 49 | krr_trainer<kernel_type> trainer; |
| 50 | |
| 51 | // Here we set the kernel we want to use for training. The radial_basis_kernel |
| 52 | // has a parameter called gamma that we need to determine. As a rule of thumb, a good |
| 53 | // gamma to try is 1.0/(mean squared distance between your sample points). So |
| 54 | // below we are using a similar value computed from at most 2000 randomly selected |
| 55 | // samples. |
| 56 | const double gamma = 3.0/compute_mean_squared_distance(randomly_subsample(samples, 2000)); |
| 57 | cout << "using gamma of " << gamma << endl; |
| 58 | trainer.set_kernel(kernel_type(gamma)); |
| 59 | |
| 60 | // now train a function based on our sample points |
| 61 | decision_function<kernel_type> test = trainer.train(samples, labels); |
| 62 | |
| 63 | // now we output the value of the sinc function for a few test points as well as the |
| 64 | // value predicted by our regression. |
| 65 | m(0) = 2.5; cout << sinc(m(0)) << " " << test(m) << endl; |
| 66 | m(0) = 0.1; cout << sinc(m(0)) << " " << test(m) << endl; |
| 67 | m(0) = -4; cout << sinc(m(0)) << " " << test(m) << endl; |
| 68 | m(0) = 5.0; cout << sinc(m(0)) << " " << test(m) << endl; |
| 69 | |
| 70 | // The output is as follows: |
| 71 | //using gamma of 0.075 |
| 72 | // 0.239389 0.239389 |
| 73 | // 0.998334 0.998362 |
| 74 | // -0.189201 -0.189254 |
| 75 | // -0.191785 -0.186618 |
| 76 | |
| 77 | // The first column is the true value of the sinc function and the second |
| 78 | // column is the output from the krr estimate. |
| 79 | |
| 80 | |
| 81 | // Note that the krr_trainer has the ability to tell us the leave-one-out predictions |
| 82 | // for each sample. |
| 83 | std::vector<double> loo_values; |
nothing calls this directly
no test coverage detected