DenoisingAutoencoderTutorial.cpp
Go to the documentation of this file.
1 
2 #include <shark/Data/Pgm.h> //for exporting the learned filters
3 #include <shark/Data/SparseData.h>//for reading in the images as sparseData/Libsvm format
4 #include <shark/Models/Autoencoder.h>//normal autoencoder model
5 #include <shark/Models/TiedAutoencoder.h>//autoencoder with tied weights
6 #include <shark/Models/ImpulseNoiseModel.h>//noise source to corrupt the inputs
7 #include <shark/Models/ConcatenatedModel.h>//to concatenate the noise with the model
9 #include <shark/Algorithms/GradientDescent/Rprop.h>// the Rprop optimization algorithm
10 #include <shark/ObjectiveFunctions/Loss/SquaredLoss.h> // squared loss used for regression
11 #include <shark/ObjectiveFunctions/Regularizer.h> //L2 regulariziation
12 
13 using namespace std;
14 using namespace shark;
15 
16 //training of an auto encoder with one hidden layer
17 template<class AutoencoderModel>
19  UnlabeledData<RealVector> const& data,//the data to train with
20  std::size_t numHidden,//number of features in the autoencoder
21  std::size_t iterations, //number of iterations to optimize
22  double regularisation,//strength of the regularisation
23  double noiseStrength // strength of the added noise
24 ){
25  //create the model
26  std::size_t inputs = dataDimension(data);
27  AutoencoderModel baseModel;
28  baseModel.setStructure(inputs, numHidden);
29  initRandomUniform(baseModel,-0.1*std::sqrt(1.0/inputs),0.1*std::sqrt(1.0/inputs));
30  ImpulseNoiseModel noise(noiseStrength,0.0);//set an input pixel with probability p to 0
31  ConcatenatedModel<RealVector,RealVector> model = noise>> baseModel;
32  //create the objective function
33  LabeledData<RealVector,RealVector> trainSet(data,data);//labels identical to inputs
35  ErrorFunction error(trainSet, &model, &loss);
36  TwoNormRegularizer regularizer(error.numberOfVariables());
37  error.setRegularizer(regularisation,&regularizer);
38  //set up optimizer
39  IRpropPlusFull optimizer;
40  optimizer.init(error);
41  std::cout<<"Optimizing model: "+model.name()<<std::endl;
42  for(std::size_t i = 0; i != iterations; ++i){
43  optimizer.step(error);
44  std::cout<<i<<" "<<optimizer.solution().value<<std::endl;
45  }
46  model.setParameterVector(optimizer.solution().point);
47  return baseModel;
48 }
49 int main(int argc, char **argv)
50 {
51  if(argc < 2) {
52  cerr << "usage: " << argv[0] << " path/to/mnist_subset.libsvm" << endl;
53  return 1;
54  }
55  std::size_t numHidden = 200;
56  std::size_t iterations = 200;
57  double regularisation = 0.01;
58  double noiseStrengt = 0.5;
59 
61  importSparseData( train, argv[1] );
62 
63  std::size_t numElems = train.numberOfElements();
64  for(std::size_t i = 0; i != numElems; ++i){
65  for(std::size_t j = 0; j != 784; ++j){
66  if(train.element(i).input(j) > 0.5){
67  train.element(i).input(j) = 1;
68  }else{
69  train.element(i).input(j) = 0;
70  }
71  }
72  }
73 
76 
77  Autoencoder1 net1 = trainAutoencoderModel<Autoencoder1>(train.inputs(),numHidden,iterations,regularisation,noiseStrengt);
78  Autoencoder2 net2 = trainAutoencoderModel<Autoencoder2>(train.inputs(),numHidden,iterations,regularisation,noiseStrengt);
79 
80  exportFiltersToPGMGrid("features1",net1.encoderMatrix(),28,28);
81  exportFiltersToPGMGrid("features2",net2.encoderMatrix(),28,28);
82 }