MCPcopy Create free account
hub / github.com/davisking/dlib / main

Function main

examples/rvm_regression_ex.cpp:26–99  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

24}
25
26int 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 rvm_regression_trainer object. This is the
48 // object that we will later use to do the training.
49 rvm_regression_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. Note also that using an inappropriately large
55 // gamma will cause the RVM training algorithm to run extremely slowly. What
56 // "large" means is relative to how spread out your data is. So it is important
57 // to use a rule like this as a starting point for determining the gamma value
58 // if you want to use the RVM. It is also probably a good idea to normalize your
59 // samples as shown in the rvm_ex.cpp example program.
60 const double gamma = 2.0/compute_mean_squared_distance(samples);
61 cout << "using gamma of " << gamma << endl;
62 trainer.set_kernel(kernel_type(gamma));
63
64 // One thing you can do to reduce the RVM training time is to make its
65 // stopping epsilon bigger. However, this might make the outputs less
66 // reliable. But sometimes it works out well. 0.001 is the default.
67 trainer.set_epsilon(0.001);
68
69 // now train a function based on our sample points
70 decision_function<kernel_type> test = trainer.train(samples, labels);
71
72 // now we output the value of the sinc function for a few test points as well as the
73 // value predicted by our regression.
74 m(0) = 2.5; cout << sinc(m(0)) << " " << test(m) << endl;
75 m(0) = 0.1; cout << sinc(m(0)) << " " << test(m) << endl;
76 m(0) = -4; cout << sinc(m(0)) << " " << test(m) << endl;
77 m(0) = 5.0; cout << sinc(m(0)) << " " << test(m) << endl;
78
79 // The output is as follows:
80 //using gamma of 0.05
81 //0.239389 0.240989
82 //0.998334 0.999538
83 //-0.189201 -0.188453

Callers

nothing calls this directly

Calls 9

sincFunction · 0.70
testClass · 0.70
serializeFunction · 0.70
deserializeFunction · 0.70
push_backMethod · 0.45
set_kernelMethod · 0.45
set_epsilonMethod · 0.45
trainMethod · 0.45

Tested by

no test coverage detected