소스 검색

better structure when computing LUT T for raw version

Alexander Freytag 9 년 전
부모
커밋
d9cfc82b2b
2개의 변경된 파일52개의 추가작업 그리고 50개의 파일을 삭제
  1. 49 48
      GMHIKernelRaw.cpp
  2. 3 2
      GPHIKRawClassifier.cpp

+ 49 - 48
GMHIKernelRaw.cpp

@@ -290,57 +290,58 @@ void GMHIKernelRaw::updateTableT ( const NICE::Vector _x ) const
         int indexElem = 0;
         // element of the feature
         double elem = i->value;
-
-        for (uint idxProto = 0; idxProto < hmax; idxProto++) // previously j
+        
+        idxProtoElem = this->q->quantize ( elem, dim );
+
+        uint idxProto;
+        double * itProtoVal = prototypes + dim*hmax;
+        double * itT = this->table_T + dim*hmax;
+        
+        // special case 1:
+        // loop over all prototypes smaller then the smallest quantized example in this dimension
+        for ( idxProto = 0; idxProto < idxProtoElem; idxProto++, itProtoVal++, itT++) // idxProto previously j
         {
-          double fvalProto = prototypes[ dim*hmax + idxProto ];
-          double t;
-
-
-          idxProtoElem = this->q->quantize ( elem, dim );
+          // current prototype is smaller than all known examples
+          // -> resulting value = fval * sum_l=1^n alpha_l          
+          (*itT) = (*itProtoVal) * ( this->table_B[ dim ][ nnz-1 ] );          
+        }//for-loop over prototypes -- special case 1
 
-
-          if (  (indexElem == 0) && (idxProto < idxProtoElem) )
-          {
-            // current prototype is smaller than everything else
-            // resulting value = fval * sum_l=1^n alpha_l
-            t = fvalProto*( this->table_B[ dim ][ nnz-1 ] );
-          }
-          else
-          {
-            //move to next example, which is smaller then the current prototype (if necessary)
+        // standard case: prototypes larger then the smallest element, but smaller then the largest one in the corrent dimension        
+        for ( ; idxProto < hmax; idxProto++, itProtoVal++, itT++)
+        {
+            //move to next example, which is smaller then the current prototype after quantization
             // pay attentation to not loop over the number of non-zero elements
-               while ( (idxProto >= idxProtoElem) && ( indexElem < ( nnz - 1 ) ) ) //(this->ui_n-1-nrZeroIndices)) )
-               {
-                 indexElem++;
-                 iPredecessor = i;
-                 i++;
-
-                 if ( i->value !=  iPredecessor->value )
-                 {
-                   idxProtoElem = this->q->quantize ( i->value, dim );
-                 }
-               }
-               // compute current element in the lookup table and keep in mind that
-               // indexElem is the next element and not the previous one
-
-
-               if ( (idxProto >= idxProtoElem) && ( indexElem==( nnz-1 ) ) )
-               {
-                 // the current prototype is equal to or larger than the largest training example in this dimension
-                 // -> the term B[ dim ][ nnz-1 ] - B[ dim ][ indexElem ] is equal to zero and vanishes, which is logical, since all elements are smaller than j!
-                 t = table_A[ dim ][ indexElem ];
-               }
-               else
-               {
-                 // standard case
-                 t = table_A[ dim ][ indexElem-1 ] + fvalProto*( table_B[ dim ][ nnz-1 ] - table_B[ dim ][ indexElem-1 ] );
-               }
-
-           }
-
-           this->table_T[ dim*hmax + idxProto ] = t;
-        }//for-loop over prototypes
+            while ( (idxProto >= idxProtoElem) && ( indexElem < ( nnz - 1 ) ) ) //(this->ui_n-1-nrZeroIndices)) )
+            {
+              indexElem++;
+              iPredecessor = i;
+              i++;
+
+              // only quantize if value changed
+              if ( i->value !=  iPredecessor->value )
+              {
+                idxProtoElem = this->q->quantize ( i->value, dim );
+              }
+            }
+            
+            // did we looped over the largest element in this dimension?
+            if ( indexElem==( nnz-1 ) )
+            {
+              break;
+            }
+
+            (*itT) = table_A[ dim ][ indexElem-1 ] + (*itProtoVal)*( table_B[ dim ][ nnz-1 ] - table_B[ dim ][ indexElem-1 ] );
+        }//for-loop over prototypes -- standard case 
+            
+        // special case 2:
+        // the current prototype is equal to or larger than the largest training example in this dimension
+        // -> the term B[ dim ][ nnz-1 ] - B[ dim ][ indexElem ] is equal to zero and vanishes, which is logical, since all elements are smaller than the remaining prototypes!
+
+        for ( ; idxProto < hmax; idxProto++, itProtoVal++, itT++)
+        {
+          (*itT) = table_A[ dim ][ indexElem ];
+        }//for-loop over prototypes -- special case 2
+        
     }//for-loop over dimensions
 
 

+ 3 - 2
GPHIKRawClassifier.cpp

@@ -292,10 +292,11 @@ void GPHIKRawClassifier::classify ( const NICE::SparseVector * _xstar,
 
           for (SparseVector::const_iterator i = _xstar->begin(); i != _xstar->end(); i++ )
           {
-            uint dim = i->first;
-            double v = i->second;
+            uint dim  = i->first;
+            double v  = i->second;
             uint qBin = this->q->quantize( v, dim );
 
+            //FIXME do we have problems indexing with uints if number of bins is quite large?
             beta += T[dim * this->q->getNumberOfBins() + qBin];
           }//for-loop over dimensions of test input