Эх сурвалжийг харах

solved bug: added class labels for binary setup

Alexander Freytag 12 жил өмнө
parent
commit
f44f974f13

+ 33 - 12
FMKGPHyperparameterOptimization.cpp

@@ -42,6 +42,10 @@ FMKGPHyperparameterOptimization::FMKGPHyperparameterOptimization()
   verbose = false;
   verbose = false;
   verboseTime = false;
   verboseTime = false;
   debug = false;
   debug = false;
+  
+  //stupid unneeded default values
+  binaryLabelPositive = -1;
+  binaryLabelNegative = -2;
 }
 }
 
 
 FMKGPHyperparameterOptimization::FMKGPHyperparameterOptimization ( const Config *_conf, ParameterizedFunction *_pf, FastMinKernel *_fmk, const string & _confSection )
 FMKGPHyperparameterOptimization::FMKGPHyperparameterOptimization ( const Config *_conf, ParameterizedFunction *_pf, FastMinKernel *_fmk, const string & _confSection )
@@ -53,6 +57,10 @@ FMKGPHyperparameterOptimization::FMKGPHyperparameterOptimization ( const Config
   fmk = NULL;
   fmk = NULL;
   q = NULL;
   q = NULL;
   precomputedTForVarEst = NULL;
   precomputedTForVarEst = NULL;
+  
+  //stupid unneeded default values
+  binaryLabelPositive = -1;
+  binaryLabelNegative = -2;  
 
 
   if ( _fmk == NULL )
   if ( _fmk == NULL )
     this->initialize ( _conf, _pf ); //then the confSection is also the default value
     this->initialize ( _conf, _pf ); //then the confSection is also the default value
@@ -588,7 +596,9 @@ int FMKGPHyperparameterOptimization::prepareBinaryLabels ( map<int, NICE::Vector
   myClasses.clear();
   myClasses.clear();
   for ( NICE::Vector::const_iterator it = y.begin(); it != y.end(); it++ )
   for ( NICE::Vector::const_iterator it = y.begin(); it != y.end(); it++ )
     if ( myClasses.find ( *it ) == myClasses.end() )
     if ( myClasses.find ( *it ) == myClasses.end() )
+    {
       myClasses.insert ( *it );
       myClasses.insert ( *it );
+    }
 
 
   //count how many different classes appear in our data
   //count how many different classes appear in our data
   int nrOfClasses = myClasses.size();
   int nrOfClasses = myClasses.size();
@@ -611,18 +621,19 @@ int FMKGPHyperparameterOptimization::prepareBinaryLabels ( map<int, NICE::Vector
   }
   }
   else if ( nrOfClasses == 2 )
   else if ( nrOfClasses == 2 )
   {
   {
-    std::cerr << "binary setting -- prepare two binary label vectors with opposite signs" << std::endl;
+//     std::cerr << "binary setting -- prepare two binary label vectors with opposite signs" << std::endl;
     Vector yb ( y );
     Vector yb ( y );
 
 
-    int negativeClass = *(myClasses.begin());
+    binaryLabelNegative = *(myClasses.begin());
     std::set<int>::const_iterator classIt = myClasses.begin(); classIt++;
     std::set<int>::const_iterator classIt = myClasses.begin(); classIt++;
-    int positiveClass = *classIt;
+    binaryLabelPositive = *classIt;
     
     
-    std::cerr << "positiveClass : " << positiveClass << " negativeClass: " << negativeClass << std::endl;
+//     std::cerr << "positiveClass : " << binaryLabelPositive << " negativeClass: " << binaryLabelNegative << std::endl;
+
     for ( uint i = 0 ; i < yb.size() ; i++ )
     for ( uint i = 0 ; i < yb.size() ; i++ )
-      yb[i] = ( y[i] == negativeClass ) ? -1.0 : 1.0;
+      yb[i] = ( y[i] == binaryLabelNegative ) ? -1.0 : 1.0;
     
     
-    binaryLabels[ positiveClass ] = yb;
+    binaryLabels[ binaryLabelPositive ] = yb;
 	  //binaryLabels[ 1 ] = yb;
 	  //binaryLabels[ 1 ] = yb;
     
     
     //uncomment the following, if you want to perform real binary computations with 2 classes
     //uncomment the following, if you want to perform real binary computations with 2 classes
@@ -630,7 +641,7 @@ int FMKGPHyperparameterOptimization::prepareBinaryLabels ( map<int, NICE::Vector
 //     binaryLabels[ negativeClass ] = yb;
 //     binaryLabels[ negativeClass ] = yb;
 //     binaryLabels[ negativeClass ] *= -1.0;  
 //     binaryLabels[ negativeClass ] *= -1.0;  
     
     
-    std::cerr << "binaryLabels.size(): " << binaryLabels.size() << std::endl;
+//     std::cerr << "binaryLabels.size(): " << binaryLabels.size() << std::endl;
     
     
 //     binaryLabels[ 0 ] = yb;
 //     binaryLabels[ 0 ] = yb;
 //     binaryLabels[ 0 ] *= -1.0;
 //     binaryLabels[ 0 ] *= -1.0;
@@ -646,6 +657,7 @@ int FMKGPHyperparameterOptimization::prepareBinaryLabels ( map<int, NICE::Vector
   else //OCC setting
   else //OCC setting
   {
   {
     //we set the labels to 1, independent of the previously given class number
     //we set the labels to 1, independent of the previously given class number
+    //however, the original class numbers are stored and returned in classification
     Vector yNew ( y.size(), 1 );
     Vector yNew ( y.size(), 1 );
     myClasses.clear();
     myClasses.clear();
     myClasses.insert ( 1 );
     myClasses.insert ( 1 );
@@ -681,6 +693,7 @@ void FMKGPHyperparameterOptimization::optimize ( std::map<int, NICE::Vector> & b
   int nrOfClasses = binaryLabels.size();
   int nrOfClasses = binaryLabels.size();
   std::set<int> classesToUse;
   std::set<int> classesToUse;
   classesToUse.clear();
   classesToUse.clear();
+  
   for (std::map<int, NICE::Vector>::const_iterator clIt = binaryLabels.begin(); clIt != binaryLabels.end(); clIt++)
   for (std::map<int, NICE::Vector>::const_iterator clIt = binaryLabels.begin(); clIt != binaryLabels.end(); clIt++)
   {
   {
     classesToUse.insert(clIt->first);
     classesToUse.insert(clIt->first);
@@ -1246,17 +1259,16 @@ int FMKGPHyperparameterOptimization::classify ( const NICE::SparseVector & xstar
     scores[ classno ] = beta;
     scores[ classno ] = beta;
   }
   }
   scores.setDim ( maxClassNo + 1 );
   scores.setDim ( maxClassNo + 1 );
-
+  
   if ( precomputedA.size() > 1 ) {
   if ( precomputedA.size() > 1 ) {
     // multi-class classification
     // multi-class classification
     return scores.maxElement();
     return scores.maxElement();
   } else {
   } else {
     // binary setting
     // binary setting
     // FIXME: not really flexible for every situation
     // FIXME: not really flexible for every situation
-    scores[1] = -scores[0];
-    scores[0] = scores[0];
-    scores.setDim ( 2 );
-    return scores[ 0 ] <= 0.0 ? 0 : 1;
+    scores[binaryLabelNegative] = -scores[binaryLabelPositive];
+    
+    return scores[ binaryLabelPositive ] <= 0.0 ? binaryLabelNegative : binaryLabelPositive;
   }
   }
 }
 }
 
 
@@ -1625,6 +1637,12 @@ void FMKGPHyperparameterOptimization::restore ( std::istream & is, int format )
 
 
       //the last one is the GHIK - which we do not have to restore, but simple reset it lateron
       //the last one is the GHIK - which we do not have to restore, but simple reset it lateron
     }
     }
+    
+    //restore the class numbers for binary settings (if mc-settings, these values will be negative by default)
+    is >> tmp; // "binaryLabelPositive: " 
+    is >> binaryLabelPositive;
+    is >> tmp; // " binaryLabelNegative: "
+    is >> binaryLabelNegative;          
   }
   }
   else
   else
   {
   {
@@ -1743,6 +1761,9 @@ void FMKGPHyperparameterOptimization::store ( std::ostream & os, int format ) co
       }
       }
       ikmSumIt++;
       ikmSumIt++;
     }
     }
+    
+    //store the class numbers for binary settings (if mc-settings, these values will be negative by default)
+    os << "binaryLabelPositive: " << binaryLabelPositive << " binaryLabelNegative: " << binaryLabelNegative << std::endl;
   }
   }
   else
   else
   {
   {

+ 5 - 0
FMKGPHyperparameterOptimization.h

@@ -148,6 +148,11 @@ class FMKGPHyperparameterOptimization : NICE::Persistent
     //! use the alphas from the last iteration as initial guess for the ILS?
     //! use the alphas from the last iteration as initial guess for the ILS?
     bool usePreviousAlphas;
     bool usePreviousAlphas;
     
     
+    //! store the class number of the positive class (i.e., larger class no), only used in binary settings
+    int binaryLabelPositive;
+    //! store the class number of the negative class (i.e., smaller class no), only used in binary settings
+    int binaryLabelNegative;
+    
   public:  
   public:  
     
     
 
 

+ 1 - 0
GPHIKClassifier.cpp

@@ -165,6 +165,7 @@ void GPHIKClassifier::train ( const std::vector< NICE::SparseVector *> & example
 
 
   if (verbose)
   if (verbose)
     cerr << "Learning ..." << endl;
     cerr << "Learning ..." << endl;
+
   // go go go
   // go go go
   gphyper->optimize ( labels );
   gphyper->optimize ( labels );
   if (verbose)
   if (verbose)

+ 2 - 0
parameterizedFunctions/PFMKL.h

@@ -52,6 +52,8 @@ class PFMKL : public ParameterizedFunction
       if ( index < *it)
       if ( index < *it)
         return x * m_parameters[dummyCnt];
         return x * m_parameters[dummyCnt];
     }
     }
+    //default value, should never be reached
+    return 0.0;
   }
   }
 
 
   bool isOrderPreserving() const { return true; };
   bool isOrderPreserving() const { return true; };