FastMinKernel.cpp 60 KB

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