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

classification and variance prediction fixed, still with debug outputs

Alexander Freytag 9 жил өмнө
parent
commit
5646db0dd6

+ 111 - 84
FMKGPHyperparameterOptimization.cpp

@@ -661,15 +661,17 @@ inline void FMKGPHyperparameterOptimization::setupGPLikelihoodApprox ( GPLikelih
   gplike = new GPLikelihoodApprox ( binaryLabels, ikmsum, linsolver, eig, verifyApproximation, nrOfEigenvaluesToConsider );
   gplike->setDebug( this->b_debug );
   gplike->setVerbose( this->b_verbose );
-  parameterVectorSize = ikmsum->getNumParameters();
+  parameterVectorSize = this->ikmsum->getNumParameters();
 }
 
-void FMKGPHyperparameterOptimization::updateEigenDecomposition( const int & i_noEigenValues )
+void FMKGPHyperparameterOptimization::updateEigenDecomposition( const int & _noEigenValues )
 {
   //compute the largest eigenvalue of K + noise   
+  
+  std::cerr << "IKM rows: " << ikmsum->rows() << " cols: " << ikmsum->cols() << std::endl;
   try 
   {
-    eig->getEigenvalues ( *ikmsum,  eigenMax, eigenMaxVectors, i_noEigenValues  );
+    this->eig->getEigenvalues ( *ikmsum,  eigenMax, eigenMaxVectors, _noEigenValues  );
   }
   catch ( char const* exceptionMsg)
   {
@@ -683,7 +685,7 @@ void FMKGPHyperparameterOptimization::updateEigenDecomposition( const int & i_no
 
 void FMKGPHyperparameterOptimization::performOptimization ( GPLikelihoodApprox & gplike, const uint & parameterVectorSize )
 {
-  if (verbose)
+  if ( this->b_verbose )
     std::cerr << "perform optimization" << std::endl;
     
   if ( optimizationMethod == OPT_GREEDY )
@@ -855,7 +857,7 @@ void FMKGPHyperparameterOptimization::optimize ( const sparse_t & data, const NI
   if ( fmk != NULL ) delete fmk;
   fmk = new FastMinKernel ( data, noise, examples );
   t.stop();
-  if (verboseTime)
+  if ( this->b_verboseTime )
     std::cerr << "Time used for initializing the FastMinKernel structure: " << t.getLast() << std::endl;
   
   optimize ( y );
@@ -905,7 +907,10 @@ int FMKGPHyperparameterOptimization::prepareBinaryLabels ( std::map<int, NICE::V
     this->i_binaryLabelPositive = *classIt;
     
     if ( this->b_verbose )
+    {
       std::cerr << "positiveClass : " << this->i_binaryLabelPositive << " negativeClass: " << this->i_binaryLabelNegative << std::endl;
+      std::cerr << " all labels: " << y << std::endl << std::endl;
+    }
 
     for ( uint i = 0 ; i < yb.size() ; i++ )
       yb[i] = ( y[i] == this->i_binaryLabelNegative ) ? -1.0 : 1.0;
@@ -967,12 +972,12 @@ void FMKGPHyperparameterOptimization::optimize ( std::map<int, NICE::Vector> & b
   //how many different classes do we have right now?
   int nrOfClasses = binaryLabels.size();
   
-  if (verbose)
+  if ( this->b_verbose )
   {
-    std::cerr << "Initial noise level: " << fmk->getNoise() << std::endl;
+    std::cerr << "Initial noise level: " << this->fmk->getNoise() << std::endl;
 
     std::cerr << "Number of classes (=1 means we have a binary setting):" << nrOfClasses << std::endl;
-    std::cerr << "Effective number of classes (neglecting classes without positive examples): " << knownClasses.size() << std::endl;
+    std::cerr << "Effective number of classes (neglecting classes without positive examples): " << this->knownClasses.size() << std::endl;
   }
 
   // combine standard model and noise model
@@ -981,7 +986,7 @@ void FMKGPHyperparameterOptimization::optimize ( std::map<int, NICE::Vector> & b
 
   t1.start();
   //setup the kernel combination
-  ikmsum = new IKMLinearCombination ();
+  this->ikmsum = new IKMLinearCombination ();
 
   if ( this->b_verbose )
   {
@@ -989,15 +994,15 @@ void FMKGPHyperparameterOptimization::optimize ( std::map<int, NICE::Vector> & b
   }
 
   //First model: noise
-  ikmsum->addModel ( new IKMNoise ( fmk->get_n(), fmk->getNoise(), optimizeNoise ) );
+  this->ikmsum->addModel ( new IKMNoise ( this->fmk->get_n(), this->fmk->getNoise(), this->optimizeNoise ) );
   
   // set pretty low built-in noise, because we explicitely add the noise with the IKMNoise
-  fmk->setNoise ( 0.0 );
+  this->fmk->setNoise ( 0.0 );
 
-  ikmsum->addModel ( new GMHIKernel ( fmk, pf, NULL /* no quantization */ ) );
+  this->ikmsum->addModel ( new GMHIKernel ( this->fmk, this->pf, NULL /* no quantization */ ) );
 
   t1.stop();
-  if (verboseTime)
+  if ( this->b_verboseTime )
     std::cerr << "Time used for setting up the ikm-objects: " << t1.getLast() << std::endl;
 
   GPLikelihoodApprox * gplike;
@@ -1007,10 +1012,10 @@ void FMKGPHyperparameterOptimization::optimize ( std::map<int, NICE::Vector> & b
   this->setupGPLikelihoodApprox ( gplike, binaryLabels, parameterVectorSize );
   t1.stop();
     
-  if (verboseTime)
+  if ( this->b_verboseTime )
     std::cerr << "Time used for setting up the gplike-objects: " << t1.getLast() << std::endl;
 
-  if (verbose)
+  if ( this->b_verbose )
   {
     std::cerr << "parameterVectorSize: " << parameterVectorSize << std::endl;
   }
@@ -1018,18 +1023,21 @@ void FMKGPHyperparameterOptimization::optimize ( std::map<int, NICE::Vector> & b
   t1.start();
   // we compute all needed eigenvectors for standard classification and variance prediction at ones.
   // nrOfEigenvaluesToConsiderForVarApprox should NOT be larger than 1 if a method different than approximate_fine is used!
+  std::cerr << "EV for Arnoldi: " << std::max ( this->nrOfEigenvaluesToConsider, this->nrOfEigenvaluesToConsiderForVarApprox) << std::endl;
+  
   this->updateEigenDecomposition(  std::max ( this->nrOfEigenvaluesToConsider, this->nrOfEigenvaluesToConsiderForVarApprox) );
+    
   t1.stop();
-  if (verboseTime)
+  if ( this->b_verboseTime )
     std::cerr << "Time used for setting up the eigenvectors-objects: " << t1.getLast() << std::endl;
 
   if ( this->b_verbose )
-    std::cerr << "resulting eigenvalues for first class: " << eigenMax[0] << std::endl;
+    std::cerr << "resulting eigenvalues for first class: " << this->eigenMax[0] << std::endl;
 
   t1.start();
   this->performOptimization ( *gplike, parameterVectorSize );
   t1.stop();
-  if (verboseTime)
+  if ( this->b_verboseTime )
     std::cerr << "Time used for performing the optimization: " << t1.getLast() << std::endl;
 
   if ( this->b_verbose )
@@ -1038,13 +1046,13 @@ void FMKGPHyperparameterOptimization::optimize ( std::map<int, NICE::Vector> & b
   t1.start();
   this->transformFeaturesWithOptimalParameters ( *gplike, parameterVectorSize );
   t1.stop();
-  if (verboseTime)
+  if ( this->b_verboseTime )
     std::cerr << "Time used for transforming features with optimal parameters: " << t1.getLast() << std::endl;
 
   t1.start();
   this->computeMatricesAndLUTs ( *gplike );
   t1.stop();
-  if (verboseTime)
+  if ( this->b_verboseTime )
     std::cerr << "Time used for setting up the A'nB -objects: " << t1.getLast() << std::endl;
 
   t.stop();
@@ -1093,20 +1101,20 @@ int FMKGPHyperparameterOptimization::classify ( const NICE::SparseVector & xstar
     fthrow ( Exception, "The precomputation vector is zero...have you trained this classifier?" );
   }
 
-  uint maxClassNo = 0;
-  for ( std::map<int, PrecomputedType>::const_iterator i = precomputedA.begin() ; i != precomputedA.end(); i++ )
+  int maxClassNo = 0;
+  for ( std::map<int, PrecomputedType>::const_iterator i = this->precomputedA.begin() ; i != this->precomputedA.end(); i++ )
   {
-    uint classno = i->first;
+    int classno = i->first;
     maxClassNo = std::max ( maxClassNo, classno );
     double beta;
 
-    if ( q != NULL ) {
-      std::map<int, double *>::const_iterator j = precomputedT.find ( classno );
+    if ( this->q != NULL ) {
+      std::map<int, double *>::const_iterator j = this->precomputedT.find ( classno );
       double *T = j->second;
-      fmk->hik_kernel_sum_fast ( T, *q, xstar, beta );
+      this->fmk->hik_kernel_sum_fast ( T, *q, xstar, beta );
     } else {
       const PrecomputedType & A = i->second;
-      std::map<int, PrecomputedType>::const_iterator j = precomputedB.find ( classno );
+      std::map<int, PrecomputedType>::const_iterator j = this->precomputedB.find ( classno );
       const PrecomputedType & B = j->second;
 
       // fmk->hik_kernel_sum ( A, B, xstar, beta ); if A, B are of type Matrix
@@ -1116,14 +1124,14 @@ int FMKGPHyperparameterOptimization::classify ( const NICE::SparseVector & xstar
       // searching for upper and lower bounds ( findFirst... functions ) require original feature
       // values as inputs. However, for calculation we need the transformed features values.
 
-      fmk->hik_kernel_sum ( A, B, xstar, beta, pf );
+      this->fmk->hik_kernel_sum ( A, B, xstar, beta, pf );
     }
 
     scores[ classno ] = beta;
   }
   scores.setDim ( maxClassNo + 1 );
   
-  if ( precomputedA.size() > 1 )
+  if ( this->precomputedA.size() > 1 )
   { // multi-class classification
     return scores.maxElement();
   }
@@ -1140,26 +1148,32 @@ int FMKGPHyperparameterOptimization::classify ( const NICE::SparseVector & xstar
 
 int FMKGPHyperparameterOptimization::classify ( const NICE::Vector & xstar, NICE::SparseVector & scores ) const
 {
+  
   // loop through all classes
-  if ( precomputedA.size() == 0 )
+  if ( this->precomputedA.size() == 0 )
   {
     fthrow ( Exception, "The precomputation vector is zero...have you trained this classifier?" );
   }
 
-  uint maxClassNo = 0;
-  for ( std::map<int, PrecomputedType>::const_iterator i = precomputedA.begin() ; i != precomputedA.end(); i++ )
+  int maxClassNo = -std::numeric_limits<int>::max();
+  for ( std::map<int, PrecomputedType>::const_iterator i = this->precomputedA.begin() ; i != this->precomputedA.end(); i++ )
   {
-    uint classno = i->first;
+    int classno = i->first;
+    std::cerr << " classno: " << classno  << std::endl;
     maxClassNo = std::max ( maxClassNo, classno );
+    std::cerr << " current max class number: " << maxClassNo << std::endl;
     double beta;
 
-    if ( q != NULL ) {
-      std::map<int, double *>::const_iterator j = precomputedT.find ( classno );
+    if ( this->q != NULL )
+    {
+      std::map<int, double *>::const_iterator j = this->precomputedT.find ( classno );
       double *T = j->second;
-      fmk->hik_kernel_sum_fast ( T, *q, xstar, beta );
-    } else {
+      this->fmk->hik_kernel_sum_fast ( T, *q, xstar, beta );
+    }
+    else
+    {
       const PrecomputedType & A = i->second;
-      std::map<int, PrecomputedType>::const_iterator j = precomputedB.find ( classno );
+      std::map<int, PrecomputedType>::const_iterator j = this->precomputedB.find ( classno );
       const PrecomputedType & B = j->second;
 
       // fmk->hik_kernel_sum ( A, B, xstar, beta ); if A, B are of type Matrix
@@ -1169,20 +1183,24 @@ int FMKGPHyperparameterOptimization::classify ( const NICE::Vector & xstar, NICE
       // searching for upper and lower bounds ( findFirst... functions ) require original feature
       // values as inputs. However, for calculation we need the transformed features values.
 
-      fmk->hik_kernel_sum ( A, B, xstar, beta, pf );
+      std::cerr << " call this->fmk->hik_kernel_sum ( A, B, xstar, beta, pf ); " << std::endl;
+      this->fmk->hik_kernel_sum ( A, B, xstar, beta, this->pf );
     }
 
+    std::cerr << "score for " << classno << " : " << beta << std::endl;
     scores[ classno ] = beta;
   }
   scores.setDim ( maxClassNo + 1 );
   
-  if ( precomputedA.size() > 1 )
+  
+  if ( this->precomputedA.size() > 1 )
   { // multi-class classification
     return scores.maxElement();
   }
   else if ( this->knownClasses.size() == 2 ) // binary setting
   {      
     scores[ this->i_binaryLabelNegative ] = -scores[ this->i_binaryLabelPositive ];     
+        
     return scores[ this->i_binaryLabelPositive ] <= 0.0 ? this->i_binaryLabelNegative : this->i_binaryLabelPositive;
   }
   else //OCC or regression setting
@@ -1312,7 +1330,7 @@ void FMKGPHyperparameterOptimization::computePredictiveVarianceApproximateFine (
 void FMKGPHyperparameterOptimization::computePredictiveVarianceExact ( const NICE::SparseVector & x, double & predVariance ) const
 {
   // security check!  
-  if ( ikmsum->getNumberOfModels() == 0 )
+  if ( this->ikmsum->getNumberOfModels() == 0 )
   {
     fthrow ( Exception, "ikmsum is empty... have you trained this classifer? Aborting..." );
   }  
@@ -1323,7 +1341,7 @@ void FMKGPHyperparameterOptimization::computePredictiveVarianceExact ( const NIC
   double kSelf ( 0.0 );
   for ( NICE::SparseVector::const_iterator it = x.begin(); it != x.end(); it++ )
   {
-    kSelf += pf->f ( 0, it->second );
+    kSelf += this->pf->f ( 0, it->second );
     // if weighted dimensions:
     //kSelf += pf->f(it->first,it->second);
   }
@@ -1412,10 +1430,12 @@ void FMKGPHyperparameterOptimization::computePredictiveVarianceApproximateRough
   predVariance = kSelf - ( 1.0 / eigenMax[0] )* normKStar;
 }
 
-void FMKGPHyperparameterOptimization::computePredictiveVarianceApproximateFine ( const NICE::Vector & x, double & predVariance ) const
+void FMKGPHyperparameterOptimization::computePredictiveVarianceApproximateFine ( const NICE::Vector & _x, 
+                                                                                 double & _predVariance 
+                                                                               ) const
 {
   // security check!
-  if ( eigenMaxVectors.rows() == 0 )
+  if ( this->eigenMaxVectors.rows() == 0 )
   {
       fthrow ( Exception, "eigenMaxVectors is empty...have you trained this classifer? Aborting..." );
   }  
@@ -1423,16 +1443,16 @@ void FMKGPHyperparameterOptimization::computePredictiveVarianceApproximateFine (
   // ---------------- compute the first term --------------------
 
   double kSelf ( 0.0 );
-  int dim ( 0 );
-  for ( NICE::Vector::const_iterator it = x.begin(); it != x.end(); it++, dim++ )
+  uint dim ( 0 );
+  for ( NICE::Vector::const_iterator it = _x.begin(); it != _x.end(); it++, dim++ )
   {
-    kSelf += pf->f ( 0, *it );
+    kSelf += this->pf->f ( 0, *it );
     // if weighted dimensions:
     //kSelf += pf->f(dim,*it);
   }
   // ---------------- compute the approximation of the second term --------------------
   NICE::Vector kStar;
-  fmk->hikComputeKernelVector ( x, kStar );
+  this->fmk->hikComputeKernelVector ( _x, kStar );
 
 
     //ok, there seems to be a nasty thing in computing multiplicationResults.multiply ( *eigenMaxVectorIt, kStar, true/* transpose */ );
@@ -1441,9 +1461,9 @@ void FMKGPHyperparameterOptimization::computePredictiveVarianceApproximateFine (
 //     NICE::Vector multiplicationResults; // will contain nrOfEigenvaluesToConsiderForVarApprox many entries
 //     multiplicationResults.multiply ( *eigenMaxVectorIt, kStar, true/* transpose */ );
 
-    NICE::Vector multiplicationResults( nrOfEigenvaluesToConsiderForVarApprox-1, 0.0 );
-    NICE::Matrix::const_iterator eigenVecIt = eigenMaxVectors.begin();
-    for ( int tmpJ = 0; tmpJ < nrOfEigenvaluesToConsiderForVarApprox-1; tmpJ++)
+    NICE::Vector multiplicationResults(this-> nrOfEigenvaluesToConsiderForVarApprox-1, 0.0 );
+    NICE::Matrix::const_iterator eigenVecIt = this->eigenMaxVectors.begin();
+    for ( int tmpJ = 0; tmpJ < this->nrOfEigenvaluesToConsiderForVarApprox-1; tmpJ++)
     {
       for ( NICE::Vector::const_iterator kStarIt = kStar.begin(); kStarIt != kStar.end(); kStarIt++,eigenVecIt++)
       {        
@@ -1457,10 +1477,10 @@ void FMKGPHyperparameterOptimization::computePredictiveVarianceApproximateFine (
     int cnt ( 0 );
     NICE::Vector::const_iterator it = multiplicationResults.begin();
 
-    while ( cnt < ( nrOfEigenvaluesToConsiderForVarApprox - 1 ) )
+    while ( cnt < ( this->nrOfEigenvaluesToConsiderForVarApprox - 1 ) )
     {
       projectionLength = ( *it );
-      currentSecondTerm += ( 1.0 / eigenMax[cnt] ) * pow ( projectionLength, 2 );
+      currentSecondTerm += ( 1.0 / this->eigenMax[cnt] ) * pow ( projectionLength, 2 );
       sumOfProjectionLengths += pow ( projectionLength, 2 );
       
       it++;
@@ -1470,43 +1490,48 @@ void FMKGPHyperparameterOptimization::computePredictiveVarianceApproximateFine (
     
     double normKStar ( pow ( kStar.normL2 (), 2 ) );
 
-    currentSecondTerm += ( 1.0 / eigenMax[nrOfEigenvaluesToConsiderForVarApprox-1] ) * ( normKStar - sumOfProjectionLengths );
+    currentSecondTerm += ( 1.0 / this->eigenMax[nrOfEigenvaluesToConsiderForVarApprox-1] ) * ( normKStar - sumOfProjectionLengths );
     
 
     if ( ( normKStar - sumOfProjectionLengths ) < 0 )
     {
       std::cerr << "Attention: normKStar - sumOfProjectionLengths is smaller than zero -- strange!" << std::endl;
     }
-    predVariance = kSelf - currentSecondTerm; 
+    _predVariance = kSelf - currentSecondTerm; 
 }
 
-void FMKGPHyperparameterOptimization::computePredictiveVarianceExact ( const NICE::Vector & x, double & predVariance ) const
+void FMKGPHyperparameterOptimization::computePredictiveVarianceExact ( const NICE::Vector & _x, 
+                                                                       double & _predVariance 
+                                                                     ) const
 {
-  if ( ikmsum->getNumberOfModels() == 0 )
+  if ( this->ikmsum->getNumberOfModels() == 0 )
   {
     fthrow ( Exception, "ikmsum is empty... have you trained this classifer? Aborting..." );
   }  
 
   // ---------------- compute the first term --------------------
   double kSelf ( 0.0 );
-  int dim ( 0 );
-  for ( NICE::Vector::const_iterator it = x.begin(); it != x.end(); it++, dim++ )
+  uint dim ( 0 );
+  for ( NICE::Vector::const_iterator it = _x.begin(); it != _x.end(); it++, dim++ )
   {
-    kSelf += pf->f ( 0, *it );
+    kSelf += this->pf->f ( 0, *it );
     // if weighted dimensions:
     //kSelf += pf->f(dim,*it);
   }
+  
 
   // ---------------- compute the second term --------------------
   NICE::Vector kStar;
-  fmk->hikComputeKernelVector ( x, kStar );
+  this->fmk->hikComputeKernelVector ( _x, kStar );
 
+  std::cerr << " kStar: " << kStar << std::endl;
+  
   //now run the ILS method
   NICE::Vector diagonalElements;
-  ikmsum->getDiagonalElements ( diagonalElements );
+  this->ikmsum->getDiagonalElements ( diagonalElements );
 
   // init simple jacobi pre-conditioning
-  ILSConjugateGradients *linsolver_cg = dynamic_cast<ILSConjugateGradients *> ( linsolver );
+  ILSConjugateGradients *linsolver_cg = dynamic_cast<ILSConjugateGradients *> ( this->linsolver );
 
 
   //perform pre-conditioning
@@ -1527,13 +1552,15 @@ void FMKGPHyperparameterOptimization::computePredictiveVarianceExact ( const NIC
       * v = k_*
       *  This reduces the number of iterations by 5 or 8
       */  
-  beta = (kStar * (1.0 / eigenMax[0]) );
-  linsolver->solveLin ( *ikmsum, kStar, beta );
+  beta = (kStar * (1.0 / this->eigenMax[0]) );
+  this->linsolver->solveLin ( *ikmsum, kStar, beta );
 
   beta *= kStar;
   
   double currentSecondTerm( beta.Sum() );
-  predVariance = kSelf - currentSecondTerm;
+  
+  std::cerr << "kSelf: " << kSelf <<  " currentSecondTerm: " << currentSecondTerm << std::endl;
+  _predVariance = kSelf - currentSecondTerm;
 }    
 
 ///////////////////// INTERFACE PERSISTENT /////////////////////
@@ -1604,13 +1631,13 @@ void FMKGPHyperparameterOptimization::restore ( std::istream & _is,
       ///////////////////////////////////
       if ( tmp.compare("verbose") == 0 )
       {
-        _is >> verbose;
+        _is >> this->b_verbose;
         _is >> tmp; // end of block 
         tmp = this->removeEndTag ( tmp );
       }
       else if ( tmp.compare("verboseTime") == 0 )
       {
-        _is >> verboseTime;
+        _is >> this->b_verboseTime;
         _is >> tmp; // end of block 
         tmp = this->removeEndTag ( tmp );
       }
@@ -1625,36 +1652,36 @@ void FMKGPHyperparameterOptimization::restore ( std::istream & _is,
       //////////////////////////////////////
       else if ( tmp.compare("b_performRegression") == 0 )
       {
-        _is >> b_performRegression;
+        _is >> this->b_performRegression;
         _is >> tmp; // end of block 
         tmp = this->removeEndTag ( tmp );
       }     
       else if ( tmp.compare("fmk") == 0 )
       {
-        if ( fmk != NULL )
-          delete fmk;        
-        fmk = new FastMinKernel();
-        fmk->restore( _is, _format );
+        if ( this->fmk != NULL )
+          delete this->fmk;        
+        this->fmk = new FastMinKernel();
+        this->fmk->restore( _is, _format );
 
         _is >> tmp; // end of block 
         tmp = this->removeEndTag ( tmp );
       }
       else if ( tmp.compare("q") == 0 )
       {
-        std::string _isNull;
+        std::string isNull;
         _is >> isNull; // NOTNULL or NULL
         if (isNull.compare("NOTNULL") == 0)
         {   
-          if ( q != NULL )
-            delete q;
-          q = new Quantization();
-          q->restore ( _is, _format );
+          if ( this->q != NULL )
+            delete this->q;
+          this->q = new Quantization();
+          this->q->restore ( _is, _format );
         }
         else
         {
-          if ( q != NULL )
-            delete q;
-          q = NULL;
+          if ( this->q != NULL )
+            delete this->q;
+          this->q = NULL;
         }
         _is >> tmp; // end of block 
         tmp = this->removeEndTag ( tmp );        
@@ -1693,7 +1720,7 @@ void FMKGPHyperparameterOptimization::restore ( std::istream & _is,
           fthrow(Exception, "Transformation type is unknown " << transform);
         }
         
-        pf->restore(is, _format);
+        this->pf->restore( _is, _format);
         
         _is >> tmp; // end of block 
         tmp = this->removeEndTag ( tmp );
@@ -2407,7 +2434,7 @@ void FMKGPHyperparameterOptimization::addExample( const NICE::SparseVector * exa
   // could be dealt with implicitely.
   // Therefore, we insert its label here...
   if ( (newClasses.size() > 0 ) && ( (this->knownClasses.size() - newClasses.size() ) == 2 ) )
-    newClasses.insert( binaryLabelNegative );    
+    newClasses.insert( this->i_binaryLabelNegative );    
 
   // add the new example to our data structure
   // It is necessary to do this already here and not lateron for internal reasons (see GMHIKernel for more details)
@@ -2490,7 +2517,7 @@ void FMKGPHyperparameterOptimization::addMultipleExamples( const std::vector< co
     // could be dealt with implicitely.
     // Therefore, we insert its label here...
     if ( (newClasses.size() > 0 ) && ( (this->knownClasses.size() - newClasses.size() ) == 2 ) )
-      newClasses.insert( binaryLabelNegative );      
+      newClasses.insert( this->i_binaryLabelNegative );      
       
   }
   // in a regression setting, we do not have to remember any "class labels"

+ 100 - 25
FastMinKernel.cpp

@@ -159,6 +159,7 @@ void FastMinKernel::hik_prepare_kernel_multiplications(const std::vector< const
                                                       )
 {
   //FIXME why do we hand over the feature matrix here?  
+  std::cerr << "FastMinKernel::hik_prepare_kernel_multiplications -- size of _X : " << _X.size() << std::endl;
   _X_sorted.set_features( _X, _dimensionsOverExamples, _dim );
 }
 
@@ -550,7 +551,7 @@ void FastMinKernel::hik_kernel_multiply(const NICE::VVector & _A,
       // have to care about them, because it is multiplied with
       // the feature value
       // DEBUG for Björns code
-      if ( (uint)dim >= _B.size() )
+      if ( dim >= _B.size() )
         fthrow(Exception, "dim exceeds B.size: " << dim << " " << _B.size() );
       if ( _B[dim].size() == 0 )
         fthrow(Exception, "B[dim] is empty");
@@ -618,7 +619,12 @@ void FastMinKernel::hik_kernel_sum(const NICE::VVector & _A,
     uint dim = i->first;
     double fval = i->second;
     
-    uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
+    std::cerr << " FastMinKernel::hik_kernel_sum (sparse)-- dim : " << dim << std::endl;    
+    std::cerr << " FastMinKernel::hik_kernel_sum (sparse)-- fval : " << fval << std::endl;    
+    
+    uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);    
+    std::cerr << " FastMinKernel::hik_kernel_sum -- nrZeroIndices : " << nrZeroIndices << std::endl;    
+    
     if ( nrZeroIndices == this->ui_n ) {
       // all features are zero and let us ignore it completely
       continue;
@@ -630,15 +636,29 @@ void FastMinKernel::hik_kernel_sum(const NICE::VVector & _A,
     //the sorted array? -> perform binary search, runtime O(log(n))
     // search using the original value
     this->X_sorted.findFirstLargerInDimension(dim, fval, position);
-    position--;
+    
+    bool posIsZero ( position == 0 );
+    
+    if ( !posIsZero )
+    {
+      position--;
+    }
+    
+    std::cerr << " FastMinKernel::hik_kernel_sum -- position : " << position << std::endl;
+    
+    std::cerr << " FastMinKernel::hik_kernel_sum -- this->ui_n : " << this->ui_n << std::endl;    
   
     //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 (position >= 0) 
+    if ( !posIsZero && ((position-nrZeroIndices) < this->ui_n) ) 
+    {
+      std::cerr << " attempt to access A in " << position-nrZeroIndices;
       firstPart = (_A[dim][position-nrZeroIndices]);
+      std::cerr << " with : " << firstPart << std::endl;
+    }      
     
     // sum_{u \in U_k} alpha_u
     
@@ -646,17 +666,24 @@ void FastMinKernel::hik_kernel_sum(const NICE::VVector & _A,
     // => double secondPart( B(dim, n-1) - B(dim, position));
     //TODO in the next line there occurs the following error
     // Invalid read of size 8      
+    std::cerr << " attempt to access B in " << this->ui_n-1-nrZeroIndices;    
     double secondPart( _B[dim][this->ui_n-1-nrZeroIndices]);
+    std::cerr << " with : " << secondPart << std::endl;    
     //TODO in the "overnext" line there occurs the following error
     // Invalid read of size 8    
-    if (position >= 0) 
+    if ( !posIsZero && (position >= nrZeroIndices) )
+    {
+      std::cerr << " attempt to access B in " << position-nrZeroIndices;
       secondPart-= _B[dim][position-nrZeroIndices];
+      std::cerr << " with : " << _B[dim][position-nrZeroIndices] << std::endl;
+    }
     
     if ( _pf != NULL )
     {
       fval = _pf->f ( dim, fval );
     }   
     
+    std::cerr << " result dim " << dim << " impact " << firstPart + secondPart* fval << " firstPart " << firstPart << " secondPart " << secondPart <<  " fval " << fval << std::endl;    
     // but apply using the transformed one
     _beta += firstPart + secondPart* fval;
   }
@@ -673,10 +700,14 @@ void FastMinKernel::hik_kernel_sum(const NICE::VVector & _A,
   uint dim ( 0 );
   for (NICE::Vector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++, dim++)
   {
+    std::cerr << " FastMinKernel::hik_kernel_sum -- dim : " << dim << std::endl;
   
     double fval = *i;
+    std::cerr << " FastMinKernel::hik_kernel_sum -- fval : " << fval << std::endl;
     
     uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
+    std::cerr << " FastMinKernel::hik_kernel_sum -- nrZeroIndices : " << nrZeroIndices << std::endl;
+    
     if ( nrZeroIndices == this->ui_n ) {
       // all features are zero and let us ignore it completely
       continue;
@@ -688,15 +719,33 @@ void FastMinKernel::hik_kernel_sum(const NICE::VVector & _A,
     //the sorted array? -> perform binary search, runtime O(log(n))
     // search using the original value
     this->X_sorted.findFirstLargerInDimension(dim, fval, position);
-    position--;
+    
+    bool posIsZero ( position == 0 );
+    
+    if ( !posIsZero )
+    {
+      position--;
+    }
+        
+    std::cerr << " FastMinKernel::hik_kernel_sum -- position : " << position << std::endl;
+    
+    std::cerr << " FastMinKernel::hik_kernel_sum -- this->ui_n : " << this->ui_n << std::endl;
+    
+    
   
     //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 (position >= 0) 
+    
+    
+    if ( !posIsZero && ((position-nrZeroIndices) < this->ui_n)  ) 
+    {
+      std::cerr << " attempt to access A in " << position-nrZeroIndices;
       firstPart = (_A[dim][position-nrZeroIndices]);
+      std::cerr << " with : " << firstPart << std::endl;
+    }
     
     // sum_{u \in U_k} alpha_u
     
@@ -704,17 +753,27 @@ void FastMinKernel::hik_kernel_sum(const NICE::VVector & _A,
     // => 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]);
+    std::cerr << " attempt to access B in " << this->ui_n-1-nrZeroIndices;
+    double secondPart( _B[dim][this->ui_n-1-nrZeroIndices] );
+    std::cerr << " with : " << secondPart << std::endl;
     //TODO in the "overnext" line there occurs the following error
     // Invalid read of size 8    
-    if (position >= 0) 
+    
+    if ( !posIsZero && (position >= nrZeroIndices) )
+    {
+      std::cerr << " attempt to access B in " << position-nrZeroIndices;
       secondPart-= _B[dim][position-nrZeroIndices];
+      std::cerr << " with : " << _B[dim][position-nrZeroIndices] << std::endl;
+    }
+    
+      
     
     if ( _pf != NULL )
     {
       fval = _pf->f ( dim, fval );
     }   
     
+    std::cerr << " result dim " << dim << " impact " << firstPart + secondPart* fval << " firstPart " << firstPart << " secondPart " << secondPart <<  " fval " << fval << std::endl;
     // but apply using the transformed one
     _beta += firstPart + secondPart* fval;
   }
@@ -1302,31 +1361,37 @@ void FastMinKernel::hikComputeKVNApproximation(const NICE::VVector & _A,
       continue;
     }
 
+    
     uint position;
 
     //where is the example x^z_i located in
     //the sorted array? -> perform binary search, runtime O(log(n))
     // search using the original value
     this->X_sorted.findFirstLargerInDimension(dim, fval, position);
-    position--;
+    
+    bool posIsZero ( position == 0 );
+    
+    if ( !posIsZero )
+    {
+      position--;
+    }  
   
     //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
     double firstPart(0.0);
     //TODO in the "overnext" line there occurs the following error
     // Invalid read of size 8    
-    if (position >= 0) 
+    if ( !posIsZero && ((position-nrZeroIndices) < this->ui_n) ) 
       firstPart = (_A[dim][position-nrZeroIndices]);
-    else
-      firstPart = 0.0;
     
-    double secondPart( 0.0);
-      
+     
     if ( _pf != NULL )
       fval = _pf->f ( dim, fval );
     
     fval = fval * fval;
     
-    if (position >= 0) 
+    double secondPart( 0.0);    
+    
+    if ( !posIsZero )
       secondPart = fval * (this->ui_n-nrZeroIndices-(position+1));
     else //if x_d^* is smaller than every non-zero training example
       secondPart = fval * (this->ui_n-nrZeroIndices);
@@ -1384,7 +1449,7 @@ void FastMinKernel::hikComputeKernelVector ( const NICE::SparseVector& _xstar,
     //the sorted array? -> perform binary search, runtime O(log(n))
     // search using the original value
     this->X_sorted.findFirstLargerInDimension(dim, fval, position);
-    position--;
+    //position--;
     
     //get the non-zero elements for this dimension  
     const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
@@ -1395,7 +1460,7 @@ void FastMinKernel::hikComputeKernelVector ( const NICE::SparseVector& _xstar,
     for ( SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin(); i != nonzeroElements.end(); i++, count++ )
     {
       uint origIndex(i->second.first); //orig index (i->second.second would be the transformed feature value)
-      if (count <= position)
+      if (count < position)
         _kstar[origIndex] += i->first; //orig feature value
       else
         _kstar[origIndex] += fval;
@@ -1431,16 +1496,20 @@ void FastMinKernel::hikComputeKVNApproximation(const NICE::VVector & _A,
     //the sorted array? -> perform binary search, runtime O(log(n))
     // search using the original value
     this->X_sorted.findFirstLargerInDimension(dim, fval, position);
-    position--;
+    
+    bool posIsZero ( position == 0 );
+    
+    if ( !posIsZero )
+    {
+      position--;
+    } 
   
     //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
     double firstPart(0.0);
     //TODO in the "overnext" line there occurs the following error
     // Invalid read of size 8    
-    if (position >= 0) 
+    if ( !posIsZero && ((position-nrZeroIndices) < this->ui_n) ) 
       firstPart = (_A[dim][position-nrZeroIndices]);
-    else
-      firstPart = 0.0;
     
     double secondPart( 0.0);
       
@@ -1449,7 +1518,7 @@ void FastMinKernel::hikComputeKVNApproximation(const NICE::VVector & _A,
     
     fval = fval * fval;
     
-    if (position >= 0) 
+    if ( !posIsZero ) 
       secondPart = fval * (this->ui_n-nrZeroIndices-(position+1));
     else //if x_d^* is smaller than every non-zero training example
       secondPart = fval * (this->ui_n-nrZeroIndices);
@@ -1486,6 +1555,9 @@ void FastMinKernel::hikComputeKernelVector( const NICE::Vector & _xstar,
   //init
   _kstar.resize(this->ui_n);
   _kstar.set(0.0);
+
+    std::cerr << "test example: " << _xstar << std::endl;
+    std::cerr << " init _kstar: " << _kstar << std::endl;  
   
   //let's start :)
   uint dim ( 0 );
@@ -1493,6 +1565,7 @@ void FastMinKernel::hikComputeKernelVector( const NICE::Vector & _xstar,
   {
   
     double fval = *i;
+    std::cerr << "   dim: " << dim << " fval: " << fval << std::endl;
     
     uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
     if ( nrZeroIndices == this->ui_n ) {
@@ -1507,7 +1580,9 @@ void FastMinKernel::hikComputeKernelVector( const NICE::Vector & _xstar,
     //the sorted array? -> perform binary search, runtime O(log(n))
     // search using the original value
     this->X_sorted.findFirstLargerInDimension(dim, fval, position);
-    position--;
+    //position--;    
+    
+    std::cerr << "   dim: " << dim << " fval: " << fval << std::endl;
     
     //get the non-zero elements for this dimension  
     const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
@@ -1518,7 +1593,7 @@ void FastMinKernel::hikComputeKernelVector( const NICE::Vector & _xstar,
     for ( SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin(); i != nonzeroElements.end(); i++, count++ )
     {
       uint origIndex(i->second.first); //orig index (i->second.second would be the transformed feature value)
-      if (count <= position)
+      if (count < position)
         _kstar[origIndex] += i->first; //orig feature value
       else
         _kstar[origIndex] += fval;

+ 47 - 39
FeatureMatrixT.tcc

@@ -399,7 +399,7 @@ namespace NICE {
                                                 ) const
     {
       _position = 0;
-      if ( (_dim < 0) || (_dim > this->ui_d))
+      if ( _dim > this->ui_d )
         return;
 
       std::pair< typename SortedVectorSparse<T>::elementpointer, typename SortedVectorSparse<T>::elementpointer > eit;
@@ -420,7 +420,7 @@ namespace NICE {
                                                ) const
     {
       _position = 0;
-      if ( (_dim < 0) || (_dim > this->ui_d))
+      if ( _dim > this->ui_d )
         return;
 
       std::pair< typename SortedVectorSparse<T>::const_elementpointer, typename SortedVectorSparse<T>::const_elementpointer > eit =  this->features[_dim].nonzeroElements().equal_range ( _elem );
@@ -439,7 +439,7 @@ namespace NICE {
                                                       ) const
     {
       _position = 0;
-      if ( (_dim < 0) || (_dim > this->ui_d))
+      if ( _dim > this->ui_d )
         return;
       
       //no non-zero elements?
@@ -449,27 +449,39 @@ namespace NICE {
         if (_elem > this->features[_dim].getTolerance() )
           _position = this->ui_n;
         
-        //if not, position is -1
+        //if not, position is 0
         return;
       }
       
       if (this->features[_dim].getNonZeros() == 1)
       {
-        // if element is greater than the only nonzero element, than it is larger as everything else
+        std::cerr << " FeatureMatrixT<T>::findFirstLargerInDimension  -- only one non-zero element: " << this->features[_dim].getNonZeros() << " with value : " << this->features[_dim].nonzeroElements().begin()->first << std::endl;
+        // 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;
         
         //if not, but the element is still greater than zero, than 
         else if (_elem > this->features[_dim].getTolerance() )
           _position = this->ui_n -1;
-          
+        
+        std::cerr << " -> decided position: " << _position << std::endl;
         return;
       }
       
+      
+      // standard case - not everything is zero and not only a single element is zero
+      
+      // 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 !!!
+      // 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
       
       _position = distance( this->features[_dim].nonzeroElements().begin(), it );
+      
+      
+      T valtmp ( it->second.second );
+      std::cerr << " FeatureMatrixT<T>::findFirstLargerInDimension  --distance is : " << _position << " with value: " << (double) valtmp << std::endl;
 
       if ( _elem > this->features[_dim].getTolerance() )
       {
@@ -630,10 +642,7 @@ namespace NICE {
                                         )
     {
       this->features.clear();
-      if (_dim < 0)
-        this->set_d( _features.size() );
-      else
-        this->set_d( _dim );
+      this->set_d( std::max ( _dim, _features.size() ) );
       
       if ( this->ui_d > 0 )
         this->ui_n = _features[0].size();
@@ -654,10 +663,7 @@ namespace NICE {
                                         )
     {
       this->features.clear();
-      if ( _dim < 0)
-        this->set_d( _features.size() );
-      else
-        this->set_d( _dim );
+      this->set_d( std::max ( _dim, _features.size() ) );
       
       if ( this->ui_d > 0 )
         this->ui_n = _features[0].size();
@@ -677,10 +683,8 @@ namespace NICE {
                                         )
     {
       this->features.clear();
-      if (_dim < 0)
-        this->set_d(_features.size());
-      else
-        this->set_d(_dim);
+      uint featsize ( _features.size() ); // necessary for some compiler reasons
+      this->set_d( std::max ( _dim, featsize ) );
       
       if ( this->ui_d > 0 )
         this->ui_n = _features[0].size();
@@ -703,34 +707,38 @@ namespace NICE {
       {
         std::cerr << "set_features without features" << std::endl;
       }
-            
-      // resize our data structure      
-      if (_dim >= 0) //did the user specified the number of dimensions?
-        this->set_d(_dim);
-      else //dimensions not specified by users
+      
+      
+      // resize our data structure  
+      //therefore, let's first of all figure out if the user specified a dimension or not.
+      uint dimTmp ( _dim ); 
+      
+      if (_dimensionsOverExamples) //do we have dim x examples ?
       {
-        if (_dimensionsOverExamples) //do we have dim x examples ?
+        if ( _features.size() > dimTmp )
         {
-          this->set_d(_features.size());
+          dimTmp = _features.size();
         }
-        else //we have examples x dimes (as usually done)
+      }
+      else //we have examples x dimes (as usually done)
+      {
+        if (_features.size() > 0) //and have at least one example
         {
-          if (_features.size() > 0) //and have at least one example
-          {
-            try{
-              this->set_d(_features[0]->getDim());  
-            }
-            catch(...)
+          try{
+            if ( _features[0]->getDim() > dimTmp )
             {
-              std::cerr << "FeatureMatrixT<T>::set_features -- something went wrong using getDim() of SparseVectors" << std::endl;
+              dimTmp = _features[0]->getDim();
             }
           }
-          else //no example, so set the dim to 0, since we have no idea at all
+          catch(...)
           {
-           this-> set_d(0);
-          }          
-        }
-      }  
+            std::cerr << "FeatureMatrixT<T>::set_features -- something went wrong using getDim() of SparseVectors" << std::endl;
+          }
+        }        
+      }
+      this->set_d( dimTmp ); 
+      
+      std::cerr << "FeatureMatrixT<T>::set_features -- dimension: " << this->get_d() << std::endl;
             
       // set number of examples n
       if ( this->ui_d > 0 )
@@ -754,7 +762,7 @@ namespace NICE {
         if ( this->b_debug )
           std::cerr << "FeatureMatrixT<T>::set_features " << this->ui_n << " new examples" << std::endl;
         //loop over every example to add its content
-        for (iint nr = 0; nr < this->ui_n; nr++)
+        for (uint nr = 0; nr < this->ui_n; nr++)
         {
           if ( this->b_debug )
             std::cerr << "add feature nr. " << nr << " / " << _features.size() << " ";

+ 31 - 9
GPHIKClassifier.cpp

@@ -163,11 +163,14 @@ void GPHIKClassifier::classify ( const NICE::Vector * example,  int & result, Sp
 
 void GPHIKClassifier::classify ( const SparseVector * example,  int & result, SparseVector & scores, double & uncertainty ) const
 {
+  std::cerr << " classify sparse vectors " << std::endl;
   if ( ! this->b_isTrained )
      fthrow(Exception, "Classifier not trained yet -- aborting!" );
   
-  scores.clear();
+  example->store ( std::cerr );
   
+  scores.clear(); 
+ 
   result = gphyper->classify ( *example, scores );
 
   if ( scores.size() == 0 ) {
@@ -193,41 +196,54 @@ void GPHIKClassifier::classify ( const SparseVector * example,  int & result, Sp
   }    
 }
 
-void GPHIKClassifier::classify ( const NICE::Vector * example,  int & result, SparseVector & scores, double & uncertainty ) const
+void GPHIKClassifier::classify ( const NICE::Vector * _example,  
+                                 int & _result, 
+                                 SparseVector & _scores, 
+                                 double & _uncertainty 
+                               ) const
 {
+  std::cerr << " classify non-sparse vectors " << std::endl;
+  
   if ( ! this->b_isTrained )
      fthrow(Exception, "Classifier not trained yet -- aborting!" );  
   
-  scores.clear();
+  _scores.clear();
+  
+  _result = gphyper->classify ( *_example, _scores );
+  //_scores.clear(); //debugging
   
-  result = gphyper->classify ( *example, scores );
 
-  if ( scores.size() == 0 ) {
-    fthrow(Exception, "Zero scores, something is likely to be wrong here: svec.size() = " << example->size() );
+  if ( _scores.size() == 0 ) {
+    fthrow(Exception, "Zero scores, something is likely to be wrong here: svec.size() = " << _example->size() );
   }
     
   if (uncertaintyPredictionForClassification)
   {
     if (varianceApproximation != NONE)
     {
-      this->predictUncertainty( example, uncertainty );
+      this->predictUncertainty( _example, _uncertainty );
     }  
     else
     {
       //do nothing
-      uncertainty = std::numeric_limits<double>::max();
+      _uncertainty = std::numeric_limits<double>::max();
     }
   }
   else
   {
     //do nothing
-    uncertainty = std::numeric_limits<double>::max();
+    _uncertainty = std::numeric_limits<double>::max();
   }  
 }
 
 /** training process */
 void GPHIKClassifier::train ( const std::vector< const NICE::SparseVector *> & examples, const NICE::Vector & labels )
 {
+  
+  //FIXME add check whether the classifier has been trained already. if so, discard all previous results.
+  
+    std::cerr << "1. train method" << std::endl;  
+  
   // security-check: examples and labels have to be of same size
   if ( examples.size() != labels.size() ) 
   {
@@ -243,6 +259,11 @@ void GPHIKClassifier::train ( const std::vector< const NICE::SparseVector *> & e
   t.start();
   
   FastMinKernel *fmk = new FastMinKernel ( examples, noise, this->debug );
+  //foo bar check that all examples are correct
+  if (verbose)
+  {
+    fmk->store ( std::cerr ); // does not work correctly...
+  }
   gphyper->setFastMinKernel ( fmk ); 
   
   t.stop();
@@ -295,6 +316,7 @@ void GPHIKClassifier::train ( const std::vector< const NICE::SparseVector *> & e
 /** training process */
 void GPHIKClassifier::train ( const std::vector< const NICE::SparseVector *> & examples, std::map<int, NICE::Vector> & binLabels )
 { 
+    std::cerr << "2. train method" << std::endl;
   // security-check: examples and labels have to be of same size
   for ( std::map< int, NICE::Vector >::const_iterator binLabIt = binLabels.begin();
         binLabIt != binLabels.end();

+ 104 - 104
SortedVectorSparse.h

@@ -51,7 +51,7 @@ template<class T> class SortedVectorSparse : NICE::Persistent{
     std::multimap< T, dataelement > nzData;
 
     //! non zero index mapping, original index -> pointer to the element
-    std::map<int, elementpointer > nonzero_indices;
+    std::map<uint, elementpointer > nonzero_indices;
 
   public:
     /**
@@ -215,7 +215,7 @@ template<class T> class SortedVectorSparse : NICE::Persistent{
     *
     * @return value of the element
     */
-    inline T access ( int _a ) const
+    inline T access ( uint _a ) const
     {
       typename std::map<uint, elementpointer>::const_iterator i = this->nonzero_indices.find ( _a );
       if ( i != this->nonzero_indices.end() ) {
@@ -595,8 +595,8 @@ template<class T> class SortedVectorSparse : NICE::Persistent{
     }
     
     /** set b_verbose flag used for restore-functionality*/
-    void setVerbose( const bool & _b_verbose) { this->b_verbose = _b_verbose;};
-    bool getVerbose( ) const { this->return b_verbose;};
+    void setVerbose( const bool & _verbose) { this->b_verbose = _verbose;};
+    bool getVerbose( ) const { return this->b_verbose;};
     
     
     /** Persistent interface */
@@ -607,106 +607,105 @@ template<class T> class SortedVectorSparse : NICE::Persistent{
       bool b_restoreVerbose ( false );
       if ( _is.good() )
       {
-	if ( b_restoreVerbose ) 
-	  std::cerr << " restore SortedVectorSparse" << std::endl;
-	
-	std::string tmp;
-	_is >> tmp; //class name 
-	
-	if ( ! this->isStartTag( tmp, "SortedVectorSparse" ) )
-	{
-	    std::cerr << " WARNING - attempt to restore SortedVectorSparse, but start flag " << tmp << " does not match! Aborting... " << std::endl;
-	    throw;
-	}   
-	    
-	_is.precision ( std::numeric_limits<double>::digits10 + 1);
-	
-	bool b_endOfBlock ( false ) ;
-	
-	while ( !b_endOfBlock )
-	{
-	  _is >> tmp; // start of block 
-	  
-	  if ( this->isEndTag( tmp, "SortedVectorSparse" ) )
-	  {
-	    b_endOfBlock = true;
-	    continue;
-	  }      
-	  
-	  tmp = this->removeStartTag ( tmp );
-	  
-	  if ( b_restoreVerbose )
-	    std::cerr << " currently restore section " << tmp << " in SortedVectorSparse" << std::endl;
-	  
-	  if ( tmp.compare("tolerance") == 0 )
-	  {
-	    _is >> this->tolerance;        
-	    _is >> tmp; // end of block 
-	    tmp = this->removeEndTag ( tmp );
-	  }
-	  else if ( tmp.compare("ui_n") == 0 )
-	  {
-	    _is >> this->ui_n;        
-	    _is >> tmp; // end of block 
-	    tmp = this->removeEndTag ( tmp );
-	  }
-	  else if ( tmp.compare("underlying_data_(sorted)") == 0 )
-	  {
-	    _is >> tmp; // start of block 
-	    
-	    uint nonZeros;
-	    if ( ! this->isStartTag( tmp, "nonZeros" ) )
-	    {
-	      std::cerr << "Attempt to restore SortedVectorSparse, but found no information about nonZeros elements. Aborting..." << std::endl;
-	      throw;
-	    }
-	    else
-	    {
-	      _is >> nonZeros;
-	      _is >> tmp; // end of block 
-	      tmp = this->removeEndTag ( tmp );     
-	    }
-	    
-	    _is >> tmp; // start of block 
-	    
-	    if ( ! this->isStartTag( tmp, "data" ) )
-	    {
-	      std::cerr << "Attempt to restore SortedVectorSparse, but found no data. Aborting..." << std::endl;
-	      throw;
-	    }
-	    else
-	    {	    
-	      T origValue;
-	      _int origIndex;
-	      T transformedValue;
-	      
-	      this->nzData.clear();
-	      for ( uint i = 0; i < nonZeros; i++)
-	      {
-	      
-		_is >> origValue;
-		_is >> origIndex;
-		_is >> transformedValue;
-	      
-		std::pair<T, dataelement > p ( origValue, dataelement ( origIndex, transformedValue ) );
-		elementpointer it = this->nzData.insert ( p);
-		this->nonzero_indices.insert ( std::pair<uint, elementpointer> ( origIndex, it ) );
-	      }
-	      
-	      _is >> tmp; // end of block 
-	      tmp = this->removeEndTag ( tmp );  
-	    }
-	    
-	    
-	    _is >> tmp; // end of block 
-	    tmp = this->removeEndTag ( tmp );	    
-	  }
-	  else
-	  {
-	    std::cerr << "WARNING -- unexpected SortedVectorSparse object -- " << tmp << " -- for restoration... aborting" << std::endl;
-	    throw;	
-	  }
-	}        
+        if ( b_restoreVerbose ) 
+          std::cerr << " restore SortedVectorSparse" << std::endl;
+        
+        std::string tmp;
+        _is >> tmp; //class name 
+        
+        if ( ! this->isStartTag( tmp, "SortedVectorSparse" ) )
+        {
+            std::cerr << " WARNING - attempt to restore SortedVectorSparse, but start flag " << tmp << " does not match! Aborting... " << std::endl;
+            throw;
+        }   
+            
+        _is.precision ( std::numeric_limits<double>::digits10 + 1);
+        
+        bool b_endOfBlock ( false ) ;
+        
+        while ( !b_endOfBlock )
+        {
+          _is >> tmp; // start of block 
+          
+          if ( this->isEndTag( tmp, "SortedVectorSparse" ) )
+          {
+            b_endOfBlock = true;
+            continue;
+          }      
+          
+          tmp = this->removeStartTag ( tmp );
+          
+          if ( b_restoreVerbose )
+            std::cerr << " currently restore section " << tmp << " in SortedVectorSparse" << std::endl;
+          
+          if ( tmp.compare("tolerance") == 0 )
+          {
+            _is >> this->tolerance;        
+            _is >> tmp; // end of block 
+            tmp = this->removeEndTag ( tmp );
+          }
+          else if ( tmp.compare("ui_n") == 0 )
+          {
+            _is >> this->ui_n;        
+            _is >> tmp; // end of block 
+            tmp = this->removeEndTag ( tmp );
+          }
+          else if ( tmp.compare("underlying_data_(sorted)") == 0 )
+          {
+            _is >> tmp; // start of block 
+            
+            uint nonZeros;
+            if ( ! this->isStartTag( tmp, "nonZeros" ) )
+            {
+              std::cerr << "Attempt to restore SortedVectorSparse, but found no information about nonZeros elements. Aborting..." << std::endl;
+              throw;
+            }
+            else
+            {
+              _is >> nonZeros;
+              _is >> tmp; // end of block 
+              tmp = this->removeEndTag ( tmp );     
+            }
+            
+            _is >> tmp; // start of block 
+            
+            if ( ! this->isStartTag( tmp, "data" ) )
+            {
+              std::cerr << "Attempt to restore SortedVectorSparse, but found no data. Aborting..." << std::endl;
+              throw;
+            }
+            else
+            {
+              T origValue;
+              uint origIndex;
+              T transformedValue;
+              
+              this->nzData.clear();
+              for ( uint i = 0; i < nonZeros; i++)
+              {              
+                _is >> origValue;
+                _is >> origIndex;
+                _is >> transformedValue;
+                    
+                std::pair<T, dataelement > p ( origValue, dataelement ( origIndex, transformedValue ) );
+                elementpointer it = this->nzData.insert ( p);
+                this->nonzero_indices.insert ( std::pair<uint, elementpointer> ( origIndex, it ) );
+              }
+              
+              _is >> tmp; // end of block 
+              tmp = this->removeEndTag ( tmp );  
+            }
+            
+            
+            _is >> tmp; // end of block 
+            tmp = this->removeEndTag ( tmp );
+          }
+          else
+          {
+            std::cerr << "WARNING -- unexpected SortedVectorSparse object -- " << tmp << " -- for restoration... aborting" << std::endl;
+            throw;
+          }
+        }        
 
       }
       else
@@ -715,6 +714,7 @@ template<class T> class SortedVectorSparse : NICE::Persistent{
         throw;
       }      
     };
+    
     virtual void store ( std::ostream & _os, 
                          int _format = 0 
                        ) const

+ 5 - 5
matlab/ConverterMatlabToNICE.cpp

@@ -57,7 +57,7 @@ std::vector< const NICE::SparseVector * > MatlabConversion::convertSparseMatrixT
           )
       {
           //note: no complex data supported her
-          sparseMatrix[ ir[current_row_index] ]->insert( std::pair<int, double>( col, pr[total++] ) );
+          sparseMatrix[ ir[current_row_index] ]->insert( std::pair<uint, double>( col, pr[total++] ) );
       } // for-loop
       
     }
@@ -116,9 +116,9 @@ NICE::SparseVector MatlabConversion::convertSparseVectorToNice(
           //note: no complex data supported her
             double value ( pr[total++] );
             if ( b_adaptIndexMtoC ) 
-                svec.insert( std::pair<int, double>( row+1,  value ) );
+                svec.insert( std::pair<uint, double>( row+1,  value ) );
             else
-                svec.insert( std::pair<int, double>( row,  value ) );
+                svec.insert( std::pair<uint, double>( row,  value ) );
         }
     } // for loop over cols      
   }
@@ -131,9 +131,9 @@ NICE::SparseVector MatlabConversion::convertSparseVectorToNice(
         //note: no complex data supported her
         double value ( pr[total++] );
         if ( b_adaptIndexMtoC ) 
-            svec.insert( std::pair<int, double>( ir[colNonZero]+1, value  ) );
+            svec.insert( std::pair<uint, double>( ir[colNonZero]+1, value  ) );
         else
-            svec.insert( std::pair<int, double>( ir[colNonZero], value  ) );
+            svec.insert( std::pair<uint, double>( ir[colNonZero], value  ) );
     }          
   }
 

+ 4 - 0
matlab/GPHIKClassifierMex.cpp

@@ -252,6 +252,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
     // create object
     if ( !strcmp("new", cmd.c_str() ) )
     {
+      std::cerr << "create gphik mex object" << std::endl;
         // check output variable
         if (nlhs != 1)
             mexErrMsgTxt("New: One output expected.");
@@ -328,6 +329,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
         yMultiTrain = MatlabConversion::convertDoubleVectorToNice(prhs[3]);
 
         //----------------- train our classifier -------------
+        std::cerr << "call train from GPHIKClassifierMex" << std::endl;
         classifier->train ( examplesTrain , yMultiTrain );
 
         //----------------- clean up -------------
@@ -357,7 +359,9 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
         {
             NICE::SparseVector * example;
             example = new NICE::SparseVector ( MatlabConversion::convertSparseVectorToNice( prhs[2] ) );
+            std::cerr << "do classification" << std::endl;
             classifier->classify ( example,  result, scores, uncertainty );
+            std::cerr << "classification done" << std::endl;
             
             //----------------- clean up -------------
             delete example;

+ 8 - 6
matlab/plot1dExampleClassification.m

@@ -11,10 +11,10 @@ myLabels = [1; 2; 2];
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %% boolean
 %interested in time measurements?
-b_verboseTime                       = false;
+b_verboseTime                       = true;
 
 %interested in outputs?
-b_verbose                           = false;  
+b_verbose                           = true;  
 
 % important for plotting!
 b_uncertaintyPredictionForClassification ...
@@ -45,14 +45,14 @@ d_parameter_upper_bound             = 1.0;
 s_ils_method                        = 'CG'; % default
 
 % options: 'none', 'greedy', 'downhillsimplex'
-s_optimization_method               = 'downhillsimplex';
+s_optimization_method               = 'none';
 
 % options:  'identity', 'abs', 'absexp'
 % with settings above, this equals 'identity'
 s_transform                         = 'absexp'; 
 
 % options: 'exact', 'approximate_fine', 'approximate_rough', and 'none'
-s_varianceApproximation             = 'approximate_fine'; 
+s_varianceApproximation             = 'exact'; 
 
 % init new GPHIKClassifier object
 myGPHIKClassifier = ...
@@ -81,7 +81,7 @@ myGPHIKClassifier = ...
         );
 
 %% run train method
-myGPHIKClassifier.train( myData, myLabels );
+myGPHIKClassifier.train( sparse(myData), myLabels );
 
 
 %% evaluate model on test data
@@ -95,7 +95,9 @@ scores = zeros(size(myDataTest,1),1);
 uncertainties = zeros(size(myDataTest,1),1);
 for i=1:size(myDataTest,1)
     example = myDataTest(i,:);
+    example = sparse( example );
     [ classNoEst, score, uncertainties(i)] = myGPHIKClassifier.classify( example );
+    %[ classNoEst, score] = myGPHIKClassifier.classify( example );
     scores(i) = score(1);
 end
 
@@ -108,7 +110,7 @@ set ( classificationFig, 'name', 'Classification with GPHIK');
 hold on;
 
 %#initialize x array
-x=0:0.01:1;
+x=myDataTest(:,1)';
 
 %#create first curve
 uncLower=scores-uncertainties;

+ 3 - 3
matlab/plot1dExampleRegression.m

@@ -45,7 +45,7 @@ d_parameter_upper_bound             = 1.0;
 s_ils_method                        = 'CG'; % default
 
 % options: 'none', 'greedy', 'downhillsimplex'
-s_optimization_method               = 'downhillsimplex';
+s_optimization_method               = 'none';
 
 % options:  'identity', 'abs', 'absexp'
 % with settings above, this equals 'identity'
@@ -83,7 +83,7 @@ myGPHIKRegression = ...
     
 
 %% run train method
-myGPHIKRegression.train( myData, myValues );
+myGPHIKRegression.train( sparse(myData), myValues );
 
 
 %% evaluate model on test data
@@ -96,7 +96,7 @@ myDataTest = cat(1, myDataTest, 1-myDataTest)';
 scores = zeros(size(myDataTest,1),1);
 uncertainties = zeros(size(myDataTest,1),1);
 for i=1:size(myDataTest,1)
-    example = myDataTest(i,:);
+    example = sparse(myDataTest(i,:));
     [ scores(i), uncertainties(i)] = myGPHIKRegression.estimate( example );
 end