| 26 | } |
| 27 | |
| 28 | int main() |
| 29 | { |
| 30 | // Here we declare that our samples will be 1 dimensional column vectors. In general, |
| 31 | // you can use N dimensional vectors as inputs to the krls object. But here we only |
| 32 | // have 1 dimension to make the example simple. (Note that if you don't know the |
| 33 | // dimensionality of your vectors at compile time you can change the first number to |
| 34 | // a 0 and then set the size at runtime) |
| 35 | typedef matrix<double,1,1> sample_type; |
| 36 | |
| 37 | // Now we are making a typedef for the kind of kernel we want to use. I picked the |
| 38 | // radial basis kernel because it only has one parameter and generally gives good |
| 39 | // results without much fiddling. |
| 40 | typedef radial_basis_kernel<sample_type> kernel_type; |
| 41 | |
| 42 | // Here we declare an instance of the krls object. The first argument to the constructor |
| 43 | // is the kernel we wish to use. The second is a parameter that determines the numerical |
| 44 | // accuracy with which the object will perform part of the regression algorithm. Generally |
| 45 | // smaller values give better results but cause the algorithm to run slower. You just have |
| 46 | // to play with it to decide what balance of speed and accuracy is right for your problem. |
| 47 | // Here we have set it to 0.001. |
| 48 | krls<kernel_type> test(kernel_type(0.1),0.001); |
| 49 | |
| 50 | // now we train our object on a few samples of the sinc function. |
| 51 | sample_type m; |
| 52 | for (double x = -10; x <= 4; x += 1) |
| 53 | { |
| 54 | m(0) = x; |
| 55 | test.train(m, sinc(x)); |
| 56 | } |
| 57 | |
| 58 | // now we output the value of the sinc function for a few test points as well as the |
| 59 | // value predicted by krls object. |
| 60 | m(0) = 2.5; cout << sinc(m(0)) << " " << test(m) << endl; |
| 61 | m(0) = 0.1; cout << sinc(m(0)) << " " << test(m) << endl; |
| 62 | m(0) = -4; cout << sinc(m(0)) << " " << test(m) << endl; |
| 63 | m(0) = 5.0; cout << sinc(m(0)) << " " << test(m) << endl; |
| 64 | |
| 65 | // The output is as follows: |
| 66 | // 0.239389 0.239362 |
| 67 | // 0.998334 0.998333 |
| 68 | // -0.189201 -0.189201 |
| 69 | // -0.191785 -0.197267 |
| 70 | |
| 71 | |
| 72 | // The first column is the true value of the sinc function and the second |
| 73 | // column is the output from the krls estimate. |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | // Another thing that is worth knowing is that just about everything in dlib is serializable. |
| 80 | // So for example, you can save the test object to disk and recall it later like so: |
| 81 | serialize("saved_krls_object.dat") << test; |
| 82 | |
| 83 | // Now let's open that file back up and load the krls object it contains. |
| 84 | deserialize("saved_krls_object.dat") >> test; |
| 85 |
nothing calls this directly
no test coverage detected