OneVersusOne.cpp
Go to the documentation of this file.
1//===========================================================================
2/*!
3 *
4 *
5 * \brief Example program for the one-versus-one classifier based on SVMs
6 *
7 *
8 *
9 * \author T. Glasmachers
10 * \date 2012
11 *
12 *
13 * \par Copyright 1995-2017 Shark Development Team
14 *
15 * <BR><HR>
16 * This file is part of Shark.
17 * <https://shark-ml.github.io/Shark/>
18 *
19 * Shark is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU Lesser General Public License as published
21 * by the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
23 *
24 * Shark is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU Lesser General Public License for more details.
28 *
29 * You should have received a copy of the GNU Lesser General Public License
30 * along with Shark. If not, see <http://www.gnu.org/licenses/>.
31 *
32 */
33//===========================================================================
34
35
36#include <shark/Core/Random.h>
42#include <iostream>
43
44
45using namespace shark;
46using namespace std;
47
48
49// data generating distribution for our toy
50// multi-category classification problem
51/// @cond EXAMPLE_SYMBOLS
52class Problem : public LabeledDataDistribution<RealVector, unsigned int>
53{
54public:
55 void draw(RealVector& input, unsigned int& label)const
56 {
58 input.resize(1);
59 input(0) = random::gauss(random::globalRng) + 3.0 * label;
60 }
61};
62/// @endcond
63
64int main()
65{
66 // experiment settings
67 unsigned int classes = 5;
68 std::size_t ell = 100;
69 std::size_t tests = 10000;
70 double C = 10.0;
71 double gamma = 0.5;
72
73 // generate a very simple dataset with a little noise
74 Problem problem;
75 ClassificationDataset training = problem.generateDataset(ell);
76 ClassificationDataset test = problem.generateDataset(tests);
77 repartitionByClass(training);
78
79 // kernel function
80 GaussianRbfKernel<> kernel(gamma);
81 // train the OVO machine
83 unsigned int pairs = classes * (classes - 1) / 2;
84 std::vector< KernelClassifier<RealVector> > bin_svm(pairs);
85 for (std::size_t n=0, c=1; c<classes; c++)
86 {
87 std::vector< OneVersusOneClassifier<RealVector>::binary_classifier_type* > vs_c;
88 for (std::size_t e=0; e<c; e++, n++)
89 {
90 //get the binary subproblem
91 ClassificationDataset bindata = binarySubProblem(training,e,c);
92
93 // train the binary machine
94 CSvmTrainer<RealVector> trainer(&kernel, C,false);
95 trainer.train(bin_svm[n], bindata);
96 vs_c.push_back(&bin_svm[n]);
97 }
98 ovo.addClass(vs_c);
99 }
100
101 // compute errors
103 Data<unsigned int> output = ovo(training.inputs());
104 double train_error = loss.eval(training.labels(), output);
105 output = ovo(test.inputs());
106 double test_error = loss.eval(test.labels(), output);
107 cout << "training error: " << 100.0 * train_error << "%" << endl;
108 cout << " test error: " << 100.0 * test_error << "%" << endl;
109}