GPHIKClassifier.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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. this->gphyper->computePredictiveVarianceApproximateFine( *_example, _uncertainty );
  369. break;
  370. }
  371. case EXACT:
  372. {
  373. this->gphyper->computePredictiveVarianceExact( *_example, _uncertainty );
  374. break;
  375. }
  376. default:
  377. {
  378. fthrow(Exception, "GPHIKClassifier - your settings disabled the variance approximation needed for uncertainty prediction.");
  379. }
  380. }
  381. }
  382. void GPHIKClassifier::predictUncertainty( const NICE::Vector * _example,
  383. double & _uncertainty
  384. ) const
  385. {
  386. if ( this->gphyper == NULL )
  387. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  388. //we directly store the predictive variances in the vector, that contains the classification uncertainties lateron to save storage
  389. switch ( this->varianceApproximation )
  390. {
  391. case APPROXIMATE_ROUGH:
  392. {
  393. this->gphyper->computePredictiveVarianceApproximateRough( *_example, _uncertainty );
  394. break;
  395. }
  396. case APPROXIMATE_FINE:
  397. {
  398. this->gphyper->computePredictiveVarianceApproximateFine( *_example, _uncertainty );
  399. break;
  400. }
  401. case EXACT:
  402. {
  403. this->gphyper->computePredictiveVarianceExact( *_example, _uncertainty );
  404. break;
  405. }
  406. default:
  407. {
  408. fthrow(Exception, "GPHIKClassifier - your settings disabled the variance approximation needed for uncertainty prediction.");
  409. }
  410. }
  411. }
  412. ///////////////////// INTERFACE PERSISTENT /////////////////////
  413. // interface specific methods for store and restore
  414. ///////////////////// INTERFACE PERSISTENT /////////////////////
  415. void GPHIKClassifier::restore ( std::istream & _is,
  416. int _format
  417. )
  418. {
  419. //delete everything we knew so far...
  420. this->clear();
  421. bool b_restoreVerbose ( false );
  422. #ifdef B_RESTOREVERBOSE
  423. b_restoreVerbose = true;
  424. #endif
  425. if ( _is.good() )
  426. {
  427. if ( b_restoreVerbose )
  428. std::cerr << " restore GPHIKClassifier" << std::endl;
  429. std::string tmp;
  430. _is >> tmp; //class name
  431. if ( ! this->isStartTag( tmp, "GPHIKClassifier" ) )
  432. {
  433. std::cerr << " WARNING - attempt to restore GPHIKClassifier, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  434. throw;
  435. }
  436. if (gphyper != NULL)
  437. {
  438. delete gphyper;
  439. gphyper = NULL;
  440. }
  441. _is.precision (numeric_limits<double>::digits10 + 1);
  442. bool b_endOfBlock ( false ) ;
  443. while ( !b_endOfBlock )
  444. {
  445. _is >> tmp; // start of block
  446. if ( this->isEndTag( tmp, "GPHIKClassifier" ) )
  447. {
  448. b_endOfBlock = true;
  449. continue;
  450. }
  451. tmp = this->removeStartTag ( tmp );
  452. if ( b_restoreVerbose )
  453. std::cerr << " currently restore section " << tmp << " in GPHIKClassifier" << std::endl;
  454. if ( tmp.compare("confSection") == 0 )
  455. {
  456. _is >> confSection;
  457. _is >> tmp; // end of block
  458. tmp = this->removeEndTag ( tmp );
  459. }
  460. else if ( tmp.compare("gphyper") == 0 )
  461. {
  462. if ( this->gphyper == NULL )
  463. this->gphyper = new NICE::FMKGPHyperparameterOptimization();
  464. //then, load everything that we stored explicitely,
  465. // including precomputed matrices, LUTs, eigenvalues, ... and all that stuff
  466. this->gphyper->restore( _is, _format );
  467. _is >> tmp; // end of block
  468. tmp = this->removeEndTag ( tmp );
  469. }
  470. else if ( tmp.compare("b_isTrained") == 0 )
  471. {
  472. _is >> b_isTrained;
  473. _is >> tmp; // end of block
  474. tmp = this->removeEndTag ( tmp );
  475. }
  476. else if ( tmp.compare("d_noise") == 0 )
  477. {
  478. _is >> d_noise;
  479. _is >> tmp; // end of block
  480. tmp = this->removeEndTag ( tmp );
  481. }
  482. else if ( tmp.compare("b_verbose") == 0 )
  483. {
  484. _is >> b_verbose;
  485. _is >> tmp; // end of block
  486. tmp = this->removeEndTag ( tmp );
  487. }
  488. else if ( tmp.compare("b_debug") == 0 )
  489. {
  490. _is >> b_debug;
  491. _is >> tmp; // end of block
  492. tmp = this->removeEndTag ( tmp );
  493. }
  494. else if ( tmp.compare("uncertaintyPredictionForClassification") == 0 )
  495. {
  496. _is >> uncertaintyPredictionForClassification;
  497. _is >> tmp; // end of block
  498. tmp = this->removeEndTag ( tmp );
  499. }
  500. else if ( tmp.compare("varianceApproximation") == 0 )
  501. {
  502. unsigned int ui_varianceApproximation;
  503. _is >> ui_varianceApproximation;
  504. varianceApproximation = static_cast<VarianceApproximation> ( ui_varianceApproximation );
  505. _is >> tmp; // end of block
  506. tmp = this->removeEndTag ( tmp );
  507. }
  508. else
  509. {
  510. std::cerr << "WARNING -- unexpected GPHIKClassifier object -- " << tmp << " -- for restoration... aborting" << std::endl;
  511. throw;
  512. }
  513. }
  514. }
  515. else
  516. {
  517. std::cerr << "GPHIKClassifier::restore -- InStream not initialized - restoring not possible!" << std::endl;
  518. throw;
  519. }
  520. }
  521. void GPHIKClassifier::store ( std::ostream & _os,
  522. int _format
  523. ) const
  524. {
  525. if ( _os.good() )
  526. {
  527. // show starting point
  528. _os << this->createStartTag( "GPHIKClassifier" ) << std::endl;
  529. _os.precision (numeric_limits<double>::digits10 + 1);
  530. _os << this->createStartTag( "confSection" ) << std::endl;
  531. _os << confSection << std::endl;
  532. _os << this->createEndTag( "confSection" ) << std::endl;
  533. _os << this->createStartTag( "gphyper" ) << std::endl;
  534. //store the underlying data
  535. //will be done in gphyper->store(of,format)
  536. //store the optimized parameter values and all that stuff
  537. this->gphyper->store( _os, _format );
  538. _os << this->createEndTag( "gphyper" ) << std::endl;
  539. /////////////////////////////////////////////////////////
  540. // store variables which we previously set via config
  541. /////////////////////////////////////////////////////////
  542. _os << this->createStartTag( "b_isTrained" ) << std::endl;
  543. _os << b_isTrained << std::endl;
  544. _os << this->createEndTag( "b_isTrained" ) << std::endl;
  545. _os << this->createStartTag( "d_noise" ) << std::endl;
  546. _os << d_noise << std::endl;
  547. _os << this->createEndTag( "d_noise" ) << std::endl;
  548. _os << this->createStartTag( "b_verbose" ) << std::endl;
  549. _os << b_verbose << std::endl;
  550. _os << this->createEndTag( "b_verbose" ) << std::endl;
  551. _os << this->createStartTag( "b_debug" ) << std::endl;
  552. _os << b_debug << std::endl;
  553. _os << this->createEndTag( "b_debug" ) << std::endl;
  554. _os << this->createStartTag( "uncertaintyPredictionForClassification" ) << std::endl;
  555. _os << uncertaintyPredictionForClassification << std::endl;
  556. _os << this->createEndTag( "uncertaintyPredictionForClassification" ) << std::endl;
  557. _os << this->createStartTag( "varianceApproximation" ) << std::endl;
  558. _os << varianceApproximation << std::endl;
  559. _os << this->createEndTag( "varianceApproximation" ) << std::endl;
  560. // done
  561. _os << this->createEndTag( "GPHIKClassifier" ) << std::endl;
  562. }
  563. else
  564. {
  565. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  566. }
  567. }
  568. void GPHIKClassifier::clear ()
  569. {
  570. if ( this->gphyper != NULL )
  571. {
  572. delete this->gphyper;
  573. this->gphyper = NULL;
  574. }
  575. }
  576. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  577. // interface specific methods for incremental extensions
  578. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  579. void GPHIKClassifier::addExample( const NICE::SparseVector * _example,
  580. const double & _label,
  581. const bool & _performOptimizationAfterIncrement
  582. )
  583. {
  584. if ( ! this->b_isTrained )
  585. {
  586. //call train method instead
  587. std::cerr << "Classifier not initially trained yet -- run initial training instead of incremental extension!" << std::endl;
  588. std::vector< const NICE::SparseVector *> examplesVec;
  589. examplesVec.push_back ( _example );
  590. NICE::Vector labelsVec ( 1 , _label );
  591. this->train ( examplesVec, labelsVec );
  592. }
  593. else
  594. {
  595. this->gphyper->addExample( _example, _label, _performOptimizationAfterIncrement );
  596. }
  597. }
  598. void GPHIKClassifier::addMultipleExamples( const std::vector< const NICE::SparseVector * > & _newExamples,
  599. const NICE::Vector & _newLabels,
  600. const bool & _performOptimizationAfterIncrement
  601. )
  602. {
  603. //are new examples available? If not, nothing has to be done
  604. if ( _newExamples.size() < 1)
  605. return;
  606. if ( ! this->b_isTrained )
  607. {
  608. //call train method instead
  609. std::cerr << "Classifier not initially trained yet -- run initial training instead of incremental extension!" << std::endl;
  610. this->train ( _newExamples, _newLabels );
  611. }
  612. else
  613. {
  614. this->gphyper->addMultipleExamples( _newExamples, _newLabels, _performOptimizationAfterIncrement );
  615. }
  616. }