Commit 46bb6dc8 authored by Davis King's avatar Davis King

Added code showing how to get the individual decision functions out of a

multiclass decision function object.
parent 1e67beb7
...@@ -42,6 +42,8 @@ void generate_data ( ...@@ -42,6 +42,8 @@ void generate_data (
int main() int main()
{ {
try
{
std::vector<sample_type> samples; std::vector<sample_type> samples;
std::vector<double> labels; std::vector<double> labels;
...@@ -125,7 +127,7 @@ int main() ...@@ -125,7 +127,7 @@ int main()
*/ */
// Finally, if you want to save a one_vs_one_decision_function to disk, you can do // If you want to save a one_vs_one_decision_function to disk, you can do
// so. However, you must declare what kind of decision functions it contains. // so. However, you must declare what kind of decision functions it contains.
one_vs_one_decision_function<ovo_trainer, one_vs_one_decision_function<ovo_trainer,
decision_function<poly_kernel>, // This is the output of the poly_trainer decision_function<poly_kernel>, // This is the output of the poly_trainer
...@@ -152,6 +154,33 @@ int main() ...@@ -152,6 +154,33 @@ int main()
// Test df3 on the samples and labels and print the confusion matrix. // Test df3 on the samples and labels and print the confusion matrix.
cout << "test deserialized function: \n" << test_multiclass_decision_function(df3, samples, labels) << endl; cout << "test deserialized function: \n" << test_multiclass_decision_function(df3, samples, labels) << endl;
// Finally, if you want to get the binary classifiers from inside a multiclass decision
// function you can do it by calling get_binary_decision_functions() like so:
one_vs_one_decision_function<ovo_trainer>::binary_function_table functs;
functs = df.get_binary_decision_functions();
cout << "number of binary decision functions in df: " << functs.size() << endl;
// The functs object is a std::map which maps pairs of labels to binary decision
// functions. So we can access the individual decision functions like so:
decision_function<poly_kernel> df_1_2 = any_cast<decision_function<poly_kernel> >(functs[make_unordered_pair(1,2)]);
decision_function<rbf_kernel> df_1_3 = any_cast<decision_function<rbf_kernel> >(functs[make_unordered_pair(1,3)]);
// df_1_2 contains the binary decision function that votes for class 1 vs. 2.
// Similarly, df_1_3 contains the classifier that votes for 1 vs. 3.
// Note that the multiclass decision function doesn't know what kind of binary
// decision functions it contains. So we have to use any_cast to explicitly cast
// them back into the concrete type. If you make a mistake and try to any_cast a
// binary decision function into the wrong type of function any_cast will throw a
// bad_any_cast exception.
}
catch (std::exception& e)
{
cout << "exception thrown!" << endl;
cout << e.what() << endl;
}
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment