DeepNetworkTraining.cpp
Go to the documentation of this file.
1 //noisy AutoencoderModel model and deep network
2 #include <shark/Models/FFNet.h>// neural network for supervised training
3 #include <shark/Models/Autoencoder.h>// the autoencoder to train unsupervised
4 #include <shark/Models/ConcatenatedModel.h>// to concatenate Autoencoder with noise adding model
5 
6 //training the model
7 #include <shark/ObjectiveFunctions/ErrorFunction.h>//the error function performing the regularisation of the hidden neurons
8 #include <shark/ObjectiveFunctions/Loss/SquaredLoss.h> // squared loss used for unsupervised pre-training
9 #include <shark/ObjectiveFunctions/Loss/CrossEntropy.h> // loss used for supervised training
10 #include <shark/ObjectiveFunctions/Loss/ZeroOneLoss.h> // loss used for evaluation of performance
11 #include <shark/ObjectiveFunctions/Regularizer.h> //L1 and L2 regularisation
12 #include <shark/Algorithms/GradientDescent/SteepestDescent.h> //optimizer: simple gradient descent.
13 #include <shark/Algorithms/GradientDescent/Rprop.h> //optimizer for autoencoders
14 
15 using namespace std;
16 using namespace shark;
17 
18 //our artificial problem
20  std::vector<RealVector> data(320,RealVector(16));
21  std::vector<unsigned int> label(320);
22  RealVector line(4);
23  for(std::size_t k = 0; k != 10; ++k){
24  for(size_t x=0; x != 16; x++) {
25  for(size_t j=0; j != 4; j++) {
26  bool val = (x & (1<<j)) > 0;
27  line(j) = val;
29  line(j) = !val;
30  }
31 
32  for(int i=0; i != 4; i++) {
33  subrange(data[x+k*16],i*4 ,i*4 + 4) = line;
34  }
35  for(int i=0; i != 4; i++) {
36  for(int l=0; l<4; l++) {
37  data[x+k*16+160](l*4 + i) = line(l);
38  }
39  }
40  label[x+k*16] = 1;
41  label[x+k*16+160] = 0;
42  }
43  }
44  return createLabeledDataFromRange(data,label);
45 }
46 
47 //training of an auto encoder with one hidden layer
48 template<class AutoencoderModel>
50  UnlabeledData<RealVector> const& data,//the data to train with
51  std::size_t numHidden,//number of features in the AutoencoderModel
52  double regularisation,//strength of the regularisation
53  std::size_t iterations //number of iterations to optimize
54 ){
55  //create the model
56  std::size_t inputs = dataDimension(data);
57  AutoencoderModel model;
58  model.setStructure(inputs, numHidden);
59  initRandomUniform(model,-0.1*std::sqrt(1.0/inputs),0.1*std::sqrt(1.0/inputs));
60  //create the objective function
61  LabeledData<RealVector,RealVector> trainSet(data,data);//labels identical to inputs
63  ErrorFunction error(trainSet, &model, &loss);
64  TwoNormRegularizer regularizer(error.numberOfVariables());
65  error.setRegularizer(regularisation,&regularizer);
66  //set up optimizer
67  IRpropPlusFull optimizer;
68  error.init();
69  optimizer.init(error);
70  std::cout<<"Optimizing model: "+model.name()<<std::endl;
71  for(std::size_t i = 0; i != iterations; ++i){
72  optimizer.step(error);
73  std::cout<<i<<" "<<optimizer.solution().value<<std::endl;
74  }
75  model.setParameterVector(optimizer.solution().point);
76  return model;
77 }
78 
79 typedef Autoencoder<RectifierNeuron,LinearNeuron> AutoencoderModel;//type of autoencoder
80 typedef FFNet<RectifierNeuron,LinearNeuron> Network;//final supervised trained structure
81 
82 //unsupervised pre training of a network with two hidden layers
84  UnlabeledData<RealVector> const& data,
85  std::size_t numHidden1,std::size_t numHidden2, std::size_t numOutputs,
86  double regularisation, std::size_t iterations
87 ){
88  //train the first hidden layer
89  std::cout<<"training first layer"<<std::endl;
90  AutoencoderModel layer = trainAutoencoderModel<AutoencoderModel>(
91  data,numHidden1,
92  regularisation,
93  iterations
94  );
95  //compute the mapping onto the features of the first hidden layer
96  UnlabeledData<RealVector> intermediateData = layer.evalLayer(0,data);
97 
98  //train the next layer
99  std::cout<<"training second layer"<<std::endl;
100  AutoencoderModel layer2 = trainAutoencoderModel<AutoencoderModel>(
101  intermediateData,numHidden2,
102  regularisation,
103  iterations
104  );
105  //create the final network
106  Network network;
107  network.setStructure(dataDimension(data),numHidden1,numHidden2, numOutputs);
108  initRandomNormal(network,0.1);
109  network.setLayer(0,layer.encoderMatrix(),layer.hiddenBias());
110  network.setLayer(1,layer2.encoderMatrix(),layer2.hiddenBias());
111 
112  return network;
113 }
114 
115 int main()
116 {
117  //model parameters
118  std::size_t numHidden1 = 8;
119  std::size_t numHidden2 = 8;
120  //unsupervised hyper parameters
121  double unsupRegularisation = 0.001;
122  std::size_t unsupIterations = 100;
123  //supervised hyper parameters
124  double regularisation = 0.0001;
125  std::size_t iterations = 200;
126 
127  //load data and split into training and test
129  data.shuffle();
130  LabeledData<RealVector,unsigned int> test = splitAtElement(data,static_cast<std::size_t>(0.5*data.numberOfElements()));
131 
132  //unsupervised pre training
134  data.inputs(),numHidden1, numHidden2,numberOfClasses(data),
135  unsupRegularisation, unsupIterations
136  );
137 
138  //create the supervised problem. Cross Entropy loss with one norm regularisation
139  CrossEntropy loss;
140  ErrorFunction error(data, &network, &loss);
141  OneNormRegularizer regularizer(error.numberOfVariables());
142  error.setRegularizer(regularisation,&regularizer);
143 
144  //optimize the model
145  std::cout<<"training supervised model"<<std::endl;
146  IRpropPlusFull optimizer;
147  error.init();
148  optimizer.init(error);
149  for(std::size_t i = 0; i != iterations; ++i){
150  optimizer.step(error);
151  std::cout<<i<<" "<<optimizer.solution().value<<std::endl;
152  }
153  network.setParameterVector(optimizer.solution().point);
154 
155  //evaluation
157  Data<RealVector> predictionTrain = network(data.inputs());
158  cout << "classification error,train: " << loss01.eval(data.labels(), predictionTrain) << endl;
159 
160  Data<RealVector> prediction = network(test.inputs());
161  cout << "classification error,test: " << loss01.eval(test.labels(), prediction) << endl;
162 
163 }