FFNNBasicTutorial.cpp
Go to the documentation of this file.
1 #include<shark/Models/FFNet.h> //the feed forward neural network
2 #include<shark/Algorithms/GradientDescent/Rprop.h> //resilient propagation as optimizer
3 #include<shark/ObjectiveFunctions/Loss/CrossEntropy.h> // loss during training
4 #include<shark/ObjectiveFunctions/ErrorFunction.h> //error function to connect data model and loss
5 #include<shark/ObjectiveFunctions/Loss/ZeroOneLoss.h> //loss for test performance
6 
7 //evaluating probabilities
8 #include<shark/Models/Softmax.h> //transforms model output into probabilities
9 #include<shark/Models/ConcatenatedModel.h> //provides operator >> for concatenating models
10 
11 using namespace shark;
12 using namespace std;
13 
14 // define xor benchmark problem
16  //the 2D xor Problem has 4 patterns, (0,0), (0,1), (1,0), (1,1)
17  vector<RealVector> inputs(4,RealVector(2));
18  //the result is 1 if both inputs have a different value, and 0 otherwise
19  vector<unsigned int> labels(4);
20 
21  unsigned k = 0;
22  for(unsigned i=0; i < 2; i++){
23  for(unsigned j=0; j < 2; j++){
24  inputs[k](0) = i;
25  inputs[k](1) = j;
26  labels[k] = (i+j) % 2;
27  k++;
28  }
29  }
31  return dataset;
32 }
33 int main(){
34  //create network and initialize weights random uniform
35  unsigned numInput=2;
36  unsigned numHidden=4;
37  unsigned numOutput=1;
39  network.setStructure(numInput,numHidden,numOutput,FFNetStructures::Normal,true);
40 
41  //get problem data
43 
44  //create error function
45  CrossEntropy loss; // surrogate loss for training
46  ErrorFunction error(dataset,&network,&loss);
47 
48  //initialize Rprop and initialize the network randomly
49  initRandomUniform(network,-0.1,0.1);
50  IRpropPlus optimizer;
51  optimizer.init(error);
52  unsigned numberOfSteps = 1000;
53  for(unsigned step = 0; step != numberOfSteps; ++step)
54  optimizer.step(error);
55 
56 
57  // loss for evaluation
58  ZeroOneLoss<unsigned int, RealVector> loss01(0.5); // classification error, output is thresholded at 1/2
59 
60  // evaluate solution found by training
61  network.setParameterVector(optimizer.solution().point); // set weights to weights found by learning
62  Data<RealVector> prediction = network(dataset.inputs());
63  cout << "classification error after learning:\t" << loss01.eval(dataset.labels(), prediction) << endl;
64 
65  cout<<"probabilities:"<<std::endl;
66  Softmax probabilty(1);
67  for(std::size_t i = 0; i != 4; ++i){
68  cout<< (network>>probabilty)(dataset.element(i).input)<<std::endl;
69  }
70 }