| 25 | } |
| 26 | |
| 27 | int main() |
| 28 | { |
| 29 | // Here we declare that our samples will be 1 dimensional column vectors. |
| 30 | typedef matrix<double,1,1> sample_type; |
| 31 | |
| 32 | // Now we are making a typedef for the kind of kernel we want to use. I picked the |
| 33 | // radial basis kernel because it only has one parameter and generally gives good |
| 34 | // results without much fiddling. |
| 35 | typedef radial_basis_kernel<sample_type> kernel_type; |
| 36 | |
| 37 | |
| 38 | std::vector<sample_type> samples; |
| 39 | std::vector<double> targets; |
| 40 | |
| 41 | // The first thing we do is pick a few training points from the sinc() function. |
| 42 | sample_type m; |
| 43 | for (double x = -10; x <= 4; x += 1) |
| 44 | { |
| 45 | m(0) = x; |
| 46 | |
| 47 | samples.push_back(m); |
| 48 | targets.push_back(sinc(x)); |
| 49 | } |
| 50 | |
| 51 | // Now setup a SVR trainer object. It has three parameters, the kernel and |
| 52 | // two parameters specific to SVR. |
| 53 | svr_trainer<kernel_type> trainer; |
| 54 | trainer.set_kernel(kernel_type(0.1)); |
| 55 | |
| 56 | // This parameter is the usual regularization parameter. It determines the trade-off |
| 57 | // between trying to reduce the training error or allowing more errors but hopefully |
| 58 | // improving the generalization of the resulting function. Larger values encourage exact |
| 59 | // fitting while smaller values of C may encourage better generalization. |
| 60 | trainer.set_c(10); |
| 61 | |
| 62 | // Epsilon-insensitive regression means we do regression but stop trying to fit a data |
| 63 | // point once it is "close enough" to its target value. This parameter is the value that |
| 64 | // controls what we mean by "close enough". In this case, I'm saying I'm happy if the |
| 65 | // resulting regression function gets within 0.001 of the target value. |
| 66 | trainer.set_epsilon_insensitivity(0.001); |
| 67 | |
| 68 | // Now do the training and save the results |
| 69 | decision_function<kernel_type> df = trainer.train(samples, targets); |
| 70 | |
| 71 | // now we output the value of the sinc function for a few test points as well as the |
| 72 | // value predicted by SVR. |
| 73 | m(0) = 2.5; cout << sinc(m(0)) << " " << df(m) << endl; |
| 74 | m(0) = 0.1; cout << sinc(m(0)) << " " << df(m) << endl; |
| 75 | m(0) = -4; cout << sinc(m(0)) << " " << df(m) << endl; |
| 76 | m(0) = 5.0; cout << sinc(m(0)) << " " << df(m) << endl; |
| 77 | |
| 78 | // The output is as follows: |
| 79 | // 0.239389 0.23905 |
| 80 | // 0.998334 0.997331 |
| 81 | // -0.189201 -0.187636 |
| 82 | // -0.191785 -0.218924 |
| 83 | |
| 84 | // The first column is the true value of the sinc function and the second |
nothing calls this directly
no test coverage detected