"Fossies" - the Fresh Open Source Software Archive

Member "dlib-19.18/docs/krls_ex.cpp.html" (22 Sep 2019, 11300 Bytes) of package /linux/misc/dlib-19.18.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) HTML source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 <html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - krls_ex.cpp</title></head><body bgcolor='white'><pre>
    2 <font color='#009900'>// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
    3 </font><font color='#009900'>/*
    4     This is an example illustrating the use of the krls object 
    5     from the dlib C++ Library.
    6 
    7     The krls object allows you to perform online regression.  This
    8     example will train an instance of it on the sinc function.
    9 
   10 */</font>
   11 
   12 <font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>iostream<font color='#5555FF'>&gt;</font>
   13 <font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>vector<font color='#5555FF'>&gt;</font>
   14 
   15 <font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>dlib<font color='#5555FF'>/</font>svm.h<font color='#5555FF'>&gt;</font>
   16 
   17 <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std;
   18 <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> dlib;
   19 
   20 <font color='#009900'>// Here is the sinc function we will be trying to learn with the krls
   21 </font><font color='#009900'>// object.
   22 </font><font color='#0000FF'><u>double</u></font> <b><a name='sinc'></a>sinc</b><font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font> x<font face='Lucida Console'>)</font>
   23 <b>{</b>
   24     <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>x <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
   25         <font color='#0000FF'>return</font> <font color='#979000'>1</font>;
   26     <font color='#0000FF'>return</font> <font color='#BB00BB'>sin</font><font face='Lucida Console'>(</font>x<font face='Lucida Console'>)</font><font color='#5555FF'>/</font>x;
   27 <b>}</b>
   28 
   29 <font color='#0000FF'><u>int</u></font> <b><a name='main'></a>main</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
   30 <b>{</b>
   31     <font color='#009900'>// Here we declare that our samples will be 1 dimensional column vectors.  In general, 
   32 </font>    <font color='#009900'>// you can use N dimensional vectors as inputs to the krls object.  But here we only 
   33 </font>    <font color='#009900'>// have 1 dimension to make the example simple.  (Note that if you don't know the 
   34 </font>    <font color='#009900'>// dimensionality of your vectors at compile time you can change the first number to 
   35 </font>    <font color='#009900'>// a 0 and then set the size at runtime)
   36 </font>    <font color='#0000FF'>typedef</font> matrix<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font>,<font color='#979000'>1</font>,<font color='#979000'>1</font><font color='#5555FF'>&gt;</font> sample_type;
   37 
   38     <font color='#009900'>// Now we are making a typedef for the kind of kernel we want to use.  I picked the
   39 </font>    <font color='#009900'>// radial basis kernel because it only has one parameter and generally gives good
   40 </font>    <font color='#009900'>// results without much fiddling.
   41 </font>    <font color='#0000FF'>typedef</font> radial_basis_kernel<font color='#5555FF'>&lt;</font>sample_type<font color='#5555FF'>&gt;</font> kernel_type;
   42 
   43     <font color='#009900'>// Here we declare an instance of the krls object.  The first argument to the constructor
   44 </font>    <font color='#009900'>// is the kernel we wish to use.  The second is a parameter that determines the numerical 
   45 </font>    <font color='#009900'>// accuracy with which the object will perform part of the regression algorithm.  Generally
   46 </font>    <font color='#009900'>// smaller values give better results but cause the algorithm to run slower.  You just have
   47 </font>    <font color='#009900'>// to play with it to decide what balance of speed and accuracy is right for your problem.
   48 </font>    <font color='#009900'>// Here we have set it to 0.001.
   49 </font>    krls<font color='#5555FF'>&lt;</font>kernel_type<font color='#5555FF'>&gt;</font> <font color='#BB00BB'>test</font><font face='Lucida Console'>(</font><font color='#BB00BB'>kernel_type</font><font face='Lucida Console'>(</font><font color='#979000'>0.1</font><font face='Lucida Console'>)</font>,<font color='#979000'>0.001</font><font face='Lucida Console'>)</font>;
   50 
   51     <font color='#009900'>// now we train our object on a few samples of the sinc function.
   52 </font>    sample_type m;
   53     <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font> x <font color='#5555FF'>=</font> <font color='#5555FF'>-</font><font color='#979000'>10</font>; x <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>4</font>; x <font color='#5555FF'>+</font><font color='#5555FF'>=</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>
   54     <b>{</b>
   55         <font color='#BB00BB'>m</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> x;
   56         test.<font color='#BB00BB'>train</font><font face='Lucida Console'>(</font>m, <font color='#BB00BB'>sinc</font><font face='Lucida Console'>(</font>x<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
   57     <b>}</b>
   58 
   59     <font color='#009900'>// now we output the value of the sinc function for a few test points as well as the 
   60 </font>    <font color='#009900'>// value predicted by krls object.
   61 </font>    <font color='#BB00BB'>m</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>2.5</font>; cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>sinc</font><font face='Lucida Console'>(</font><font color='#BB00BB'>m</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>   </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>test</font><font face='Lucida Console'>(</font>m<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
   62     <font color='#BB00BB'>m</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>0.1</font>; cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>sinc</font><font face='Lucida Console'>(</font><font color='#BB00BB'>m</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>   </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>test</font><font face='Lucida Console'>(</font>m<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
   63     <font color='#BB00BB'>m</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#5555FF'>-</font><font color='#979000'>4</font>;  cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>sinc</font><font face='Lucida Console'>(</font><font color='#BB00BB'>m</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>   </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>test</font><font face='Lucida Console'>(</font>m<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
   64     <font color='#BB00BB'>m</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>5.0</font>; cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>sinc</font><font face='Lucida Console'>(</font><font color='#BB00BB'>m</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>   </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>test</font><font face='Lucida Console'>(</font>m<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
   65 
   66     <font color='#009900'>// The output is as follows:
   67 </font>    <font color='#009900'>// 0.239389   0.239362
   68 </font>    <font color='#009900'>// 0.998334   0.998333
   69 </font>    <font color='#009900'>// -0.189201   -0.189201
   70 </font>    <font color='#009900'>// -0.191785   -0.197267
   71 </font>
   72 
   73     <font color='#009900'>// The first column is the true value of the sinc function and the second
   74 </font>    <font color='#009900'>// column is the output from the krls estimate.  
   75 </font>
   76     
   77 
   78 
   79 
   80     <font color='#009900'>// Another thing that is worth knowing is that just about everything in dlib is serializable.
   81 </font>    <font color='#009900'>// So for example, you can save the test object to disk and recall it later like so:
   82 </font>    <font color='#BB00BB'>serialize</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>saved_krls_object.dat</font>"<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> test;
   83 
   84     <font color='#009900'>// Now let's open that file back up and load the krls object it contains.
   85 </font>    <font color='#BB00BB'>deserialize</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>saved_krls_object.dat</font>"<font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font> test;
   86 
   87     <font color='#009900'>// If you don't want to save the whole krls object (it might be a bit large) 
   88 </font>    <font color='#009900'>// you can save just the decision function it has learned so far.  You can get 
   89 </font>    <font color='#009900'>// the decision function out of it by calling test.get_decision_function() and
   90 </font>    <font color='#009900'>// then you can serialize that object instead.  E.g.
   91 </font>    decision_function<font color='#5555FF'>&lt;</font>kernel_type<font color='#5555FF'>&gt;</font> funct <font color='#5555FF'>=</font> test.<font color='#BB00BB'>get_decision_function</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
   92     <font color='#BB00BB'>serialize</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>saved_krls_function.dat</font>"<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> funct;
   93 <b>}</b>
   94 
   95 
   96 
   97 </pre></body></html>