2009-03-09 01:38:04 +08:00
|
|
|
// 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 dlib C++ library's
|
|
|
|
implementation of the pegasos algorithm for online training of support
|
|
|
|
vector machines.
|
|
|
|
|
|
|
|
This example creates a simple binary classification problem and shows
|
|
|
|
you how to train a support vector machine on that data.
|
|
|
|
|
|
|
|
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>
|
2009-03-09 02:36:06 +08:00
|
|
|
#include <ctime>
|
2009-03-09 01:38:04 +08:00
|
|
|
#include <vector>
|
2012-12-08 22:32:13 +08:00
|
|
|
#include <dlib/svm.h>
|
2009-03-09 01:38:04 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dlib;
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2010-04-02 20:56:57 +08:00
|
|
|
// The svm functions use column vectors to contain a lot of the data on which they
|
2009-03-09 01:38:04 +08:00
|
|
|
// 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;
|
|
|
|
|
|
|
|
|
|
|
|
// Here we create an instance of the pegasos svm trainer object we will be using.
|
|
|
|
svm_pegasos<kernel_type> trainer;
|
|
|
|
// Here we setup the parameters to this object. See the dlib documentation for a
|
|
|
|
// description of what these parameters are.
|
|
|
|
trainer.set_lambda(0.00001);
|
|
|
|
trainer.set_kernel(kernel_type(0.005));
|
2009-03-15 05:57:33 +08:00
|
|
|
|
|
|
|
// Set the maximum number of support vectors we want the trainer object to use
|
|
|
|
// in representing the decision function it is going to learn. In general,
|
|
|
|
// supplying a bigger number here will only ever give you a more accurate
|
|
|
|
// answer. However, giving a smaller number will make the algorithm run
|
|
|
|
// faster and decision rules that involve fewer support vectors also take
|
|
|
|
// less time to evaluate.
|
|
|
|
trainer.set_max_num_sv(10);
|
2009-03-09 01:38:04 +08:00
|
|
|
|
|
|
|
std::vector<sample_type> samples;
|
|
|
|
std::vector<double> labels;
|
|
|
|
|
|
|
|
// make an instance of a sample matrix so we can use it below
|
|
|
|
sample_type sample, center;
|
|
|
|
|
|
|
|
center = 20, 20;
|
|
|
|
|
|
|
|
// Now lets go into a loop and randomly generate 1000 samples.
|
|
|
|
srand(time(0));
|
2009-08-05 08:11:50 +08:00
|
|
|
for (int i = 0; i < 10000; ++i)
|
2009-03-09 01:38:04 +08:00
|
|
|
{
|
|
|
|
// Make a random sample vector.
|
|
|
|
sample = randm(2,1)*40 - center;
|
|
|
|
|
|
|
|
// Now if that random vector is less than 10 units from the origin then it is in
|
|
|
|
// the +1 class.
|
|
|
|
if (length(sample) <= 10)
|
|
|
|
{
|
|
|
|
// let the svm_pegasos learn about this sample
|
|
|
|
trainer.train(sample,+1);
|
|
|
|
|
|
|
|
// save this sample so we can use it with the batch training examples below
|
|
|
|
samples.push_back(sample);
|
2009-03-09 02:01:43 +08:00
|
|
|
labels.push_back(+1);
|
2009-03-09 01:38:04 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// let the svm_pegasos learn about this sample
|
|
|
|
trainer.train(sample,-1);
|
|
|
|
|
|
|
|
// save this sample so we can use it with the batch training examples below
|
|
|
|
samples.push_back(sample);
|
|
|
|
labels.push_back(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we have trained our SVM. Lets see how well it did.
|
2009-03-09 02:01:43 +08:00
|
|
|
// Each of these statements prints out the output of the SVM given a particular sample.
|
2009-03-09 01:38:04 +08:00
|
|
|
// The SVM outputs a number > 0 if a sample is predicted to be in the +1 class and < 0
|
|
|
|
// if a sample is predicted to be in the -1 class.
|
|
|
|
|
|
|
|
sample(0) = 3.123;
|
|
|
|
sample(1) = 4;
|
|
|
|
cout << "This is a +1 example, its SVM output is: " << trainer(sample) << endl;
|
|
|
|
|
|
|
|
sample(0) = 13.123;
|
|
|
|
sample(1) = 9.3545;
|
|
|
|
cout << "This is a -1 example, its SVM output is: " << trainer(sample) << endl;
|
|
|
|
|
|
|
|
sample(0) = 13.123;
|
|
|
|
sample(1) = 0;
|
|
|
|
cout << "This is a -1 example, its SVM output is: " << trainer(sample) << endl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-03-09 02:01:43 +08:00
|
|
|
// The previous part of this example program showed you how to perform online training
|
2009-03-09 01:38:04 +08:00
|
|
|
// with the pegasos algorithm. But it is often the case that you have a dataset and you
|
|
|
|
// just want to perform batch learning on that dataset and get the resulting decision
|
|
|
|
// function. To support this the dlib library provides functions for converting an online
|
|
|
|
// training object like svm_pegasos into a batch training object.
|
|
|
|
|
|
|
|
// First lets clear out anything in the trainer object.
|
|
|
|
trainer.clear();
|
|
|
|
|
|
|
|
// Now to begin with, you might want to compute the cross validation score of a trainer object
|
2009-08-05 08:10:23 +08:00
|
|
|
// on your data. To do this you should use the batch_cached() function to convert the svm_pegasos object
|
2009-08-30 22:10:42 +08:00
|
|
|
// into a batch training object. Note that the second argument to batch_cached() is the minimum
|
|
|
|
// learning rate the trainer object must report for the batch_cached() function to consider training
|
2009-03-09 01:38:04 +08:00
|
|
|
// complete. So smaller values of this parameter cause training to take longer but may result
|
|
|
|
// in a more accurate solution.
|
|
|
|
// Here we perform 4-fold cross validation and print the results
|
2009-08-05 08:10:23 +08:00
|
|
|
cout << "cross validation: " << cross_validate_trainer(batch_cached(trainer,0.1), samples, labels, 4);
|
2009-03-09 01:38:04 +08:00
|
|
|
|
2009-08-05 08:10:23 +08:00
|
|
|
// Here is an example of creating a decision function. Note that we have used the verbose_batch_cached()
|
|
|
|
// function instead of batch_cached() as above. They do the same things except verbose_batch_cached() will
|
2009-03-09 01:38:04 +08:00
|
|
|
// print status messages to standard output while training is under way.
|
2009-08-05 08:10:23 +08:00
|
|
|
decision_function<kernel_type> df = verbose_batch_cached(trainer,0.1).train(samples, labels);
|
2009-03-09 01:38:04 +08:00
|
|
|
|
|
|
|
// At this point we have obtained a decision function from the above batch mode training.
|
|
|
|
// Now we can use it on some test samples exactly as we did above.
|
|
|
|
|
|
|
|
sample(0) = 3.123;
|
|
|
|
sample(1) = 4;
|
|
|
|
cout << "This is a +1 example, its SVM output is: " << df(sample) << endl;
|
|
|
|
|
|
|
|
sample(0) = 13.123;
|
|
|
|
sample(1) = 9.3545;
|
|
|
|
cout << "This is a -1 example, its SVM output is: " << df(sample) << endl;
|
|
|
|
|
|
|
|
sample(0) = 13.123;
|
|
|
|
sample(1) = 0;
|
|
|
|
cout << "This is a -1 example, its SVM output is: " << df(sample) << endl;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|