| 26 | |
| 27 | |
| 28 | int main() |
| 29 | { |
| 30 | // The svm functions use column vectors to contain a lot of the data on which they |
| 31 | // operate. So the first thing we do here is declare a convenient typedef. |
| 32 | |
| 33 | // This typedef declares a matrix with 2 rows and 1 column. It will be the |
| 34 | // object that contains each of our 2 dimensional samples. (Note that if you wanted |
| 35 | // more than 2 features in this vector you can simply change the 2 to something else. |
| 36 | // Or if you don't know how many features you want until runtime then you can put a 0 |
| 37 | // here and use the matrix.set_size() member function) |
| 38 | typedef matrix<double, 2, 1> sample_type; |
| 39 | |
| 40 | |
| 41 | // This is a typedef for the type of kernel we are going to use in this example. |
| 42 | // In this case I have selected the radial basis kernel that can operate on our |
| 43 | // 2D sample_type objects |
| 44 | typedef radial_basis_kernel<sample_type> kernel_type; |
| 45 | |
| 46 | |
| 47 | // Here we create an instance of the pegasos svm trainer object we will be using. |
| 48 | svm_pegasos<kernel_type> trainer; |
| 49 | // Here we setup the parameters to this object. See the dlib documentation for a |
| 50 | // description of what these parameters are. |
| 51 | trainer.set_lambda(0.00001); |
| 52 | trainer.set_kernel(kernel_type(0.005)); |
| 53 | |
| 54 | // Set the maximum number of support vectors we want the trainer object to use |
| 55 | // in representing the decision function it is going to learn. In general, |
| 56 | // supplying a bigger number here will only ever give you a more accurate |
| 57 | // answer. However, giving a smaller number will make the algorithm run |
| 58 | // faster and decision rules that involve fewer support vectors also take |
| 59 | // less time to evaluate. |
| 60 | trainer.set_max_num_sv(10); |
| 61 | |
| 62 | std::vector<sample_type> samples; |
| 63 | std::vector<double> labels; |
| 64 | |
| 65 | // make an instance of a sample matrix so we can use it below |
| 66 | sample_type sample, center; |
| 67 | |
| 68 | center = 20, 20; |
| 69 | |
| 70 | // Now let's go into a loop and randomly generate 1000 samples. |
| 71 | srand(time(0)); |
| 72 | for (int i = 0; i < 10000; ++i) |
| 73 | { |
| 74 | // Make a random sample vector. |
| 75 | sample = randm(2,1)*40 - center; |
| 76 | |
| 77 | // Now if that random vector is less than 10 units from the origin then it is in |
| 78 | // the +1 class. |
| 79 | if (length(sample) <= 10) |
| 80 | { |
| 81 | // let the svm_pegasos learn about this sample |
| 82 | trainer.train(sample,+1); |
| 83 | |
| 84 | // save this sample so we can use it with the batch training examples below |
| 85 | samples.push_back(sample); |
nothing calls this directly
no test coverage detected