From b5a3dbda004c5b5330d1d8d7c67871add6ba8507 Mon Sep 17 00:00:00 2001 From: Davis King Date: Fri, 31 Dec 2010 14:17:51 +0000 Subject: [PATCH] Added a SVR example program --HG-- extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%404061 --- examples/CMakeLists.txt | 2 +- examples/svr_ex.cpp | 88 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 examples/svr_ex.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 83d5b7072..886a19544 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -84,10 +84,10 @@ add_example(surf_ex) add_example(svm_ex) add_example(svm_pegasos_ex) add_example(svm_sparse_ex) +add_example(svr_ex) add_example(threaded_object_ex) add_example(thread_function_ex) add_example(thread_pool_ex) add_example(threads_ex) add_example(timer_ex) add_example(xml_parser_ex) - diff --git a/examples/svr_ex.cpp b/examples/svr_ex.cpp new file mode 100644 index 000000000..b87dbd96e --- /dev/null +++ b/examples/svr_ex.cpp @@ -0,0 +1,88 @@ +// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt +/* + This is an example illustrating the use of the epsilon-insensitive support vector + regression object from the dlib C++ Library. + + In this example we will draw some points from the sinc() function and do a + non-linear regression on them. +*/ + +#include +#include + +#include "dlib/svm.h" + +using namespace std; +using namespace dlib; + +// Here is the sinc function we will be trying to learn with the svr_trainer +// 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. + typedef matrix 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 kernel_type; + + + std::vector samples; + std::vector targets; + + // The first thing we do is pick a few training points from the sinc() function. + sample_type m; + for (double x = -10; x <= 4; x += 1) + { + m(0) = x; + + samples.push_back(m); + targets.push_back(sinc(x)); + } + + // Now setup a SVR trainer object. It has three parameters, the kernel and + // two parameters specific to SVR. + svr_trainer trainer; + trainer.set_kernel(kernel_type(0.1)); + + // This parameter is the usual regularization parameter. It determines the trade-off + // between trying to reduce the training error or allowing more errors but hopefully + // improving the generalization of the resulting function. Larger values encourage exact + // fitting while smaller values of C may encourage better generalization. + trainer.set_c(10); + + // Epsilon-insensitive regression means we do regression but stop trying to fit a data + // point once it is "close enough" to its target value. This parameter is the value that + // controls what we mean by "close enough". In this case, I'm saying I'm happy if the + // resulting regression function gets within 0.001 of the target value. + trainer.set_epsilon_insensitivity(0.001); + + // Now do the training and save the results + decision_function df = trainer.train(samples, targets); + + // now we output the value of the sinc function for a few test points as well as the + // value predicted by SVR. + m(0) = 2.5; cout << sinc(m(0)) << " " << df(m) << endl; + m(0) = 0.1; cout << sinc(m(0)) << " " << df(m) << endl; + m(0) = -4; cout << sinc(m(0)) << " " << df(m) << endl; + m(0) = 5.0; cout << sinc(m(0)) << " " << df(m) << endl; + + // The output is as follows: + // 0.239389 0.23905 + // 0.998334 0.997331 + // -0.189201 -0.187636 + // -0.191785 -0.218924 + + // The first column is the true value of the sinc function and the second + // column is the output from the SVR estimate. +} + +