StoppingCriteria.cpp
Go to the documentation of this file.
1 #include <shark/Data/Csv.h>
2 #include <shark/Models/FFNet.h> //Feed forward neural network class
3 #include <shark/Algorithms/GradientDescent/Rprop.h> //Optimization algorithm
4 #include <shark/ObjectiveFunctions/Loss/CrossEntropy.h> //Loss used for training
5 #include <shark/ObjectiveFunctions/Loss/ZeroOneLoss.h> //The real loss for testing.
6 #include <shark/Algorithms/Trainers/OptimizationTrainer.h> // Trainer wrapping iterative optimization
7 #include <shark/Algorithms/StoppingCriteria/MaxIterations.h> //A simple stopping criterion that stops after a fixed number of iterations
8 #include <shark/Algorithms/StoppingCriteria/TrainingError.h> //Stops when the algorithm seems to converge
9 #include <shark/Algorithms/StoppingCriteria/GeneralizationQuotient.h> //Uses the validation error to track the progress
10 #include <shark/Algorithms/StoppingCriteria/ValidatedStoppingCriterion.h> //Adds the validation error to the value of the point
11 
12 #include <iostream>
13 
14 using namespace shark;
15 using namespace std;
16 
17 //this program demonstrates the effect of different stopping criteria on the performance of a neural network.
18 template<class T>
19 double experiment(AbstractStoppingCriterion<T> & stoppingCriterion, ClassificationDataset const& trainingset, ClassificationDataset const& testset){
20  //create a feed forward neural network with one layer of 10 hidden neurons and one output for every class
22  network.setStructure(inputDimension(trainingset),10,numberOfClasses(trainingset));
23  initRandomUniform(network,-0.1,0.1);
24 
25  //The Cross Entropy maximises the activation of the cth output neuron
26  // compared to all other outputs for a sample with class c.
27  CrossEntropy loss;
28 
29  //we use IRpropPlus for network optimization
30  IRpropPlus optimizer;
31 
32  //create an optimization trainer and train the model
33  OptimizationTrainer<FFNet<LogisticNeuron,LinearNeuron>,unsigned int > trainer(&loss, &optimizer, &stoppingCriterion);
34  trainer.train(network, trainingset);
35 
36  //evaluate the performance on the test set using the classification loss we choose 0.5 as threshold since Logistic neurons have values between 0 and 1.
37 
39  Data<RealVector> predictions = network(testset.inputs());
40  return loss01(testset.labels(),predictions);
41 }
42 int main(){
43  //load the diabetes dataset shuffle its entries and split it in training, validation and test set.
45  importCSV(data, "data/diabetes.csv",LAST_COLUMN, ',');
46  data.shuffle();
47  ClassificationDataset test = splitAtElement(data,static_cast<std::size_t>(0.75*data.numberOfElements()));
48  ClassificationDataset validation = splitAtElement(data,static_cast<std::size_t>(0.66*data.numberOfElements()));
49 
50  //simple stopping criterion which allows for n iterations (here n = 10,100,500)
51  MaxIterations<> maxIterations(10);
52  double resultMaxIterations1 = experiment(maxIterations,data,test);
53  maxIterations.setMaxIterations(100);
54  double resultMaxIterations2 = experiment(maxIterations,data,test);
55  maxIterations.setMaxIterations(500);
56  double resultMaxIterations3 = experiment(maxIterations,data,test);
57 
58  TrainingError<> trainingError(10,1.e-5);
59  double resultTrainingError = experiment(trainingError,data,test);
60 
61  //for the validated stopping criteria we need to define an error function using the validation set
63  network.setStructure(inputDimension(data),10,numberOfClasses(data));
64  CrossEntropy loss;
65  ErrorFunction validationFunction(validation,&network,&loss);
66 
67  GeneralizationQuotient<> generalizationQuotient(10,0.1);
68  ValidatedStoppingCriterion validatedLoss(&validationFunction,&generalizationQuotient);
69  double resultGeneralizationQuotient = experiment(validatedLoss,data,test);
70 
71  //print the results
72  cout << "RESULTS: " << endl;
73  cout << "======== \n" << endl;
74  cout << "10 iterations : " << resultMaxIterations1 << endl;
75  cout << "100 iterations : " << resultMaxIterations2 << endl;
76  cout << "500 iterations : " << resultMaxIterations3 << endl;
77  cout << "training Error : " << resultTrainingError << endl;
78  cout << "generalization Quotient : " << resultGeneralizationQuotient << endl;
79 }