McSvmLinear.cpp
Go to the documentation of this file.
1 
2 #include <shark/Data/Dataset.h>
8 
9 using namespace shark;
10 
11 
12 double const noise = 1.0;
13 typedef CompressedRealVector VectorType;
14 typedef CompressedRealMatrix MatrixType;
15 typedef CompressedRealMatrixRow RowType;
16 
17 
18 // data generating distribution for our toy
19 // multi-category classification problem
20 /// @cond EXAMPLE_SYMBOLS
21 class Problem : public LabeledDataDistribution<VectorType, unsigned int>
22 {
23 public:
24  void draw(VectorType& input, unsigned int& label)const
25  {
26  label = Rng::discrete(0, 4);
27  input.resize(1000002);
28  input(1000000) = noise * Rng::gauss() + 3.0 * std::cos((double)label);
29  input(1000001) = noise * Rng::gauss() + 3.0 * std::sin((double)label);
30  }
31 };
32 /// @endcond
33 
34 
35 int main(int argc, char** argv)
36 {
37  if (argc != 4)
38  {
39  std::cout << "required parameters: ell C epsilon" << std::endl;
40  return 1;
41  }
42 
43  // experiment settings
44  unsigned int ell = std::atoi(argv[1]);
45  double C = std::atof(argv[2]);
46  double epsilon = std::atof(argv[3]);
47  unsigned int tests = 10000;
48  std::cout << "ell=" << ell << std::endl;
49  std::cout << "C=" << C << std::endl;
50  std::cout << "epsilon=" << epsilon << std::endl;
51 
52  // generate a very simple dataset with a little noise
53  Problem problem;
54  LabeledData<VectorType, unsigned int> training = problem.generateDataset(ell);
55  LabeledData<VectorType, unsigned int> test = problem.generateDataset(tests);
56 
57  // define the model
59 
60  // train the machine
61  std::cout << "machine training ..." << std::endl;
62  LinearMcSvmOVATrainer<VectorType> trainer(C, epsilon);
63  trainer.train(svm, training);
64  std::cout << "done." << std::endl;
65 
66  // loss measuring classification errors
68 
69  Data<unsigned int> output = svm(training.inputs());
70  double train_error = loss.eval(training.labels(), output);
71  output = svm(test.inputs());
72  double test_error = loss.eval(test.labels(), output);
73  std::cout <<"training error= "<< train_error <<" test error= "<< test_error<<std::endl;
74 }