added an rvm regression example

--HG--
rename : examples/krls_ex.cpp => examples/rvm_regression_ex.cpp
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402513
This commit is contained in:
Davis King 2008-09-14 15:38:45 +00:00
parent 753bc33406
commit 6be5d5a174
2 changed files with 76 additions and 0 deletions

View File

@ -48,6 +48,7 @@ add_example(pipe_ex)
add_example(queue_ex)
add_example(rank_features_ex)
add_example(rvm_ex)
add_example(rvm_regression_ex)
add_example(server_http_ex)
add_example(sockets_ex)
add_example(sockets_ex_2)

View File

@ -0,0 +1,75 @@
/*
This is an example illustrating the use of the RVM regression object
from the dlib C++ Library.
This example will train on data from the sinc function.
*/
#include <iostream>
#include <vector>
#include "dlib/svm.h"
using namespace std;
using namespace dlib;
// Here is the sinc function we will be trying to learn with rvm regression
double sinc(double x)
{
if (x == 0)
return 1;
return sin(x)/x;
}
int main()
{
// Here we declare that our samples will be 1 dimensional column vectors.
typedef matrix<double,1,1> sample_type;
// Now we are making a typedef for the kind of kernel we want to use. I picked the
// radial basis kernel because it only has one parameter and generally gives good
// results without much fiddling.
typedef radial_basis_kernel<sample_type> kernel_type;
// Here we declare an instance of the rvm_regression_trainer object. This is the
// object that we will later use to do the training.
rvm_regression_trainer<kernel_type> trainer;
// Here we set the kernel we want to use for training. The 0.05 is the gamma
// parameter to the radial_basis_kernel.
trainer.set_kernel(kernel_type(0.05));
// Now sample some points from the sinc() function
sample_type m;
std::vector<sample_type> samples;
std::vector<double> labels;
for (double x = -10; x <= 4; x += 1)
{
m(0) = x;
samples.push_back(m);
labels.push_back(sinc(x));
}
// now train a function based on our sample points
decision_function<kernel_type> test = trainer.train(samples, labels);
// now we output the value of the sinc function for a few test points as well as the
// value predicted by our regression.
m(0) = 2.5; cout << sinc(m(0)) << " " << test(m) << endl;
m(0) = 0.1; cout << sinc(m(0)) << " " << test(m) << endl;
m(0) = -4; cout << sinc(m(0)) << " " << test(m) << endl;
m(0) = 5.0; cout << sinc(m(0)) << " " << test(m) << endl;
// The output is as follows:
//0.239389 0.240989
//0.998334 0.999538
//-0.189201 -0.188453
//-0.191785 -0.226516
// The first column is the true value of the sinc function and the second
// column is the output from the rvm estimate.
}