Parcourir la source

support for EV settings, bug fix for capu computation, clean-up

Alexander Freytag il y a 9 ans
Parent
commit
5d91a69c0a

+ 21 - 21
FMKGPHyperparameterOptimization.cpp

@@ -602,7 +602,10 @@ void FMKGPHyperparameterOptimization::initFromConfig ( const Config *_conf,
   // this->eig = new EigValuesTRLAN();
   // My time measurements show that both methods use equal time, a comparision
   // of their numerical performance has not been done yet  
-  this->eig = new EVArnoldi ( _conf->gB ( _confSection, "eig_verbose", false ) /* verbose flag */, 10 );
+  int eigValueMaxIterations = _conf->gI ( _confSection, "eig_value_max_iterations", 10 );
+  this->eig = new EVArnoldi ( _conf->gB ( _confSection, "eig_verbose", false ) /* verbose flag */,
+                               eigValueMaxIterations /*eigValueMaxIterations*/
+                            );
 
   this->nrOfEigenvaluesToConsider = std::max ( 1, _conf->gI ( _confSection, "nrOfEigenvaluesToConsider", 1 ) );
   
@@ -1352,31 +1355,28 @@ void FMKGPHyperparameterOptimization::computePredictiveVarianceApproximateFine (
 /*  t.stop();
   std::cerr << "ApproxFine -- time for kernel vector: "  << t.getLast()  << std::endl;*/
     
-//     NICE::Vector multiplicationResults; // will contain nrOfEigenvaluesToConsiderForVarApprox many entries
-//     multiplicationResults.multiply ( *eigenMaxVectorIt, kStar, true/* transpose */ );
+
     NICE::Vector multiplicationResults( this->nrOfEigenvaluesToConsiderForVarApprox-1, 0.0 );
-    //ok, there seems to be a nasty thing in computing multiplicationResults.multiply ( *eigenMaxVectorIt, kStar, true/* transpose */ );
-    //wherefor it takes aeons...
-    //so we compute it by ourselves
-    
-  if ( this->b_debug )
-  {
-    std::cerr << "FMKGPHyp::VarApproxFine  -- nrOfEigenvaluesToConsiderForVarApprox: " << this->nrOfEigenvaluesToConsiderForVarApprox << std::endl;
-    std::cerr << "FMKGPHyp::VarApproxFine  -- initial multiplicationResults: " << multiplicationResults << std::endl;    
-  }    
+
+    if ( this->b_debug )
+    {
+        std::cerr << "FMKGPHyp::VarApproxFine  -- nrOfEigenvaluesToConsiderForVarApprox: " << this->nrOfEigenvaluesToConsiderForVarApprox << std::endl;
+        std::cerr << "FMKGPHyp::VarApproxFine  -- initial multiplicationResults: " << multiplicationResults << std::endl;
+    }
     
     
     
-//     for ( uint tmpI = 0; tmpI < kStar.size(); tmpI++)
     NICE::Matrix::const_iterator eigenVecIt = this->eigenMaxVectors.begin();
-//       double kStarI ( kStar[tmpI] );
-    for ( int tmpJ = 0; tmpJ < this->nrOfEigenvaluesToConsiderForVarApprox-1; tmpJ++)
     {
-      for ( NICE::Vector::const_iterator kStarIt = kStar.begin(); kStarIt != kStar.end(); kStarIt++,eigenVecIt++)
-      {        
-        multiplicationResults[tmpJ] += (*kStarIt) * (*eigenVecIt);//eigenMaxVectors(tmpI,tmpJ);
+    NICE::Vector::iterator multResIt = multiplicationResults.begin();
+    for ( int tmpJ = 0; tmpJ < this->nrOfEigenvaluesToConsiderForVarApprox-1; tmpJ++, multResIt++)
+    {
+      for ( NICE::Vector::const_iterator kStarIt = kStar.begin(); kStarIt != kStar.end(); kStarIt++ ,eigenVecIt++)
+      {
+        (*multResIt) += (*kStarIt) * (*eigenVecIt);
       }
     }
+   }
     
   if ( this->b_debug )
   {
@@ -1391,8 +1391,8 @@ void FMKGPHyperparameterOptimization::computePredictiveVarianceApproximateFine (
 
     while ( cnt < ( this->nrOfEigenvaluesToConsiderForVarApprox - 1 ) )
     {
-      projectionLength = ( *it );
-      currentSecondTerm += ( 1.0 / this->eigenMax[cnt] ) * pow ( projectionLength, 2 );
+      projectionLength        = ( *it );
+      currentSecondTerm      += ( 1.0 / this->eigenMax[cnt] ) * pow ( projectionLength, 2 );
       sumOfProjectionLengths += pow ( projectionLength, 2 );
       
       it++;
@@ -1406,7 +1406,7 @@ void FMKGPHyperparameterOptimization::computePredictiveVarianceApproximateFine (
     
     if ( ( normKStar - sumOfProjectionLengths ) < 0 )
     {
-      std::cerr << "Attention: normKStar - sumOfProjectionLengths is smaller than zero -- strange!" << std::endl;
+        std::cerr << "Attention: normKStar: " << normKStar << " - sumOfProjectionLengths: " <<  sumOfProjectionLengths << " is smaller than zero -- strange!" << std::endl;
     }
     _predVariance = kSelf - currentSecondTerm; 
 }

+ 10 - 21
FastMinKernel.cpp

@@ -713,11 +713,8 @@ void FastMinKernel::hik_kernel_sum(const NICE::VVector & _A,
     }
 
 
-    //NOTE again - pay attention! This is only valid if all entries are NOT negative! - if not, ask wether the current feature is greater than zero. If so, subtract the nrZeroIndices, if not do not
     //sum_{l \in L_k} \alpha_l x^l_k
     double firstPart(0.0);
-    //TODO in the "overnext" line there occurs the following error
-    // Invalid read of size 8
 
 
     if ( !posIsZero && ((position-nrZeroIndices) < this->ui_n)  )
@@ -726,14 +723,7 @@ void FastMinKernel::hik_kernel_sum(const NICE::VVector & _A,
     }
 
     // sum_{u \in U_k} alpha_u
-
-    // sum_{u \in U_k} alpha_u
-    // => double secondPart( B(dim, n-1) - B(dim, position));
-    //TODO in the next line there occurs the following error
-    // Invalid read of size 8
     double secondPart( _B[dim][this->ui_n-1-nrZeroIndices] );
-    //TODO in the "overnext" line there occurs the following error
-    // Invalid read of size 8
 
     if ( !posIsZero && (position >= nrZeroIndices) )
     {
@@ -1353,7 +1343,7 @@ void FastMinKernel::hikComputeKVNApproximation(const NICE::VVector & _A,
   for (SparseVector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++)
   {
 
-    uint dim = i->first;
+    uint dim    = i->first;
     double fval = i->second;
 
     uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
@@ -1379,25 +1369,24 @@ void FastMinKernel::hikComputeKVNApproximation(const NICE::VVector & _A,
 
     double firstPart(0.0);
 
-    if ( !posIsZero && ((position-nrZeroIndices) < this->ui_n) )
+    if ( !posIsZero && ((position-nrZeroIndices) < this->ui_n)  )
+    {
       firstPart = (_A[dim][position-nrZeroIndices]);
-
-
-    if ( _pf != NULL )
-      fval = _pf->f ( dim, fval );
-
-    fval = fval * fval;
+    }
 
     //default value: x_d^* is smaller than every non-zero training example
-    double secondPart( this->ui_n-nrZeroIndices-1 );
+    double secondPart( this->ui_n-nrZeroIndices );
 
     if ( !posIsZero && (position >= nrZeroIndices) )
     {
-      secondPart = (position-nrZeroIndices);
+      secondPart -= (position-nrZeroIndices);
     }
 
+    if ( _pf != NULL )
+      fval = _pf->f ( dim, fval );
+
     // but apply using the transformed one
-    _norm += firstPart + secondPart* fval;
+    _norm += firstPart + secondPart* pow( fval, 2 );
   }
 }
 

+ 11 - 33
FeatureMatrixT.tcc

@@ -425,50 +425,28 @@ namespace NICE {
                                                        uint & _position
                                                       ) const
     {
-      _position = 0;
-      if ( _dim > this->ui_d )
-        return;
-
-      //no non-zero elements?
-      if (this->features[_dim].getNonZeros() <= 0)
-      {
-        // if element is greater than zero, than is should be added at the last position
-        if (_elem > this->features[_dim].getTolerance() )
-          _position = this->ui_n;
-
-        //if not, position is 0
-        return;
-      }
 
-      if (this->features[_dim].getNonZeros() == 1)
-      {
-        // if element is greater than the only nonzero element, then it is larger than everything else
-        if (this->features[_dim].nonzeroElements().begin()->first <= _elem)
-          _position = this->ui_n;
+        // number of zero elements
+        uint nz = this->ui_n - this->features[_dim].getNonZeros();
+        // number of non-zero elements
+        uint nnz = this->features[_dim].getNonZeros();
 
-        //if not, but the element is still greater than zero, than
-        else if (_elem > this->features[_dim].getTolerance() )
-          _position = this->ui_n -1;
+        _position = 0; // this is the index of the first non-zero element.
 
-        return;
-      }
-
-
-      // standard case - not everything is zero and not only a single element is zero
+      // standard case
 
       // find pointer to last non-zero element
-      // FIXME no idea why this should be necessary...
-      typename SortedVectorSparse<T>::const_elementpointer it =  this->features[_dim].nonzeroElements().end(); //this is needed !!!
+      typename SortedVectorSparse<T>::const_elementpointer it =  this->features[_dim].nonzeroElements().end();
       // find pointer to first element largern than the given value
-      it = this->features[_dim].nonzeroElements().upper_bound ( _elem ); //if all values are smaller, this does not do anything at all
+      it = this->features[_dim].nonzeroElements().upper_bound ( _elem );
 
       _position = distance( this->features[_dim].nonzeroElements().begin(), it );
 
 
-      if ( _elem > this->features[_dim].getTolerance() )
+      // special case 1: element is (almost) zero -> every other value is considered to be larger
+      if ( _elem >= this->features[_dim].getTolerance() )
       {
-        //position += features[dim].getZeros();
-        _position += this->ui_n - this->features[_dim].getNonZeros();
+          _position += nz;
       }
     }
 

+ 14 - 3
GPHIKRawClassifier.cpp

@@ -118,6 +118,12 @@ void GPHIKRawClassifier::initFromConfig(const Config *_conf,
                                                        ils_min_delta,
                                                        ils_min_residual
                                                      );
+
+  // variables for the eigen value decomposition technique
+  this->b_eig_verbose              = _conf->gB ( _confSection, "eig_verbose", false );
+  this->i_eig_value_max_iterations = _conf->gI ( _confSection, "eig_value_max_iterations", 10 );
+
+
   if ( this->b_verbose )
   {
       std::cerr << "GPHIKRawClassifier::initFromConfig " <<std::endl;
@@ -128,6 +134,8 @@ void GPHIKRawClassifier::initFromConfig(const Config *_conf,
       std::cerr << "   ils_min_delta " << ils_min_delta << std::endl;
       std::cerr << "   ils_min_residual " << ils_min_residual << std::endl;
       std::cerr << "   ils_verbose " << ils_verbose << std::endl;
+      std::cerr << "   b_eig_verbose " << b_eig_verbose << std::endl;
+      std::cerr << "   i_eig_value_max_iterations " << i_eig_value_max_iterations << std::endl;
   }
 }
 
@@ -336,9 +344,12 @@ void GPHIKRawClassifier::train ( const std::vector< const NICE::SparseVector *>
   // for reproducibility during debuggin
   srand ( 0 );
   srand48 ( 0 );
-  NICE::EigValues * eig = new EVArnoldi ( false /* verbose flag */,
-                                        10 /*_maxiterations*/
-                                      );
+
+  NICE::EigValues * eig = new EVArnoldi ( this->b_eig_verbose ,
+                                          this->i_eig_value_max_iterations
+                                        );
+
+
   eig->getEigenvalues( *gm, eigenMax, eigenMaxV, 1 /*rank*/ );
   delete eig;
 

+ 7 - 0
GPHIKRawClassifier.h

@@ -57,6 +57,13 @@ class GPHIKRawClassifier //: public NICE::Persistent
     /** Header in configfile where variable settings are stored */
     std::string confSection;
 
+    //////////////////////////////////////
+    //     EigenValue Decomposition     //
+    //////////////////////////////////////
+
+    bool b_eig_verbose;
+    int i_eig_value_max_iterations;
+
     //////////////////////////////////////
     // classification related variables //
     //////////////////////////////////////

+ 2 - 1
matlab/GPHIKClassifierMex.cpp

@@ -116,7 +116,8 @@ NICE::Config parseParametersGPHIKClassifier(const mxArray *prhs[], int nrhs)
     // READ STRICT POSITIVE INT VARIABLES
     /////////////////////////////////////////
     if ( (variable == "num_bins") || 
-         (variable == "ils_max_iterations")
+         (variable == "ils_max_iterations") ||
+         (variable == "eig_value_max_iterations")
        )
     {
       if ( mxIsDouble( prhs[i+1] ) )

+ 3 - 1
matlab/GPHIKRawClassifierMex.cpp

@@ -92,7 +92,9 @@ NICE::Config parseParametersGPHIKRawClassifier(const mxArray *prhs[], int nrhs)
     /////////////////////////////////////////
     // READ STRICT POSITIVE INT VARIABLES
     /////////////////////////////////////////
-    if ( variable == "ils_max_iterations" )
+    if ( ( variable == "ils_max_iterations" )||
+         ( variable == "eig_value_max_iterations" )
+       )
     {
       if ( mxIsDouble( prhs[i+1] ) )
       {