GPHIKClassifier.cpp 22 KB

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