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

Function main

examples/sequence_labeler_ex.cpp:204–299  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

202// ----------------------------------------------------------------------------------------
203
204int main()
205{
206 // We need a dataset to test the machine learning algorithms. So we are going to
207 // define a HMM based on the following two matrices and then randomly sample a
208 // set of data from it. Then we will see if the machine learning method can
209 // recover the HMM model from the training data.
210
211
212 matrix<double> transition_probabilities(num_label_states, num_label_states);
213 transition_probabilities = 0.05, 0.90, 0.05,
214 0.05, 0.05, 0.90,
215 0.90, 0.05, 0.05;
216
217 matrix<double> emission_probabilities(num_label_states,num_sample_states);
218 emission_probabilities = 0.5, 0.5, 0.0,
219 0.0, 0.5, 0.5,
220 0.5, 0.0, 0.5;
221
222 std::vector<std::vector<unsigned long> > samples;
223 std::vector<std::vector<unsigned long> > labels;
224 // sample 1000 labeled sequences from the HMM.
225 make_dataset(transition_probabilities,emission_probabilities,
226 samples, labels, 1000);
227
228 // print out some of the randomly sampled sequences
229 for (int i = 0; i < 10; ++i)
230 {
231 cout << "hidden states: " << trans(mat(labels[i]));
232 cout << "observed states: " << trans(mat(samples[i]));
233 cout << "******************************" << endl;
234 }
235
236 // Next we use the structural_sequence_labeling_trainer to learn our
237 // prediction model based on just the samples and labels.
238 structural_sequence_labeling_trainer<feature_extractor> trainer;
239 // This is the common SVM C parameter. Larger values encourage the
240 // trainer to attempt to fit the data exactly but might overfit.
241 // In general, you determine this parameter by cross-validation.
242 trainer.set_c(4);
243 // This trainer can use multiple CPU cores to speed up the training.
244 // So set this to the number of available CPU cores.
245 trainer.set_num_threads(4);
246
247
248 // Learn to do sequence labeling from the dataset
249 sequence_labeler<feature_extractor> labeler = trainer.train(samples, labels);
250
251 // Test the learned labeler on one of the training samples. In this
252 // case it will give the correct sequence of labels.
253 std::vector<unsigned long> predicted_labels = labeler(samples[0]);
254 cout << "true hidden states: "<< trans(mat(labels[0]));
255 cout << "predicted hidden states: "<< trans(mat(predicted_labels));
256
257
258
259 // We can also do cross-validation. The confusion_matrix is defined as:
260 // - confusion_matrix(T,P) == the number of times a sequence element with label T
261 // was predicted to have a label of P.

Callers

nothing calls this directly

Calls 15

logFunction · 0.85
join_colsFunction · 0.85
reshape_to_column_vectorFunction · 0.85
test_sequence_labelerFunction · 0.85
make_datasetFunction · 0.70
serializeFunction · 0.70
deserializeFunction · 0.70
transFunction · 0.50
matFunction · 0.50
sumFunction · 0.50
diagFunction · 0.50

Tested by

no test coverage detected