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-02 22:19:38 +08:00
|
|
|
/*
|
|
|
|
|
|
|
|
This is an example illustrating the use of the support 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 svm 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>
|
2012-12-08 22:32:13 +08:00
|
|
|
#include <dlib/svm.h>
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dlib;
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2013-01-18 08:20:38 +08:00
|
|
|
// The svm functions use column vectors to contain a lot of the data on which they
|
2008-07-07 01:25:50 +08:00
|
|
|
// operate. So the first thing we do here is declare a convenient typedef.
|
2008-05-02 22:19:38 +08:00
|
|
|
|
2013-01-18 08:20:38 +08:00
|
|
|
// 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)
|
2008-05-02 22:19:38 +08:00
|
|
|
typedef matrix<double, 2, 1> sample_type;
|
|
|
|
|
2013-01-18 08:20:38 +08:00
|
|
|
// 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
|
2008-05-02 22:19:38 +08:00
|
|
|
typedef radial_basis_kernel<sample_type> kernel_type;
|
|
|
|
|
|
|
|
|
2008-07-07 01:25:50 +08:00
|
|
|
// Now we make objects to contain our samples and their respective labels.
|
|
|
|
std::vector<sample_type> samples;
|
|
|
|
std::vector<double> labels;
|
2008-05-02 22:19:38 +08:00
|
|
|
|
2014-02-23 05:07:17 +08:00
|
|
|
// Now let's put some data into our samples and labels objects. We do this by looping
|
2013-01-18 08:20:38 +08:00
|
|
|
// over a bunch of points and labeling them according to their distance from the
|
|
|
|
// origin.
|
2008-05-02 22:19:38 +08:00
|
|
|
for (int r = -20; r <= 20; ++r)
|
|
|
|
{
|
|
|
|
for (int c = -20; c <= 20; ++c)
|
|
|
|
{
|
2008-07-07 01:25:50 +08:00
|
|
|
sample_type samp;
|
|
|
|
samp(0) = r;
|
|
|
|
samp(1) = c;
|
|
|
|
samples.push_back(samp);
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
// if this point is less than 10 from the origin
|
|
|
|
if (sqrt((double)r*r + c*c) <= 10)
|
2008-07-07 01:25:50 +08:00
|
|
|
labels.push_back(+1);
|
2008-05-02 22:19:38 +08:00
|
|
|
else
|
2008-07-07 01:25:50 +08:00
|
|
|
labels.push_back(-1);
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-18 08:20:38 +08:00
|
|
|
// 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 06:35:32 +08:00
|
|
|
vector_normalizer<sample_type> normalizer;
|
|
|
|
// let the normalizer learn the mean and standard deviation of the samples
|
|
|
|
normalizer.train(samples);
|
2008-07-07 01:25:50 +08:00
|
|
|
// now normalize each sample
|
|
|
|
for (unsigned long i = 0; i < samples.size(); ++i)
|
2008-10-13 06:35:32 +08:00
|
|
|
samples[i] = normalizer(samples[i]);
|
2008-07-07 01:25:50 +08:00
|
|
|
|
|
|
|
|
2013-01-18 08:20:38 +08:00
|
|
|
// Now that we have some data we want to train on it. However, there are two
|
|
|
|
// parameters to the training. These are the nu and gamma parameters. Our choice for
|
|
|
|
// these parameters will influence how good the resulting decision function is. To
|
|
|
|
// test how good a particular choice of these parameters is 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
|
|
|
|
// above. 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 than
|
|
|
|
// the second half. This would screw up the cross validation process but we can fix it
|
|
|
|
// by randomizing the order of the samples with the following function call.
|
2008-05-02 22:19:38 +08:00
|
|
|
randomize_samples(samples, labels);
|
|
|
|
|
|
|
|
|
2013-01-18 08:20:38 +08:00
|
|
|
// The nu parameter has a maximum value that is dependent on the ratio of the +1 to -1
|
2008-05-02 22:19:38 +08:00
|
|
|
// labels in the training data. This function finds that value.
|
|
|
|
const double max_nu = maximum_nu(labels);
|
|
|
|
|
2008-07-07 01:25:50 +08:00
|
|
|
// here we make an instance of the svm_nu_trainer object that uses our kernel type.
|
|
|
|
svm_nu_trainer<kernel_type> trainer;
|
|
|
|
|
2008-05-02 22:19:38 +08:00
|
|
|
// Now we loop over some different nu and gamma values to see how good they are. Note
|
2013-01-18 08:20:38 +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
|
2009-10-19 10:43:41 +08:00
|
|
|
// strategies for determining good parameter choices.
|
2008-05-02 22:19:38 +08:00
|
|
|
cout << "doing cross validation" << endl;
|
2010-12-23 04:37:31 +08:00
|
|
|
for (double gamma = 0.00001; gamma <= 1; gamma *= 5)
|
2008-05-02 22:19:38 +08:00
|
|
|
{
|
2010-12-23 04:37:31 +08:00
|
|
|
for (double nu = 0.00001; nu < max_nu; nu *= 5)
|
2008-05-02 22:19:38 +08:00
|
|
|
{
|
2008-07-07 01:25:50 +08:00
|
|
|
// tell the trainer the parameters we want to use
|
|
|
|
trainer.set_kernel(kernel_type(gamma));
|
|
|
|
trainer.set_nu(nu);
|
|
|
|
|
2008-05-02 22:19:38 +08:00
|
|
|
cout << "gamma: " << gamma << " nu: " << nu;
|
2013-01-18 08:20:38 +08:00
|
|
|
// Print out the cross validation accuracy for 3-fold cross validation using
|
|
|
|
// the current gamma and nu. 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
|
2008-05-02 22:19:38 +08:00
|
|
|
// examples correctly classified.
|
2008-07-07 01:25:50 +08:00
|
|
|
cout << " cross validation accuracy: " << cross_validate_trainer(trainer, samples, labels, 3);
|
2008-05-02 22:19:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-18 08:20:38 +08:00
|
|
|
// From looking at the output of the above loop it turns out that a good value for nu
|
|
|
|
// and gamma for this problem is 0.15625 for both. So that is what we will use.
|
2008-05-02 22:19:38 +08:00
|
|
|
|
2013-01-18 08:20:38 +08:00
|
|
|
// Now we train on the full set of data and obtain the resulting decision function. We
|
|
|
|
// use the value of 0.15625 for nu and 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.
|
2010-12-23 04:37:31 +08:00
|
|
|
trainer.set_kernel(kernel_type(0.15625));
|
|
|
|
trainer.set_nu(0.15625);
|
2008-10-13 06:35:32 +08:00
|
|
|
typedef decision_function<kernel_type> dec_funct_type;
|
|
|
|
typedef normalized_function<dec_funct_type> funct_type;
|
|
|
|
|
2013-01-18 08:20:38 +08:00
|
|
|
// 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.
|
2008-10-13 06:35:32 +08:00
|
|
|
funct_type learned_function;
|
|
|
|
learned_function.normalizer = normalizer; // save normalization information
|
|
|
|
learned_function.function = trainer.train(samples, labels); // perform the actual SVM training and save the results
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
// print out the number of support vectors in the resulting decision function
|
2008-10-13 06:35:32 +08:00
|
|
|
cout << "\nnumber of support vectors in our learned_function is "
|
2013-01-18 08:18:35 +08:00
|
|
|
<< learned_function.function.basis_vectors.size() << endl;
|
2008-05-02 22:19:38 +08:00
|
|
|
|
2014-02-23 05:07:17 +08:00
|
|
|
// Now let's try this decision_function on some samples we haven't seen before.
|
2008-05-02 22:19:38 +08:00
|
|
|
sample_type sample;
|
|
|
|
|
|
|
|
sample(0) = 3.123;
|
|
|
|
sample(1) = 2;
|
2013-04-13 08:52:05 +08:00
|
|
|
cout << "This is a +1 class example, the classifier output is " << learned_function(sample) << endl;
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
sample(0) = 3.123;
|
|
|
|
sample(1) = 9.3545;
|
2013-04-13 08:52:05 +08:00
|
|
|
cout << "This is a +1 class example, the classifier output is " << learned_function(sample) << endl;
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
sample(0) = 13.123;
|
|
|
|
sample(1) = 9.3545;
|
2013-04-13 08:52:05 +08:00
|
|
|
cout << "This is a -1 class example, the classifier output is " << learned_function(sample) << endl;
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
sample(0) = 13.123;
|
|
|
|
sample(1) = 0;
|
2013-04-13 08:52:05 +08:00
|
|
|
cout << "This is a -1 class example, the classifier output is " << learned_function(sample) << endl;
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
|
2013-01-18 08:20:38 +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
|
2008-07-07 01:25:50 +08:00
|
|
|
// of doing that follows:
|
2008-10-13 06:35:32 +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-05-11 02:53:10 +08:00
|
|
|
// Now we have a function that returns the probability that a given sample is of the +1 class.
|
2008-05-02 22:19:38 +08:00
|
|
|
|
2008-07-07 01:25:50 +08:00
|
|
|
// print out the number of support vectors in the resulting decision function.
|
|
|
|
// (it should be the same as in the one above)
|
2008-10-13 06:35:32 +08:00
|
|
|
cout << "\nnumber of support vectors in our learned_pfunct is "
|
2013-01-18 08:18:35 +08:00
|
|
|
<< learned_pfunct.function.decision_funct.basis_vectors.size() << endl;
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
sample(0) = 3.123;
|
|
|
|
sample(1) = 2;
|
2013-04-13 08:52:05 +08:00
|
|
|
cout << "This +1 class example should have high probability. Its probability is: "
|
|
|
|
<< learned_pfunct(sample) << endl;
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
sample(0) = 3.123;
|
|
|
|
sample(1) = 9.3545;
|
2013-04-13 08:52:05 +08:00
|
|
|
cout << "This +1 class example should have high probability. Its probability is: "
|
|
|
|
<< learned_pfunct(sample) << endl;
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
sample(0) = 13.123;
|
|
|
|
sample(1) = 9.3545;
|
2013-04-13 08:52:05 +08:00
|
|
|
cout << "This -1 class example should have low probability. Its probability is: "
|
|
|
|
<< learned_pfunct(sample) << endl;
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
sample(0) = 13.123;
|
|
|
|
sample(1) = 0;
|
2013-04-13 08:52:05 +08:00
|
|
|
cout << "This -1 class example should have low probability. Its probability is: "
|
|
|
|
<< learned_pfunct(sample) << endl;
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
|
2008-07-25 11:28:36 +08:00
|
|
|
|
2013-01-18 08:20:38 +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:
|
2008-11-01 02:12:18 +08:00
|
|
|
ofstream fout("saved_function.dat",ios::binary);
|
|
|
|
serialize(learned_pfunct,fout);
|
|
|
|
fout.close();
|
|
|
|
|
2014-02-23 05:07:17 +08:00
|
|
|
// Now let's open that file back up and load the function object it contains.
|
2008-11-01 02:12:18 +08:00
|
|
|
ifstream fin("saved_function.dat",ios::binary);
|
|
|
|
deserialize(learned_pfunct, fin);
|
|
|
|
|
2013-01-18 08:20:38 +08:00
|
|
|
// Note that there is also an example program that comes with dlib called the
|
|
|
|
// file_to_code_ex.cpp example. It is a simple program that takes a file and outputs a
|
|
|
|
// piece of C++ code that is able to fully reproduce the file's contents in the form of
|
|
|
|
// a std::string object. So you can use that along with the std::istringstream to save
|
|
|
|
// learned decision functions inside your actual C++ code files if you want.
|
2008-11-01 02:12:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-25 11:28:36 +08:00
|
|
|
|
2013-01-18 08:20:38 +08:00
|
|
|
// Lastly, note that the decision functions we trained above involved well over 200
|
2009-12-06 04:01:22 +08:00
|
|
|
// basis vectors. Support vector machines in general tend to find decision functions
|
2013-01-18 08:20:38 +08:00
|
|
|
// that involve a lot of basis vectors. This is significant because the more basis
|
|
|
|
// vectors in a decision function, the longer it takes to classify new examples. So
|
|
|
|
// dlib provides the ability to find an approximation to the normal output of a trainer
|
|
|
|
// using fewer basis vectors.
|
|
|
|
|
|
|
|
// Here we determine the cross validation accuracy when we approximate the output using
|
|
|
|
// only 10 basis vectors. To do this we use the reduced2() function. It takes a
|
|
|
|
// trainer object and the number of basis vectors to use and returns a new trainer
|
|
|
|
// object that applies the necessary post processing during the creation of decision
|
|
|
|
// function objects.
|
2008-07-25 11:28:36 +08:00
|
|
|
cout << "\ncross validation accuracy with only 10 support vectors: "
|
|
|
|
<< cross_validate_trainer(reduced2(trainer,10), samples, labels, 3);
|
|
|
|
|
2014-02-23 05:07:17 +08:00
|
|
|
// Let's print out the original cross validation score too for comparison.
|
2008-07-25 11:28:36 +08:00
|
|
|
cout << "cross validation accuracy with all the original support vectors: "
|
|
|
|
<< cross_validate_trainer(trainer, samples, labels, 3);
|
|
|
|
|
2013-01-18 08:20:38 +08:00
|
|
|
// When you run this program you should see that, for this problem, you can reduce the
|
|
|
|
// number of basis vectors down to 10 without hurting the cross validation accuracy.
|
2008-07-25 11:28:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
// To get the reduced decision function out we would just do this:
|
2008-10-13 06:35:32 +08:00
|
|
|
learned_function.function = reduced2(trainer,10).train(samples, labels);
|
2008-07-25 11:28:36 +08:00
|
|
|
// And similarly for the probabilistic_decision_function:
|
2008-10-13 06:35:32 +08:00
|
|
|
learned_pfunct.function = train_probabilistic_decision_function(reduced2(trainer,10), samples, labels, 3);
|
2008-05-02 22:19:38 +08:00
|
|
|
}
|
|
|
|
|