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

Class simple_custom_trainer

examples/custom_trainer_ex.cpp:77–131  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

75// ----------------------------------------------------------------------------------------
76
77class simple_custom_trainer
78{
79 /*!
80 WHAT THIS OBJECT REPRESENTS
81 This is our example custom binary classifier trainer object. It simply
82 computes the means of the +1 and -1 classes, puts them into our
83 custom_decision_function, and returns the results.
84
85 Below we define the train() function. I have also included the
86 requires/ensures definition for a generic binary classifier's train()
87 !*/
88public:
89
90
91 custom_decision_function train (
92 const std::vector<sample_type>& samples,
93 const std::vector<double>& labels
94 ) const
95 /*!
96 requires
97 - is_binary_classification_problem(samples, labels) == true
98 (e.g. labels consists of only +1 and -1 values, samples.size() == labels.size())
99 ensures
100 - returns a decision function F with the following properties:
101 - if (new_x is a sample predicted have +1 label) then
102 - F(new_x) >= 0
103 - else
104 - F(new_x) < 0
105 !*/
106 {
107 sample_type positive_center, negative_center;
108
109 // compute sums of each class
110 positive_center = 0;
111 negative_center = 0;
112 for (unsigned long i = 0; i < samples.size(); ++i)
113 {
114 if (labels[i] == +1)
115 positive_center += samples[i];
116 else // this is a -1 sample
117 negative_center += samples[i];
118 }
119
120 // divide by number of +1 samples
121 positive_center /= sum(mat(labels) == +1);
122 // divide by number of -1 samples
123 negative_center /= sum(mat(labels) == -1);
124
125 custom_decision_function df;
126 df.positive_center = positive_center;
127 df.negative_center = negative_center;
128
129 return df;
130 }
131};
132
133// ----------------------------------------------------------------------------------------
134

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected