瀏覽代碼

minor naming conventions, fix of interfaces in test-programs

Alexander Freytag 9 年之前
父節點
當前提交
ba57a383d5
共有 5 個文件被更改,包括 94 次插入92 次删除
  1. 42 42
      GPHIKClassifier.cpp
  2. 3 6
      GPHIKClassifier.h
  3. 24 19
      progs/classifyDatasetGPHIK.cpp
  4. 12 12
      progs/toyExample.cpp
  5. 13 13
      progs/toyExampleStoreRestore.cpp

+ 42 - 42
GPHIKClassifier.cpp

@@ -81,26 +81,26 @@ GPHIKClassifier::GPHIKClassifier( const Config *_conf,
 
 GPHIKClassifier::~GPHIKClassifier()
 {
-  if ( gphyper != NULL )
-    delete gphyper;
+  if ( this->gphyper != NULL )
+    delete this->gphyper;
 }
 
 void GPHIKClassifier::initFromConfig(const Config *_conf, 
                                      const string & _confSection
                                     )
 { 
-  this->noise       = _conf->gD(confSection, "noise", 0.01);
+  this->d_noise     = _conf->gD( _confSection, "noise", 0.01);
 
   this->confSection = _confSection;
-  this->verbose     = _conf->gB(confSection, "verbose", false);
-  this->debug       = _conf->gB(confSection, "debug", false);
+  this->b_verbose   = _conf->gB( _confSection, "verbose", false);
+  this->b_debug     = _conf->gB( _confSection, "debug", false);
   this->uncertaintyPredictionForClassification 
-                    = _conf->gB( confSection, "uncertaintyPredictionForClassification", false );
+                    = _conf->gB( _confSection, "uncertaintyPredictionForClassification", false );
   
 
    
   //how do we approximate the predictive variance for classification uncertainty?
-  string s_varianceApproximation = _conf->gS(confSection, "varianceApproximation", "approximate_fine"); //default: fine approximative uncertainty prediction
+  string s_varianceApproximation = _conf->gS(_confSection, "varianceApproximation", "approximate_fine"); //default: fine approximative uncertainty prediction
   if ( (s_varianceApproximation.compare("approximate_rough") == 0) || ((s_varianceApproximation.compare("1") == 0)) )
   {
     this->varianceApproximation = APPROXIMATE_ROUGH;
@@ -113,7 +113,7 @@ void GPHIKClassifier::initFromConfig(const Config *_conf,
     this->varianceApproximation = APPROXIMATE_FINE;    
     
     //security check - compute at least one eigenvalue for this approximation strategy
-    this->gphyper->setNrOfEigenvaluesToConsiderForVarApprox ( std::max( _conf->gI(confSection, "nrOfEigenvaluesToConsiderForVarApprox", 1 ), 1) );
+    this->gphyper->setNrOfEigenvaluesToConsiderForVarApprox ( std::max( _conf->gI(_confSection, "nrOfEigenvaluesToConsiderForVarApprox", 1 ), 1) );
   }
   else if ( (s_varianceApproximation.compare("exact") == 0)  || ((s_varianceApproximation.compare("3") == 0)) )
   {
@@ -130,7 +130,7 @@ void GPHIKClassifier::initFromConfig(const Config *_conf,
     this->gphyper->setNrOfEigenvaluesToConsiderForVarApprox ( 0 );
   } 
   
-  if ( this->verbose )
+  if ( this->b_verbose )
     std::cerr << "varianceApproximationStrategy: " << s_varianceApproximation  << std::endl;
   
   //NOTE init all member pointer variables here as well
@@ -183,7 +183,7 @@ void GPHIKClassifier::classify ( const SparseVector * _example,
     
   _scores.clear(); 
   
-  if ( this->debug )
+  if ( this->b_debug )
   {
     std::cerr << "GPHIKClassifier::classify (sparse)" << std::endl;
     _example->store( std::cerr );  
@@ -191,7 +191,7 @@ void GPHIKClassifier::classify ( const SparseVector * _example,
  
   _result = gphyper->classify ( *_example, _scores );
 
-  if ( this->debug )
+  if ( this->b_debug )
   {  
     _scores.store ( std::cerr ); 
     std::cerr << "_result: " << _result << std::endl;
@@ -203,7 +203,7 @@ void GPHIKClassifier::classify ( const SparseVector * _example,
   
   if ( this->uncertaintyPredictionForClassification )
   {
-    if ( this->debug )
+    if ( this->b_debug )
     {
       std::cerr << "GPHIKClassifier::classify -- uncertaintyPredictionForClassification is true"  << std::endl;
     }
@@ -220,7 +220,7 @@ void GPHIKClassifier::classify ( const SparseVector * _example,
   }
   else
   {
-    if ( this->debug )
+    if ( this->b_debug )
     {
       std::cerr << "GPHIKClassifier::classify -- uncertaintyPredictionForClassification is false"  << std::endl;
     }    
@@ -242,7 +242,7 @@ void GPHIKClassifier::classify ( const NICE::Vector * _example,
   
   _scores.clear();
   
-  if ( this->debug )
+  if ( this->b_debug )
   {  
     std::cerr << "GPHIKClassifier::classify (non-sparse)" << std::endl;
     std::cerr << *_example << std::endl;
@@ -250,7 +250,7 @@ void GPHIKClassifier::classify ( const NICE::Vector * _example,
     
   _result = this->gphyper->classify ( *_example, _scores );
   
-  if ( this->debug )
+  if ( this->b_debug )
   {  
     std::cerr << "GPHIKClassifier::classify (non-sparse) -- classification done " << std::endl;
   }
@@ -293,7 +293,7 @@ void GPHIKClassifier::train ( const std::vector< const NICE::SparseVector *> & _
     fthrow(Exception, "Given examples do not match label vector in size -- aborting!" );  
   }  
   
-  if (verbose)
+  if (b_verbose)
   {
     std::cerr << "GPHIKClassifier::train" << std::endl;
   }
@@ -301,21 +301,21 @@ void GPHIKClassifier::train ( const std::vector< const NICE::SparseVector *> & _
   Timer t;
   t.start();
   
-  FastMinKernel *fmk = new FastMinKernel ( _examples, noise, this->debug );
+  FastMinKernel *fmk = new FastMinKernel ( _examples, d_noise, this->b_debug );
 
   this->gphyper->setFastMinKernel ( fmk ); 
   
   t.stop();
-  if (verbose)
+  if (b_verbose)
     std::cerr << "Time used for setting up the fmk object: " << t.getLast() << std::endl;  
  
 
-  if (verbose)
+  if (b_verbose)
     std::cerr << "Learning ..." << endl;
 
   // go go go
   this->gphyper->optimize ( _labels );
-  if (verbose)
+  if (b_verbose)
     std::cerr << "optimization done" << std::endl;
   
   if ( ( this->varianceApproximation != NONE ) )
@@ -348,7 +348,7 @@ void GPHIKClassifier::train ( const std::vector< const NICE::SparseVector *> & _
   this->b_isTrained = true;
 
   // clean up all examples ??
-  if (verbose)
+  if (b_verbose)
     std::cerr << "Learning finished" << std::endl;
 }
 
@@ -369,28 +369,28 @@ void GPHIKClassifier::train ( const std::vector< const NICE::SparseVector *> & _
     }
   }
   
-  if ( this->verbose )
+  if ( this->b_verbose )
     std::cerr << "GPHIKClassifier::train" << std::endl;
  
   Timer t;
   t.start();
   
-  FastMinKernel *fmk = new FastMinKernel ( _examples, noise, this->debug );
+  FastMinKernel *fmk = new FastMinKernel ( _examples, d_noise, this->b_debug );
   this->gphyper->setFastMinKernel ( fmk );  
   
   t.stop();
-  if ( this->verbose )
+  if ( this->b_verbose )
     std::cerr << "Time used for setting up the fmk object: " << t.getLast() << std::endl;  
 
 
 
-  if ( this->verbose )
+  if ( this->b_verbose )
     std::cerr << "Learning ..." << std::endl;
   
   // go go go
   this->gphyper->optimize ( _binLabels );
   
-  if ( this->verbose )
+  if ( this->b_verbose )
     std::cerr << "optimization done, now prepare for the uncertainty prediction" << std::endl;
   
   if ( ( this->varianceApproximation != NONE ) )
@@ -423,7 +423,7 @@ void GPHIKClassifier::train ( const std::vector< const NICE::SparseVector *> & _
   this->b_isTrained = true;
 
   // clean up all examples ??
-  if ( this->verbose )
+  if ( this->b_verbose )
     std::cerr << "Learning finished" << std::endl;
 }
 
@@ -578,21 +578,21 @@ void GPHIKClassifier::restore ( std::istream & _is,
         _is >> tmp; // end of block 
         tmp = this->removeEndTag ( tmp );
       }
-      else if ( tmp.compare("noise") == 0 )
+      else if ( tmp.compare("d_noise") == 0 )
       {
-        _is >> noise;        
+        _is >> d_noise;        
         _is >> tmp; // end of block 
         tmp = this->removeEndTag ( tmp );
       }      
-      else if ( tmp.compare("verbose") == 0 )
+      else if ( tmp.compare("b_verbose") == 0 )
       {
-        _is >> verbose;        
+        _is >> b_verbose;        
         _is >> tmp; // end of block 
         tmp = this->removeEndTag ( tmp );
       }      
-      else if ( tmp.compare("debug") == 0 )
+      else if ( tmp.compare("b_debug") == 0 )
       {
-        _is >> debug;        
+        _is >> b_debug;        
         _is >> tmp; // end of block 
         tmp = this->removeEndTag ( tmp );
       }      
@@ -654,18 +654,18 @@ void GPHIKClassifier::store ( std::ostream & _os,
     _os << b_isTrained << std::endl;
     _os << this->createEndTag( "b_isTrained" ) << std::endl; 
     
-    _os << this->createStartTag( "noise" ) << std::endl;
-    _os << noise << std::endl;
-    _os << this->createEndTag( "noise" ) << std::endl;
+    _os << this->createStartTag( "d_noise" ) << std::endl;
+    _os << d_noise << std::endl;
+    _os << this->createEndTag( "d_noise" ) << std::endl;
     
     
-    _os << this->createStartTag( "verbose" ) << std::endl;
-    _os << verbose << std::endl;
-    _os << this->createEndTag( "verbose" ) << std::endl; 
+    _os << this->createStartTag( "b_verbose" ) << std::endl;
+    _os << b_verbose << std::endl;
+    _os << this->createEndTag( "b_verbose" ) << std::endl; 
     
-    _os << this->createStartTag( "debug" ) << std::endl;
-    _os << debug << std::endl;
-    _os << this->createEndTag( "debug" ) << std::endl; 
+    _os << this->createStartTag( "b_debug" ) << std::endl;
+    _os << b_debug << std::endl;
+    _os << this->createEndTag( "b_debug" ) << std::endl; 
     
     _os << this->createStartTag( "uncertaintyPredictionForClassification" ) << std::endl;
     _os << uncertaintyPredictionForClassification << std::endl;

+ 3 - 6
GPHIKClassifier.h

@@ -46,9 +46,9 @@ class GPHIKClassifier : public NICE::Persistent, public NICE::OnlineLearnable
     ///////////////////////////////////
     
     /** verbose flag for useful output*/
-    bool verbose;
+    bool b_verbose;
     /** debug flag for several outputs useful for debugging*/
-    bool debug;
+    bool b_debug;
     
     //////////////////////////////////////
     //      general specifications      //
@@ -69,7 +69,7 @@ class GPHIKClassifier : public NICE::Persistent, public NICE::OnlineLearnable
     
     
     /** Gaussian label noise for model regularization */
-    double noise;
+    double d_noise;
 
     enum VarianceApproximation{
       APPROXIMATE_ROUGH,
@@ -297,9 +297,6 @@ class GPHIKClassifier : public NICE::Persistent, public NICE::OnlineLearnable
                                       const NICE::Vector & _newLabels,
                                       const bool & _performOptimizationAfterIncrement = true
                                     );       
-
-
-
 };
 
 }

+ 24 - 19
progs/classifyDatasetGPHIK.cpp

@@ -19,20 +19,23 @@
 #include "gp-hik-core/GPHIKClassifier.h"
 
 
-void readSparseExamples ( const std::string & fn,  std::vector< const NICE::SparseVector * > & examples, NICE::Vector & labels )
+void readSparseExamples ( const std::string & _fn,  
+                          std::vector< const NICE::SparseVector * > & _examples, 
+                          NICE::Vector & _labels 
+                        )
 {
   // initially cleaning of variables
-  examples.clear();
-  labels.clear();
+    _examples.clear();
+    _labels.clear();
   
   std::vector<double> labels_std;
   labels_std.clear();
   
-  std::cerr << "Reading " << fn << std::endl;
-  std::ifstream ifs ( fn.c_str(), std::ios::in );
+  std::cerr << "Reading " << _fn << std::endl;
+  std::ifstream ifs ( _fn.c_str(), std::ios::in );
   if ( ! ifs.good() )
   {
-      std::cerr <<  "Unable to read " << fn << std::endl;
+      std::cerr <<  "Unable to read " << _fn << std::endl;
       return;
   }
   
@@ -67,23 +70,25 @@ void readSparseExamples ( const std::string & fn,  std::vector< const NICE::Spar
     }
           
     
-    examples.push_back ( v );
+        _examples.push_back ( v );
   }
   ifs.close();
   
-  labels = NICE::Vector( labels_std );
+    _labels = NICE::Vector( labels_std );
 }
 
-void mapClassNumbersToIndices( const NICE::Vector & labels, std::map<int,int> & mapClassNoToIdx )
+void mapClassNumbersToIndices( const NICE::Vector & _labels, 
+                               std::map< uint, uint > & _mapClassNoToIdx 
+                             )
 {
-  mapClassNoToIdx.clear();
+  _mapClassNoToIdx.clear();
   int classCnt ( 0 );
   
-  for ( NICE::Vector::const_iterator it_labels = labels.begin(); it_labels != labels.end(); it_labels++ )
+  for ( NICE::Vector::const_iterator it_labels = _labels.begin(); it_labels != _labels.end(); it_labels++ )
   {
-    if ( mapClassNoToIdx.find( *it_labels ) == mapClassNoToIdx.end() )
+    if ( _mapClassNoToIdx.find( *it_labels ) == _mapClassNoToIdx.end() )
     {
-      mapClassNoToIdx.insert( std::pair<int,int>( (int) round(*it_labels), classCnt ) );
+            _mapClassNoToIdx.insert( std::pair< uint, uint >( (uint) round(*it_labels), classCnt ) );
       classCnt++;
     }
   }
@@ -114,7 +119,7 @@ int main (int argc, char* argv[])
   readSparseExamples ( s_fn_trainingSet, examplesTrain, labelsTrain );
 
   //map the occuring classes to a minimal set of indices
-  std::map<int,int> map_classNoToClassIdx_train; // < classNo, Idx>
+  std::map< uint, uint > map_classNoToClassIdx_train; // < classNo, Idx>
   
   mapClassNumbersToIndices( labelsTrain, map_classNoToClassIdx_train );
   
@@ -136,7 +141,7 @@ int main (int argc, char* argv[])
   readSparseExamples ( s_fn_testSet, examplesTest, labelsTest );
   
   //map the occuring classes to a minimal set of indices
-  std::map<int,int> map_classNoToClassIdx_test; // < classNo, Idx>
+  std::map< uint, uint > map_classNoToClassIdx_test; // < classNo, Idx>
   
   mapClassNumbersToIndices( labelsTest, map_classNoToClassIdx_test );
 
@@ -151,14 +156,14 @@ int main (int argc, char* argv[])
   
   for (std::vector< const NICE::SparseVector *>::const_iterator itTestExamples = examplesTest.begin(); itTestExamples != examplesTest.end(); itTestExamples++, idx++)
   {
-    int classno_groundtruth = labelsTest( idx );
-    int classno_predicted;
+    uint classno_groundtruth = labelsTest( idx );
+    uint classno_predicted;
 
     classifier.classify ( *itTestExamples, classno_predicted, scores /* not needed anyway in that evaluation*/ );
     
     
-    int idx_classno_groundtruth ( map_classNoToClassIdx_test[ classno_groundtruth ] );
-    int idx_classno_predicted ( map_classNoToClassIdx_train[ classno_predicted ] );
+    uint idx_classno_groundtruth ( map_classNoToClassIdx_test[ classno_groundtruth ] );
+    uint idx_classno_predicted ( map_classNoToClassIdx_train[ classno_predicted ] );
         
     confusion( idx_classno_groundtruth, idx_classno_predicted ) += 1;
   }

+ 12 - 12
progs/toyExample.cpp

@@ -132,17 +132,17 @@ int main (int argc, char* argv[])
   
   // determine classes known during training and corresponding mapping
   // thereby allow for non-continous class labels
-  std::set<int> classesKnownTraining = classifier->getKnownClassNumbers();
+  std::set< uint > classesKnownTraining = classifier->getKnownClassNumbers();
   
-  int noClassesKnownTraining ( classesKnownTraining.size() );
-  std::map<int,int> mapClNoToIdxTrain;
-  std::set<int>::const_iterator clTrIt = classesKnownTraining.begin();
+  uint noClassesKnownTraining ( classesKnownTraining.size() );
+  std::map< uint, uint > mapClNoToIdxTrain;
+  std::set< uint >::const_iterator clTrIt = classesKnownTraining.begin();
   for ( int i=0; i < noClassesKnownTraining; i++, clTrIt++ )
-      mapClNoToIdxTrain.insert ( std::pair<int,int> ( *clTrIt, i )  );
+      mapClNoToIdxTrain.insert ( std::pair< uint, uint > ( *clTrIt, i )  );
   
   // determine classes known during testing and corresponding mapping
   // thereby allow for non-continous class labels
-  std::set<int> classesKnownTest;
+  std::set< uint > classesKnownTest;
   classesKnownTest.clear();
   
 
@@ -156,11 +156,11 @@ int main (int argc, char* argv[])
     }
   }          
   
-  int noClassesKnownTest ( classesKnownTest.size() );  
-  std::map<int,int> mapClNoToIdxTest;
-  std::set<int>::const_iterator clTestIt = classesKnownTest.begin();
-  for ( int i=0; i < noClassesKnownTest; i++, clTestIt++ )
-      mapClNoToIdxTest.insert ( std::pair<int,int> ( *clTestIt, i )  ); 
+  uint noClassesKnownTest ( classesKnownTest.size() );  
+  std::map< uint, uint > mapClNoToIdxTest;
+  std::set< uint >::const_iterator clTestIt = classesKnownTest.begin();
+  for ( uint i=0; i < noClassesKnownTest; i++, clTestIt++ )
+      mapClNoToIdxTest.insert ( std::pair< uint, uint > ( *clTestIt, i )  ); 
           
   
   NICE::Matrix confusionMatrix( noClassesKnownTraining, noClassesKnownTest, 0.0);
@@ -177,7 +177,7 @@ int main (int argc, char* argv[])
   {
     NICE::Vector example ( dataTest.getRow(i) );
     NICE::SparseVector scores;
-    int result;
+    uint result;
     
     // and classify
     t.start();

+ 13 - 13
progs/toyExampleStoreRestore.cpp

@@ -121,17 +121,17 @@ int main (int argc, char* argv[])
   
   // determine classes known during training and corresponding mapping
   // thereby allow for non-continous class labels
-  std::set<int> classesKnownTraining = classifier->getKnownClassNumbers();
+  std::set< uint > classesKnownTraining = classifier->getKnownClassNumbers();
   
-  int noClassesKnownTraining ( classesKnownTraining.size() );
-  std::map<int,int> mapClNoToIdxTrain;
-  std::set<int>::const_iterator clTrIt = classesKnownTraining.begin();
-  for ( int i=0; i < noClassesKnownTraining; i++, clTrIt++ )
-      mapClNoToIdxTrain.insert ( std::pair<int,int> ( *clTrIt, i )  );
+  uint noClassesKnownTraining ( classesKnownTraining.size() );
+  std::map< uint, uint > mapClNoToIdxTrain;
+  std::set< uint >::const_iterator clTrIt = classesKnownTraining.begin();
+  for ( uint i=0; i < noClassesKnownTraining; i++, clTrIt++ )
+      mapClNoToIdxTrain.insert ( std::pair< uint, uint > ( *clTrIt, i )  );
   
   // determine classes known during testing and corresponding mapping
   // thereby allow for non-continous class labels
-  std::set<int> classesKnownTest;
+  std::set< uint > classesKnownTest;
   classesKnownTest.clear();
   
 
@@ -145,11 +145,11 @@ int main (int argc, char* argv[])
     }
   }          
   
-  int noClassesKnownTest ( classesKnownTest.size() );  
-  std::map<int,int> mapClNoToIdxTest;
-  std::set<int>::const_iterator clTestIt = classesKnownTest.begin();
-  for ( int i=0; i < noClassesKnownTest; i++, clTestIt++ )
-      mapClNoToIdxTest.insert ( std::pair<int,int> ( *clTestIt, i )  ); 
+  uint noClassesKnownTest ( classesKnownTest.size() );  
+  std::map< uint, uint > mapClNoToIdxTest;
+  std::set< uint >::const_iterator clTestIt = classesKnownTest.begin();
+  for ( uint i=0; i < noClassesKnownTest; i++, clTestIt++ )
+      mapClNoToIdxTest.insert ( std::pair< uint, uint > ( *clTestIt, i )  ); 
           
   
   NICE::Matrix confusionMatrix         ( noClassesKnownTraining, noClassesKnownTest, 0.0);
@@ -165,7 +165,7 @@ int main (int argc, char* argv[])
   {
     NICE::Vector example ( dataTest.getRow(i) );
     NICE::SparseVector scores;
-    int result;
+    uint result;
     
     // classify with trained classifier 
     t.start();