MCPcopy Create free account
hub / github.com/davisking/dlib / main

Function main

examples/svm_sparse_ex.cpp:24–119  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22
23
24int main()
25{
26 // In this example program we will be dealing with feature vectors that are sparse (i.e. most
27 // of the values in each vector are zero). So rather than using a dlib::matrix we can use
28 // one of the containers from the STL to represent our sample vectors. In particular, we
29 // can use the std::map to represent sparse vectors. (Note that you don't have to use std::map.
30 // Any STL container of std::pair objects that is sorted can be used. So for example, you could
31 // use a std::vector<std::pair<unsigned long,double> > here so long as you took care to sort every vector)
32 typedef std::map<unsigned long,double> sample_type;
33
34
35 // This is a typedef for the type of kernel we are going to use in this example.
36 // Since our data is linearly separable I picked the linear kernel. Note that if you
37 // are using a sparse vector representation like std::map then you have to use a kernel
38 // meant to be used with that kind of data type.
39 typedef sparse_linear_kernel<sample_type> kernel_type;
40
41
42 // Here we create an instance of the pegasos svm trainer object we will be using.
43 svm_pegasos<kernel_type> trainer;
44 // Here we setup a parameter to this object. See the dlib documentation for a
45 // description of what this parameter does.
46 trainer.set_lambda(0.00001);
47
48 // Let's also use the svm trainer specially optimized for the linear_kernel and
49 // sparse_linear_kernel.
50 svm_c_linear_trainer<kernel_type> linear_trainer;
51 // This trainer solves the "C" formulation of the SVM. See the documentation for
52 // details.
53 linear_trainer.set_c(10);
54
55 std::vector<sample_type> samples;
56 std::vector<double> labels;
57
58 // make an instance of a sample vector so we can use it below
59 sample_type sample;
60
61
62 // Now let's go into a loop and randomly generate 10000 samples.
63 srand(time(0));
64 double label = +1;
65 for (int i = 0; i < 10000; ++i)
66 {
67 // flip this flag
68 label *= -1;
69
70 sample.clear();
71
72 // now make a random sparse sample with at most 10 non-zero elements
73 for (int j = 0; j < 10; ++j)
74 {
75 int idx = std::rand()%100;
76 double value = static_cast<double>(std::rand())/RAND_MAX;
77
78 sample[idx] = label*value;
79 }
80
81 // let the svm_pegasos learn about this sample.

Callers

nothing calls this directly

Calls 6

randClass · 0.50
set_lambdaMethod · 0.45
set_cMethod · 0.45
clearMethod · 0.45
trainMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected