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

Function main

examples/svm_struct_ex.cpp:42–77  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

40// ----------------------------------------------------------------------------------------
41
42int main()
43{
44 // In this example, we have three types of samples: class 0, 1, or 2. That is, each of
45 // our sample vectors falls into one of three classes. To keep this example very
46 // simple, each sample vector is zero everywhere except at one place. The non-zero
47 // dimension of each vector determines the class of the vector. So for example, the
48 // first element of samples has a class of 1 because samples[0](1) is the only non-zero
49 // element of samples[0].
50 sample_type samp(3);
51 std::vector<sample_type> samples;
52 samp = 0,2,0; samples.push_back(samp);
53 samp = 1,0,0; samples.push_back(samp);
54 samp = 0,4,0; samples.push_back(samp);
55 samp = 0,0,3; samples.push_back(samp);
56 // Since we want to use a machine learning method to learn a 3-class classifier we need
57 // to record the labels of our samples. Here samples[i] has a class label of labels[i].
58 std::vector<int> labels;
59 labels.push_back(1);
60 labels.push_back(0);
61 labels.push_back(1);
62 labels.push_back(2);
63
64
65 // Now that we have some training data we can tell the structural SVM to learn the
66 // parameters of our 3-class classifier model. The details of this will be explained
67 // later. For now, just note that it finds the weights (i.e. a vector of real valued
68 // parameters) such that predict_label(weights, sample) always returns the correct
69 // label for a sample vector.
70 column_vector weights = train_three_class_classifier(samples, labels);
71
72 // Print the weights and then evaluate predict_label() on each of our training samples.
73 // Note that the correct label is predicted for each sample.
74 cout << weights << endl;
75 for (unsigned long i = 0; i < samples.size(); ++i)
76 cout << "predicted label for sample["<<i<<"]: " << predict_label(weights, samples[i]) << endl;
77}
78
79// ----------------------------------------------------------------------------------------
80

Callers

nothing calls this directly

Calls 4

predict_labelFunction · 0.70
push_backMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected