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

Function predict_label

examples/svm_struct_ex.cpp:81–114  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

79// ----------------------------------------------------------------------------------------
80
81int predict_label (
82 const column_vector& weights,
83 const sample_type& sample
84)
85/*!
86 requires
87 - weights.size() == 9
88 - sample.size() == 3
89 ensures
90 - Given the 9-dimensional weight vector which defines a 3 class classifier, this
91 function predicts the class of the given 3-dimensional sample vector.
92 Therefore, the output of this function is either 0, 1, or 2 (i.e. one of the
93 three possible labels).
94!*/
95{
96 // Our 3-class classifier model can be thought of as containing 3 separate linear
97 // classifiers. So to predict the class of a sample vector we evaluate each of these
98 // three classifiers and then whatever classifier has the largest output "wins" and
99 // predicts the label of the sample. This is the popular one-vs-all multi-class
100 // classifier model.
101 //
102 // Keeping this in mind, the code below simply pulls the three separate weight vectors
103 // out of weights and then evaluates each against sample. The individual classifier
104 // scores are stored in scores and the highest scoring index is returned as the label.
105 column_vector w0, w1, w2;
106 w0 = rowm(weights, range(0,2));
107 w1 = rowm(weights, range(3,5));
108 w2 = rowm(weights, range(6,8));
109
110 column_vector scores(3);
111 scores = dot(w0, sample), dot(w1, sample), dot(w2, sample);
112
113 return index_of_max(scores);
114}
115
116// ----------------------------------------------------------------------------------------
117// ----------------------------------------------------------------------------------------

Callers 1

mainFunction · 0.70

Calls 4

index_of_maxFunction · 0.85
rowmFunction · 0.50
rangeFunction · 0.50
dotFunction · 0.50

Tested by

no test coverage detected