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-09-14 21:47:26 +08:00
|
|
|
/*
|
|
|
|
|
|
|
|
This is an example illustrating the use of the relevance vector machine
|
|
|
|
utilities from the dlib C++ Library.
|
|
|
|
|
|
|
|
This example creates a simple set of data to train on and then shows
|
|
|
|
you how to use the cross validation and rvm training functions
|
|
|
|
to find a good decision function that can classify examples in our
|
|
|
|
data set.
|
|
|
|
|
|
|
|
|
|
|
|
The data used in this example will be 2 dimensional data and will
|
|
|
|
come from a distribution where points with a distance less than 10
|
|
|
|
from the origin are labeled +1 and all other points are labeled
|
|
|
|
as -1.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include "dlib/svm.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dlib;
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// The rvm functions use column vectors to contain a lot of the data on which they they
|
|
|
|
// operate. So the first thing we do here is declare a convenient typedef.
|
|
|
|
|
|
|
|
// This typedef declares a matrix with 2 rows and 1 column. It will be the
|
|
|
|
// object that contains each of our 2 dimensional samples. (Note that if you wanted
|
|
|
|
// more than 2 features in this vector you can simply change the 2 to something else.
|
|
|
|
// Or if you don't know how many features you want until runtime then you can put a 0
|
|
|
|
// here and use the matrix.set_size() member function)
|
|
|
|
typedef matrix<double, 2, 1> sample_type;
|
|
|
|
|
|
|
|
// This is a typedef for the type of kernel we are going to use in this example.
|
|
|
|
// In this case I have selected the radial basis kernel that can operate on our
|
|
|
|
// 2D sample_type objects
|
|
|
|
typedef radial_basis_kernel<sample_type> kernel_type;
|
|
|
|
|
|
|
|
|
|
|
|
// Now we make objects to contain our samples and their respective labels.
|
|
|
|
std::vector<sample_type> samples;
|
|
|
|
std::vector<double> labels;
|
|
|
|
|
|
|
|
// Now lets put some data into our samples and labels objects. We do this
|
|
|
|
// by looping over a bunch of points and labeling them according to their
|
|
|
|
// distance from the origin.
|
|
|
|
for (int r = -20; r <= 20; ++r)
|
|
|
|
{
|
|
|
|
for (int c = -20; c <= 20; ++c)
|
|
|
|
{
|
|
|
|
sample_type samp;
|
|
|
|
samp(0) = r;
|
|
|
|
samp(1) = c;
|
|
|
|
samples.push_back(samp);
|
|
|
|
|
|
|
|
// if this point is less than 10 from the origin
|
|
|
|
if (sqrt((double)r*r + c*c) <= 10)
|
|
|
|
labels.push_back(+1);
|
|
|
|
else
|
|
|
|
labels.push_back(-1);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Here we normalize all the samples by subtracting their mean and dividing by their standard deviation.
|
|
|
|
// This is generally a good idea since it often heads off numerical stability problems and also
|
|
|
|
// prevents one large feature from smothering others. Doing this doesn't matter much in this example
|
|
|
|
// so I'm just doing this here so you can see an easy way to accomplish this with
|
|
|
|
// the library.
|
2008-10-13 10:22:53 +08:00
|
|
|
vector_normalizer<sample_type> normalizer;
|
|
|
|
// let the normalizer learn the mean and standard deviation of the samples
|
|
|
|
normalizer.train(samples);
|
2008-09-14 21:47:26 +08:00
|
|
|
// now normalize each sample
|
|
|
|
for (unsigned long i = 0; i < samples.size(); ++i)
|
2008-10-13 10:22:53 +08:00
|
|
|
samples[i] = normalizer(samples[i]);
|
|
|
|
|
2008-09-14 21:47:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Now that we have some data we want to train on it. However, there is a parameter to the
|
|
|
|
// training. This is the gamma parameter of the RBF kernel. Our choice for this parameter will
|
|
|
|
// influence how good the resulting decision function is. To test how good a particular choice of
|
|
|
|
// kernel parameters are we can use the cross_validate_trainer() function to perform n-fold cross
|
|
|
|
// validation on our training data. However, there is a problem with the way we have sampled
|
|
|
|
// our distribution. The problem is that there is a definite ordering to the samples.
|
|
|
|
// That is, the first half of the samples look like they are from a different distribution
|
2009-10-19 11:08:26 +08:00
|
|
|
// than the second half. This would screw up the cross validation process but we can
|
2008-09-14 21:47:26 +08:00
|
|
|
// fix it by randomizing the order of the samples with the following function call.
|
|
|
|
randomize_samples(samples, labels);
|
|
|
|
|
|
|
|
|
|
|
|
// here we make an instance of the rvm_trainer object that uses our kernel type.
|
|
|
|
rvm_trainer<kernel_type> trainer;
|
|
|
|
|
|
|
|
// Now we loop over some different gamma values to see how good they are. Note
|
2009-10-19 10:43:41 +08:00
|
|
|
// that this is a very simple way to try out a few possible parameter choices. You
|
|
|
|
// should look at the model_selection_ex.cpp program for examples of more sophisticated
|
|
|
|
// strategies for determining good parameter choices.
|
2008-09-14 21:47:26 +08:00
|
|
|
cout << "doing cross validation" << endl;
|
|
|
|
for (double gamma = 0.00001; gamma <= 1; gamma += 0.1)
|
|
|
|
{
|
|
|
|
// tell the trainer the parameters we want to use
|
|
|
|
trainer.set_kernel(kernel_type(gamma));
|
|
|
|
|
|
|
|
cout << "gamma: " << gamma;
|
|
|
|
// Print out the cross validation accuracy for 3-fold cross validation using the current gamma.
|
|
|
|
// cross_validate_trainer() returns a row vector. The first element of the vector is the fraction
|
|
|
|
// of +1 training examples correctly classified and the second number is the fraction of -1 training
|
|
|
|
// examples correctly classified.
|
|
|
|
cout << " cross validation accuracy: " << cross_validate_trainer(trainer, samples, labels, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// From looking at the output of the above loop it turns out that a good value for
|
|
|
|
// gamma for this problem is 0.1. So that is what we will use.
|
|
|
|
|
|
|
|
// Now we train on the full set of data and obtain the resulting decision function. We use the
|
|
|
|
// value of 0.1 for gamma. The decision function will return values >= 0 for samples it predicts
|
|
|
|
// are in the +1 class and numbers < 0 for samples it predicts to be in the -1 class.
|
|
|
|
trainer.set_kernel(kernel_type(0.1));
|
2008-10-13 10:22:53 +08:00
|
|
|
typedef decision_function<kernel_type> dec_funct_type;
|
|
|
|
typedef normalized_function<dec_funct_type> funct_type;
|
|
|
|
|
|
|
|
|
|
|
|
// Here we are making an instance of the normalized_function object. This object provides a convenient
|
|
|
|
// way to store the vector normalization information along with the decision function we are
|
|
|
|
// going to learn.
|
|
|
|
funct_type learned_function;
|
|
|
|
learned_function.normalizer = normalizer; // save normalization information
|
2008-12-13 04:04:14 +08:00
|
|
|
learned_function.function = trainer.train(samples, labels); // perform the actual RVM training and save the results
|
2008-09-14 21:47:26 +08:00
|
|
|
|
2009-11-30 02:59:24 +08:00
|
|
|
// print out the number of relevance vectors in the resulting decision function
|
|
|
|
cout << "\nnumber of relevance vectors in our learned_function is "
|
|
|
|
<< learned_function.function.basis_vectors.nr() << endl;
|
2008-09-14 21:47:26 +08:00
|
|
|
|
|
|
|
// now lets try this decision_function on some samples we haven't seen before
|
|
|
|
sample_type sample;
|
|
|
|
|
|
|
|
sample(0) = 3.123;
|
|
|
|
sample(1) = 2;
|
2008-10-13 10:22:53 +08:00
|
|
|
cout << "This sample should be >= 0 and it is classified as a " << learned_function(sample) << endl;
|
2008-09-14 21:47:26 +08:00
|
|
|
|
|
|
|
sample(0) = 3.123;
|
|
|
|
sample(1) = 9.3545;
|
2008-10-13 10:22:53 +08:00
|
|
|
cout << "This sample should be >= 0 and it is classified as a " << learned_function(sample) << endl;
|
2008-09-14 21:47:26 +08:00
|
|
|
|
|
|
|
sample(0) = 13.123;
|
|
|
|
sample(1) = 9.3545;
|
2008-10-13 10:22:53 +08:00
|
|
|
cout << "This sample should be < 0 and it is classified as a " << learned_function(sample) << endl;
|
2008-09-14 21:47:26 +08:00
|
|
|
|
|
|
|
sample(0) = 13.123;
|
|
|
|
sample(1) = 0;
|
2008-10-13 10:22:53 +08:00
|
|
|
cout << "This sample should be < 0 and it is classified as a " << learned_function(sample) << endl;
|
2008-09-14 21:47:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
// We can also train a decision function that reports a well conditioned probability
|
|
|
|
// instead of just a number > 0 for the +1 class and < 0 for the -1 class. An example
|
|
|
|
// of doing that follows:
|
2008-10-13 10:22:53 +08:00
|
|
|
typedef probabilistic_decision_function<kernel_type> probabilistic_funct_type;
|
|
|
|
typedef normalized_function<probabilistic_funct_type> pfunct_type;
|
|
|
|
|
|
|
|
pfunct_type learned_pfunct;
|
|
|
|
learned_pfunct.normalizer = normalizer;
|
|
|
|
learned_pfunct.function = train_probabilistic_decision_function(trainer, samples, labels, 3);
|
2008-09-14 21:47:26 +08:00
|
|
|
// Now we have a function that returns the probability that a given sample is of the +1 class.
|
|
|
|
|
2009-11-30 02:59:24 +08:00
|
|
|
// print out the number of relevance vectors in the resulting decision function.
|
2008-09-14 21:47:26 +08:00
|
|
|
// (it should be the same as in the one above)
|
2009-11-30 02:59:24 +08:00
|
|
|
cout << "\nnumber of relevance vectors in our learned_pfunct is "
|
|
|
|
<< learned_pfunct.function.decision_funct.basis_vectors.nr() << endl;
|
2008-09-14 21:47:26 +08:00
|
|
|
|
|
|
|
sample(0) = 3.123;
|
|
|
|
sample(1) = 2;
|
2009-03-15 04:40:31 +08:00
|
|
|
cout << "This +1 example should have high probability. Its probability is: " << learned_pfunct(sample) << endl;
|
2008-09-14 21:47:26 +08:00
|
|
|
|
|
|
|
sample(0) = 3.123;
|
|
|
|
sample(1) = 9.3545;
|
2009-03-15 04:40:31 +08:00
|
|
|
cout << "This +1 example should have high probability. Its probability is: " << learned_pfunct(sample) << endl;
|
2008-09-14 21:47:26 +08:00
|
|
|
|
|
|
|
sample(0) = 13.123;
|
|
|
|
sample(1) = 9.3545;
|
2009-03-15 04:40:31 +08:00
|
|
|
cout << "This -1 example should have low probability. Its probability is: " << learned_pfunct(sample) << endl;
|
2008-09-14 21:47:26 +08:00
|
|
|
|
|
|
|
sample(0) = 13.123;
|
|
|
|
sample(1) = 0;
|
2009-03-15 04:40:31 +08:00
|
|
|
cout << "This -1 example should have low probability. Its probability is: " << learned_pfunct(sample) << endl;
|
2008-09-14 21:47:26 +08:00
|
|
|
|
|
|
|
|
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 learned_pfunct object to disk and recall it later like so:
|
|
|
|
ofstream fout("saved_function.dat",ios::binary);
|
|
|
|
serialize(learned_pfunct,fout);
|
|
|
|
fout.close();
|
|
|
|
|
|
|
|
// now lets open that file back up and load the function object it contains
|
|
|
|
ifstream fin("saved_function.dat",ios::binary);
|
|
|
|
deserialize(learned_pfunct, fin);
|
|
|
|
|
|
|
|
|
2008-09-14 21:47:26 +08:00
|
|
|
}
|
|
|
|
|