CSvmTutorial.cpp
Go to the documentation of this file.
1 #include <shark/Algorithms/Trainers/CSvmTrainer.h> // the C-SVM trainer
2 #include <shark/Models/Kernels/GaussianRbfKernel.h> //the used kernel for the SVM
3 #include <shark/ObjectiveFunctions/Loss/ZeroOneLoss.h> //used for evaluation of the classifier
4 #include <shark/Data/DataDistribution.h> //includes small toy distributions
5 
6 using namespace shark;
7 using namespace std;
8 
9 int main(int argc, char** argv)
10 {
11  // experiment settings
12  unsigned int ell = 500; // number of training data point
13  unsigned int tests = 10000; // number of test data points
14  double gamma = 0.5; // kernel bandwidth parameter
15  double C = 1000.0; // regularization parameter
16  bool bias = true; // use bias/offset parameter
17 
18  GaussianRbfKernel<> kernel(gamma); // Gaussian kernel
19  KernelClassifier<RealVector> kc; // (affine) linear function in kernel-induced feature space
20 
21  // generate dataset
22  Chessboard problem; // artificial benchmark data
23  ClassificationDataset training = problem.generateDataset(ell);
24  ClassificationDataset test = problem.generateDataset(tests);
25  // define the machine
26  CSvmTrainer<RealVector> trainer(&kernel, C, bias);
27 
28  // train the machine
29  cout << "Algorithm: " << trainer.name() << "\ntraining ..." << flush; // Shark algorithms know their names
30  trainer.train(kc, training);
31  cout << "\n number of iterations: " << trainer.solutionProperties().iterations;
32  cout << "\n dual value: " << trainer.solutionProperties().value;
33  cout << "\n training time: " << trainer.solutionProperties().seconds << " seconds\ndone." << endl;
34 
35  // evaluate
36  ZeroOneLoss<unsigned int> loss; // 0-1 loss
37  Data<unsigned int> output = kc(training.inputs()); // evaluate on training set
38  double train_error = loss.eval(training.labels(), output);
39  cout << "training error:\t" << train_error << endl;
40  output = kc(test.inputs()); // evaluate on test set
41  double test_error = loss.eval(test.labels(), output);
42  cout << "test error:\t" << test_error << endl;
43 
44  // ADDITIONAL/ADVANCED SVM SOLVER OPTIONS:
45  {
46  //to use "double" as kernel matrix cache type internally instead of float:
47  CSvmTrainer<RealVector, double> trainer(&kernel, C, bias);
48  //to keep non-support vectors after training:
49  trainer.sparsify() = false;
50  //to relax or tighten the stopping criterion from 1e-3 (here, tightened to 1e-6)
51  trainer.stoppingCondition().minAccuracy = 1e-6;
52  //to set the cache size to 128MB for double (16**6 times sizeof(double), when double was selected as cache type above)
53  //or to 64MB for float (16**6 times sizeof(float), when the CSvmTrainer is declared without second template argument)
54  trainer.setCacheSize( 0x1000000 );
55  trainer.train(kc, training);
56  std::cout << "Needed " << trainer.solutionProperties().seconds << " seconds to reach a dual of " << trainer.solutionProperties().value << std::endl;
57  }
58 }