Browse Source

security check in train method added

Alexander Freytag 11 years ago
parent
commit
9a2118674b
2 changed files with 21 additions and 1 deletions
  1. 18 0
      GPHIKClassifier.cpp
  2. 3 1
      matlab/testGPHIKClassifier.m

+ 18 - 0
GPHIKClassifier.cpp

@@ -243,6 +243,12 @@ void GPHIKClassifier::classify ( const NICE::Vector * example,  int & result, Sp
 /** training process */
 void GPHIKClassifier::train ( const std::vector< const NICE::SparseVector *> & examples, const NICE::Vector & labels )
 {
+  // security-check: examples and labels have to be of same size
+  if ( examples.size() != labels.size() ) 
+  {
+    fthrow(Exception, "Given examples do not match label vector in size -- aborting!" );  
+  }  
+  
   if (verbose)
   {
     std::cerr << "GPHIKClassifier::train" << std::endl;
@@ -315,6 +321,18 @@ 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 )
 { 
+  // 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();
+        binLabIt++ 
+      )
+  {
+    if ( examples.size() != binLabIt->second.size() ) 
+    {
+      fthrow(Exception, "Given examples do not match label vector in size -- aborting!" );  
+    }
+  }
+  
   if (verbose)
     std::cerr << "GPHIKClassifier::train" << std::endl;
   

+ 3 - 1
matlab/testGPHIKClassifier.m

@@ -56,4 +56,6 @@ uncertainty = myGPHIKClassifier.uncertainty( myDataTest )
 [ arr, confMat, scores] = myGPHIKClassifier.test( myDataTest, myLabelsTest )
 
 % clean up and delete object
-myGPHIKClassifier.delete();
+myGPHIKClassifier.delete();
+
+clear ( 'myGPHIKClassifier' );