MaxLogLikelihood.cpp
Go to the documentation of this file.
1 #include <shark/Data/Csv.h> //load the csv file
3 
5 
7 #include<shark/Models/ConvexCombination.h>
9 
13 
14 using namespace shark;
15 using namespace std;
16 
17 
18 int main(int argc, char **argv) {
19  if(argc < 2) {
20  cerr << "usage: " << argv[0] << " (filename)" << endl;
21  return 1;
22  }
24  try {
25  importCSV(data, argv[1], ' ');
26  Normalizer<> normalizer;
27  NormalizeComponentsUnitVariance<> normalizingTrainer(true); // zero-mean
28  normalizingTrainer.train(normalizer, data);
29  data = normalizer(data);
30  }
31  catch (...) {
32  cerr << "unable to read data from file " << argv[1] << endl;
33  return 1;
34  }
35 
36  std::size_t inputs=dataDimension(data);
37  std::size_t hiddens = 2;
38  unsigned numberOfSteps = 100;
39 
40  //create mixture of gaussian mixture and initialize weights
41  RBFLayer gaussians;
42  gaussians.setStructure(inputs,hiddens);
43  ConvexCombination combination;
44  combination.setStructure(hiddens);
45  ConcatenatedModel<RealVector,RealVector> mixture= gaussians >> combination;
46  initRandomUniform(mixture,-0.1,0.1);
47  kMeans(data, gaussians);
48 
49  //create error function
50  NegativeLogLikelihood error(data,&mixture);
51  //initialize Rprop
52  IRpropPlus optimizer;
53  error.init();
54  optimizer.init(error);
55 
56  RealMatrix initCenters = gaussians.centers();
57  RealVector initWidth = gaussians.gamma();
58 
59  std::cout<<0<<" "<<optimizer.solution().value<<std::endl;;
60  for(unsigned step = 0; step != numberOfSteps; ++step){
61  optimizer.step(error);
62  if(step % 10 == 9)
63  std::cout<<step<<" "<<optimizer.solution().value<<std::endl;;
64  }
65 
66  std::cout<<"center solutions"<<std::endl;
67  std::cout<<"init:"<<initCenters<<std::endl;
68  std::cout<<"after optimization:"<<gaussians.centers()<<std::endl;
69  std::cout<<"width solutions"<<std::endl;
70  std::cout<<"init:"<<initWidth<<std::endl;
71  std::cout<<"after optimization:"<<gaussians.gamma()<<std::endl;
72 }