GPHIKClassifier.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /**
  2. * @file GPHIKClassifier.cpp
  3. * @brief Main interface for our GP HIK classifier (similar to the feature pool classifier interface in vislearning) (Implementation)
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 02/01/2012
  6. */
  7. // STL includes
  8. #include <iostream>
  9. // NICE-core includes
  10. #include <core/basics/numerictools.h>
  11. #include <core/basics/Timer.h>
  12. // gp-hik-core includes
  13. #include "GPHIKClassifier.h"
  14. #include "gp-hik-core/parameterizedFunctions/PFAbsExp.h"
  15. #include "gp-hik-core/parameterizedFunctions/PFExp.h"
  16. #include "gp-hik-core/parameterizedFunctions/PFMKL.h"
  17. using namespace std;
  18. using namespace NICE;
  19. /////////////////////////////////////////////////////
  20. /////////////////////////////////////////////////////
  21. // PROTECTED METHODS
  22. /////////////////////////////////////////////////////
  23. /////////////////////////////////////////////////////
  24. /////////////////////////////////////////////////////
  25. /////////////////////////////////////////////////////
  26. // PUBLIC METHODS
  27. /////////////////////////////////////////////////////
  28. /////////////////////////////////////////////////////
  29. GPHIKClassifier::GPHIKClassifier( )
  30. {
  31. //default settings, may be overwritten lateron
  32. gphyper = NULL;
  33. pf = NULL;
  34. //just a default value
  35. uncertaintyPredictionForClassification = false;
  36. this->confSection = "";
  37. }
  38. GPHIKClassifier::GPHIKClassifier( const Config *conf, const string & s_confSection )
  39. {
  40. //default settings, may be overwritten lateron
  41. gphyper = NULL;
  42. pf = NULL;
  43. //just a default value
  44. uncertaintyPredictionForClassification = false;
  45. this->confSection = s_confSection;
  46. // if no config file was given, we either restore the classifier from an external file, or run ::init with
  47. // an emtpy config (using default values thereby) when calling the train-method
  48. if ( conf != NULL )
  49. {
  50. this->initFromConfig(conf, confSection);
  51. }
  52. }
  53. GPHIKClassifier::~GPHIKClassifier()
  54. {
  55. if ( gphyper != NULL )
  56. delete gphyper;
  57. if (pf != NULL)
  58. delete pf;
  59. }
  60. void GPHIKClassifier::initFromConfig(const Config *conf, const string & s_confSection)
  61. {
  62. double parameterUpperBound = conf->gD(confSection, "parameter_upper_bound", 5.0 );
  63. double parameterLowerBound = conf->gD(confSection, "parameter_lower_bound", 1.0 );
  64. this->noise = conf->gD(confSection, "noise", 0.01);
  65. string transform = conf->gS(confSection, "transform", "absexp" );
  66. if (pf == NULL)
  67. {
  68. if ( transform == "absexp" )
  69. {
  70. this->pf = new PFAbsExp( 1.0, parameterLowerBound, parameterUpperBound );
  71. } else if ( transform == "exp" ) {
  72. this->pf = new PFExp( 1.0, parameterLowerBound, parameterUpperBound );
  73. }else if ( transform == "MKL" ) {
  74. //TODO generic, please :) load from a separate file or something like this!
  75. std::set<int> steps; steps.insert(4000); steps.insert(6000); //specific for VISAPP
  76. this->pf = new PFMKL( steps, parameterLowerBound, parameterUpperBound );
  77. } else {
  78. fthrow(Exception, "Transformation type is unknown " << transform);
  79. }
  80. }
  81. else
  82. {
  83. //we already know the pf from the restore-function
  84. }
  85. this->confSection = confSection;
  86. this->verbose = conf->gB(confSection, "verbose", false);
  87. this->debug = conf->gB(confSection, "debug", false);
  88. this->uncertaintyPredictionForClassification = conf->gB( confSection, "uncertaintyPredictionForClassification", false );
  89. //how do we approximate the predictive variance for classification uncertainty?
  90. string s_varianceApproximation = conf->gS(confSection, "varianceApproximation", "approximate_fine"); //default: fine approximative uncertainty prediction
  91. if ( (s_varianceApproximation.compare("approximate_rough") == 0) || ((s_varianceApproximation.compare("1") == 0)) )
  92. {
  93. this->varianceApproximation = APPROXIMATE_ROUGH;
  94. //no additional eigenvalue is needed here at all.
  95. this->gphyper->setNrOfEigenvaluesToConsiderForVarApprox ( 0 );
  96. //this->conf->sI ( confSection, "nrOfEigenvaluesToConsiderForVarApprox", 0 );
  97. }
  98. else if ( (s_varianceApproximation.compare("approximate_fine") == 0) || ((s_varianceApproximation.compare("2") == 0)) )
  99. {
  100. this->varianceApproximation = APPROXIMATE_FINE;
  101. //security check - compute at least one eigenvalue for this approximation strategy
  102. this->gphyper->setNrOfEigenvaluesToConsiderForVarApprox ( std::max( conf->gI(confSection, "nrOfEigenvaluesToConsiderForVarApprox", 1 ), 1) );
  103. //this->conf->sI ( confSection, "nrOfEigenvaluesToConsiderForVarApprox", std::max( conf->gI(confSection, "nrOfEigenvaluesToConsiderForVarApprox", 1 ), 1) );
  104. }
  105. else if ( (s_varianceApproximation.compare("exact") == 0) || ((s_varianceApproximation.compare("3") == 0)) )
  106. {
  107. this->varianceApproximation = EXACT;
  108. //no additional eigenvalue is needed here at all.
  109. this->gphyper->setNrOfEigenvaluesToConsiderForVarApprox ( 1 );
  110. //this->conf->sI ( confSection, "nrOfEigenvaluesToConsiderForVarApprox", 1 );
  111. //TODO check why here 1, and 0 above
  112. }
  113. else
  114. {
  115. this->varianceApproximation = NONE;
  116. //no additional eigenvalue is needed here at all.
  117. this->gphyper->setNrOfEigenvaluesToConsiderForVarApprox ( 1 );
  118. //this->conf->sI ( confSection, "nrOfEigenvaluesToConsiderForVarApprox", 1 );
  119. }
  120. if ( this->verbose )
  121. std::cerr << "varianceApproximationStrategy: " << s_varianceApproximation << std::endl;
  122. this->gphyper->initFromConfig ( conf, confSection /*possibly delete the handing of confSection*/);
  123. }
  124. ///////////////////// ///////////////////// /////////////////////
  125. // GET / SET
  126. ///////////////////// ///////////////////// /////////////////////
  127. std::set<int> GPHIKClassifier::getKnownClassNumbers ( ) const
  128. {
  129. if (gphyper == NULL)
  130. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  131. return gphyper->getKnownClassNumbers();
  132. }
  133. ///////////////////// ///////////////////// /////////////////////
  134. // CLASSIFIER STUFF
  135. ///////////////////// ///////////////////// /////////////////////
  136. void GPHIKClassifier::classify ( const SparseVector * example, int & result, SparseVector & scores ) const
  137. {
  138. double tmpUncertainty;
  139. this->classify( example, result, scores, tmpUncertainty );
  140. }
  141. void GPHIKClassifier::classify ( const NICE::Vector * example, int & result, SparseVector & scores ) const
  142. {
  143. double tmpUncertainty;
  144. this->classify( example, result, scores, tmpUncertainty );
  145. }
  146. void GPHIKClassifier::classify ( const SparseVector * example, int & result, SparseVector & scores, double & uncertainty ) const
  147. {
  148. if (gphyper == NULL)
  149. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  150. scores.clear();
  151. result = gphyper->classify ( *example, scores );
  152. if ( scores.size() == 0 ) {
  153. fthrow(Exception, "Zero scores, something is likely to be wrong here: svec.size() = " << example->size() );
  154. }
  155. if (uncertaintyPredictionForClassification)
  156. {
  157. if (varianceApproximation != NONE)
  158. {
  159. this->predictUncertainty( example, uncertainty );
  160. }
  161. else
  162. {
  163. //do nothing
  164. uncertainty = std::numeric_limits<double>::max();
  165. }
  166. }
  167. else
  168. {
  169. //do nothing
  170. uncertainty = std::numeric_limits<double>::max();
  171. }
  172. }
  173. void GPHIKClassifier::classify ( const NICE::Vector * example, int & result, SparseVector & scores, double & uncertainty ) const
  174. {
  175. if (gphyper == NULL)
  176. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  177. scores.clear();
  178. result = gphyper->classify ( *example, scores );
  179. if ( scores.size() == 0 ) {
  180. fthrow(Exception, "Zero scores, something is likely to be wrong here: svec.size() = " << example->size() );
  181. }
  182. if (uncertaintyPredictionForClassification)
  183. {
  184. if (varianceApproximation != NONE)
  185. {
  186. this->predictUncertainty( example, uncertainty );
  187. }
  188. else
  189. {
  190. //do nothing
  191. uncertainty = std::numeric_limits<double>::max();
  192. }
  193. }
  194. else
  195. {
  196. //do nothing
  197. uncertainty = std::numeric_limits<double>::max();
  198. }
  199. }
  200. /** training process */
  201. void GPHIKClassifier::train ( const std::vector< const NICE::SparseVector *> & examples, const NICE::Vector & labels )
  202. {
  203. // security-check: examples and labels have to be of same size
  204. if ( examples.size() != labels.size() )
  205. {
  206. fthrow(Exception, "Given examples do not match label vector in size -- aborting!" );
  207. }
  208. if (verbose)
  209. {
  210. std::cerr << "GPHIKClassifier::train" << std::endl;
  211. }
  212. // if ( this->conf == NULL )
  213. if ( this->pf == NULL ) // pf will only be set in ::init or in ::restore
  214. {
  215. std::cerr << "WARNING -- No config used so far, initialize values with empty config file now..." << std::endl;
  216. NICE::Config tmpConfEmpty ;
  217. this->initFromConfig ( &tmpConfEmpty, this->confSection );
  218. }
  219. Timer t;
  220. t.start();
  221. FastMinKernel *fmk = new FastMinKernel ( examples, noise, this->debug );
  222. t.stop();
  223. if (verbose)
  224. std::cerr << "Time used for setting up the fmk object: " << t.getLast() << std::endl;
  225. /* if (gphyper != NULL)
  226. delete gphyper;
  227. */
  228. if ( gphyper == NULL )
  229. {
  230. // security check, which should always be skipped since gphyper is created in this->init
  231. NICE::Config tmpConfEmpty ;
  232. gphyper = new FMKGPHyperparameterOptimization ( &tmpConfEmpty, confSection );
  233. }
  234. if ( ( varianceApproximation != APPROXIMATE_FINE) )
  235. {
  236. //TODO check whether this is easier then the version above
  237. this->gphyper->setNrOfEigenvaluesToConsiderForVarApprox ( 0 );
  238. //conf->sI ( confSection, "nrOfEigenvaluesToConsiderForVarApprox", 0);
  239. }
  240. //those two methods replace the previous call of the "full" constructor
  241. gphyper->setParameterizedFunction ( pf );
  242. gphyper->setFastMinKernel ( fmk );
  243. // gphyper->init (pf, fmk );
  244. // gphyper = new FMKGPHyperparameterOptimization ( conf, pf, fmk, confSection );
  245. if (verbose)
  246. cerr << "Learning ..." << endl;
  247. // go go go
  248. gphyper->optimize ( labels );
  249. if (verbose)
  250. std::cerr << "optimization done" << std::endl;
  251. if ( ( varianceApproximation != NONE ) )
  252. {
  253. switch (varianceApproximation)
  254. {
  255. case APPROXIMATE_ROUGH:
  256. {
  257. gphyper->prepareVarianceApproximationRough();
  258. break;
  259. }
  260. case APPROXIMATE_FINE:
  261. {
  262. gphyper->prepareVarianceApproximationFine();
  263. break;
  264. }
  265. case EXACT:
  266. {
  267. //nothing to prepare
  268. break;
  269. }
  270. default:
  271. {
  272. //nothing to prepare
  273. }
  274. }
  275. }
  276. // clean up all examples ??
  277. if (verbose)
  278. std::cerr << "Learning finished" << std::endl;
  279. }
  280. /** training process */
  281. void GPHIKClassifier::train ( const std::vector< const NICE::SparseVector *> & examples, std::map<int, NICE::Vector> & binLabels )
  282. {
  283. // security-check: examples and labels have to be of same size
  284. for ( std::map< int, NICE::Vector >::const_iterator binLabIt = binLabels.begin();
  285. binLabIt != binLabels.end();
  286. binLabIt++
  287. )
  288. {
  289. if ( examples.size() != binLabIt->second.size() )
  290. {
  291. fthrow(Exception, "Given examples do not match label vector in size -- aborting!" );
  292. }
  293. }
  294. if (verbose)
  295. std::cerr << "GPHIKClassifier::train" << std::endl;
  296. // if ( this->conf == NULL )
  297. if ( this->pf == NULL ) // pf will only be set in ::init or in ::restore
  298. {
  299. std::cerr << "WARNING -- No config used so far, initialize values with empty config file now..." << std::endl;
  300. NICE::Config tmpConfEmpty ;
  301. this->initFromConfig ( &tmpConfEmpty, this->confSection );
  302. }
  303. Timer t;
  304. t.start();
  305. FastMinKernel *fmk = new FastMinKernel ( examples, noise, this->debug );
  306. t.stop();
  307. if (verbose)
  308. std::cerr << "Time used for setting up the fmk object: " << t.getLast() << std::endl;
  309. // if (gphyper != NULL)
  310. // delete gphyper;
  311. if ( gphyper == NULL )
  312. {
  313. // security check, which should always be skipped since gphyper is created in this->init
  314. NICE::Config tmpConfEmpty ;
  315. gphyper = new FMKGPHyperparameterOptimization ( &tmpConfEmpty, confSection );
  316. }
  317. //those two methods replace the previous call of the "full" constructor
  318. gphyper->setParameterizedFunction ( pf );
  319. gphyper->setFastMinKernel ( fmk );
  320. if (verbose)
  321. cerr << "Learning ..." << endl;
  322. // go go go
  323. gphyper->optimize ( binLabels );
  324. if (verbose)
  325. std::cerr << "optimization done, now prepare for the uncertainty prediction" << std::endl;
  326. if ( ( varianceApproximation != NONE ) )
  327. {
  328. switch (varianceApproximation)
  329. {
  330. case APPROXIMATE_ROUGH:
  331. {
  332. gphyper->prepareVarianceApproximationRough();
  333. break;
  334. }
  335. case APPROXIMATE_FINE:
  336. {
  337. gphyper->prepareVarianceApproximationFine();
  338. break;
  339. }
  340. case EXACT:
  341. {
  342. //nothing to prepare
  343. break;
  344. }
  345. default:
  346. {
  347. //nothing to prepare
  348. }
  349. }
  350. }
  351. // clean up all examples ??
  352. if (verbose)
  353. std::cerr << "Learning finished" << std::endl;
  354. }
  355. GPHIKClassifier *GPHIKClassifier::clone () const
  356. {
  357. fthrow(Exception, "GPHIKClassifier: clone() not yet implemented" );
  358. return NULL;
  359. }
  360. void GPHIKClassifier::predictUncertainty( const NICE::SparseVector * example, double & uncertainty ) const
  361. {
  362. if (gphyper == NULL)
  363. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  364. //we directly store the predictive variances in the vector, that contains the classification uncertainties lateron to save storage
  365. switch (varianceApproximation)
  366. {
  367. case APPROXIMATE_ROUGH:
  368. {
  369. gphyper->computePredictiveVarianceApproximateRough( *example, uncertainty );
  370. break;
  371. }
  372. case APPROXIMATE_FINE:
  373. {
  374. gphyper->computePredictiveVarianceApproximateFine( *example, uncertainty );
  375. break;
  376. }
  377. case EXACT:
  378. {
  379. gphyper->computePredictiveVarianceExact( *example, uncertainty );
  380. break;
  381. }
  382. default:
  383. {
  384. fthrow(Exception, "GPHIKClassifier - your settings disabled the variance approximation needed for uncertainty prediction.");
  385. // uncertainty = numeric_limits<double>::max();
  386. // break;
  387. }
  388. }
  389. }
  390. void GPHIKClassifier::predictUncertainty( const NICE::Vector * example, double & uncertainty ) const
  391. {
  392. if (gphyper == NULL)
  393. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  394. //we directly store the predictive variances in the vector, that contains the classification uncertainties lateron to save storage
  395. switch (varianceApproximation)
  396. {
  397. case APPROXIMATE_ROUGH:
  398. {
  399. gphyper->computePredictiveVarianceApproximateRough( *example, uncertainty );
  400. break;
  401. }
  402. case APPROXIMATE_FINE:
  403. {
  404. gphyper->computePredictiveVarianceApproximateFine( *example, uncertainty );
  405. break;
  406. }
  407. case EXACT:
  408. {
  409. gphyper->computePredictiveVarianceExact( *example, uncertainty );
  410. break;
  411. }
  412. default:
  413. {
  414. fthrow(Exception, "GPHIKClassifier - your settings disabled the variance approximation needed for uncertainty prediction.");
  415. // uncertainty = numeric_limits<double>::max();
  416. // break;
  417. }
  418. }
  419. }
  420. ///////////////////// INTERFACE PERSISTENT /////////////////////
  421. // interface specific methods for store and restore
  422. ///////////////////// INTERFACE PERSISTENT /////////////////////
  423. void GPHIKClassifier::restore ( std::istream & is, int format )
  424. {
  425. //delete everything we knew so far...
  426. this->clear();
  427. bool b_restoreVerbose ( false );
  428. #ifdef B_RESTOREVERBOSE
  429. b_restoreVerbose = true;
  430. #endif
  431. if ( is.good() )
  432. {
  433. if ( b_restoreVerbose )
  434. std::cerr << " restore GPHIKClassifier" << std::endl;
  435. std::string tmp;
  436. is >> tmp; //class name
  437. if ( ! this->isStartTag( tmp, "GPHIKClassifier" ) )
  438. {
  439. std::cerr << " WARNING - attempt to restore GPHIKClassifier, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  440. throw;
  441. }
  442. if (pf != NULL)
  443. {
  444. delete pf;
  445. pf = NULL;
  446. }
  447. //
  448. if (gphyper != NULL)
  449. {
  450. delete gphyper;
  451. gphyper = NULL;
  452. }
  453. is.precision (numeric_limits<double>::digits10 + 1);
  454. bool b_endOfBlock ( false ) ;
  455. while ( !b_endOfBlock )
  456. {
  457. is >> tmp; // start of block
  458. if ( this->isEndTag( tmp, "GPHIKClassifier" ) )
  459. {
  460. b_endOfBlock = true;
  461. continue;
  462. }
  463. tmp = this->removeStartTag ( tmp );
  464. if ( b_restoreVerbose )
  465. std::cerr << " currently restore section " << tmp << " in GPHIKClassifier" << std::endl;
  466. if ( tmp.compare("confSection") == 0 )
  467. {
  468. is >> confSection;
  469. is >> tmp; // end of block
  470. tmp = this->removeEndTag ( tmp );
  471. }
  472. else if ( tmp.compare("pf") == 0 )
  473. {
  474. is >> tmp; // start of block
  475. if ( this->isEndTag( tmp, "pf" ) )
  476. {
  477. std::cerr << " ParameterizedFunction object can not be restored. Aborting..." << std::endl;
  478. throw;
  479. }
  480. std::string transform = this->removeStartTag ( tmp );
  481. if ( transform == "PFAbsExp" )
  482. {
  483. this->pf = new PFAbsExp ();
  484. } else if ( transform == "PFExp" ) {
  485. this->pf = new PFExp ();
  486. } else {
  487. fthrow(Exception, "Transformation type is unknown " << transform);
  488. }
  489. pf->restore(is, format);
  490. is >> tmp; // end of block
  491. tmp = this->removeEndTag ( tmp );
  492. }
  493. else if ( tmp.compare("gphyper") == 0 )
  494. {
  495. if ( gphyper == NULL )
  496. gphyper = new NICE::FMKGPHyperparameterOptimization();
  497. //then, load everything that we stored explicitely,
  498. // including precomputed matrices, LUTs, eigenvalues, ... and all that stuff
  499. gphyper->restore(is, format);
  500. is >> tmp; // end of block
  501. tmp = this->removeEndTag ( tmp );
  502. }
  503. else
  504. {
  505. std::cerr << "WARNING -- unexpected GPHIKClassifier object -- " << tmp << " -- for restoration... aborting" << std::endl;
  506. throw;
  507. }
  508. }
  509. //load every settings as well as default options
  510. //TODO check that this is not needed anymore!!!
  511. // std::cerr << "run this->init" << std::endl;
  512. // this->init(confCopy, confSection);
  513. // std::cerr << "run gphyper->initialize" << std::endl;
  514. // gphyper->initialize ( confCopy, pf, NULL, confSection );
  515. }
  516. else
  517. {
  518. std::cerr << "GPHIKClassifier::restore -- InStream not initialized - restoring not possible!" << std::endl;
  519. throw;
  520. }
  521. }
  522. void GPHIKClassifier::store ( std::ostream & os, int format ) const
  523. {
  524. if (gphyper == NULL)
  525. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  526. if (os.good())
  527. {
  528. // show starting point
  529. os << this->createStartTag( "GPHIKClassifier" ) << std::endl;
  530. os.precision (numeric_limits<double>::digits10 + 1);
  531. os << this->createStartTag( "confSection" ) << std::endl;
  532. os << confSection << std::endl;
  533. os << this->createEndTag( "confSection" ) << std::endl;
  534. os << this->createStartTag( "pf" ) << std::endl;
  535. pf->store(os, format);
  536. os << this->createEndTag( "pf" ) << std::endl;
  537. os << this->createStartTag( "gphyper" ) << std::endl;
  538. //store the underlying data
  539. //will be done in gphyper->store(of,format)
  540. //store the optimized parameter values and all that stuff
  541. gphyper->store(os, format);
  542. os << this->createEndTag( "gphyper" ) << std::endl;
  543. // done
  544. os << this->createEndTag( "GPHIKClassifier" ) << std::endl;
  545. }
  546. else
  547. {
  548. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  549. }
  550. }
  551. void GPHIKClassifier::clear ()
  552. {
  553. if ( gphyper != NULL )
  554. {
  555. delete gphyper;
  556. gphyper = NULL;
  557. }
  558. if (pf != NULL)
  559. {
  560. delete pf;
  561. pf = NULL;
  562. }
  563. }
  564. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  565. // interface specific methods for incremental extensions
  566. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  567. void GPHIKClassifier::addExample( const NICE::SparseVector * example,
  568. const double & label,
  569. const bool & performOptimizationAfterIncrement
  570. )
  571. {
  572. if ( this->gphyper == NULL )
  573. {
  574. //call train method instead
  575. std::cerr << "Classifier not initially trained yet -- run initial training instead of incremental extension!" << std::endl;
  576. std::vector< const NICE::SparseVector *> examplesVec;
  577. examplesVec.push_back ( example );
  578. NICE::Vector labelsVec ( 1 , label );
  579. this->train ( examplesVec, labelsVec );
  580. }
  581. else
  582. {
  583. this->gphyper->addExample( example, label, performOptimizationAfterIncrement );
  584. }
  585. }
  586. void GPHIKClassifier::addMultipleExamples( const std::vector< const NICE::SparseVector * > & newExamples,
  587. const NICE::Vector & newLabels,
  588. const bool & performOptimizationAfterIncrement
  589. )
  590. {
  591. //are new examples available? If not, nothing has to be done
  592. if ( newExamples.size() < 1)
  593. return;
  594. if ( this->gphyper == NULL )
  595. {
  596. //call train method instead
  597. std::cerr << "Classifier not initially trained yet -- run initial training instead of incremental extension!" << std::endl;
  598. this->train ( newExamples, newLabels );
  599. }
  600. else
  601. {
  602. this->gphyper->addMultipleExamples( newExamples, newLabels, performOptimizationAfterIncrement );
  603. }
  604. }