GPHIKClassifier.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. void GPHIKClassifier::classify ( const NICE::SparseVector * _example,
  227. uint & _result,
  228. NICE::Vector & _scores,
  229. double & _uncertainty
  230. ) const
  231. {
  232. if ( ! this->b_isTrained )
  233. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  234. _result = gphyper->classify ( *_example, _scores );
  235. if ( _scores.size() == 0 ) {
  236. fthrow(Exception, "Zero scores, something is likely to be wrong here: svec.size() = " << _example->size() );
  237. }
  238. if ( this->uncertaintyPredictionForClassification )
  239. {
  240. if ( this->varianceApproximation != NONE)
  241. {
  242. this->predictUncertainty( _example, _uncertainty );
  243. }
  244. else
  245. {
  246. // //do nothing
  247. _uncertainty = std::numeric_limits<double>::max();
  248. }
  249. }
  250. else
  251. {
  252. //do nothing
  253. _uncertainty = std::numeric_limits<double>::max();
  254. }
  255. }
  256. void GPHIKClassifier::classify ( const std::vector< const NICE::SparseVector *> _examples,
  257. NICE::Vector & _results,
  258. NICE::Matrix & _scores,
  259. NICE::Vector & _uncertainties
  260. ) const
  261. {
  262. if ( ! this->b_isTrained )
  263. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  264. std::set<unsigned int> knownClasses = (this->getKnownClassNumbers());
  265. _scores.resize( _examples.size(), * (knownClasses.rbegin()) +1 );
  266. _scores.set( 0.0 );
  267. _results.resize( _examples.size() );
  268. _results.set( 0.0 );
  269. _uncertainties.resize( _examples.size() );
  270. _uncertainties.set( 0.0 );
  271. NICE::Vector::iterator resultsIt = _results.begin();
  272. NICE::Vector::iterator uncIt = _uncertainties.begin();
  273. uint exCnt ( 0 );
  274. uint resUI ( 0 );
  275. NICE::Vector scoresSingle( * (knownClasses.rbegin()) +1, -std::numeric_limits<double>::max() );
  276. double uncSingle ( 0.0 );
  277. for ( std::vector< const NICE::SparseVector *>::const_iterator exIt = _examples.begin();
  278. exIt != _examples.end();
  279. exIt++, resultsIt++, exCnt++, uncIt++
  280. )
  281. {
  282. this->classify ( *exIt,
  283. resUI,
  284. scoresSingle,
  285. uncSingle
  286. );
  287. *resultsIt = resUI;
  288. *uncIt = uncSingle;
  289. _scores.setRow( exCnt, scoresSingle );
  290. scoresSingle.set( -std::numeric_limits<double>::max() );
  291. }
  292. }
  293. /** training process */
  294. void GPHIKClassifier::train ( const std::vector< const NICE::SparseVector *> & _examples,
  295. const NICE::Vector & _labels
  296. )
  297. {
  298. //FIXME add check whether the classifier has been trained already. if so, discard all previous results.
  299. // security-check: examples and labels have to be of same size
  300. if ( _examples.size() != _labels.size() )
  301. {
  302. fthrow(Exception, "Given examples do not match label vector in size -- aborting!" );
  303. }
  304. if (b_verbose)
  305. {
  306. std::cerr << "GPHIKClassifier::train" << std::endl;
  307. }
  308. Timer t;
  309. t.start();
  310. FastMinKernel *fmk = new FastMinKernel ( _examples, d_noise, this->b_debug );
  311. this->gphyper->setFastMinKernel ( fmk );
  312. t.stop();
  313. if (b_verbose)
  314. std::cerr << "Time used for setting up the fmk object: " << t.getLast() << std::endl;
  315. if (b_verbose)
  316. std::cerr << "Learning ..." << endl;
  317. // go go go
  318. this->gphyper->optimize ( _labels );
  319. if (b_verbose)
  320. std::cerr << "optimization done" << std::endl;
  321. if ( ( this->varianceApproximation != NONE ) )
  322. {
  323. switch ( this->varianceApproximation )
  324. {
  325. case APPROXIMATE_ROUGH:
  326. {
  327. this->gphyper->prepareVarianceApproximationRough();
  328. break;
  329. }
  330. case APPROXIMATE_FINE:
  331. {
  332. this->gphyper->prepareVarianceApproximationFine();
  333. break;
  334. }
  335. case EXACT:
  336. {
  337. //nothing to prepare
  338. break;
  339. }
  340. default:
  341. {
  342. //nothing to prepare
  343. }
  344. }
  345. }
  346. //indicate that we finished training successfully
  347. this->b_isTrained = true;
  348. // clean up all examples ??
  349. if (b_verbose)
  350. std::cerr << "Learning finished" << std::endl;
  351. }
  352. /** training process */
  353. void GPHIKClassifier::train ( const std::vector< const NICE::SparseVector *> & _examples,
  354. std::map<uint, NICE::Vector> & _binLabels
  355. )
  356. {
  357. // security-check: examples and labels have to be of same size
  358. for ( std::map< uint, NICE::Vector >::const_iterator binLabIt = _binLabels.begin();
  359. binLabIt != _binLabels.end();
  360. binLabIt++
  361. )
  362. {
  363. if ( _examples.size() != binLabIt->second.size() )
  364. {
  365. fthrow(Exception, "Given examples do not match label vector in size -- aborting!" );
  366. }
  367. }
  368. if ( this->b_verbose )
  369. std::cerr << "GPHIKClassifier::train" << std::endl;
  370. Timer t;
  371. t.start();
  372. FastMinKernel *fmk = new FastMinKernel ( _examples, d_noise, this->b_debug );
  373. this->gphyper->setFastMinKernel ( fmk );
  374. t.stop();
  375. if ( this->b_verbose )
  376. std::cerr << "Time used for setting up the fmk object: " << t.getLast() << std::endl;
  377. if ( this->b_verbose )
  378. std::cerr << "Learning ..." << std::endl;
  379. // go go go
  380. this->gphyper->optimize ( _binLabels );
  381. if ( this->b_verbose )
  382. std::cerr << "optimization done, now prepare for the uncertainty prediction" << std::endl;
  383. if ( ( this->varianceApproximation != NONE ) )
  384. {
  385. switch ( this->varianceApproximation )
  386. {
  387. case APPROXIMATE_ROUGH:
  388. {
  389. gphyper->prepareVarianceApproximationRough();
  390. break;
  391. }
  392. case APPROXIMATE_FINE:
  393. {
  394. gphyper->prepareVarianceApproximationFine();
  395. break;
  396. }
  397. case EXACT:
  398. {
  399. //nothing to prepare
  400. break;
  401. }
  402. default:
  403. {
  404. //nothing to prepare
  405. }
  406. }
  407. }
  408. //indicate that we finished training successfully
  409. this->b_isTrained = true;
  410. // clean up all examples ??
  411. if ( this->b_verbose )
  412. std::cerr << "Learning finished" << std::endl;
  413. }
  414. GPHIKClassifier *GPHIKClassifier::clone () const
  415. {
  416. fthrow(Exception, "GPHIKClassifier: clone() not yet implemented" );
  417. return NULL;
  418. }
  419. void GPHIKClassifier::predictUncertainty( const NICE::SparseVector * _example,
  420. double & _uncertainty
  421. ) const
  422. {
  423. if ( this->gphyper == NULL )
  424. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  425. //we directly store the predictive variances in the vector, that contains the classification uncertainties lateron to save storage
  426. switch ( this->varianceApproximation )
  427. {
  428. case APPROXIMATE_ROUGH:
  429. {
  430. this->gphyper->computePredictiveVarianceApproximateRough( *_example, _uncertainty );
  431. break;
  432. }
  433. case APPROXIMATE_FINE:
  434. {
  435. this->gphyper->computePredictiveVarianceApproximateFine( *_example, _uncertainty );
  436. break;
  437. }
  438. case EXACT:
  439. {
  440. this->gphyper->computePredictiveVarianceExact( *_example, _uncertainty );
  441. break;
  442. }
  443. default:
  444. {
  445. fthrow(Exception, "GPHIKClassifier - your settings disabled the variance approximation needed for uncertainty prediction.");
  446. }
  447. }
  448. }
  449. void GPHIKClassifier::predictUncertainty( const NICE::Vector * _example,
  450. double & _uncertainty
  451. ) const
  452. {
  453. if ( this->gphyper == NULL )
  454. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  455. //we directly store the predictive variances in the vector, that contains the classification uncertainties lateron to save storage
  456. switch ( this->varianceApproximation )
  457. {
  458. case APPROXIMATE_ROUGH:
  459. {
  460. this->gphyper->computePredictiveVarianceApproximateRough( *_example, _uncertainty );
  461. break;
  462. }
  463. case APPROXIMATE_FINE:
  464. {
  465. this->gphyper->computePredictiveVarianceApproximateFine( *_example, _uncertainty );
  466. break;
  467. }
  468. case EXACT:
  469. {
  470. this->gphyper->computePredictiveVarianceExact( *_example, _uncertainty );
  471. break;
  472. }
  473. default:
  474. {
  475. fthrow(Exception, "GPHIKClassifier - your settings disabled the variance approximation needed for uncertainty prediction.");
  476. }
  477. }
  478. }
  479. ///////////////////// INTERFACE PERSISTENT /////////////////////
  480. // interface specific methods for store and restore
  481. ///////////////////// INTERFACE PERSISTENT /////////////////////
  482. void GPHIKClassifier::restore ( std::istream & _is,
  483. int _format
  484. )
  485. {
  486. //delete everything we knew so far...
  487. this->clear();
  488. bool b_restoreVerbose ( false );
  489. #ifdef B_RESTOREVERBOSE
  490. b_restoreVerbose = true;
  491. #endif
  492. if ( _is.good() )
  493. {
  494. if ( b_restoreVerbose )
  495. std::cerr << " restore GPHIKClassifier" << std::endl;
  496. std::string tmp;
  497. _is >> tmp; //class name
  498. if ( ! this->isStartTag( tmp, "GPHIKClassifier" ) )
  499. {
  500. std::cerr << " WARNING - attempt to restore GPHIKClassifier, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  501. throw;
  502. }
  503. if (gphyper != NULL)
  504. {
  505. delete gphyper;
  506. gphyper = NULL;
  507. }
  508. _is.precision (numeric_limits<double>::digits10 + 1);
  509. bool b_endOfBlock ( false ) ;
  510. while ( !b_endOfBlock )
  511. {
  512. _is >> tmp; // start of block
  513. if ( this->isEndTag( tmp, "GPHIKClassifier" ) )
  514. {
  515. b_endOfBlock = true;
  516. continue;
  517. }
  518. tmp = this->removeStartTag ( tmp );
  519. if ( b_restoreVerbose )
  520. std::cerr << " currently restore section " << tmp << " in GPHIKClassifier" << std::endl;
  521. if ( tmp.compare("confSection") == 0 )
  522. {
  523. _is >> confSection;
  524. _is >> tmp; // end of block
  525. tmp = this->removeEndTag ( tmp );
  526. }
  527. else if ( tmp.compare("gphyper") == 0 )
  528. {
  529. if ( this->gphyper == NULL )
  530. this->gphyper = new NICE::FMKGPHyperparameterOptimization();
  531. //then, load everything that we stored explicitely,
  532. // including precomputed matrices, LUTs, eigenvalues, ... and all that stuff
  533. this->gphyper->restore( _is, _format );
  534. _is >> tmp; // end of block
  535. tmp = this->removeEndTag ( tmp );
  536. }
  537. else if ( tmp.compare("b_isTrained") == 0 )
  538. {
  539. _is >> b_isTrained;
  540. _is >> tmp; // end of block
  541. tmp = this->removeEndTag ( tmp );
  542. }
  543. else if ( tmp.compare("d_noise") == 0 )
  544. {
  545. _is >> d_noise;
  546. _is >> tmp; // end of block
  547. tmp = this->removeEndTag ( tmp );
  548. }
  549. else if ( tmp.compare("b_verbose") == 0 )
  550. {
  551. _is >> b_verbose;
  552. _is >> tmp; // end of block
  553. tmp = this->removeEndTag ( tmp );
  554. }
  555. else if ( tmp.compare("b_debug") == 0 )
  556. {
  557. _is >> b_debug;
  558. _is >> tmp; // end of block
  559. tmp = this->removeEndTag ( tmp );
  560. }
  561. else if ( tmp.compare("uncertaintyPredictionForClassification") == 0 )
  562. {
  563. _is >> uncertaintyPredictionForClassification;
  564. _is >> tmp; // end of block
  565. tmp = this->removeEndTag ( tmp );
  566. }
  567. else if ( tmp.compare("varianceApproximation") == 0 )
  568. {
  569. unsigned int ui_varianceApproximation;
  570. _is >> ui_varianceApproximation;
  571. varianceApproximation = static_cast<VarianceApproximation> ( ui_varianceApproximation );
  572. _is >> tmp; // end of block
  573. tmp = this->removeEndTag ( tmp );
  574. }
  575. else
  576. {
  577. std::cerr << "WARNING -- unexpected GPHIKClassifier object -- " << tmp << " -- for restoration... aborting" << std::endl;
  578. throw;
  579. }
  580. }
  581. }
  582. else
  583. {
  584. std::cerr << "GPHIKClassifier::restore -- InStream not initialized - restoring not possible!" << std::endl;
  585. throw;
  586. }
  587. }
  588. void GPHIKClassifier::store ( std::ostream & _os,
  589. int _format
  590. ) const
  591. {
  592. if ( _os.good() )
  593. {
  594. // show starting point
  595. _os << this->createStartTag( "GPHIKClassifier" ) << std::endl;
  596. _os.precision (numeric_limits<double>::digits10 + 1);
  597. _os << this->createStartTag( "confSection" ) << std::endl;
  598. _os << confSection << std::endl;
  599. _os << this->createEndTag( "confSection" ) << std::endl;
  600. _os << this->createStartTag( "gphyper" ) << std::endl;
  601. //store the underlying data
  602. //will be done in gphyper->store(of,format)
  603. //store the optimized parameter values and all that stuff
  604. this->gphyper->store( _os, _format );
  605. _os << this->createEndTag( "gphyper" ) << std::endl;
  606. /////////////////////////////////////////////////////////
  607. // store variables which we previously set via config
  608. /////////////////////////////////////////////////////////
  609. _os << this->createStartTag( "b_isTrained" ) << std::endl;
  610. _os << b_isTrained << std::endl;
  611. _os << this->createEndTag( "b_isTrained" ) << std::endl;
  612. _os << this->createStartTag( "d_noise" ) << std::endl;
  613. _os << d_noise << std::endl;
  614. _os << this->createEndTag( "d_noise" ) << std::endl;
  615. _os << this->createStartTag( "b_verbose" ) << std::endl;
  616. _os << b_verbose << std::endl;
  617. _os << this->createEndTag( "b_verbose" ) << std::endl;
  618. _os << this->createStartTag( "b_debug" ) << std::endl;
  619. _os << b_debug << std::endl;
  620. _os << this->createEndTag( "b_debug" ) << std::endl;
  621. _os << this->createStartTag( "uncertaintyPredictionForClassification" ) << std::endl;
  622. _os << uncertaintyPredictionForClassification << std::endl;
  623. _os << this->createEndTag( "uncertaintyPredictionForClassification" ) << std::endl;
  624. _os << this->createStartTag( "varianceApproximation" ) << std::endl;
  625. _os << varianceApproximation << std::endl;
  626. _os << this->createEndTag( "varianceApproximation" ) << std::endl;
  627. // done
  628. _os << this->createEndTag( "GPHIKClassifier" ) << std::endl;
  629. }
  630. else
  631. {
  632. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  633. }
  634. }
  635. void GPHIKClassifier::clear ()
  636. {
  637. if ( this->gphyper != NULL )
  638. {
  639. delete this->gphyper;
  640. this->gphyper = NULL;
  641. }
  642. }
  643. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  644. // interface specific methods for incremental extensions
  645. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  646. void GPHIKClassifier::addExample( const NICE::SparseVector * _example,
  647. const double & _label,
  648. const bool & _performOptimizationAfterIncrement
  649. )
  650. {
  651. if ( ! this->b_isTrained )
  652. {
  653. //call train method instead
  654. std::cerr << "Classifier not initially trained yet -- run initial training instead of incremental extension!" << std::endl;
  655. std::vector< const NICE::SparseVector *> examplesVec;
  656. examplesVec.push_back ( _example );
  657. NICE::Vector labelsVec ( 1 , _label );
  658. this->train ( examplesVec, labelsVec );
  659. }
  660. else
  661. {
  662. this->gphyper->addExample( _example, _label, _performOptimizationAfterIncrement );
  663. }
  664. }
  665. void GPHIKClassifier::addMultipleExamples( const std::vector< const NICE::SparseVector * > & _newExamples,
  666. const NICE::Vector & _newLabels,
  667. const bool & _performOptimizationAfterIncrement
  668. )
  669. {
  670. //are new examples available? If not, nothing has to be done
  671. if ( _newExamples.size() < 1)
  672. return;
  673. if ( ! this->b_isTrained )
  674. {
  675. //call train method instead
  676. std::cerr << "Classifier not initially trained yet -- run initial training instead of incremental extension!" << std::endl;
  677. this->train ( _newExamples, _newLabels );
  678. }
  679. else
  680. {
  681. this->gphyper->addMultipleExamples( _newExamples, _newLabels, _performOptimizationAfterIncrement );
  682. }
  683. }