BinaryRBM.cpp
Go to the documentation of this file.
1 //used for training the RBM
4 
5 //the problem
7 
8 //for evaluation
10 #include <iostream>
11 
12 using namespace shark;
13 using namespace std;
14 
15 //initialize weights of rbm to be between -0.1 and 0.1
17  RealVector weights(rbm.numberOfParameters());
18  for(size_t i = 0; i != weights.size(); ++i){
19  weights(i) = Rng::uni(-0.1,0.1);
20  }
21  rbm.setParameterVector(weights);
22 }
23 
24 int main(){
25 
26  //we first create the problem. in this tutorial, we use BarsAndStripes
27  BarsAndStripes problem;
28  UnlabeledData<RealVector> data = problem.data();
29 
30  //some constants needed for training
31  size_t numberOfHidden = 32;//hidden units of the rbm
32  size_t numberOfVisible = problem.inputDimension();//visible units of the inputs
33 
34  //create rbm with simple binary units
35  BinaryRBM rbm(Rng::globalRng);
36  rbm.setStructure(numberOfVisible,numberOfHidden);
37 
38  //create derivative to optimize the rbm
39  //we want a simple vanilla CD-1
40  BinaryCD cd(&rbm);
41  cd.setK(1);
42  cd.setData(data);
43 
44  //generate optimizer
45  SteepestDescent optimizer;
46  optimizer.setMomentum(0);
47  optimizer.setLearningRate(0.1);
48 
49  //now we train the rbm and evaluate the mean negative log-likelihood at the end
50  unsigned int numIterations = 1000;//iterations for training
51  unsigned int numTrials = 10;//number of trials for training
52  double meanResult = 0;
53  for(unsigned int trial = 0; trial != numTrials; ++trial) {
54  initializeWeights(rbm);
55  optimizer.init(cd);
56 
57  for(unsigned int iteration = 0; iteration != numIterations; ++iteration) {
58  optimizer.step(cd);
59  }
60  //evaluate exact likelihood after training. this is only possible for small problems!
61  double likelihood = negativeLogLikelihood(rbm,data);
62  std::cout<<trial<<" "<<likelihood<<std::endl;
63  meanResult +=likelihood;
64  }
65  meanResult /= numTrials;
66 
67  //print the mean performance
68  cout << "RESULTS: " << std::endl;
69  cout << "======== " << std::endl;
70  cout << "mean negative log likelihood: " << meanResult << std::endl;
71 
72 }
73