2009-02-17 09:45:57 +08:00
|
|
|
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
|
2008-05-23 08:08:49 +08:00
|
|
|
/*
|
|
|
|
This is an example illustrating the use of the krls object
|
|
|
|
from the dlib C++ Library.
|
|
|
|
|
|
|
|
The krls object allows you to perform online regression. This
|
|
|
|
example will train an instance of it on the sinc function.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
|
2012-12-08 22:32:13 +08:00
|
|
|
#include <dlib/svm.h>
|
2008-05-23 08:08:49 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dlib;
|
|
|
|
|
|
|
|
// Here is the sinc function we will be trying to learn with the krls
|
|
|
|
// object.
|
|
|
|
double sinc(double x)
|
|
|
|
{
|
|
|
|
if (x == 0)
|
|
|
|
return 1;
|
|
|
|
return sin(x)/x;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2010-12-31 21:58:00 +08:00
|
|
|
// Here we declare that our samples will be 1 dimensional column vectors. In general,
|
|
|
|
// you can use N dimensional vectors as inputs to the krls object. But here we only
|
|
|
|
// have 1 dimension to make the example simple. (Note that if you don't know the
|
|
|
|
// dimensionality of your vectors at compile time you can change the first number to
|
|
|
|
// a 0 and then set the size at runtime)
|
2008-05-23 08:08:49 +08:00
|
|
|
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 krls object. The first argument to the constructor
|
|
|
|
// is the kernel we wish to use. The second is a parameter that determines the numerical
|
|
|
|
// accuracy with which the object will perform part of the regression algorithm. Generally
|
|
|
|
// smaller values give better results but cause the algorithm to run slower. You just have
|
|
|
|
// to play with it to decide what balance of speed and accuracy is right for your problem.
|
|
|
|
// Here we have set it to 0.001.
|
|
|
|
krls<kernel_type> test(kernel_type(0.1),0.001);
|
|
|
|
|
|
|
|
// now we train our object on a few samples of the sinc function.
|
|
|
|
sample_type m;
|
|
|
|
for (double x = -10; x <= 4; x += 1)
|
|
|
|
{
|
|
|
|
m(0) = x;
|
|
|
|
test.train(m, sinc(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we output the value of the sinc function for a few test points as well as the
|
|
|
|
// value predicted by krls object.
|
|
|
|
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:
|
2008-06-14 10:18:35 +08:00
|
|
|
// 0.239389 0.239362
|
|
|
|
// 0.998334 0.998333
|
|
|
|
// -0.189201 -0.189201
|
|
|
|
// -0.191785 -0.197267
|
|
|
|
|
2008-05-23 08:08:49 +08:00
|
|
|
|
|
|
|
// The first column is the true value of the sinc function and the second
|
|
|
|
// column is the output from the krls estimate.
|
|
|
|
|
2008-11-01 02:12:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Another thing that is worth knowing is that just about everything in dlib is serializable.
|
|
|
|
// So for example, you can save the test object to disk and recall it later like so:
|
|
|
|
ofstream fout("saved_krls_object.dat",ios::binary);
|
|
|
|
serialize(test,fout);
|
|
|
|
fout.close();
|
|
|
|
|
2014-02-23 05:07:17 +08:00
|
|
|
// Now let's open that file back up and load the krls object it contains.
|
2008-11-01 02:12:18 +08:00
|
|
|
ifstream fin("saved_krls_object.dat",ios::binary);
|
|
|
|
deserialize(test, fin);
|
|
|
|
|
|
|
|
// If you don't want to save the whole krls object (it might be a bit large)
|
|
|
|
// you can save just the decision function it has learned so far. You can get
|
|
|
|
// the decision function out of it by calling test.get_decision_function() and
|
|
|
|
// then you can serialize that object instead. E.g.
|
|
|
|
decision_function<kernel_type> funct = test.get_decision_function();
|
|
|
|
fout.open("saved_krls_function.dat",ios::binary);
|
|
|
|
serialize(funct, fout);
|
2008-05-23 08:08:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|