krls_ex.cpp 3.62 KB
Newer Older
1
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
Davis King's avatar
Davis King committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
    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>

#include "dlib/svm.h"

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()
{
    // Here we declare that our samples will be 1 dimensional column vectors.  The reason for
    // using a matrix here is that 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.
33
34
    // (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)
Davis King's avatar
Davis King committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    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:
Davis King's avatar
Davis King committed
66
67
68
69
70
    // 0.239389   0.239362
    // 0.998334   0.998333
    // -0.189201   -0.189201
    // -0.191785   -0.197267

Davis King's avatar
Davis King committed
71
72
73
74

    // The first column is the true value of the sinc function and the second
    // column is the output from the krls estimate.  

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    



    // 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();

    // now lets open that file back up and load the krls object it contains
    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);
Davis King's avatar
Davis King committed
96
97
98
}