FastMinKernel.cpp 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784
  1. /**
  2. * @file FastMinKernel.cpp
  3. * @brief Efficient GPs with HIK for classification by regression (Implementation)
  4. * @author Alexander Freytag
  5. * @date 06-12-2011 (dd-mm-yyyy)
  6. */
  7. // STL includes
  8. #include <iostream>
  9. // NICE-core includes
  10. #include <core/basics/vectorio.h>
  11. #include <core/basics/Timer.h>
  12. // gp-hik-core includes
  13. #include "FastMinKernel.h"
  14. using namespace std;
  15. using namespace NICE;
  16. /* protected methods*/
  17. /////////////////////////////////////////////////////
  18. /////////////////////////////////////////////////////
  19. // PUBLIC METHODS
  20. /////////////////////////////////////////////////////
  21. /////////////////////////////////////////////////////
  22. FastMinKernel::FastMinKernel()
  23. {
  24. this->ui_d = 0;
  25. this->ui_n = 0;
  26. this->d_noise = 1.0;
  27. this->approxScheme = MEDIAN;
  28. this->b_verbose = false;
  29. this->setDebug(false);
  30. }
  31. FastMinKernel::FastMinKernel( const std::vector<std::vector<double> > & _X,
  32. const double _noise,
  33. const bool _debug,
  34. const uint & _dim
  35. )
  36. {
  37. this->setDebug(_debug);
  38. this->hik_prepare_kernel_multiplications ( _X, this->X_sorted, _dim);
  39. this->ui_d = this->X_sorted.get_d();
  40. this->ui_n = this->X_sorted.get_n();
  41. this->d_noise = _noise;
  42. this->approxScheme = MEDIAN;
  43. this->b_verbose = false;
  44. }
  45. #ifdef NICE_USELIB_MATIO
  46. FastMinKernel::FastMinKernel ( const sparse_t & _X,
  47. const double _noise,
  48. const std::map<uint, uint> & _examples,
  49. const bool _debug,
  50. const uint & _dim
  51. ) : this->X_sorted( _X, _examples, _dim )
  52. {
  53. this->ui_d = this->X_sorted.get_d();
  54. this->ui_n = this->X_sorted.get_n();
  55. this->d_noise = _noise;
  56. this->approxScheme = MEDIAN;
  57. this->b_verbose = false;
  58. this->setDebug(_debug);
  59. }
  60. #endif
  61. FastMinKernel::FastMinKernel ( const std::vector< const NICE::SparseVector * > & _X,
  62. const double _noise,
  63. const bool _debug,
  64. const bool & _dimensionsOverExamples,
  65. const uint & _dim)
  66. {
  67. this->setDebug(_debug);
  68. this->hik_prepare_kernel_multiplications ( _X, this->X_sorted, _dimensionsOverExamples, _dim);
  69. this->ui_d = this->X_sorted.get_d();
  70. this->ui_n = this->X_sorted.get_n();
  71. this->d_noise = _noise;
  72. this->approxScheme = MEDIAN;
  73. this->b_verbose = false;
  74. }
  75. FastMinKernel::~FastMinKernel()
  76. {
  77. }
  78. ///////////////////// ///////////////////// /////////////////////
  79. // GET / SET
  80. // INCLUDING ACCESS OPERATORS
  81. ///////////////////// ///////////////////// ////////////////////
  82. uint FastMinKernel::get_n() const
  83. {
  84. return this->ui_n;
  85. }
  86. uint FastMinKernel::get_d() const
  87. {
  88. return this->ui_d;
  89. }
  90. double FastMinKernel::getSparsityRatio() const
  91. {
  92. return this->X_sorted.computeSparsityRatio();
  93. }
  94. void FastMinKernel::setVerbose( const bool & _verbose)
  95. {
  96. this->b_verbose = _verbose;
  97. }
  98. bool FastMinKernel::getVerbose( ) const
  99. {
  100. return this->b_verbose;
  101. }
  102. void FastMinKernel::setDebug( const bool & _debug)
  103. {
  104. this->b_debug = _debug;
  105. this->X_sorted.setDebug( _debug );
  106. }
  107. bool FastMinKernel::getDebug( ) const
  108. {
  109. return this->b_debug;
  110. }
  111. ///////////////////// ///////////////////// /////////////////////
  112. // CLASSIFIER STUFF
  113. ///////////////////// ///////////////////// /////////////////////
  114. void FastMinKernel::applyFunctionToFeatureMatrix ( const NICE::ParameterizedFunction *_pf)
  115. {
  116. this->X_sorted.applyFunctionToFeatureMatrix( _pf );
  117. }
  118. void FastMinKernel::hik_prepare_kernel_multiplications(const std::vector<std::vector<double> > & _X,
  119. NICE::FeatureMatrixT<double> & _X_sorted,
  120. const uint & _dim
  121. )
  122. {
  123. //FIXME why do we hand over the feature matrix here?
  124. _X_sorted.set_features( _X, _dim);
  125. }
  126. void FastMinKernel::hik_prepare_kernel_multiplications(const std::vector< const NICE::SparseVector * > & _X,
  127. NICE::FeatureMatrixT<double> & _X_sorted,
  128. const bool & _dimensionsOverExamples,
  129. const uint & _dim
  130. )
  131. {
  132. //FIXME why do we hand over the feature matrix here?
  133. std::cerr << "FastMinKernel::hik_prepare_kernel_multiplications -- size of _X : " << _X.size() << std::endl;
  134. _X_sorted.set_features( _X, _dimensionsOverExamples, _dim );
  135. }
  136. void FastMinKernel::hik_prepare_alpha_multiplications(const NICE::Vector & _alpha,
  137. NICE::VVector & _A,
  138. NICE::VVector & _B) const
  139. {
  140. // std::cerr << "FastMinKernel::hik_prepare_alpha_multiplications" << std::endl;
  141. // std::cerr << "alpha: " << alpha << std::endl;
  142. _A.resize( this->ui_d );
  143. _B.resize( this->ui_d );
  144. // efficient calculation of k*alpha
  145. // ---------------------------------
  146. //
  147. // sum_i alpha_i k(x^i,x) = sum_i alpha_i sum_k min(x^i_k,x_k)
  148. // = sum_k sum_i alpha_i min(x^i_k, x_k)
  149. //
  150. // now let us define l_k = { i | x^i_k <= x_k }
  151. // and u_k = { i | x^i_k > x_k }, this leads to
  152. //
  153. // = sum_k ( sum_{l \in l_k} alpha_l x^i_k + sum_{u \in u_k} alpha_u x_k
  154. // = sum_k ( sum_{l \in l_k} \alpha_l x^l_k + x_k * sum_{u \in u_k}
  155. // alpha_u
  156. //
  157. // We also define
  158. // l^j_k = { i | x^i_j <= x^j_k } and
  159. // u^j_k = { i | x^i_k > x^j_k }
  160. //
  161. // We now need the partial sums
  162. //
  163. // (Definition 1)
  164. // a_{k,j} = \sum_{l \in l^j_k} \alpha_l x^l_k
  165. //
  166. // and \sum_{u \in u^j_k} \alpha_u
  167. // according to increasing values of x^l_k
  168. //
  169. // With
  170. // (Definition 2)
  171. // b_{k,j} = \sum_{l \in l^j_k} \alpha_l,
  172. //
  173. // we get
  174. // \sum_{u \in u^j_k} \alpha_u = \sum_{u=1}^n alpha_u - \sum_{l \in l^j_k} \alpha_l
  175. // = b_{k,n} - b_{k,j}
  176. // we only need as many entries as we have nonZero entries in our features for the corresponding dimensions
  177. for (uint i = 0; i < this->ui_d; i++)
  178. {
  179. uint numNonZero = this->X_sorted.getNumberOfNonZeroElementsPerDimension(i);
  180. //DEBUG
  181. //std::cerr << "number of non-zero elements in dimension " << i << " / " << d << ": " << numNonZero << std::endl;
  182. _A[i].resize( numNonZero );
  183. _B[i].resize( numNonZero );
  184. }
  185. // for more information see hik_prepare_alpha_multiplications
  186. for (uint dim = 0; dim < this->ui_d; dim++)
  187. {
  188. double alpha_sum(0.0);
  189. double alpha_times_x_sum(0.0);
  190. uint cntNonzeroFeat(0);
  191. const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
  192. // loop through all elements in sorted order
  193. for ( SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin(); i != nonzeroElements.end(); i++ )
  194. {
  195. const SortedVectorSparse<double>::dataelement & de = i->second;
  196. // index of the feature
  197. int index = de.first;
  198. // transformed element of the feature
  199. //
  200. double elem( de.second );
  201. alpha_times_x_sum += _alpha[index] * elem;
  202. _A[dim][cntNonzeroFeat] = alpha_times_x_sum;
  203. alpha_sum += _alpha[index];
  204. _B[dim][cntNonzeroFeat] = alpha_sum;
  205. cntNonzeroFeat++;
  206. }
  207. }
  208. }
  209. double *FastMinKernel::hik_prepare_alpha_multiplications_fast(const NICE::VVector & _A,
  210. const NICE::VVector & _B,
  211. const Quantization & _q,
  212. const ParameterizedFunction *_pf
  213. ) const
  214. {
  215. //NOTE keep in mind: for doing this, we already have precomputed A and B using hik_prepare_alpha_multiplications!
  216. // number of quantization bins
  217. uint hmax = _q.size();
  218. // store (transformed) prototypes
  219. double *prototypes = new double [ hmax ];
  220. for ( uint i = 0 ; i < hmax ; i++ )
  221. if ( _pf != NULL ) {
  222. // FIXME: the transformed prototypes could change from dimension to another dimension
  223. // We skip this flexibility ...but it should be changed in the future
  224. prototypes[i] = _pf->f ( 1, _q.getPrototype(i) );
  225. } else {
  226. prototypes[i] = _q.getPrototype(i);
  227. }
  228. // creating the lookup table as pure C, which might be beneficial
  229. // for fast evaluation
  230. double *Tlookup = new double [ hmax * this->ui_d ];
  231. // std::cerr << "size of LUT: " << hmax * this->ui_d << std::endl;
  232. // sizeOfLUT = hmax * this->d;
  233. // loop through all dimensions
  234. for ( uint dim = 0; dim < this->ui_d; dim++ )
  235. {
  236. uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
  237. if ( nrZeroIndices == this->ui_n )
  238. continue;
  239. const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
  240. SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin();
  241. SortedVectorSparse<double>::const_elementpointer iPredecessor = nonzeroElements.begin();
  242. // index of the element, which is always bigger than the current value fval
  243. uint index = 0;
  244. // we use the quantization of the original features! the transformed feature were
  245. // already used to calculate A and B, this of course assumes monotonic functions!!!
  246. uint qBin = _q.quantize ( i->first );
  247. // the next loop is linear in max(hmax, n)
  248. // REMARK: this could be changed to hmax*log(n), when
  249. // we use binary search
  250. for (uint j = 0; j < hmax; j++)
  251. {
  252. double fval = prototypes[j];
  253. double t;
  254. if ( (index == 0) && (j < qBin) ) {
  255. // current element is smaller than everything else
  256. // resulting value = fval * sum_l=1^n alpha_l
  257. t = fval*( _B[dim][this->ui_n-1 - nrZeroIndices] );
  258. } else {
  259. // move to next example, if necessary
  260. while ( (j >= qBin) && ( index < (this->ui_n-1-nrZeroIndices)) )
  261. {
  262. index++;
  263. iPredecessor = i;
  264. i++;
  265. if ( i->first != iPredecessor->first )
  266. qBin = _q.quantize ( i->first );
  267. }
  268. // compute current element in the lookup table and keep in mind that
  269. // index is the next element and not the previous one
  270. //NOTE pay attention: this is only valid if we all entries are positiv! - if not, ask wether the current feature is greater than zero. If so, subtract the nrZeroIndices, if not do not
  271. if ( (j >= qBin) && ( index==(this->ui_n-1-nrZeroIndices) ) ) {
  272. // the current element (fval) is equal or bigger to the element indexed by index
  273. // in fact, the term B[dim][this->n-1-nrZeroIndices] - B[dim][index] is equal to zero and vanishes, which is logical, since all elements are smaller than j!
  274. t = _A[dim][index];// + fval*( _B[dim][this->ui_n-1-nrZeroIndices] - _B[dim][index] );
  275. } else {
  276. // standard case
  277. t = _A[dim][index-1] + fval*( _B[dim][this->ui_n-1-nrZeroIndices] - _B[dim][index-1] );
  278. }
  279. }
  280. Tlookup[ dim*hmax + j ] = t;
  281. }
  282. }
  283. delete [] prototypes;
  284. return Tlookup;
  285. }
  286. double *FastMinKernel::hikPrepareLookupTable(const NICE::Vector & _alpha,
  287. const Quantization & _q,
  288. const ParameterizedFunction *_pf
  289. ) const
  290. {
  291. // number of quantization bins
  292. uint hmax = _q.size();
  293. // store (transformed) prototypes
  294. double *prototypes = new double [ hmax ];
  295. for ( uint i = 0 ; i < hmax ; i++ )
  296. if ( _pf != NULL ) {
  297. // FIXME: the transformed prototypes could change from dimension to another dimension
  298. // We skip this flexibility ...but it should be changed in the future
  299. prototypes[i] = _pf->f ( 1, _q.getPrototype(i) );
  300. } else {
  301. prototypes[i] = _q.getPrototype(i);
  302. }
  303. // creating the lookup table as pure C, which might be beneficial
  304. // for fast evaluation
  305. double *Tlookup = new double [ hmax * this->ui_d ];
  306. // sizeOfLUT = hmax * this->d;
  307. // loop through all dimensions
  308. for (uint dim = 0; dim < this->ui_d; dim++)
  309. {
  310. uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
  311. if ( nrZeroIndices == this->ui_n )
  312. continue;
  313. const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
  314. double alphaSumTotalInDim(0.0);
  315. double alphaTimesXSumTotalInDim(0.0);
  316. for ( SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin(); i != nonzeroElements.end(); i++ )
  317. {
  318. alphaSumTotalInDim += _alpha[i->second.first];
  319. alphaTimesXSumTotalInDim += _alpha[i->second.first] * i->second.second;
  320. }
  321. SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin();
  322. SortedVectorSparse<double>::const_elementpointer iPredecessor = nonzeroElements.begin();
  323. // index of the element, which is always bigger than the current value fval
  324. uint index = 0;
  325. // we use the quantization of the original features! Nevetheless, the resulting lookupTable is computed using the transformed ones
  326. uint qBin = _q.quantize ( i->first );
  327. double alpha_sum(0.0);
  328. double alpha_times_x_sum(0.0);
  329. double alpha_sum_prev(0.0);
  330. double alpha_times_x_sum_prev(0.0);
  331. for (uint j = 0; j < hmax; j++)
  332. {
  333. double fval = prototypes[j];
  334. double t;
  335. if ( (index == 0) && (j < qBin) ) {
  336. // current element is smaller than everything else
  337. // resulting value = fval * sum_l=1^n alpha_l
  338. //t = fval*( B[dim][this->n-1 - nrZeroIndices] );
  339. t = fval*alphaSumTotalInDim;
  340. } else {
  341. // move to next example, if necessary
  342. while ( (j >= qBin) && ( index < (this->ui_n-1-nrZeroIndices)) )
  343. {
  344. alpha_times_x_sum_prev = alpha_times_x_sum;
  345. alpha_sum_prev = alpha_sum;
  346. alpha_times_x_sum += _alpha[i->second.first] * i->second.second; //i->dataElement.transformedFeatureValue
  347. alpha_sum += _alpha[i->second.first]; //i->dataElement.OrigIndex
  348. index++;
  349. iPredecessor = i;
  350. i++;
  351. if ( i->first != iPredecessor->first )
  352. qBin = _q.quantize ( i->first );
  353. }
  354. // compute current element in the lookup table and keep in mind that
  355. // index is the next element and not the previous one
  356. //NOTE pay attention: this is only valid if all entries are positiv! - if not, ask wether the current feature is greater than zero. If so, subtract the nrZeroIndices, if not do not
  357. if ( (j >= qBin) && ( index==(this->ui_n-1-nrZeroIndices) ) ) {
  358. // the current element (fval) is equal or bigger to the element indexed by index
  359. // in fact, the term B[dim][this->n-1-nrZeroIndices] - B[dim][index] is equal to zero and vanishes, which is logical, since all elements are smaller than j!
  360. // double lastTermAlphaTimesXSum;
  361. // double lastTermAlphaSum;
  362. t = alphaTimesXSumTotalInDim;
  363. } else {
  364. // standard case
  365. t = alpha_times_x_sum + fval*( alphaSumTotalInDim - alpha_sum );
  366. }
  367. }
  368. Tlookup[ dim*hmax + j ] = t;
  369. }
  370. }
  371. delete [] prototypes;
  372. return Tlookup;
  373. }
  374. void FastMinKernel::hikUpdateLookupTable(double * _T,
  375. const double & _alphaNew,
  376. const double & _alphaOld,
  377. const uint & _idx,
  378. const Quantization & _q,
  379. const ParameterizedFunction *_pf
  380. ) const
  381. {
  382. if (_T == NULL)
  383. {
  384. fthrow(Exception, "FastMinKernel::hikUpdateLookupTable LUT not initialized, run FastMinKernel::hikPrepareLookupTable first!");
  385. return;
  386. }
  387. // number of quantization bins
  388. uint hmax = _q.size();
  389. // store (transformed) prototypes
  390. double *prototypes = new double [ hmax ];
  391. for ( uint i = 0 ; i < hmax ; i++ )
  392. if ( _pf != NULL ) {
  393. // FIXME: the transformed prototypes could change from dimension to another dimension
  394. // We skip this flexibility ...but it should be changed in the future
  395. prototypes[i] = _pf->f ( 1, _q.getPrototype(i) );
  396. } else {
  397. prototypes[i] = _q.getPrototype(i);
  398. }
  399. double diffOfAlpha(_alphaNew - _alphaOld);
  400. // loop through all dimensions
  401. for ( uint dim = 0; dim < this->ui_d; dim++ )
  402. {
  403. double x_i ( (this->X_sorted( dim, _idx)) );
  404. //TODO we could also check wether x_i < tol, if we would store the tol explicitely
  405. if ( x_i == 0.0 ) //nothing to do in this dimension
  406. continue;
  407. //TODO we could speed up this by first doing a binary search for the position where the min changes, and then do two separate for-loops
  408. for (uint j = 0; j < hmax; j++)
  409. {
  410. double fval;
  411. uint q_bin = _q.quantize(x_i);
  412. if ( q_bin > j )
  413. fval = prototypes[j];
  414. else
  415. fval = x_i;
  416. _T[ dim*hmax + j ] += diffOfAlpha*fval;
  417. }
  418. }
  419. delete [] prototypes;
  420. }
  421. void FastMinKernel::hik_kernel_multiply(const NICE::VVector & _A,
  422. const NICE::VVector & _B,
  423. const NICE::Vector & _alpha,
  424. NICE::Vector & _beta
  425. ) const
  426. {
  427. _beta.resize( this->ui_n );
  428. _beta.set(0.0);
  429. // runtime is O(n*d), we do no benefit from an additional lookup table here
  430. for (uint dim = 0; dim < this->ui_d; dim++)
  431. {
  432. // -- efficient sparse solution
  433. const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
  434. uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
  435. if ( nrZeroIndices == this->ui_n ) {
  436. // all values are zero in this dimension :) and we can simply ignore the feature
  437. continue;
  438. }
  439. uint cnt(0);
  440. for ( multimap< double, SortedVectorSparse<double>::dataelement>::const_iterator i = nonzeroElements.begin(); i != nonzeroElements.end(); i++, cnt++)
  441. {
  442. const SortedVectorSparse<double>::dataelement & de = i->second;
  443. uint feat = de.first;
  444. uint inversePosition = cnt;
  445. double fval = de.second;
  446. // in which position was the element sorted in? actually we only care about the nonzero elements, so we have to subtract the number of zero elements.
  447. //NOTE pay attention: this is only valid if all entries are positiv! - if not, ask wether the current feature is greater than zero. If so, subtract the nrZeroIndices, if not do not
  448. //we definitly know that this element exists in inversePermutation, so we have not to check wether find returns .end() or not
  449. //int inversePosition(inversePermutation.find(feat)->second - nrZeroIndices);
  450. // sum_{l \in L_k} \alpha_l x^l_k
  451. //
  452. // A is zero for zero feature values (x^l_k is zero for all l \in L_k)
  453. double firstPart( _A[dim][inversePosition] );
  454. // sum_{u \in U_k} alpha_u
  455. // B is not zero for zero feature values, but we do not
  456. // have to care about them, because it is multiplied with
  457. // the feature value
  458. // DEBUG for Björns code
  459. if ( dim >= _B.size() )
  460. fthrow(Exception, "dim exceeds B.size: " << dim << " " << _B.size() );
  461. if ( _B[dim].size() == 0 )
  462. fthrow(Exception, "B[dim] is empty");
  463. if ( (this->ui_n-1-nrZeroIndices < 0) || ((uint)(this->ui_n-1-nrZeroIndices) >= _B[dim].size() ) )
  464. fthrow(Exception, "n-1-nrZeroIndices is invalid: " << this->ui_n << " " << nrZeroIndices << " " << _B[dim].size() << " d: " << this->ui_d);
  465. if ( inversePosition < 0 || (uint)inversePosition >= _B[dim].size() )
  466. fthrow(Exception, "inverse position is invalid: " << inversePosition << " " << _B[dim].size() );
  467. double secondPart( _B[dim][this->ui_n-1-nrZeroIndices] - _B[dim][inversePosition]);
  468. _beta[feat] += firstPart + fval * secondPart; // i->elementpointer->dataElement->Value
  469. }
  470. }
  471. //FIXME
  472. //do we really want to considere noisy labels?
  473. for (uint feat = 0; feat < this->ui_n; feat++)
  474. {
  475. _beta[feat] += this->d_noise*_alpha[feat];
  476. }
  477. }
  478. void FastMinKernel::hik_kernel_multiply_fast(const double *_Tlookup,
  479. const Quantization & _q,
  480. const NICE::Vector & _alpha,
  481. NICE::Vector & _beta) const
  482. {
  483. _beta.resize( this->ui_n );
  484. _beta.set(0.0);
  485. // runtime is O(n*d), we do no benefit from an additional lookup table here
  486. for (uint dim = 0; dim < this->ui_d; dim++)
  487. {
  488. // -- efficient sparse solution
  489. const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
  490. uint cnt(0);
  491. for ( multimap< double, SortedVectorSparse<double>::dataelement>::const_iterator i = nonzeroElements.begin(); i != nonzeroElements.end(); i++, cnt++)
  492. {
  493. const SortedVectorSparse<double>::dataelement & de = i->second;
  494. uint feat = de.first;
  495. uint qBin = _q.quantize(i->first);
  496. _beta[feat] += _Tlookup[dim*_q.size() + qBin];
  497. }
  498. }
  499. //do we really want to considere noisy labels?
  500. for (uint feat = 0; feat < this->ui_n; feat++)
  501. {
  502. _beta[feat] += this->d_noise*_alpha[feat];
  503. }
  504. }
  505. void FastMinKernel::hik_kernel_sum(const NICE::VVector & _A,
  506. const NICE::VVector & _B,
  507. const NICE::SparseVector & _xstar,
  508. double & _beta,
  509. const ParameterizedFunction *_pf) const
  510. {
  511. // sparse version of hik_kernel_sum, no really significant changes,
  512. // we are just skipping zero elements
  513. _beta = 0.0;
  514. for (SparseVector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++)
  515. {
  516. uint dim = i->first;
  517. double fval = i->second;
  518. std::cerr << " FastMinKernel::hik_kernel_sum (sparse)-- dim : " << dim << std::endl;
  519. std::cerr << " FastMinKernel::hik_kernel_sum (sparse)-- fval : " << fval << std::endl;
  520. uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
  521. std::cerr << " FastMinKernel::hik_kernel_sum -- nrZeroIndices : " << nrZeroIndices << std::endl;
  522. if ( nrZeroIndices == this->ui_n ) {
  523. // all features are zero and let us ignore it completely
  524. continue;
  525. }
  526. uint position;
  527. //where is the example x^z_i located in
  528. //the sorted array? -> perform binary search, runtime O(log(n))
  529. // search using the original value
  530. this->X_sorted.findFirstLargerInDimension(dim, fval, position);
  531. bool posIsZero ( position == 0 );
  532. if ( !posIsZero )
  533. {
  534. position--;
  535. }
  536. std::cerr << " FastMinKernel::hik_kernel_sum -- position : " << position << std::endl;
  537. std::cerr << " FastMinKernel::hik_kernel_sum -- this->ui_n : " << this->ui_n << std::endl;
  538. //NOTE again - pay attention! This is only valid if all entries are NOT negative! - if not, ask wether the current feature is greater than zero. If so, subtract the nrZeroIndices, if not do not
  539. //sum_{l \in L_k} \alpha_l x^l_k
  540. double firstPart(0.0);
  541. //TODO in the "overnext" line there occurs the following error
  542. // Invalid read of size 8
  543. if ( !posIsZero && ((position-nrZeroIndices) < this->ui_n) )
  544. {
  545. std::cerr << " attempt to access A in " << position-nrZeroIndices;
  546. firstPart = (_A[dim][position-nrZeroIndices]);
  547. std::cerr << " with : " << firstPart << std::endl;
  548. }
  549. // sum_{u \in U_k} alpha_u
  550. // sum_{u \in U_k} alpha_u
  551. // => double secondPart( B(dim, n-1) - B(dim, position));
  552. //TODO in the next line there occurs the following error
  553. // Invalid read of size 8
  554. std::cerr << " attempt to access B in " << this->ui_n-1-nrZeroIndices;
  555. double secondPart( _B[dim][this->ui_n-1-nrZeroIndices]);
  556. std::cerr << " with : " << secondPart << std::endl;
  557. //TODO in the "overnext" line there occurs the following error
  558. // Invalid read of size 8
  559. if ( !posIsZero && (position >= nrZeroIndices) )
  560. {
  561. std::cerr << " attempt to access B in " << position-nrZeroIndices;
  562. secondPart-= _B[dim][position-nrZeroIndices];
  563. std::cerr << " with : " << _B[dim][position-nrZeroIndices] << std::endl;
  564. }
  565. if ( _pf != NULL )
  566. {
  567. fval = _pf->f ( dim, fval );
  568. }
  569. std::cerr << " result dim " << dim << " impact " << firstPart + secondPart* fval << " firstPart " << firstPart << " secondPart " << secondPart << " fval " << fval << std::endl;
  570. // but apply using the transformed one
  571. _beta += firstPart + secondPart* fval;
  572. }
  573. }
  574. void FastMinKernel::hik_kernel_sum(const NICE::VVector & _A,
  575. const NICE::VVector & _B,
  576. const NICE::Vector & _xstar,
  577. double & _beta,
  578. const ParameterizedFunction *_pf
  579. ) const
  580. {
  581. _beta = 0.0;
  582. uint dim ( 0 );
  583. for (NICE::Vector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++, dim++)
  584. {
  585. std::cerr << " FastMinKernel::hik_kernel_sum -- dim : " << dim << std::endl;
  586. double fval = *i;
  587. std::cerr << " FastMinKernel::hik_kernel_sum -- fval : " << fval << std::endl;
  588. uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
  589. std::cerr << " FastMinKernel::hik_kernel_sum -- nrZeroIndices : " << nrZeroIndices << std::endl;
  590. if ( nrZeroIndices == this->ui_n ) {
  591. // all features are zero and let us ignore it completely
  592. continue;
  593. }
  594. uint position;
  595. //where is the example x^z_i located in
  596. //the sorted array? -> perform binary search, runtime O(log(n))
  597. // search using the original value
  598. this->X_sorted.findFirstLargerInDimension(dim, fval, position);
  599. bool posIsZero ( position == 0 );
  600. if ( !posIsZero )
  601. {
  602. position--;
  603. }
  604. std::cerr << " FastMinKernel::hik_kernel_sum -- position : " << position << std::endl;
  605. std::cerr << " FastMinKernel::hik_kernel_sum -- this->ui_n : " << this->ui_n << std::endl;
  606. //NOTE again - pay attention! This is only valid if all entries are NOT negative! - if not, ask wether the current feature is greater than zero. If so, subtract the nrZeroIndices, if not do not
  607. //sum_{l \in L_k} \alpha_l x^l_k
  608. double firstPart(0.0);
  609. //TODO in the "overnext" line there occurs the following error
  610. // Invalid read of size 8
  611. if ( !posIsZero && ((position-nrZeroIndices) < this->ui_n) )
  612. {
  613. std::cerr << " attempt to access A in " << position-nrZeroIndices;
  614. firstPart = (_A[dim][position-nrZeroIndices]);
  615. std::cerr << " with : " << firstPart << std::endl;
  616. }
  617. // sum_{u \in U_k} alpha_u
  618. // sum_{u \in U_k} alpha_u
  619. // => double secondPart( B(dim, n-1) - B(dim, position));
  620. //TODO in the next line there occurs the following error
  621. // Invalid read of size 8
  622. std::cerr << " attempt to access B in " << this->ui_n-1-nrZeroIndices;
  623. double secondPart( _B[dim][this->ui_n-1-nrZeroIndices] );
  624. std::cerr << " with : " << secondPart << std::endl;
  625. //TODO in the "overnext" line there occurs the following error
  626. // Invalid read of size 8
  627. if ( !posIsZero && (position >= nrZeroIndices) )
  628. {
  629. std::cerr << " attempt to access B in " << position-nrZeroIndices;
  630. secondPart-= _B[dim][position-nrZeroIndices];
  631. std::cerr << " with : " << _B[dim][position-nrZeroIndices] << std::endl;
  632. }
  633. if ( _pf != NULL )
  634. {
  635. fval = _pf->f ( dim, fval );
  636. }
  637. std::cerr << " result dim " << dim << " impact " << firstPart + secondPart* fval << " firstPart " << firstPart << " secondPart " << secondPart << " fval " << fval << std::endl;
  638. // but apply using the transformed one
  639. _beta += firstPart + secondPart* fval;
  640. }
  641. }
  642. void FastMinKernel::hik_kernel_sum_fast(const double *_Tlookup,
  643. const Quantization & _q,
  644. const NICE::Vector & _xstar,
  645. double & _beta
  646. ) const
  647. {
  648. _beta = 0.0;
  649. if ( _xstar.size() != this->ui_d)
  650. {
  651. fthrow(Exception, "FastMinKernel::hik_kernel_sum_fast sizes of xstar and training data does not match!");
  652. return;
  653. }
  654. // runtime is O(d) if the quantizer is O(1)
  655. for ( uint dim = 0; dim < this->ui_d; dim++)
  656. {
  657. double v = _xstar[dim];
  658. uint qBin = _q.quantize(v);
  659. _beta += _Tlookup[dim*_q.size() + qBin];
  660. }
  661. }
  662. void FastMinKernel::hik_kernel_sum_fast(const double *_Tlookup,
  663. const Quantization & _q,
  664. const NICE::SparseVector & _xstar,
  665. double & _beta
  666. ) const
  667. {
  668. _beta = 0.0;
  669. // sparse version of hik_kernel_sum_fast, no really significant changes,
  670. // we are just skipping zero elements
  671. // for additional comments see the non-sparse version of hik_kernel_sum_fast
  672. // runtime is O(d) if the quantizer is O(1)
  673. for (SparseVector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++ )
  674. {
  675. uint dim = i->first;
  676. double v = i->second;
  677. uint qBin = _q.quantize(v);
  678. _beta += _Tlookup[dim*_q.size() + qBin];
  679. }
  680. }
  681. double *FastMinKernel::solveLin(const NICE::Vector & _y,
  682. NICE::Vector & _alpha,
  683. const Quantization & _q,
  684. const ParameterizedFunction *_pf,
  685. const bool & _useRandomSubsets,
  686. uint _maxIterations,
  687. const uint & _sizeOfRandomSubset,
  688. double _minDelta,
  689. bool _timeAnalysis
  690. ) const
  691. {
  692. uint sizeOfRandomSubset(_sizeOfRandomSubset);
  693. bool verboseMinimal ( false );
  694. // number of quantization bins
  695. uint hmax = _q.size();
  696. NICE::Vector diagonalElements(_y.size(),0.0);
  697. this->X_sorted.hikDiagonalElements(diagonalElements);
  698. diagonalElements += this->d_noise;
  699. NICE::Vector pseudoResidual (_y.size(),0.0);
  700. NICE::Vector delta_alpha (_y.size(),0.0);
  701. double alpha_old;
  702. double alpha_new;
  703. double x_i;
  704. // initialization
  705. if (_alpha.size() != _y.size())
  706. {
  707. _alpha.resize( _y.size() );
  708. }
  709. _alpha.set(0.0);
  710. double *Tlookup = new double [ hmax * this->ui_d ];
  711. if ( (hmax*this->ui_d) <= 0 )
  712. return Tlookup;
  713. memset(Tlookup, 0, sizeof(Tlookup[0])*hmax*this->ui_d);
  714. uint iter;
  715. Timer t;
  716. if ( _timeAnalysis )
  717. t.start();
  718. if (_useRandomSubsets)
  719. {
  720. std::vector<uint> indices( _y.size() );
  721. for (uint i = 0; i < _y.size(); i++)
  722. indices[i] = i;
  723. if (sizeOfRandomSubset <= 0)
  724. sizeOfRandomSubset = _y.size();
  725. for ( iter = 1; iter <= _maxIterations; iter++ )
  726. {
  727. NICE::Vector perm;
  728. randomPermutation( perm, indices, _sizeOfRandomSubset );
  729. if ( _timeAnalysis )
  730. {
  731. t.stop();
  732. Vector r;
  733. this->hik_kernel_multiply_fast(Tlookup, _q, _alpha, r);
  734. r = r - _y;
  735. double res = r.normL2();
  736. double resMax = r.normInf();
  737. cerr << "SimpleGradientDescent: TIME " << t.getSum() << " " << res << " " << resMax << endl;
  738. t.start();
  739. }
  740. for ( uint i = 0; i < sizeOfRandomSubset; i++)
  741. {
  742. pseudoResidual(perm[i]) = -_y(perm[i]) + (this->d_noise * _alpha(perm[i]));
  743. for (uint j = 0; j < this->ui_d; j++)
  744. {
  745. x_i = this->X_sorted(j,perm[i]);
  746. pseudoResidual(perm[i]) += Tlookup[j*hmax + _q.quantize(x_i)];
  747. }
  748. //NOTE: this threshhold could also be a parameter of the function call
  749. if ( fabs(pseudoResidual(perm[i])) > 1e-7 )
  750. {
  751. alpha_old = _alpha(perm[i]);
  752. alpha_new = alpha_old - (pseudoResidual(perm[i])/diagonalElements(perm[i]));
  753. _alpha(perm[i]) = alpha_new;
  754. delta_alpha(perm[i]) = alpha_old-alpha_new;
  755. this->hikUpdateLookupTable(Tlookup, alpha_new, alpha_old, perm[i], _q, _pf ); // works correctly
  756. } else
  757. {
  758. delta_alpha(perm[i]) = 0.0;
  759. }
  760. }
  761. // after this only residual(i) is the valid residual... we should
  762. // really update the whole vector somehow
  763. double delta = delta_alpha.normL2();
  764. if ( this->b_verbose ) {
  765. cerr << "FastMinKernel::solveLin: iteration " << iter << " / " << _maxIterations << endl;
  766. cerr << "FastMinKernel::solveLin: delta = " << delta << endl;
  767. cerr << "FastMinKernel::solveLin: pseudo residual = " << pseudoResidual.scalarProduct(pseudoResidual) << endl;
  768. }
  769. if ( delta < _minDelta )
  770. {
  771. if ( this->b_verbose )
  772. cerr << "FastMinKernel::solveLin: small delta" << endl;
  773. break;
  774. }
  775. }
  776. }
  777. else //don't use random subsets
  778. {
  779. for ( iter = 1; iter <= _maxIterations; iter++ )
  780. {
  781. for ( uint i = 0; i < _y.size(); i++ )
  782. {
  783. pseudoResidual(i) = -_y(i) + (this->d_noise* _alpha(i));
  784. for (uint j = 0; j < this->ui_d; j++)
  785. {
  786. x_i = this->X_sorted(j,i);
  787. pseudoResidual(i) += Tlookup[j*hmax + _q.quantize(x_i)];
  788. }
  789. //NOTE: this threshhold could also be a parameter of the function call
  790. if ( fabs(pseudoResidual(i)) > 1e-7 )
  791. {
  792. alpha_old = _alpha(i);
  793. alpha_new = alpha_old - (pseudoResidual(i)/diagonalElements(i));
  794. _alpha(i) = alpha_new;
  795. delta_alpha(i) = alpha_old-alpha_new;
  796. this->hikUpdateLookupTable(Tlookup, alpha_new, alpha_old, i, _q, _pf ); // works correctly
  797. } else
  798. {
  799. delta_alpha(i) = 0.0;
  800. }
  801. }
  802. double delta = delta_alpha.normL2();
  803. if ( this->b_verbose ) {
  804. cerr << "FastMinKernel::solveLin: iteration " << iter << " / " << _maxIterations << endl;
  805. cerr << "FastMinKernel::solveLin: delta = " << delta << endl;
  806. cerr << "FastMinKernel::solveLin: pseudo residual = " << pseudoResidual.scalarProduct(pseudoResidual) << endl;
  807. }
  808. if ( delta < _minDelta )
  809. {
  810. if ( this->b_verbose )
  811. cerr << "FastMinKernel::solveLin: small delta" << endl;
  812. break;
  813. }
  814. }
  815. }
  816. if (verboseMinimal)
  817. std::cerr << "FastMinKernel::solveLin -- needed " << iter << " iterations" << std::endl;
  818. return Tlookup;
  819. }
  820. void FastMinKernel::randomPermutation(NICE::Vector & _permutation,
  821. const std::vector<uint> & _oldIndices,
  822. const uint & _newSize
  823. ) const
  824. {
  825. std::vector<uint> indices(_oldIndices);
  826. const uint oldSize = _oldIndices.size();
  827. uint resultingSize (std::min( oldSize, _newSize) );
  828. _permutation.resize(resultingSize);
  829. for ( uint i = 0; i < resultingSize; i++)
  830. {
  831. uint newIndex(rand() % indices.size());
  832. _permutation[i] = indices[newIndex ];
  833. indices.erase(indices.begin() + newIndex);
  834. }
  835. }
  836. double FastMinKernel::getFrobNormApprox()
  837. {
  838. double frobNormApprox(0.0);
  839. switch (this->approxScheme)
  840. {
  841. case MEDIAN:
  842. {
  843. //\| K \|_F^1 ~ (n/2)^2 \left( \sum_k \median_k \right)^2
  844. //motivation: estimate half of the values in dim k to zero and half of them to the median (-> lower bound expectation)
  845. for ( uint i = 0; i < this->ui_d; i++ )
  846. {
  847. double median = this->X_sorted.getFeatureValues(i).getMedian();
  848. frobNormApprox += median;
  849. }
  850. frobNormApprox = fabs(frobNormApprox) * this->ui_n/2.0;
  851. break;
  852. }
  853. case EXPECTATION:
  854. {
  855. std::cerr << "EXPECTATION" << std::endl;
  856. //\| K \|_F^1^2 ~ \sum K_{ii}^2 + (n^2 - n) \left( \frac{1}{3} \sum_k \left( 2 a_k + b_k \right) \right)
  857. // with a_k = minimal value in dim k and b_k maximal value
  858. //first term
  859. NICE::Vector diagEl;
  860. X_sorted.hikDiagonalElements(diagEl);
  861. frobNormApprox += diagEl.normL2();
  862. //second term
  863. double secondTerm(0.0);
  864. for ( uint i = 0; i < this->ui_d; i++ )
  865. {
  866. double minInDim;
  867. minInDim = this->X_sorted.getFeatureValues(i).getMin();
  868. double maxInDim;
  869. maxInDim = this->X_sorted.getFeatureValues(i).getMax();
  870. std::cerr << "min: " << minInDim << " max: " << maxInDim << std::endl;
  871. secondTerm += 2.0*minInDim + maxInDim;
  872. }
  873. secondTerm /= 3.0;
  874. secondTerm = pow(secondTerm, 2);
  875. secondTerm *= (this->ui_n * ( this->ui_n - 1 ));
  876. frobNormApprox += secondTerm;
  877. frobNormApprox = sqrt(frobNormApprox);
  878. break;
  879. }
  880. default:
  881. { //do nothing, approximate with zero :)
  882. break;
  883. }
  884. }
  885. return frobNormApprox;
  886. }
  887. void FastMinKernel::setApproximationScheme(const int & _approxScheme)
  888. {
  889. switch(_approxScheme)
  890. {
  891. case 0:
  892. {
  893. this->approxScheme = MEDIAN;
  894. break;
  895. }
  896. case 1:
  897. {
  898. this->approxScheme = EXPECTATION;
  899. break;
  900. }
  901. default:
  902. {
  903. this->approxScheme = MEDIAN;
  904. break;
  905. }
  906. }
  907. }
  908. void FastMinKernel::hikPrepareKVNApproximation(NICE::VVector & _A) const
  909. {
  910. _A.resize( this->ui_d );
  911. // efficient calculation of |k_*|^2 = k_*^T * k_*
  912. // ---------------------------------
  913. //
  914. // \sum_{i=1}^{n} \left( \sum_{d=1}^{D} \min (x_d^*, x_d^i) \right)^2
  915. // <=\sum_{i=1}^{n} \sum_{d=1}^{D} \left( \min (x_d^*, x_d^i) \right)^2
  916. // = \sum_{d=1}^{D} \sum_{i=1}^{n} \left( \min (x_d^*, x_d^i) \right)^2
  917. // = \sum_{d=1}^{D} \left( \sum_{i:x_d^i < x_*^d} (x_d^i)^2 + \sum_{j: x_d^* \leq x_d^j} (x_d^*)^2 \right)
  918. //
  919. // again let us define l_d = { i | x_d^i <= x_d^* }
  920. // and u_d = { i | x_d^i > x_d^* }, this leads to
  921. //
  922. // = \sum_{d=1}^{D} ( \sum_{l \in l_d} (x_d^l)^2 + \sum_{u \in u_d} (x_d^*)^2
  923. // = \sum_{d=1}^{D} ( \sum_{l \in l_d} (x_d^l)^2 + (x_d^*)^2 \sum_{u \in u_d} 1
  924. //
  925. // We also define
  926. // l_d^j = { i | x_d^i <= x_d^j } and
  927. // u_d^j = { i | x_d^i > x_d^j }
  928. //
  929. // We now need the partial sums
  930. //
  931. // (Definition 1)
  932. // a_{d,j} = \sum_{l \in l_d^j} (x_d^l)^2
  933. // according to increasing values of x_d^l
  934. //
  935. // We end at
  936. // |k_*|^2 <= \sum_{d=1}^{D} \left( a_{d,r_d} + (x_d^*)^2 * |u_d^{r_d}| \right)
  937. // with r_d being the index of the last example in the ordered sequence for dimension d, that is not larger than x_d^*
  938. // we only need as many entries as we have nonZero entries in our features for the corresponding dimensions
  939. for ( uint i = 0; i < this->ui_d; i++ )
  940. {
  941. uint numNonZero = this->X_sorted.getNumberOfNonZeroElementsPerDimension(i);
  942. _A[i].resize( numNonZero );
  943. }
  944. // for more information see hik_prepare_alpha_multiplications
  945. for (uint dim = 0; dim < this->ui_d; dim++)
  946. {
  947. double squared_sum(0.0);
  948. uint cntNonzeroFeat(0);
  949. const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
  950. // loop through all elements in sorted order
  951. for ( SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin(); i != nonzeroElements.end(); i++ )
  952. {
  953. const SortedVectorSparse<double>::dataelement & de = i->second;
  954. // de: first - index, second - transformed feature
  955. double elem( de.second );
  956. squared_sum += pow( elem, 2 );
  957. _A[dim][cntNonzeroFeat] = squared_sum;
  958. cntNonzeroFeat++;
  959. }
  960. }
  961. }
  962. double * FastMinKernel::hikPrepareKVNApproximationFast(NICE::VVector & _A,
  963. const Quantization & _q,
  964. const ParameterizedFunction *_pf ) const
  965. {
  966. //NOTE keep in mind: for doing this, we already have precomputed A using hikPrepareSquaredKernelVector!
  967. // number of quantization bins
  968. uint hmax = _q.size();
  969. // store (transformed) prototypes
  970. double *prototypes = new double [ hmax ];
  971. for ( uint i = 0 ; i < hmax ; i++ )
  972. if ( _pf != NULL ) {
  973. // FIXME: the transformed prototypes could change from dimension to another dimension
  974. // We skip this flexibility ...but it should be changed in the future
  975. prototypes[i] = _pf->f ( 1, _q.getPrototype(i) );
  976. } else {
  977. prototypes[i] = _q.getPrototype(i);
  978. }
  979. // creating the lookup table as pure C, which might be beneficial
  980. // for fast evaluation
  981. double *Tlookup = new double [ hmax * this->ui_d ];
  982. // loop through all dimensions
  983. for (uint dim = 0; dim < this->ui_d; dim++)
  984. {
  985. uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
  986. if ( nrZeroIndices == this->ui_n )
  987. continue;
  988. const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
  989. SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin();
  990. SortedVectorSparse<double>::const_elementpointer iPredecessor = nonzeroElements.begin();
  991. // index of the element, which is always bigger than the current value fval
  992. uint index = 0;
  993. // we use the quantization of the original features! the transformed feature were
  994. // already used to calculate A and B, this of course assumes monotonic functions!!!
  995. uint qBin = _q.quantize ( i->first );
  996. // the next loop is linear in max(hmax, n)
  997. // REMARK: this could be changed to hmax*log(n), when
  998. // we use binary search
  999. //FIXME we should do this!
  1000. for (uint j = 0; j < hmax; j++)
  1001. {
  1002. double fval = prototypes[j];
  1003. double t;
  1004. if ( (index == 0) && (j < qBin) ) {
  1005. // current element is smaller than everything else
  1006. // resulting value = fval * sum_l=1^n 1
  1007. t = pow( fval, 2 ) * (this->ui_n-nrZeroIndices-index);
  1008. } else {
  1009. // move to next example, if necessary
  1010. while ( (j >= qBin) && ( index < (this->ui_n-nrZeroIndices)) )
  1011. {
  1012. index++;
  1013. iPredecessor = i;
  1014. i++;
  1015. if ( i->first != iPredecessor->first )
  1016. qBin = _q.quantize ( i->first );
  1017. }
  1018. // compute current element in the lookup table and keep in mind that
  1019. // index is the next element and not the previous one
  1020. //NOTE pay attention: this is only valid if all entries are positiv! - if not, ask wether the current feature is greater than zero. If so, subtract the nrZeroIndices, if not do not
  1021. if ( (j >= qBin) && ( index==(this->ui_n-1-nrZeroIndices) ) ) {
  1022. // the current element (fval) is equal or bigger to the element indexed by index
  1023. // the second term vanishes, which is logical, since all elements are smaller than j!
  1024. t = _A[dim][index];
  1025. } else {
  1026. // standard case
  1027. t = _A[dim][index-1] + pow( fval, 2 ) * (this->ui_n-nrZeroIndices-(index) );
  1028. // A[dim][index-1] + fval * (n-nrZeroIndices-(index) );//fval*fval * (n-nrZeroIndices-(index-1) );
  1029. }
  1030. }
  1031. Tlookup[ dim*hmax + j ] = t;
  1032. }
  1033. }
  1034. delete [] prototypes;
  1035. return Tlookup;
  1036. }
  1037. double* FastMinKernel::hikPrepareLookupTableForKVNApproximation(const Quantization & _q,
  1038. const ParameterizedFunction *_pf
  1039. ) const
  1040. {
  1041. // number of quantization bins
  1042. uint hmax = _q.size();
  1043. // store (transformed) prototypes
  1044. double *prototypes = new double [ hmax ];
  1045. for ( uint i = 0 ; i < hmax ; i++ )
  1046. if ( _pf != NULL ) {
  1047. // FIXME: the transformed prototypes could change from dimension to another dimension
  1048. // We skip this flexibility ...but it should be changed in the future
  1049. prototypes[i] = _pf->f ( 1, _q.getPrototype(i) );
  1050. } else {
  1051. prototypes[i] = _q.getPrototype(i);
  1052. }
  1053. // creating the lookup table as pure C, which might be beneficial
  1054. // for fast evaluation
  1055. double *Tlookup = new double [ hmax * this->ui_d ];
  1056. // loop through all dimensions
  1057. for (uint dim = 0; dim < this->ui_d; dim++)
  1058. {
  1059. uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
  1060. if ( nrZeroIndices == this->ui_n )
  1061. continue;
  1062. const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
  1063. SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin();
  1064. SortedVectorSparse<double>::const_elementpointer iPredecessor = nonzeroElements.begin();
  1065. // index of the element, which is always bigger than the current value fval
  1066. uint index = 0;
  1067. // we use the quantization of the original features! Nevetheless, the resulting lookupTable is computed using the transformed ones
  1068. uint qBin = _q.quantize ( i->first );
  1069. double sum(0.0);
  1070. for (uint j = 0; j < hmax; j++)
  1071. {
  1072. double fval = prototypes[j];
  1073. double t;
  1074. if ( (index == 0) && (j < qBin) ) {
  1075. // current element is smaller than everything else
  1076. // resulting value = fval * sum_l=1^n 1
  1077. t = pow( fval, 2 ) * (this->ui_n-nrZeroIndices-index);
  1078. } else {
  1079. // move to next example, if necessary
  1080. while ( (j >= qBin) && ( index < (this->ui_n-nrZeroIndices)) )
  1081. {
  1082. sum += pow( i->second.second, 2 ); //i->dataElement.transformedFeatureValue
  1083. index++;
  1084. iPredecessor = i;
  1085. i++;
  1086. if ( i->first != iPredecessor->first )
  1087. qBin = _q.quantize ( i->first );
  1088. }
  1089. // compute current element in the lookup table and keep in mind that
  1090. // index is the next element and not the previous one
  1091. //NOTE pay attention: this is only valid if we all entries are positiv! - if not, ask wether the current feature is greater than zero. If so, subtract the nrZeroIndices, if not do not
  1092. if ( (j >= qBin) && ( index==(this->ui_n-1-nrZeroIndices) ) ) {
  1093. // the current element (fval) is equal or bigger to the element indexed by index
  1094. // the second term vanishes, which is logical, since all elements are smaller than j!
  1095. t = sum;
  1096. } else {
  1097. // standard case
  1098. t = sum + pow( fval, 2 ) * (this->ui_n-nrZeroIndices-(index) );
  1099. }
  1100. }
  1101. Tlookup[ dim*hmax + j ] = t;
  1102. }
  1103. }
  1104. delete [] prototypes;
  1105. return Tlookup;
  1106. }
  1107. //////////////////////////////////////////
  1108. // variance computation: sparse inputs
  1109. //////////////////////////////////////////
  1110. void FastMinKernel::hikComputeKVNApproximation(const NICE::VVector & _A,
  1111. const NICE::SparseVector & _xstar,
  1112. double & _norm,
  1113. const ParameterizedFunction *_pf )
  1114. {
  1115. _norm = 0.0;
  1116. for (SparseVector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++)
  1117. {
  1118. uint dim = i->first;
  1119. double fval = i->second;
  1120. uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
  1121. if ( nrZeroIndices == this->ui_n ) {
  1122. // all features are zero so let us ignore them completely
  1123. continue;
  1124. }
  1125. uint position;
  1126. //where is the example x^z_i located in
  1127. //the sorted array? -> perform binary search, runtime O(log(n))
  1128. // search using the original value
  1129. this->X_sorted.findFirstLargerInDimension(dim, fval, position);
  1130. bool posIsZero ( position == 0 );
  1131. if ( !posIsZero )
  1132. {
  1133. position--;
  1134. }
  1135. //NOTE again - pay attention! This is only valid if all entries are NOT negative! - if not, ask wether the current feature is greater than zero. If so, subtract the nrZeroIndices, if not do not
  1136. double firstPart(0.0);
  1137. //TODO in the "overnext" line there occurs the following error
  1138. // Invalid read of size 8
  1139. if ( !posIsZero && ((position-nrZeroIndices) < this->ui_n) )
  1140. firstPart = (_A[dim][position-nrZeroIndices]);
  1141. if ( _pf != NULL )
  1142. fval = _pf->f ( dim, fval );
  1143. fval = fval * fval;
  1144. double secondPart( 0.0);
  1145. if ( !posIsZero )
  1146. secondPart = fval * (this->ui_n-nrZeroIndices-(position+1));
  1147. else //if x_d^* is smaller than every non-zero training example
  1148. secondPart = fval * (this->ui_n-nrZeroIndices);
  1149. // but apply using the transformed one
  1150. _norm += firstPart + secondPart;
  1151. }
  1152. }
  1153. void FastMinKernel::hikComputeKVNApproximationFast(const double *_Tlookup,
  1154. const Quantization & _q,
  1155. const NICE::SparseVector & _xstar,
  1156. double & _norm
  1157. ) const
  1158. {
  1159. _norm = 0.0;
  1160. // runtime is O(d) if the quantizer is O(1)
  1161. for (SparseVector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++ )
  1162. {
  1163. uint dim = i->first;
  1164. double v = i->second;
  1165. // we do not need a parameterized function here, since the quantizer works on the original feature values.
  1166. // nonetheless, the lookup table was created using the parameterized function
  1167. uint qBin = _q.quantize(v);
  1168. _norm += _Tlookup[dim*_q.size() + qBin];
  1169. }
  1170. }
  1171. void FastMinKernel::hikComputeKernelVector ( const NICE::SparseVector& _xstar,
  1172. NICE::Vector & _kstar
  1173. ) const
  1174. {
  1175. //init
  1176. _kstar.resize( this->ui_n );
  1177. _kstar.set(0.0);
  1178. //let's start :)
  1179. for (SparseVector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++)
  1180. {
  1181. uint dim = i->first;
  1182. double fval = i->second;
  1183. uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
  1184. if ( nrZeroIndices == this->ui_n ) {
  1185. // all features are zero so let us ignore them completely
  1186. continue;
  1187. }
  1188. uint position;
  1189. //where is the example x^z_i located in
  1190. //the sorted array? -> perform binary search, runtime O(log(n))
  1191. // search using the original value
  1192. this->X_sorted.findFirstLargerInDimension(dim, fval, position);
  1193. //position--;
  1194. //get the non-zero elements for this dimension
  1195. const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
  1196. //run over the non-zero elements and add the corresponding entries to our kernel vector
  1197. uint count(nrZeroIndices);
  1198. for ( SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin(); i != nonzeroElements.end(); i++, count++ )
  1199. {
  1200. uint origIndex(i->second.first); //orig index (i->second.second would be the transformed feature value)
  1201. if (count < position)
  1202. _kstar[origIndex] += i->first; //orig feature value
  1203. else
  1204. _kstar[origIndex] += fval;
  1205. }
  1206. }
  1207. }
  1208. //////////////////////////////////////////
  1209. // variance computation: non-sparse inputs
  1210. //////////////////////////////////////////
  1211. void FastMinKernel::hikComputeKVNApproximation(const NICE::VVector & _A,
  1212. const NICE::Vector & _xstar,
  1213. double & _norm,
  1214. const ParameterizedFunction *_pf )
  1215. {
  1216. _norm = 0.0;
  1217. uint dim ( 0 );
  1218. for (Vector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++, dim++)
  1219. {
  1220. double fval = *i;
  1221. uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
  1222. if ( nrZeroIndices == this->ui_n ) {
  1223. // all features are zero so let us ignore them completely
  1224. continue;
  1225. }
  1226. uint position;
  1227. //where is the example x^z_i located in
  1228. //the sorted array? -> perform binary search, runtime O(log(n))
  1229. // search using the original value
  1230. this->X_sorted.findFirstLargerInDimension(dim, fval, position);
  1231. bool posIsZero ( position == 0 );
  1232. if ( !posIsZero )
  1233. {
  1234. position--;
  1235. }
  1236. //NOTE again - pay attention! This is only valid if all entries are NOT negative! - if not, ask wether the current feature is greater than zero. If so, subtract the nrZeroIndices, if not do not
  1237. double firstPart(0.0);
  1238. //TODO in the "overnext" line there occurs the following error
  1239. // Invalid read of size 8
  1240. if ( !posIsZero && ((position-nrZeroIndices) < this->ui_n) )
  1241. firstPart = (_A[dim][position-nrZeroIndices]);
  1242. double secondPart( 0.0);
  1243. if ( _pf != NULL )
  1244. fval = _pf->f ( dim, fval );
  1245. fval = fval * fval;
  1246. if ( !posIsZero )
  1247. secondPart = fval * (this->ui_n-nrZeroIndices-(position+1));
  1248. else //if x_d^* is smaller than every non-zero training example
  1249. secondPart = fval * (this->ui_n-nrZeroIndices);
  1250. // but apply using the transformed one
  1251. _norm += firstPart + secondPart;
  1252. }
  1253. }
  1254. void FastMinKernel::hikComputeKVNApproximationFast(const double *_Tlookup,
  1255. const Quantization & _q,
  1256. const NICE::Vector & _xstar,
  1257. double & _norm
  1258. ) const
  1259. {
  1260. _norm = 0.0;
  1261. // runtime is O(d) if the quantizer is O(1)
  1262. uint dim ( 0 );
  1263. for (Vector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++, dim++ )
  1264. {
  1265. double v = *i;
  1266. // we do not need a parameterized function here, since the quantizer works on the original feature values.
  1267. // nonetheless, the lookup table was created using the parameterized function
  1268. uint qBin = _q.quantize(v);
  1269. _norm += _Tlookup[dim*_q.size() + qBin];
  1270. }
  1271. }
  1272. void FastMinKernel::hikComputeKernelVector( const NICE::Vector & _xstar,
  1273. NICE::Vector & _kstar) const
  1274. {
  1275. //init
  1276. _kstar.resize(this->ui_n);
  1277. _kstar.set(0.0);
  1278. std::cerr << "test example: " << _xstar << std::endl;
  1279. std::cerr << " init _kstar: " << _kstar << std::endl;
  1280. //let's start :)
  1281. uint dim ( 0 );
  1282. for (NICE::Vector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++, dim++)
  1283. {
  1284. double fval = *i;
  1285. std::cerr << " dim: " << dim << " fval: " << fval << std::endl;
  1286. uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
  1287. if ( nrZeroIndices == this->ui_n ) {
  1288. // all features are zero so let us ignore them completely
  1289. continue;
  1290. }
  1291. uint position;
  1292. //where is the example x^z_i located in
  1293. //the sorted array? -> perform binary search, runtime O(log(n))
  1294. // search using the original value
  1295. this->X_sorted.findFirstLargerInDimension(dim, fval, position);
  1296. //position--;
  1297. std::cerr << " dim: " << dim << " fval: " << fval << std::endl;
  1298. //get the non-zero elements for this dimension
  1299. const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
  1300. //run over the non-zero elements and add the corresponding entries to our kernel vector
  1301. uint count(nrZeroIndices);
  1302. for ( SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin(); i != nonzeroElements.end(); i++, count++ )
  1303. {
  1304. uint origIndex(i->second.first); //orig index (i->second.second would be the transformed feature value)
  1305. if (count < position)
  1306. _kstar[origIndex] += i->first; //orig feature value
  1307. else
  1308. _kstar[origIndex] += fval;
  1309. }
  1310. }
  1311. }
  1312. ///////////////////// INTERFACE PERSISTENT /////////////////////
  1313. // interface specific methods for store and restore
  1314. ///////////////////// INTERFACE PERSISTENT /////////////////////
  1315. void FastMinKernel::restore ( std::istream & _is,
  1316. int _format )
  1317. {
  1318. bool b_restoreVerbose ( false );
  1319. if ( _is.good() )
  1320. {
  1321. if ( b_restoreVerbose )
  1322. std::cerr << " restore FastMinKernel" << std::endl;
  1323. std::string tmp;
  1324. _is >> tmp; //class name
  1325. if ( ! this->isStartTag( tmp, "FastMinKernel" ) )
  1326. {
  1327. std::cerr << " WARNING - attempt to restore FastMinKernel, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  1328. throw;
  1329. }
  1330. _is.precision (numeric_limits<double>::digits10 + 1);
  1331. bool b_endOfBlock ( false ) ;
  1332. while ( !b_endOfBlock )
  1333. {
  1334. _is >> tmp; // start of block
  1335. if ( this->isEndTag( tmp, "FastMinKernel" ) )
  1336. {
  1337. b_endOfBlock = true;
  1338. continue;
  1339. }
  1340. tmp = this->removeStartTag ( tmp );
  1341. if ( b_restoreVerbose )
  1342. std::cerr << " currently restore section " << tmp << " in FastMinKernel" << std::endl;
  1343. if ( tmp.compare("ui_n") == 0 )
  1344. {
  1345. _is >> this->ui_n;
  1346. _is >> tmp; // end of block
  1347. tmp = this->removeEndTag ( tmp );
  1348. }
  1349. else if ( tmp.compare("ui_d") == 0 )
  1350. {
  1351. _is >> this->ui_d;
  1352. _is >> tmp; // end of block
  1353. tmp = this->removeEndTag ( tmp );
  1354. }
  1355. else if ( tmp.compare("d_noise") == 0 )
  1356. {
  1357. _is >> this->d_noise;
  1358. _is >> tmp; // end of block
  1359. tmp = this->removeEndTag ( tmp );
  1360. }
  1361. else if ( tmp.compare("approxScheme") == 0 )
  1362. {
  1363. int approxSchemeInt;
  1364. _is >> approxSchemeInt;
  1365. setApproximationScheme(approxSchemeInt);
  1366. _is >> tmp; // end of block
  1367. tmp = this->removeEndTag ( tmp );
  1368. }
  1369. else if ( tmp.compare("X_sorted") == 0 )
  1370. {
  1371. this->X_sorted.restore(_is,_format);
  1372. _is >> tmp; // end of block
  1373. tmp = this->removeEndTag ( tmp );
  1374. }
  1375. else
  1376. {
  1377. std::cerr << "WARNING -- unexpected FastMinKernel object -- " << tmp << " -- for restoration... aborting" << std::endl;
  1378. throw;
  1379. }
  1380. }
  1381. }
  1382. else
  1383. {
  1384. std::cerr << "FastMinKernel::restore -- InStream not initialized - restoring not possible!" << std::endl;
  1385. }
  1386. }
  1387. void FastMinKernel::store ( std::ostream & _os,
  1388. int _format
  1389. ) const
  1390. {
  1391. if (_os.good())
  1392. {
  1393. // show starting point
  1394. _os << this->createStartTag( "FastMinKernel" ) << std::endl;
  1395. _os.precision (numeric_limits<double>::digits10 + 1);
  1396. _os << this->createStartTag( "ui_n" ) << std::endl;
  1397. _os << this->ui_n << std::endl;
  1398. _os << this->createEndTag( "ui_n" ) << std::endl;
  1399. _os << this->createStartTag( "ui_d" ) << std::endl;
  1400. _os << this->ui_d << std::endl;
  1401. _os << this->createEndTag( "ui_d" ) << std::endl;
  1402. _os << this->createStartTag( "d_noise" ) << std::endl;
  1403. _os << this->d_noise << std::endl;
  1404. _os << this->createEndTag( "d_noise" ) << std::endl;
  1405. _os << this->createStartTag( "approxScheme" ) << std::endl;
  1406. _os << this->approxScheme << std::endl;
  1407. _os << this->createEndTag( "approxScheme" ) << std::endl;
  1408. _os << this->createStartTag( "X_sorted" ) << std::endl;
  1409. //store the underlying data
  1410. this->X_sorted.store(_os, _format);
  1411. _os << this->createEndTag( "X_sorted" ) << std::endl;
  1412. // done
  1413. _os << this->createEndTag( "FastMinKernel" ) << std::endl;
  1414. }
  1415. else
  1416. {
  1417. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  1418. }
  1419. }
  1420. void FastMinKernel::clear ()
  1421. {
  1422. std::cerr << "FastMinKernel clear-function called" << std::endl;
  1423. }
  1424. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  1425. // interface specific methods for incremental extensions
  1426. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  1427. void FastMinKernel::addExample( const NICE::SparseVector * _example,
  1428. const double & _label,
  1429. const bool & _performOptimizationAfterIncrement
  1430. )
  1431. {
  1432. // no parameterized function was given - use default
  1433. this->addExample ( _example );
  1434. }
  1435. void FastMinKernel::addMultipleExamples( const std::vector< const NICE::SparseVector * > & _newExamples,
  1436. const NICE::Vector & _newLabels,
  1437. const bool & _performOptimizationAfterIncrement
  1438. )
  1439. {
  1440. // no parameterized function was given - use default
  1441. this->addMultipleExamples ( _newExamples );
  1442. }
  1443. void FastMinKernel::addExample( const NICE::SparseVector * _example,
  1444. const NICE::ParameterizedFunction *_pf
  1445. )
  1446. {
  1447. this->X_sorted.add_feature( *_example, _pf );
  1448. this->ui_n++;
  1449. }
  1450. void FastMinKernel::addMultipleExamples( const std::vector< const NICE::SparseVector * > & _newExamples,
  1451. const NICE::ParameterizedFunction *_pf
  1452. )
  1453. {
  1454. for ( std::vector< const NICE::SparseVector * >::const_iterator exIt = _newExamples.begin();
  1455. exIt != _newExamples.end();
  1456. exIt++ )
  1457. {
  1458. this->X_sorted.add_feature( **exIt, _pf );
  1459. this->ui_n++;
  1460. }
  1461. }