FastMinKernel.cpp 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  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. ) : 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->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. //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
  648. //sum_{l \in L_k} \alpha_l x^l_k
  649. double firstPart(0.0);
  650. //TODO in the "overnext" line there occurs the following error
  651. // Invalid read of size 8
  652. if ( !posIsZero && ((position-nrZeroIndices) < this->ui_n) )
  653. {
  654. firstPart = (_A[dim][position-nrZeroIndices]);
  655. }
  656. // sum_{u \in U_k} alpha_u
  657. // sum_{u \in U_k} alpha_u
  658. // => double secondPart( B(dim, n-1) - B(dim, position));
  659. //TODO in the next line there occurs the following error
  660. // Invalid read of size 8
  661. double secondPart( _B[dim][this->ui_n-1-nrZeroIndices] );
  662. //TODO in the "overnext" line there occurs the following error
  663. // Invalid read of size 8
  664. if ( !posIsZero && (position >= nrZeroIndices) )
  665. {
  666. secondPart-= _B[dim][position-nrZeroIndices];
  667. }
  668. if ( _pf != NULL )
  669. {
  670. fval = _pf->f ( dim, fval );
  671. }
  672. // but apply using the transformed one
  673. _beta += firstPart + secondPart* fval;
  674. }
  675. }
  676. void FastMinKernel::hik_kernel_sum_fast(const double *_Tlookup,
  677. const Quantization * _q,
  678. const NICE::Vector & _xstar,
  679. double & _beta
  680. ) const
  681. {
  682. _beta = 0.0;
  683. if ( _xstar.size() != this->ui_d)
  684. {
  685. fthrow(Exception, "FastMinKernel::hik_kernel_sum_fast sizes of xstar and training data does not match!");
  686. return;
  687. }
  688. // runtime is O(d) if the quantizer is O(1)
  689. for ( uint dim = 0; dim < this->ui_d; dim++)
  690. {
  691. double v = _xstar[dim];
  692. uint qBin = _q->quantize( v, dim );
  693. _beta += _Tlookup[dim*_q->getNumberOfBins() + qBin];
  694. }
  695. }
  696. void FastMinKernel::hik_kernel_sum_fast(const double *_Tlookup,
  697. const Quantization * _q,
  698. const NICE::SparseVector & _xstar,
  699. double & _beta
  700. ) const
  701. {
  702. _beta = 0.0;
  703. // sparse version of hik_kernel_sum_fast, no really significant changes,
  704. // we are just skipping zero elements
  705. // for additional comments see the non-sparse version of hik_kernel_sum_fast
  706. // runtime is O(d) if the quantizer is O(1)
  707. for (SparseVector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++ )
  708. {
  709. uint dim = i->first;
  710. double v = i->second;
  711. uint qBin = _q->quantize( v, dim );
  712. _beta += _Tlookup[dim*_q->getNumberOfBins() + qBin];
  713. }
  714. }
  715. double *FastMinKernel::solveLin(const NICE::Vector & _y,
  716. NICE::Vector & _alpha,
  717. const Quantization * _q,
  718. const ParameterizedFunction *_pf,
  719. const bool & _useRandomSubsets,
  720. uint _maxIterations,
  721. const uint & _sizeOfRandomSubset,
  722. double _minDelta,
  723. bool _timeAnalysis
  724. ) const
  725. {
  726. // note: this is the optimization done in Wu10_AFD and a
  727. // random version of it. In normal cases, IKM* should be
  728. // used together with your iterative solver of choice
  729. //
  730. uint sizeOfRandomSubset(_sizeOfRandomSubset);
  731. bool verboseMinimal ( false );
  732. // number of quantization bins
  733. uint hmax = _q->getNumberOfBins();
  734. NICE::Vector diagonalElements(_y.size(),0.0);
  735. this->X_sorted.hikDiagonalElements(diagonalElements);
  736. diagonalElements += this->d_noise;
  737. NICE::Vector pseudoResidual (_y.size(),0.0);
  738. NICE::Vector delta_alpha (_y.size(),0.0);
  739. double alpha_old;
  740. double alpha_new;
  741. double x_i;
  742. // initialization of the alpha vector
  743. if (_alpha.size() != _y.size())
  744. {
  745. _alpha.resize( _y.size() );
  746. }
  747. _alpha.set(0.0);
  748. // initialize the lookup table
  749. double *Tlookup = new double [ hmax * this->ui_d ];
  750. if ( (hmax*this->ui_d) <= 0 )
  751. return Tlookup;
  752. memset(Tlookup, 0, sizeof(Tlookup[0])*hmax*this->ui_d);
  753. uint iter;
  754. Timer t;
  755. if ( _timeAnalysis )
  756. t.start();
  757. if (_useRandomSubsets)
  758. {
  759. // FIXME: this code looks bogus, since we only iterate over a random
  760. // permutation of the training examples (several random subsets), without
  761. // during anything particular between batches
  762. std::vector<uint> indices( _y.size() );
  763. for (uint i = 0; i < _y.size(); i++)
  764. indices[i] = i;
  765. if (sizeOfRandomSubset <= 0)
  766. sizeOfRandomSubset = _y.size();
  767. if (sizeOfRandomSubset > _y.size())
  768. sizeOfRandomSubset = _y.size();
  769. for ( iter = 1; iter <= _maxIterations; iter++ )
  770. {
  771. NICE::Vector perm;
  772. this->randomPermutation( perm, indices, sizeOfRandomSubset );
  773. if ( _timeAnalysis )
  774. {
  775. t.stop();
  776. Vector r;
  777. this->hik_kernel_multiply_fast(Tlookup, _q, _alpha, r);
  778. r = r - _y;
  779. double res = r.normL2();
  780. double resMax = r.normInf();
  781. std::cerr << "SimpleGradientDescent: TIME " << t.getSum() << " " << res << " " << resMax << std::endl;
  782. t.start();
  783. }
  784. for ( uint i = 0; i < sizeOfRandomSubset; i++)
  785. {
  786. pseudoResidual(perm[i]) = -_y(perm[i]) + (this->d_noise * _alpha(perm[i]));
  787. for (uint j = 0; j < this->ui_d; j++)
  788. {
  789. x_i = this->X_sorted(j,perm[i]);
  790. pseudoResidual(perm[i]) += Tlookup[j*hmax + _q->quantize( x_i, j )];
  791. }
  792. //NOTE: this threshhold could also be a parameter of the function call
  793. if ( fabs(pseudoResidual(perm[i])) > 1e-7 )
  794. {
  795. alpha_old = _alpha(perm[i]);
  796. alpha_new = alpha_old - (pseudoResidual(perm[i])/diagonalElements(perm[i]));
  797. _alpha(perm[i]) = alpha_new;
  798. delta_alpha(perm[i]) = alpha_old-alpha_new;
  799. this->hikUpdateLookupTable(Tlookup, alpha_new, alpha_old, perm[i], _q, _pf ); // works correctly
  800. } else
  801. {
  802. delta_alpha(perm[i]) = 0.0;
  803. }
  804. }
  805. // after this only residual(i) is the valid residual... we should
  806. // really update the whole vector somehow
  807. double delta = delta_alpha.normL2();
  808. if ( this->b_verbose ) {
  809. cerr << "FastMinKernel::solveLin: iteration " << iter << " / " << _maxIterations << endl;
  810. cerr << "FastMinKernel::solveLin: delta = " << delta << endl;
  811. cerr << "FastMinKernel::solveLin: pseudo residual = " << pseudoResidual.scalarProduct(pseudoResidual) << endl;
  812. }
  813. if ( delta < _minDelta )
  814. {
  815. if ( this->b_verbose )
  816. cerr << "FastMinKernel::solveLin: small delta" << endl;
  817. break;
  818. }
  819. }
  820. }
  821. else //don't use random subsets
  822. {
  823. // this is the standard coordinate descent optimization
  824. // in each of the elements in alpha
  825. for ( iter = 1; iter <= _maxIterations; iter++ )
  826. {
  827. for ( uint i = 0; i < _y.size(); i++ )
  828. {
  829. pseudoResidual(i) = -_y(i) + (this->d_noise* _alpha(i));
  830. for (uint j = 0; j < this->ui_d; j++)
  831. {
  832. x_i = this->X_sorted(j,i);
  833. pseudoResidual(i) += Tlookup[j*hmax + _q->quantize( x_i, j )];
  834. }
  835. //NOTE: this threshhold could also be a parameter of the function call
  836. if ( fabs(pseudoResidual(i)) > 1e-7 )
  837. {
  838. alpha_old = _alpha(i);
  839. alpha_new = alpha_old - (pseudoResidual(i)/diagonalElements(i));
  840. _alpha(i) = alpha_new;
  841. delta_alpha(i) = alpha_old-alpha_new;
  842. this->hikUpdateLookupTable(Tlookup, alpha_new, alpha_old, i, _q, _pf ); // works correctly
  843. } else
  844. {
  845. delta_alpha(i) = 0.0;
  846. }
  847. }
  848. double delta = delta_alpha.normL2();
  849. if ( this->b_verbose ) {
  850. std::cerr << "FastMinKernel::solveLin: iteration " << iter << " / " << _maxIterations << std::endl;
  851. std::cerr << "FastMinKernel::solveLin: delta = " << delta << std::endl;
  852. std::cerr << "FastMinKernel::solveLin: pseudo residual = " << pseudoResidual.scalarProduct(pseudoResidual) << std::endl;
  853. }
  854. if ( delta < _minDelta )
  855. {
  856. if ( this->b_verbose )
  857. std::cerr << "FastMinKernel::solveLin: small delta" << std::endl;
  858. break;
  859. }
  860. }
  861. }
  862. if (verboseMinimal)
  863. std::cerr << "FastMinKernel::solveLin -- needed " << iter << " iterations" << std::endl;
  864. return Tlookup;
  865. }
  866. void FastMinKernel::randomPermutation(NICE::Vector & _permutation,
  867. const std::vector<uint> & _oldIndices,
  868. const uint & _newSize
  869. ) const
  870. {
  871. std::vector<uint> indices(_oldIndices);
  872. const uint oldSize = _oldIndices.size();
  873. uint resultingSize (std::min( oldSize, _newSize) );
  874. _permutation.resize(resultingSize);
  875. for ( uint i = 0; i < resultingSize; i++)
  876. {
  877. uint newIndex(rand() % indices.size());
  878. _permutation[i] = indices[newIndex ];
  879. indices.erase(indices.begin() + newIndex);
  880. }
  881. }
  882. double FastMinKernel::getFrobNormApprox()
  883. {
  884. double frobNormApprox(0.0);
  885. switch (this->approxScheme)
  886. {
  887. case MEDIAN:
  888. {
  889. //\| K \|_F^1 ~ (n/2)^2 \left( \sum_k \median_k \right)^2
  890. //motivation: estimate half of the values in dim k to zero and half of them to the median (-> lower bound expectation)
  891. for ( uint i = 0; i < this->ui_d; i++ )
  892. {
  893. double median = this->X_sorted.getFeatureValues(i).getMedian();
  894. frobNormApprox += median;
  895. }
  896. frobNormApprox = fabs(frobNormApprox) * this->ui_n/2.0;
  897. break;
  898. }
  899. case EXPECTATION:
  900. {
  901. std::cerr << "EXPECTATION" << std::endl;
  902. //\| 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)
  903. // with a_k = minimal value in dim k and b_k maximal value
  904. //first term
  905. NICE::Vector diagEl;
  906. X_sorted.hikDiagonalElements(diagEl);
  907. frobNormApprox += diagEl.normL2();
  908. //second term
  909. double secondTerm(0.0);
  910. for ( uint i = 0; i < this->ui_d; i++ )
  911. {
  912. double minInDim;
  913. minInDim = this->X_sorted.getFeatureValues(i).getMin();
  914. double maxInDim;
  915. maxInDim = this->X_sorted.getFeatureValues(i).getMax();
  916. std::cerr << "min: " << minInDim << " max: " << maxInDim << std::endl;
  917. secondTerm += 2.0*minInDim + maxInDim;
  918. }
  919. secondTerm /= 3.0;
  920. secondTerm = pow(secondTerm, 2);
  921. secondTerm *= (this->ui_n * ( this->ui_n - 1 ));
  922. frobNormApprox += secondTerm;
  923. frobNormApprox = sqrt(frobNormApprox);
  924. break;
  925. }
  926. default:
  927. { //do nothing, approximate with zero :)
  928. break;
  929. }
  930. }
  931. return frobNormApprox;
  932. }
  933. void FastMinKernel::setApproximationScheme(const int & _approxScheme)
  934. {
  935. switch(_approxScheme)
  936. {
  937. case 0:
  938. {
  939. this->approxScheme = MEDIAN;
  940. break;
  941. }
  942. case 1:
  943. {
  944. this->approxScheme = EXPECTATION;
  945. break;
  946. }
  947. default:
  948. {
  949. this->approxScheme = MEDIAN;
  950. break;
  951. }
  952. }
  953. }
  954. void FastMinKernel::hikPrepareKVNApproximation(NICE::VVector & _A) const
  955. {
  956. _A.resize( this->ui_d );
  957. // efficient calculation of |k_*|^2 = k_*^T * k_*
  958. // ---------------------------------
  959. //
  960. // \sum_{i=1}^{n} \left( \sum_{d=1}^{D} \min (x_d^*, x_d^i) \right)^2
  961. // <=\sum_{i=1}^{n} \sum_{d=1}^{D} \left( \min (x_d^*, x_d^i) \right)^2
  962. // = \sum_{d=1}^{D} \sum_{i=1}^{n} \left( \min (x_d^*, x_d^i) \right)^2
  963. // = \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)
  964. //
  965. // again let us define l_d = { i | x_d^i <= x_d^* }
  966. // and u_d = { i | x_d^i > x_d^* }, this leads to
  967. //
  968. // = \sum_{d=1}^{D} ( \sum_{l \in l_d} (x_d^l)^2 + \sum_{u \in u_d} (x_d^*)^2
  969. // = \sum_{d=1}^{D} ( \sum_{l \in l_d} (x_d^l)^2 + (x_d^*)^2 \sum_{u \in u_d} 1
  970. //
  971. // We also define
  972. // l_d^j = { i | x_d^i <= x_d^j } and
  973. // u_d^j = { i | x_d^i > x_d^j }
  974. //
  975. // We now need the partial sums
  976. //
  977. // (Definition 1)
  978. // a_{d,j} = \sum_{l \in l_d^j} (x_d^l)^2
  979. // according to increasing values of x_d^l
  980. //
  981. // We end at
  982. // |k_*|^2 <= \sum_{d=1}^{D} \left( a_{d,r_d} + (x_d^*)^2 * |u_d^{r_d}| \right)
  983. // with r_d being the index of the last example in the ordered sequence for dimension d, that is not larger than x_d^*
  984. // we only need as many entries as we have nonZero entries in our features for the corresponding dimensions
  985. for ( uint i = 0; i < this->ui_d; i++ )
  986. {
  987. uint numNonZero = this->X_sorted.getNumberOfNonZeroElementsPerDimension(i);
  988. _A[i].resize( numNonZero );
  989. }
  990. // for more information see hik_prepare_alpha_multiplications
  991. for (uint dim = 0; dim < this->ui_d; dim++)
  992. {
  993. double squared_sum(0.0);
  994. uint cntNonzeroFeat(0);
  995. const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
  996. // loop through all elements in sorted order
  997. for ( SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin(); i != nonzeroElements.end(); i++ )
  998. {
  999. const SortedVectorSparse<double>::dataelement & de = i->second;
  1000. // de: first - index, second - transformed feature
  1001. double elem( de.second );
  1002. squared_sum += pow( elem, 2 );
  1003. _A[dim][cntNonzeroFeat] = squared_sum;
  1004. cntNonzeroFeat++;
  1005. }
  1006. }
  1007. }
  1008. double * FastMinKernel::hikPrepareKVNApproximationFast(NICE::VVector & _A,
  1009. const Quantization * _q,
  1010. const ParameterizedFunction *_pf ) const
  1011. {
  1012. //NOTE keep in mind: for doing this, we already have precomputed A using hikPrepareSquaredKernelVector!
  1013. // number of quantization bins
  1014. uint hmax = _q->getNumberOfBins();
  1015. // store (transformed) prototypes
  1016. double *prototypes = new double [ hmax * this->ui_d ];
  1017. double * p_prototypes = prototypes;
  1018. for (uint dim = 0; dim < this->ui_d; dim++)
  1019. {
  1020. for ( uint i = 0 ; i < hmax ; i++ )
  1021. {
  1022. if ( _pf != NULL )
  1023. {
  1024. *p_prototypes = _pf->f ( dim, _q->getPrototype( i, dim ) );
  1025. } else
  1026. {
  1027. *p_prototypes = _q->getPrototype( i, dim );
  1028. }
  1029. p_prototypes++;
  1030. }
  1031. }
  1032. // creating the lookup table as pure C, which might be beneficial
  1033. // for fast evaluation
  1034. double *Tlookup = new double [ hmax * this->ui_d ];
  1035. // loop through all dimensions
  1036. for (uint dim = 0; dim < this->ui_d; dim++)
  1037. {
  1038. uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
  1039. if ( nrZeroIndices == this->ui_n )
  1040. continue;
  1041. const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
  1042. SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin();
  1043. SortedVectorSparse<double>::const_elementpointer iPredecessor = nonzeroElements.begin();
  1044. // index of the element, which is always bigger than the current value fval
  1045. uint index = 0;
  1046. // we use the quantization of the original features! the transformed feature were
  1047. // already used to calculate A and B, this of course assumes monotonic functions!!!
  1048. uint qBin = _q->quantize ( i->first, dim );
  1049. // the next loop is linear in max(hmax, n)
  1050. // REMARK: this could be changed to hmax*log(n), when
  1051. // we use binary search
  1052. //FIXME we should do this!
  1053. for (uint j = 0; j < hmax; j++)
  1054. {
  1055. double fval = prototypes[ dim*hmax + j];
  1056. double t;
  1057. if ( (index == 0) && (j < qBin) ) {
  1058. // current element is smaller than everything else
  1059. // resulting value = fval * sum_l=1^n 1
  1060. t = pow( fval, 2 ) * (this->ui_n-nrZeroIndices-index);
  1061. } else {
  1062. // move to next example, if necessary
  1063. while ( (j >= qBin) && ( index < (this->ui_n-nrZeroIndices)) )
  1064. {
  1065. index++;
  1066. iPredecessor = i;
  1067. i++;
  1068. if ( i->first != iPredecessor->first )
  1069. qBin = _q->quantize ( i->first, dim );
  1070. }
  1071. // compute current element in the lookup table and keep in mind that
  1072. // index is the next element and not the previous one
  1073. //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
  1074. if ( (j >= qBin) && ( index==(this->ui_n-1-nrZeroIndices) ) ) {
  1075. // the current element (fval) is equal or bigger to the element indexed by index
  1076. // the second term vanishes, which is logical, since all elements are smaller than j!
  1077. t = _A[dim][index];
  1078. } else {
  1079. // standard case
  1080. t = _A[dim][index-1] + pow( fval, 2 ) * (this->ui_n-nrZeroIndices-(index) );
  1081. }
  1082. }
  1083. Tlookup[ dim*hmax + j ] = t;
  1084. }
  1085. }
  1086. delete [] prototypes;
  1087. return Tlookup;
  1088. }
  1089. double* FastMinKernel::hikPrepareLookupTableForKVNApproximation(const Quantization * _q,
  1090. const ParameterizedFunction *_pf
  1091. ) const
  1092. {
  1093. // number of quantization bins
  1094. uint hmax = _q->getNumberOfBins();
  1095. // store (transformed) prototypes
  1096. double *prototypes = new double [ hmax * this->ui_d ];
  1097. double * p_prototypes = prototypes;
  1098. for (uint dim = 0; dim < this->ui_d; dim++)
  1099. {
  1100. for ( uint i = 0 ; i < hmax ; i++ )
  1101. {
  1102. if ( _pf != NULL )
  1103. {
  1104. *p_prototypes = _pf->f ( dim, _q->getPrototype( i, dim ) );
  1105. } else
  1106. {
  1107. *p_prototypes = _q->getPrototype( i, dim );
  1108. }
  1109. p_prototypes++;
  1110. }
  1111. }
  1112. // creating the lookup table as pure C, which might be beneficial
  1113. // for fast evaluation
  1114. double *Tlookup = new double [ hmax * this->ui_d ];
  1115. // loop through all dimensions
  1116. for (uint dim = 0; dim < this->ui_d; dim++)
  1117. {
  1118. uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
  1119. if ( nrZeroIndices == this->ui_n )
  1120. continue;
  1121. const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
  1122. SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin();
  1123. SortedVectorSparse<double>::const_elementpointer iPredecessor = nonzeroElements.begin();
  1124. // index of the element, which is always bigger than the current value fval
  1125. uint index = 0;
  1126. // we use the quantization of the original features! Nevetheless, the resulting lookupTable is computed using the transformed ones
  1127. uint qBin = _q->quantize ( i->first, dim );
  1128. double sum(0.0);
  1129. for (uint j = 0; j < hmax; j++)
  1130. {
  1131. double fval = prototypes[ dim*hmax + j];
  1132. double t;
  1133. if ( (index == 0) && (j < qBin) ) {
  1134. // current element is smaller than everything else
  1135. // resulting value = fval * sum_l=1^n 1
  1136. t = pow( fval, 2 ) * (this->ui_n-nrZeroIndices-index);
  1137. } else {
  1138. // move to next example, if necessary
  1139. while ( (j >= qBin) && ( index < (this->ui_n-nrZeroIndices)) )
  1140. {
  1141. sum += pow( i->second.second, 2 ); //i->dataElement.transformedFeatureValue
  1142. index++;
  1143. iPredecessor = i;
  1144. i++;
  1145. if ( i->first != iPredecessor->first )
  1146. qBin = _q->quantize ( i->first, dim );
  1147. }
  1148. // compute current element in the lookup table and keep in mind that
  1149. // index is the next element and not the previous one
  1150. //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
  1151. if ( (j >= qBin) && ( index==(this->ui_n-1-nrZeroIndices) ) ) {
  1152. // the current element (fval) is equal or bigger to the element indexed by index
  1153. // the second term vanishes, which is logical, since all elements are smaller than j!
  1154. t = sum;
  1155. } else {
  1156. // standard case
  1157. t = sum + pow( fval, 2 ) * (this->ui_n-nrZeroIndices-(index) );
  1158. }
  1159. }
  1160. Tlookup[ dim*hmax + j ] = t;
  1161. }
  1162. }
  1163. delete [] prototypes;
  1164. return Tlookup;
  1165. }
  1166. //////////////////////////////////////////
  1167. // variance computation: sparse inputs
  1168. //////////////////////////////////////////
  1169. void FastMinKernel::hikComputeKVNApproximation(const NICE::VVector & _A,
  1170. const NICE::SparseVector & _xstar,
  1171. double & _norm,
  1172. const ParameterizedFunction *_pf )
  1173. {
  1174. _norm = 0.0;
  1175. for (SparseVector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++)
  1176. {
  1177. uint dim = i->first;
  1178. double fval = i->second;
  1179. uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
  1180. if ( nrZeroIndices == this->ui_n ) {
  1181. // all features are zero so let us ignore them completely
  1182. continue;
  1183. }
  1184. uint position;
  1185. //where is the example x^z_i located in
  1186. //the sorted array? -> perform binary search, runtime O(log(n))
  1187. // search using the original value
  1188. this->X_sorted.findFirstLargerInDimension(dim, fval, position);
  1189. bool posIsZero ( position == 0 );
  1190. if ( !posIsZero )
  1191. {
  1192. position--;
  1193. }
  1194. //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
  1195. double firstPart(0.0);
  1196. //TODO in the "overnext" line there occurs the following error
  1197. // Invalid read of size 8
  1198. if ( !posIsZero && ((position-nrZeroIndices) < this->ui_n) )
  1199. firstPart = (_A[dim][position-nrZeroIndices]);
  1200. if ( _pf != NULL )
  1201. fval = _pf->f ( dim, fval );
  1202. fval = fval * fval;
  1203. double secondPart( 0.0);
  1204. if ( !posIsZero )
  1205. secondPart = fval * (this->ui_n-nrZeroIndices-(position+1));
  1206. else //if x_d^* is smaller than every non-zero training example
  1207. secondPart = fval * (this->ui_n-nrZeroIndices);
  1208. // but apply using the transformed one
  1209. _norm += firstPart + secondPart;
  1210. }
  1211. }
  1212. void FastMinKernel::hikComputeKVNApproximationFast(const double *_Tlookup,
  1213. const Quantization * _q,
  1214. const NICE::SparseVector & _xstar,
  1215. double & _norm
  1216. ) const
  1217. {
  1218. _norm = 0.0;
  1219. // runtime is O(d) if the quantizer is O(1)
  1220. for (SparseVector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++ )
  1221. {
  1222. uint dim = i->first;
  1223. double v = i->second;
  1224. // we do not need a parameterized function here, since the quantizer works on the original feature values.
  1225. // nonetheless, the lookup table was created using the parameterized function
  1226. uint qBin = _q->quantize( v, dim );
  1227. _norm += _Tlookup[dim*_q->getNumberOfBins() + qBin];
  1228. }
  1229. }
  1230. void FastMinKernel::hikComputeKernelVector ( const NICE::SparseVector& _xstar,
  1231. NICE::Vector & _kstar
  1232. ) const
  1233. {
  1234. //init
  1235. _kstar.resize( this->ui_n );
  1236. _kstar.set(0.0);
  1237. if ( this->b_debug )
  1238. {
  1239. std::cerr << " FastMinKernel::hikComputeKernelVector -- input: " << std::endl;
  1240. _xstar.store( std::cerr);
  1241. }
  1242. //let's start :)
  1243. for (SparseVector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++)
  1244. {
  1245. uint dim = i->first;
  1246. double fval = i->second;
  1247. if ( this->b_debug )
  1248. std::cerr << "dim: " << dim << " fval: " << fval << std::endl;
  1249. uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
  1250. if ( nrZeroIndices == this->ui_n ) {
  1251. // all features are zero so let us ignore them completely
  1252. continue;
  1253. }
  1254. uint position;
  1255. //where is the example x^z_i located in
  1256. //the sorted array? -> perform binary search, runtime O(log(n))
  1257. // search using the original value
  1258. this->X_sorted.findFirstLargerInDimension(dim, fval, position);
  1259. //position--;
  1260. if ( this->b_debug )
  1261. std::cerr << " position: " << position << std::endl;
  1262. //get the non-zero elements for this dimension
  1263. const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
  1264. //run over the non-zero elements and add the corresponding entries to our kernel vector
  1265. uint count(nrZeroIndices);
  1266. for ( SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin(); i != nonzeroElements.end(); i++, count++ )
  1267. {
  1268. uint origIndex(i->second.first); //orig index (i->second.second would be the transformed feature value)
  1269. if ( this->b_debug )
  1270. std::cerr << "i->1.2: " << i->second.first << " origIndex: " << origIndex << " count: " << count << " position: " << position << std::endl;
  1271. if (count < position)
  1272. _kstar[origIndex] += i->first; //orig feature value
  1273. else
  1274. _kstar[origIndex] += fval;
  1275. }
  1276. }
  1277. }
  1278. //////////////////////////////////////////
  1279. // variance computation: non-sparse inputs
  1280. //////////////////////////////////////////
  1281. void FastMinKernel::hikComputeKVNApproximation(const NICE::VVector & _A,
  1282. const NICE::Vector & _xstar,
  1283. double & _norm,
  1284. const ParameterizedFunction *_pf )
  1285. {
  1286. _norm = 0.0;
  1287. uint dim ( 0 );
  1288. for (Vector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++, dim++)
  1289. {
  1290. double fval = *i;
  1291. uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
  1292. if ( nrZeroIndices == this->ui_n ) {
  1293. // all features are zero so let us ignore them completely
  1294. continue;
  1295. }
  1296. uint position;
  1297. //where is the example x^z_i located in
  1298. //the sorted array? -> perform binary search, runtime O(log(n))
  1299. // search using the original value
  1300. this->X_sorted.findFirstLargerInDimension(dim, fval, position);
  1301. bool posIsZero ( position == 0 );
  1302. if ( !posIsZero )
  1303. {
  1304. position--;
  1305. }
  1306. //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
  1307. double firstPart(0.0);
  1308. //TODO in the "overnext" line there occurs the following error
  1309. // Invalid read of size 8
  1310. if ( !posIsZero && ((position-nrZeroIndices) < this->ui_n) )
  1311. firstPart = (_A[dim][position-nrZeroIndices]);
  1312. double secondPart( 0.0);
  1313. if ( _pf != NULL )
  1314. fval = _pf->f ( dim, fval );
  1315. fval = fval * fval;
  1316. if ( !posIsZero )
  1317. secondPart = fval * (this->ui_n-nrZeroIndices-(position+1));
  1318. else //if x_d^* is smaller than every non-zero training example
  1319. secondPart = fval * (this->ui_n-nrZeroIndices);
  1320. // but apply using the transformed one
  1321. _norm += firstPart + secondPart;
  1322. }
  1323. }
  1324. void FastMinKernel::hikComputeKVNApproximationFast(const double *_Tlookup,
  1325. const Quantization * _q,
  1326. const NICE::Vector & _xstar,
  1327. double & _norm
  1328. ) const
  1329. {
  1330. _norm = 0.0;
  1331. // runtime is O(d) if the quantizer is O(1)
  1332. uint dim ( 0 );
  1333. for ( NICE::Vector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++, dim++ )
  1334. {
  1335. double v = *i;
  1336. // we do not need a parameterized function here, since the quantizer works on the original feature values.
  1337. // nonetheless, the lookup table was created using the parameterized function
  1338. uint qBin = _q->quantize( v, dim );
  1339. _norm += _Tlookup[dim*_q->getNumberOfBins() + qBin];
  1340. }
  1341. }
  1342. void FastMinKernel::hikComputeKernelVector( const NICE::Vector & _xstar,
  1343. NICE::Vector & _kstar) const
  1344. {
  1345. //init
  1346. _kstar.resize(this->ui_n);
  1347. _kstar.set(0.0);
  1348. //let's start :)
  1349. uint dim ( 0 );
  1350. for (NICE::Vector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++, dim++)
  1351. {
  1352. double fval = *i;
  1353. uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
  1354. if ( nrZeroIndices == this->ui_n ) {
  1355. // all features are zero so let us ignore them completely
  1356. continue;
  1357. }
  1358. uint position;
  1359. //where is the example x^z_i located in
  1360. //the sorted array? -> perform binary search, runtime O(log(n))
  1361. // search using the original value
  1362. this->X_sorted.findFirstLargerInDimension(dim, fval, position);
  1363. //position--;
  1364. //get the non-zero elements for this dimension
  1365. const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
  1366. //run over the non-zero elements and add the corresponding entries to our kernel vector
  1367. uint count(nrZeroIndices);
  1368. for ( SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin(); i != nonzeroElements.end(); i++, count++ )
  1369. {
  1370. uint origIndex(i->second.first); //orig index (i->second.second would be the transformed feature value)
  1371. if (count < position)
  1372. _kstar[origIndex] += i->first; //orig feature value
  1373. else
  1374. _kstar[origIndex] += fval;
  1375. }
  1376. }
  1377. }
  1378. ///////////////////// INTERFACE PERSISTENT /////////////////////
  1379. // interface specific methods for store and restore
  1380. ///////////////////// INTERFACE PERSISTENT /////////////////////
  1381. void FastMinKernel::restore ( std::istream & _is,
  1382. int _format )
  1383. {
  1384. bool b_restoreVerbose ( false );
  1385. if ( _is.good() )
  1386. {
  1387. if ( b_restoreVerbose )
  1388. std::cerr << " restore FastMinKernel" << std::endl;
  1389. std::string tmp;
  1390. _is >> tmp; //class name
  1391. if ( ! this->isStartTag( tmp, "FastMinKernel" ) )
  1392. {
  1393. std::cerr << " WARNING - attempt to restore FastMinKernel, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  1394. throw;
  1395. }
  1396. _is.precision (numeric_limits<double>::digits10 + 1);
  1397. bool b_endOfBlock ( false ) ;
  1398. while ( !b_endOfBlock )
  1399. {
  1400. _is >> tmp; // start of block
  1401. if ( this->isEndTag( tmp, "FastMinKernel" ) )
  1402. {
  1403. b_endOfBlock = true;
  1404. continue;
  1405. }
  1406. tmp = this->removeStartTag ( tmp );
  1407. if ( b_restoreVerbose )
  1408. std::cerr << " currently restore section " << tmp << " in FastMinKernel" << std::endl;
  1409. if ( tmp.compare("ui_n") == 0 )
  1410. {
  1411. _is >> this->ui_n;
  1412. _is >> tmp; // end of block
  1413. tmp = this->removeEndTag ( tmp );
  1414. }
  1415. else if ( tmp.compare("ui_d") == 0 )
  1416. {
  1417. _is >> this->ui_d;
  1418. _is >> tmp; // end of block
  1419. tmp = this->removeEndTag ( tmp );
  1420. }
  1421. else if ( tmp.compare("d_noise") == 0 )
  1422. {
  1423. _is >> this->d_noise;
  1424. _is >> tmp; // end of block
  1425. tmp = this->removeEndTag ( tmp );
  1426. }
  1427. else if ( tmp.compare("approxScheme") == 0 )
  1428. {
  1429. int approxSchemeInt;
  1430. _is >> approxSchemeInt;
  1431. setApproximationScheme(approxSchemeInt);
  1432. _is >> tmp; // end of block
  1433. tmp = this->removeEndTag ( tmp );
  1434. }
  1435. else if ( tmp.compare("X_sorted") == 0 )
  1436. {
  1437. this->X_sorted.restore(_is,_format);
  1438. _is >> tmp; // end of block
  1439. tmp = this->removeEndTag ( tmp );
  1440. }
  1441. else
  1442. {
  1443. std::cerr << "WARNING -- unexpected FastMinKernel object -- " << tmp << " -- for restoration... aborting" << std::endl;
  1444. throw;
  1445. }
  1446. }
  1447. }
  1448. else
  1449. {
  1450. std::cerr << "FastMinKernel::restore -- InStream not initialized - restoring not possible!" << std::endl;
  1451. }
  1452. }
  1453. void FastMinKernel::store ( std::ostream & _os,
  1454. int _format
  1455. ) const
  1456. {
  1457. if (_os.good())
  1458. {
  1459. // show starting point
  1460. _os << this->createStartTag( "FastMinKernel" ) << std::endl;
  1461. _os.precision (numeric_limits<double>::digits10 + 1);
  1462. _os << this->createStartTag( "ui_n" ) << std::endl;
  1463. _os << this->ui_n << std::endl;
  1464. _os << this->createEndTag( "ui_n" ) << std::endl;
  1465. _os << this->createStartTag( "ui_d" ) << std::endl;
  1466. _os << this->ui_d << std::endl;
  1467. _os << this->createEndTag( "ui_d" ) << std::endl;
  1468. _os << this->createStartTag( "d_noise" ) << std::endl;
  1469. _os << this->d_noise << std::endl;
  1470. _os << this->createEndTag( "d_noise" ) << std::endl;
  1471. _os << this->createStartTag( "approxScheme" ) << std::endl;
  1472. _os << this->approxScheme << std::endl;
  1473. _os << this->createEndTag( "approxScheme" ) << std::endl;
  1474. _os << this->createStartTag( "X_sorted" ) << std::endl;
  1475. //store the underlying data
  1476. this->X_sorted.store(_os, _format);
  1477. _os << this->createEndTag( "X_sorted" ) << std::endl;
  1478. // done
  1479. _os << this->createEndTag( "FastMinKernel" ) << std::endl;
  1480. }
  1481. else
  1482. {
  1483. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  1484. }
  1485. }
  1486. void FastMinKernel::clear ()
  1487. {
  1488. std::cerr << "FastMinKernel clear-function called" << std::endl;
  1489. }
  1490. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  1491. // interface specific methods for incremental extensions
  1492. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  1493. void FastMinKernel::addExample( const NICE::SparseVector * _example,
  1494. const double & _label,
  1495. const bool & _performOptimizationAfterIncrement
  1496. )
  1497. {
  1498. // no parameterized function was given - use default
  1499. this->addExample ( _example );
  1500. }
  1501. void FastMinKernel::addMultipleExamples( const std::vector< const NICE::SparseVector * > & _newExamples,
  1502. const NICE::Vector & _newLabels,
  1503. const bool & _performOptimizationAfterIncrement
  1504. )
  1505. {
  1506. // no parameterized function was given - use default
  1507. this->addMultipleExamples ( _newExamples );
  1508. }
  1509. void FastMinKernel::addExample( const NICE::SparseVector * _example,
  1510. const NICE::ParameterizedFunction *_pf
  1511. )
  1512. {
  1513. this->X_sorted.add_feature( *_example, _pf );
  1514. this->ui_n++;
  1515. }
  1516. void FastMinKernel::addMultipleExamples( const std::vector< const NICE::SparseVector * > & _newExamples,
  1517. const NICE::ParameterizedFunction *_pf
  1518. )
  1519. {
  1520. for ( std::vector< const NICE::SparseVector * >::const_iterator exIt = _newExamples.begin();
  1521. exIt != _newExamples.end();
  1522. exIt++ )
  1523. {
  1524. this->X_sorted.add_feature( **exIt, _pf );
  1525. this->ui_n++;
  1526. }
  1527. }