123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797 |
- #include <iostream>
- #include <core/basics/vectorio.h>
- #include <core/basics/Timer.h>
- #include "FastMinKernel.h"
- using namespace std;
- using namespace NICE;
- FastMinKernel::FastMinKernel()
- {
- this->ui_d = 0;
- this->ui_n = 0;
- this->d_noise = 1.0;
- this->approxScheme = MEDIAN;
- this->b_verbose = false;
- this->setDebug(false);
- }
- FastMinKernel::FastMinKernel( const std::vector<std::vector<double> > & _X,
- const double _noise,
- const bool _debug,
- const uint & _dim
- )
- {
- this->setDebug(_debug);
- this->X_sorted.set_features( _X, _dim);
- this->ui_d = this->X_sorted.get_d();
- this->ui_n = this->X_sorted.get_n();
- this->d_noise = _noise;
- this->approxScheme = MEDIAN;
- this->b_verbose = false;
- }
- #ifdef NICE_USELIB_MATIO
- FastMinKernel::FastMinKernel ( const sparse_t & _X,
- const double _noise,
- const std::map<uint, uint> & _examples,
- const bool _debug,
- const uint & _dim
- ) : this->X_sorted( _X, _examples, _dim )
- {
- this->ui_d = this->X_sorted.get_d();
- this->ui_n = this->X_sorted.get_n();
- this->d_noise = _noise;
- this->approxScheme = MEDIAN;
- this->b_verbose = false;
- this->setDebug(_debug);
- }
- #endif
- FastMinKernel::FastMinKernel ( const std::vector< const NICE::SparseVector * > & _X,
- const double _noise,
- const bool _debug,
- const bool & _dimensionsOverExamples,
- const uint & _dim)
- {
- this->setDebug(_debug);
- this->X_sorted.set_features( _X, _dimensionsOverExamples, _dim);
- this->ui_d = this->X_sorted.get_d();
- this->ui_n = this->X_sorted.get_n();
- this->d_noise = _noise;
- this->approxScheme = MEDIAN;
- this->b_verbose = false;
- }
- FastMinKernel::~FastMinKernel()
- {
- }
- uint FastMinKernel::get_n() const
- {
- return this->ui_n;
- }
- uint FastMinKernel::get_d() const
- {
- return this->ui_d;
- }
- double FastMinKernel::getSparsityRatio() const
- {
- return this->X_sorted.computeSparsityRatio();
- }
- void FastMinKernel::setVerbose( const bool & _verbose)
- {
- this->b_verbose = _verbose;
- }
- bool FastMinKernel::getVerbose( ) const
- {
- return this->b_verbose;
- }
- void FastMinKernel::setDebug( const bool & _debug)
- {
- this->b_debug = _debug;
- this->X_sorted.setDebug( _debug );
- }
- bool FastMinKernel::getDebug( ) const
- {
- return this->b_debug;
- }
- void FastMinKernel::applyFunctionToFeatureMatrix ( const NICE::ParameterizedFunction *_pf)
- {
- this->X_sorted.applyFunctionToFeatureMatrix( _pf );
- }
- void FastMinKernel::hik_prepare_alpha_multiplications(const NICE::Vector & _alpha,
- NICE::VVector & _A,
- NICE::VVector & _B) const
- {
- _A.resize( this->ui_d );
- _B.resize( this->ui_d );
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- for (uint i = 0; i < this->ui_d; i++)
- {
- uint numNonZero = this->X_sorted.getNumberOfNonZeroElementsPerDimension(i);
- _A[i].resize( numNonZero );
- _B[i].resize( numNonZero );
- }
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- double alpha_sum(0.0);
- double alpha_times_x_sum(0.0);
- uint cntNonzeroFeat(0);
- const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
-
- for ( SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin(); i != nonzeroElements.end(); i++ )
- {
- const SortedVectorSparse<double>::dataelement & de = i->second;
-
- int index = de.first;
-
-
- double elem( de.second );
- alpha_times_x_sum += _alpha[index] * elem;
- _A[dim][cntNonzeroFeat] = alpha_times_x_sum;
- alpha_sum += _alpha[index];
- _B[dim][cntNonzeroFeat] = alpha_sum;
- cntNonzeroFeat++;
- }
- }
- }
- double *FastMinKernel::hik_prepare_alpha_multiplications_fast(const NICE::VVector & _A,
- const NICE::VVector & _B,
- const Quantization * _q,
- const ParameterizedFunction *_pf
- ) const
- {
-
-
- uint hmax = _q->getNumberOfBins();
-
- double * prototypes = new double [ hmax * this->ui_d ];
- double * p_prototypes = prototypes;
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- for ( uint i = 0 ; i < hmax ; i++ )
- {
- if ( _pf != NULL )
- {
- *p_prototypes = _pf->f ( dim, _q->getPrototype( i, dim ) );
- } else
- {
- *p_prototypes = _q->getPrototype( i, dim );
- }
- p_prototypes++;
- }
- }
-
-
- double *Tlookup = new double [ hmax * this->ui_d ];
-
- for ( uint dim = 0; dim < this->ui_d; dim++ )
- {
- uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
- if ( nrZeroIndices == this->ui_n )
- continue;
- const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
- SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin();
- SortedVectorSparse<double>::const_elementpointer iPredecessor = nonzeroElements.begin();
-
- uint index = 0;
-
-
- uint qBin = _q->quantize ( i->first, dim );
-
-
-
- for (uint j = 0; j < hmax; j++)
- {
- double fval = prototypes[ dim*hmax + j ];
- double t;
- if ( (index == 0) && (j < qBin) ) {
-
-
- t = fval*( _B[dim][this->ui_n-1 - nrZeroIndices] );
- } else {
-
- while ( (j >= qBin) && ( index < (this->ui_n-1-nrZeroIndices)) )
- {
- index++;
- iPredecessor = i;
- i++;
- if ( i->first != iPredecessor->first )
- qBin = _q->quantize ( i->first, dim );
- }
-
-
-
-
- if ( (j >= qBin) && ( index==(this->ui_n-1-nrZeroIndices) ) ) {
-
-
- t = _A[dim][index];
- } else {
-
- t = _A[dim][index-1] + fval*( _B[dim][this->ui_n-1-nrZeroIndices] - _B[dim][index-1] );
- }
- }
- Tlookup[ dim*hmax + j ] = t;
- }
- }
- delete [] prototypes;
- return Tlookup;
- }
- double *FastMinKernel::hikPrepareLookupTable(const NICE::Vector & _alpha,
- const Quantization * _q,
- const ParameterizedFunction *_pf
- ) const
- {
-
- uint hmax = _q->getNumberOfBins();
-
- double * prototypes = new double [ hmax * this->ui_d ];
- double * p_prototypes = prototypes;
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- for ( uint i = 0 ; i < hmax ; i++ )
- {
- if ( _pf != NULL )
- {
- *p_prototypes = _pf->f ( dim, _q->getPrototype( i, dim ) );
- } else
- {
- *p_prototypes = _q->getPrototype( i, dim );
- }
- p_prototypes++;
- }
- }
-
-
- double *Tlookup = new double [ hmax * this->ui_d ];
-
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
- if ( nrZeroIndices == this->ui_n )
- continue;
- const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
- double alphaSumTotalInDim(0.0);
- double alphaTimesXSumTotalInDim(0.0);
- for ( SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin(); i != nonzeroElements.end(); i++ )
- {
- alphaSumTotalInDim += _alpha[i->second.first];
- alphaTimesXSumTotalInDim += _alpha[i->second.first] * i->second.second;
- }
- SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin();
- SortedVectorSparse<double>::const_elementpointer iPredecessor = nonzeroElements.begin();
-
- uint index = 0;
-
- uint qBin = _q->quantize ( i->first, dim );
- double alpha_sum(0.0);
- double alpha_times_x_sum(0.0);
- double alpha_sum_prev(0.0);
- double alpha_times_x_sum_prev(0.0);
- for (uint j = 0; j < hmax; j++)
- {
- double fval = prototypes[ dim*hmax + j ];
- double t;
- if ( (index == 0) && (j < qBin) ) {
-
-
-
- t = fval*alphaSumTotalInDim;
- } else {
-
- while ( (j >= qBin) && ( index < (this->ui_n-1-nrZeroIndices)) )
- {
- alpha_times_x_sum_prev = alpha_times_x_sum;
- alpha_sum_prev = alpha_sum;
- alpha_times_x_sum += _alpha[i->second.first] * i->second.second;
- alpha_sum += _alpha[i->second.first];
- index++;
- iPredecessor = i;
- i++;
- if ( i->first != iPredecessor->first )
- qBin = _q->quantize ( i->first, dim );
- }
-
-
-
- if ( (j >= qBin) && ( index==(this->ui_n-1-nrZeroIndices) ) ) {
-
-
- t = alphaTimesXSumTotalInDim;
- } else {
-
- t = alpha_times_x_sum + fval*( alphaSumTotalInDim - alpha_sum );
- }
- }
- Tlookup[ dim*hmax + j ] = t;
- }
- }
- delete [] prototypes;
- return Tlookup;
- }
- void FastMinKernel::hikUpdateLookupTable(double * _T,
- const double & _alphaNew,
- const double & _alphaOld,
- const uint & _idx,
- const Quantization * _q,
- const ParameterizedFunction *_pf
- ) const
- {
- if (_T == NULL)
- {
- fthrow(Exception, "FastMinKernel::hikUpdateLookupTable LUT not initialized, run FastMinKernel::hikPrepareLookupTable first!");
- return;
- }
-
- uint hmax = _q->getNumberOfBins();
-
- double * prototypes = new double [ hmax * this->ui_d ];
- double * p_prototypes = prototypes;
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- for ( uint i = 0 ; i < hmax ; i++ )
- {
- if ( _pf != NULL )
- {
- *p_prototypes = _pf->f ( dim, _q->getPrototype( i, dim ) );
- } else
- {
- *p_prototypes = _q->getPrototype( i, dim );
- }
- p_prototypes++;
- }
- }
- double diffOfAlpha(_alphaNew - _alphaOld);
-
- for ( uint dim = 0; dim < this->ui_d; dim++ )
- {
- double x_i ( (this->X_sorted( dim, _idx)) );
-
- if ( x_i == 0.0 )
- continue;
-
- for (uint j = 0; j < hmax; j++)
- {
- double fval;
- uint q_bin = _q->quantize( x_i, dim );
- if ( q_bin > j )
- fval = prototypes[ dim*hmax + j ];
- else
- fval = x_i;
- _T[ dim*hmax + j ] += diffOfAlpha*fval;
- }
- }
- delete [] prototypes;
- }
- void FastMinKernel::hik_kernel_multiply(const NICE::VVector & _A,
- const NICE::VVector & _B,
- const NICE::Vector & _alpha,
- NICE::Vector & _beta
- ) const
- {
- _beta.resize( this->ui_n );
- _beta.set(0.0);
-
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
-
- const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
- uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
- if ( nrZeroIndices == this->ui_n ) {
-
- continue;
- }
- uint cnt(0);
- for ( multimap< double, SortedVectorSparse<double>::dataelement>::const_iterator i = nonzeroElements.begin(); i != nonzeroElements.end(); i++, cnt++)
- {
- const SortedVectorSparse<double>::dataelement & de = i->second;
- uint feat = de.first;
- uint inversePosition = cnt;
- double fval = de.second;
-
-
-
-
-
-
-
- double firstPart( _A[dim][inversePosition] );
-
-
-
-
-
- if ( dim >= _B.size() )
- fthrow(Exception, "dim exceeds B.size: " << dim << " " << _B.size() );
- if ( _B[dim].size() == 0 )
- fthrow(Exception, "B[dim] is empty");
- if ( (this->ui_n-1-nrZeroIndices < 0) || ((uint)(this->ui_n-1-nrZeroIndices) >= _B[dim].size() ) )
- fthrow(Exception, "n-1-nrZeroIndices is invalid: " << this->ui_n << " " << nrZeroIndices << " " << _B[dim].size() << " d: " << this->ui_d);
- if ( inversePosition < 0 || (uint)inversePosition >= _B[dim].size() )
- fthrow(Exception, "inverse position is invalid: " << inversePosition << " " << _B[dim].size() );
- double secondPart( _B[dim][this->ui_n-1-nrZeroIndices] - _B[dim][inversePosition]);
- _beta[feat] += firstPart + fval * secondPart;
- }
- }
-
-
-
-
-
- for (uint feat = 0; feat < this->ui_n; feat++)
- {
- _beta[feat] += this->d_noise*_alpha[feat];
- }
- }
- void FastMinKernel::hik_kernel_multiply_fast(const double *_Tlookup,
- const Quantization * _q,
- const NICE::Vector & _alpha,
- NICE::Vector & _beta) const
- {
- _beta.resize( this->ui_n );
- _beta.set(0.0);
-
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
-
- const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
- uint cnt(0);
- for ( multimap< double, SortedVectorSparse<double>::dataelement>::const_iterator i = nonzeroElements.begin(); i != nonzeroElements.end(); i++, cnt++)
- {
- const SortedVectorSparse<double>::dataelement & de = i->second;
- uint feat = de.first;
- uint qBin = _q->quantize( i->first, dim );
- _beta[feat] += _Tlookup[dim*_q->getNumberOfBins() + qBin];
- }
- }
-
- for (uint feat = 0; feat < this->ui_n; feat++)
- {
- _beta[feat] += this->d_noise*_alpha[feat];
- }
- }
- void FastMinKernel::hik_kernel_sum(const NICE::VVector & _A,
- const NICE::VVector & _B,
- const NICE::SparseVector & _xstar,
- double & _beta,
- const ParameterizedFunction *_pf) const
- {
-
-
- _beta = 0.0;
- for (SparseVector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++)
- {
- uint dim = i->first;
- double fval = i->second;
- uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
- if ( nrZeroIndices == this->ui_n ) {
-
- continue;
- }
- uint position;
-
-
-
- this->X_sorted.findFirstLargerInDimension(dim, fval, position);
- bool posIsZero ( position == 0 );
- if ( !posIsZero )
- {
- position--;
- }
-
-
- double firstPart(0.0);
-
-
- if ( !posIsZero && ((position-nrZeroIndices) < this->ui_n) )
- {
- firstPart = (_A[dim][position-nrZeroIndices]);
- }
-
-
-
-
-
- double secondPart( _B[dim][this->ui_n-1-nrZeroIndices]);
-
-
- if ( !posIsZero && (position >= nrZeroIndices) )
- {
- secondPart-= _B[dim][position-nrZeroIndices];
- }
- if ( _pf != NULL )
- {
- fval = _pf->f ( dim, fval );
- }
-
- _beta += firstPart + secondPart* fval;
- }
- }
- void FastMinKernel::hik_kernel_sum(const NICE::VVector & _A,
- const NICE::VVector & _B,
- const NICE::Vector & _xstar,
- double & _beta,
- const ParameterizedFunction *_pf
- ) const
- {
- _beta = 0.0;
- uint dim ( 0 );
- for (NICE::Vector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++, dim++)
- {
- double fval = *i;
- uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
- if ( nrZeroIndices == this->ui_n ) {
-
- continue;
- }
- uint position;
-
-
-
- this->X_sorted.findFirstLargerInDimension(dim, fval, position);
- bool posIsZero ( position == 0 );
- if ( !posIsZero )
- {
- position--;
- }
-
-
- double firstPart(0.0);
-
-
- if ( !posIsZero && ((position-nrZeroIndices) < this->ui_n) )
- {
- firstPart = (_A[dim][position-nrZeroIndices]);
- }
-
-
-
-
-
- double secondPart( _B[dim][this->ui_n-1-nrZeroIndices] );
-
-
- if ( !posIsZero && (position >= nrZeroIndices) )
- {
- secondPart-= _B[dim][position-nrZeroIndices];
- }
- if ( _pf != NULL )
- {
- fval = _pf->f ( dim, fval );
- }
-
- _beta += firstPart + secondPart* fval;
- }
- }
- void FastMinKernel::hik_kernel_sum_fast(const double *_Tlookup,
- const Quantization * _q,
- const NICE::Vector & _xstar,
- double & _beta
- ) const
- {
- _beta = 0.0;
- if ( _xstar.size() != this->ui_d)
- {
- fthrow(Exception, "FastMinKernel::hik_kernel_sum_fast sizes of xstar and training data does not match!");
- return;
- }
-
- for ( uint dim = 0; dim < this->ui_d; dim++)
- {
- double v = _xstar[dim];
- uint qBin = _q->quantize( v, dim );
- _beta += _Tlookup[dim*_q->getNumberOfBins() + qBin];
- }
- }
- void FastMinKernel::hik_kernel_sum_fast(const double *_Tlookup,
- const Quantization * _q,
- const NICE::SparseVector & _xstar,
- double & _beta
- ) const
- {
- _beta = 0.0;
-
-
-
-
- for (SparseVector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++ )
- {
- uint dim = i->first;
- double v = i->second;
- uint qBin = _q->quantize( v, dim );
- _beta += _Tlookup[dim*_q->getNumberOfBins() + qBin];
- }
- }
- double *FastMinKernel::solveLin(const NICE::Vector & _y,
- NICE::Vector & _alpha,
- const Quantization * _q,
- const ParameterizedFunction *_pf,
- const bool & _useRandomSubsets,
- uint _maxIterations,
- const uint & _sizeOfRandomSubset,
- double _minDelta,
- bool _timeAnalysis
- ) const
- {
-
-
-
-
- uint sizeOfRandomSubset(_sizeOfRandomSubset);
- bool verboseMinimal ( false );
-
- uint hmax = _q->getNumberOfBins();
- NICE::Vector diagonalElements(_y.size(),0.0);
- this->X_sorted.hikDiagonalElements(diagonalElements);
- diagonalElements += this->d_noise;
- NICE::Vector pseudoResidual (_y.size(),0.0);
- NICE::Vector delta_alpha (_y.size(),0.0);
- double alpha_old;
- double alpha_new;
- double x_i;
-
- if (_alpha.size() != _y.size())
- {
- _alpha.resize( _y.size() );
- }
- _alpha.set(0.0);
-
- double *Tlookup = new double [ hmax * this->ui_d ];
- if ( (hmax*this->ui_d) <= 0 )
- return Tlookup;
- memset(Tlookup, 0, sizeof(Tlookup[0])*hmax*this->ui_d);
- uint iter;
- Timer t;
- if ( _timeAnalysis )
- t.start();
- if (_useRandomSubsets)
- {
-
-
-
- std::vector<uint> indices( _y.size() );
- for (uint i = 0; i < _y.size(); i++)
- indices[i] = i;
- if (sizeOfRandomSubset <= 0)
- sizeOfRandomSubset = _y.size();
- if (sizeOfRandomSubset > _y.size())
- sizeOfRandomSubset = _y.size();
- for ( iter = 1; iter <= _maxIterations; iter++ )
- {
- NICE::Vector perm;
- this->randomPermutation( perm, indices, sizeOfRandomSubset );
- if ( _timeAnalysis )
- {
- t.stop();
- Vector r;
- this->hik_kernel_multiply_fast(Tlookup, _q, _alpha, r);
- r = r - _y;
- double res = r.normL2();
- double resMax = r.normInf();
- std::cerr << "SimpleGradientDescent: TIME " << t.getSum() << " " << res << " " << resMax << std::endl;
- t.start();
- }
- for ( uint i = 0; i < sizeOfRandomSubset; i++)
- {
- pseudoResidual(perm[i]) = -_y(perm[i]) + (this->d_noise * _alpha(perm[i]));
- for (uint j = 0; j < this->ui_d; j++)
- {
- x_i = this->X_sorted(j,perm[i]);
- pseudoResidual(perm[i]) += Tlookup[j*hmax + _q->quantize( x_i, j )];
- }
-
- if ( fabs(pseudoResidual(perm[i])) > 1e-7 )
- {
- alpha_old = _alpha(perm[i]);
- alpha_new = alpha_old - (pseudoResidual(perm[i])/diagonalElements(perm[i]));
- _alpha(perm[i]) = alpha_new;
- delta_alpha(perm[i]) = alpha_old-alpha_new;
- this->hikUpdateLookupTable(Tlookup, alpha_new, alpha_old, perm[i], _q, _pf );
- } else
- {
- delta_alpha(perm[i]) = 0.0;
- }
- }
-
-
- double delta = delta_alpha.normL2();
- if ( this->b_verbose ) {
- cerr << "FastMinKernel::solveLin: iteration " << iter << " / " << _maxIterations << endl;
- cerr << "FastMinKernel::solveLin: delta = " << delta << endl;
- cerr << "FastMinKernel::solveLin: pseudo residual = " << pseudoResidual.scalarProduct(pseudoResidual) << endl;
- }
- if ( delta < _minDelta )
- {
- if ( this->b_verbose )
- cerr << "FastMinKernel::solveLin: small delta" << endl;
- break;
- }
- }
- }
- else
- {
-
-
- for ( iter = 1; iter <= _maxIterations; iter++ )
- {
- for ( uint i = 0; i < _y.size(); i++ )
- {
- pseudoResidual(i) = -_y(i) + (this->d_noise* _alpha(i));
- for (uint j = 0; j < this->ui_d; j++)
- {
- x_i = this->X_sorted(j,i);
- pseudoResidual(i) += Tlookup[j*hmax + _q->quantize( x_i, j )];
- }
-
- if ( fabs(pseudoResidual(i)) > 1e-7 )
- {
- alpha_old = _alpha(i);
- alpha_new = alpha_old - (pseudoResidual(i)/diagonalElements(i));
- _alpha(i) = alpha_new;
- delta_alpha(i) = alpha_old-alpha_new;
- this->hikUpdateLookupTable(Tlookup, alpha_new, alpha_old, i, _q, _pf );
- } else
- {
- delta_alpha(i) = 0.0;
- }
- }
- double delta = delta_alpha.normL2();
- if ( this->b_verbose ) {
- std::cerr << "FastMinKernel::solveLin: iteration " << iter << " / " << _maxIterations << std::endl;
- std::cerr << "FastMinKernel::solveLin: delta = " << delta << std::endl;
- std::cerr << "FastMinKernel::solveLin: pseudo residual = " << pseudoResidual.scalarProduct(pseudoResidual) << std::endl;
- }
- if ( delta < _minDelta )
- {
- if ( this->b_verbose )
- std::cerr << "FastMinKernel::solveLin: small delta" << std::endl;
- break;
- }
- }
- }
- if (verboseMinimal)
- std::cerr << "FastMinKernel::solveLin -- needed " << iter << " iterations" << std::endl;
- return Tlookup;
- }
- void FastMinKernel::randomPermutation(NICE::Vector & _permutation,
- const std::vector<uint> & _oldIndices,
- const uint & _newSize
- ) const
- {
- std::vector<uint> indices(_oldIndices);
- const uint oldSize = _oldIndices.size();
- uint resultingSize (std::min( oldSize, _newSize) );
- _permutation.resize(resultingSize);
- for ( uint i = 0; i < resultingSize; i++)
- {
- uint newIndex(rand() % indices.size());
- _permutation[i] = indices[newIndex ];
- indices.erase(indices.begin() + newIndex);
- }
- }
- double FastMinKernel::getFrobNormApprox()
- {
- double frobNormApprox(0.0);
- switch (this->approxScheme)
- {
- case MEDIAN:
- {
-
-
- for ( uint i = 0; i < this->ui_d; i++ )
- {
- double median = this->X_sorted.getFeatureValues(i).getMedian();
- frobNormApprox += median;
- }
- frobNormApprox = fabs(frobNormApprox) * this->ui_n/2.0;
- break;
- }
- case EXPECTATION:
- {
- std::cerr << "EXPECTATION" << std::endl;
-
-
-
- NICE::Vector diagEl;
- X_sorted.hikDiagonalElements(diagEl);
- frobNormApprox += diagEl.normL2();
-
- double secondTerm(0.0);
- for ( uint i = 0; i < this->ui_d; i++ )
- {
- double minInDim;
- minInDim = this->X_sorted.getFeatureValues(i).getMin();
- double maxInDim;
- maxInDim = this->X_sorted.getFeatureValues(i).getMax();
- std::cerr << "min: " << minInDim << " max: " << maxInDim << std::endl;
- secondTerm += 2.0*minInDim + maxInDim;
- }
- secondTerm /= 3.0;
- secondTerm = pow(secondTerm, 2);
- secondTerm *= (this->ui_n * ( this->ui_n - 1 ));
- frobNormApprox += secondTerm;
- frobNormApprox = sqrt(frobNormApprox);
- break;
- }
- default:
- {
- break;
- }
- }
- return frobNormApprox;
- }
- void FastMinKernel::setApproximationScheme(const int & _approxScheme)
- {
- switch(_approxScheme)
- {
- case 0:
- {
- this->approxScheme = MEDIAN;
- break;
- }
- case 1:
- {
- this->approxScheme = EXPECTATION;
- break;
- }
- default:
- {
- this->approxScheme = MEDIAN;
- break;
- }
- }
- }
- void FastMinKernel::hikPrepareKVNApproximation(NICE::VVector & _A) const
- {
- _A.resize( this->ui_d );
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- for ( uint i = 0; i < this->ui_d; i++ )
- {
- uint numNonZero = this->X_sorted.getNumberOfNonZeroElementsPerDimension(i);
- _A[i].resize( numNonZero );
- }
-
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- double squared_sum(0.0);
- uint cntNonzeroFeat(0);
- const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
-
- for ( SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin(); i != nonzeroElements.end(); i++ )
- {
- const SortedVectorSparse<double>::dataelement & de = i->second;
-
- double elem( de.second );
- squared_sum += pow( elem, 2 );
- _A[dim][cntNonzeroFeat] = squared_sum;
- cntNonzeroFeat++;
- }
- }
- }
- double * FastMinKernel::hikPrepareKVNApproximationFast(NICE::VVector & _A,
- const Quantization * _q,
- const ParameterizedFunction *_pf ) const
- {
-
-
- uint hmax = _q->getNumberOfBins();
-
- double *prototypes = new double [ hmax * this->ui_d ];
- double * p_prototypes = prototypes;
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- for ( uint i = 0 ; i < hmax ; i++ )
- {
- if ( _pf != NULL )
- {
- *p_prototypes = _pf->f ( dim, _q->getPrototype( i, dim ) );
- } else
- {
- *p_prototypes = _q->getPrototype( i, dim );
- }
- p_prototypes++;
- }
- }
-
-
- double *Tlookup = new double [ hmax * this->ui_d ];
-
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
- if ( nrZeroIndices == this->ui_n )
- continue;
- const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
- SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin();
- SortedVectorSparse<double>::const_elementpointer iPredecessor = nonzeroElements.begin();
-
- uint index = 0;
-
-
- uint qBin = _q->quantize ( i->first, dim );
-
-
-
-
- for (uint j = 0; j < hmax; j++)
- {
- double fval = prototypes[ dim*hmax + j];
- double t;
- if ( (index == 0) && (j < qBin) ) {
-
-
- t = pow( fval, 2 ) * (this->ui_n-nrZeroIndices-index);
- } else {
-
- while ( (j >= qBin) && ( index < (this->ui_n-nrZeroIndices)) )
- {
- index++;
- iPredecessor = i;
- i++;
- if ( i->first != iPredecessor->first )
- qBin = _q->quantize ( i->first, dim );
- }
-
-
-
- if ( (j >= qBin) && ( index==(this->ui_n-1-nrZeroIndices) ) ) {
-
-
- t = _A[dim][index];
- } else {
-
- t = _A[dim][index-1] + pow( fval, 2 ) * (this->ui_n-nrZeroIndices-(index) );
- }
- }
- Tlookup[ dim*hmax + j ] = t;
- }
- }
- delete [] prototypes;
- return Tlookup;
- }
- double* FastMinKernel::hikPrepareLookupTableForKVNApproximation(const Quantization * _q,
- const ParameterizedFunction *_pf
- ) const
- {
-
- uint hmax = _q->getNumberOfBins();
-
- double *prototypes = new double [ hmax * this->ui_d ];
- double * p_prototypes = prototypes;
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- for ( uint i = 0 ; i < hmax ; i++ )
- {
- if ( _pf != NULL )
- {
- *p_prototypes = _pf->f ( dim, _q->getPrototype( i, dim ) );
- } else
- {
- *p_prototypes = _q->getPrototype( i, dim );
- }
- p_prototypes++;
- }
- }
-
-
- double *Tlookup = new double [ hmax * this->ui_d ];
-
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
- if ( nrZeroIndices == this->ui_n )
- continue;
- const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
- SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin();
- SortedVectorSparse<double>::const_elementpointer iPredecessor = nonzeroElements.begin();
-
- uint index = 0;
-
- uint qBin = _q->quantize ( i->first, dim );
- double sum(0.0);
- for (uint j = 0; j < hmax; j++)
- {
- double fval = prototypes[ dim*hmax + j];
- double t;
- if ( (index == 0) && (j < qBin) ) {
-
-
- t = pow( fval, 2 ) * (this->ui_n-nrZeroIndices-index);
- } else {
-
- while ( (j >= qBin) && ( index < (this->ui_n-nrZeroIndices)) )
- {
- sum += pow( i->second.second, 2 );
- index++;
- iPredecessor = i;
- i++;
- if ( i->first != iPredecessor->first )
- qBin = _q->quantize ( i->first, dim );
- }
-
-
-
- if ( (j >= qBin) && ( index==(this->ui_n-1-nrZeroIndices) ) ) {
-
-
- t = sum;
- } else {
-
- t = sum + pow( fval, 2 ) * (this->ui_n-nrZeroIndices-(index) );
- }
- }
- Tlookup[ dim*hmax + j ] = t;
- }
- }
- delete [] prototypes;
- return Tlookup;
- }
-
-
-
- void FastMinKernel::hikComputeKVNApproximation(const NICE::VVector & _A,
- const NICE::SparseVector & _xstar,
- double & _norm,
- const ParameterizedFunction *_pf )
- {
- _norm = 0.0;
- for (SparseVector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++)
- {
- uint dim = i->first;
- double fval = i->second;
- uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
- if ( nrZeroIndices == this->ui_n ) {
-
- continue;
- }
- uint position;
-
-
-
- this->X_sorted.findFirstLargerInDimension(dim, fval, position);
- bool posIsZero ( position == 0 );
- if ( !posIsZero )
- {
- position--;
- }
-
- double firstPart(0.0);
-
-
- if ( !posIsZero && ((position-nrZeroIndices) < this->ui_n) )
- firstPart = (_A[dim][position-nrZeroIndices]);
- if ( _pf != NULL )
- fval = _pf->f ( dim, fval );
- fval = fval * fval;
- double secondPart( 0.0);
- if ( !posIsZero )
- secondPart = fval * (this->ui_n-nrZeroIndices-(position+1));
- else
- secondPart = fval * (this->ui_n-nrZeroIndices);
-
- _norm += firstPart + secondPart;
- }
- }
- void FastMinKernel::hikComputeKVNApproximationFast(const double *_Tlookup,
- const Quantization * _q,
- const NICE::SparseVector & _xstar,
- double & _norm
- ) const
- {
- _norm = 0.0;
-
- for (SparseVector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++ )
- {
- uint dim = i->first;
- double v = i->second;
-
-
- uint qBin = _q->quantize( v, dim );
- _norm += _Tlookup[dim*_q->getNumberOfBins() + qBin];
- }
- }
- void FastMinKernel::hikComputeKernelVector ( const NICE::SparseVector& _xstar,
- NICE::Vector & _kstar
- ) const
- {
-
- _kstar.resize( this->ui_n );
- _kstar.set(0.0);
- if ( this->b_debug )
- {
- std::cerr << " FastMinKernel::hikComputeKernelVector -- input: " << std::endl;
- _xstar.store( std::cerr);
- }
-
- for (SparseVector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++)
- {
- uint dim = i->first;
- double fval = i->second;
- if ( this->b_debug )
- std::cerr << "dim: " << dim << " fval: " << fval << std::endl;
- uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
- if ( nrZeroIndices == this->ui_n ) {
-
- continue;
- }
- uint position;
-
-
-
- this->X_sorted.findFirstLargerInDimension(dim, fval, position);
-
- if ( this->b_debug )
- std::cerr << " position: " << position << std::endl;
-
- const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
-
- uint count(nrZeroIndices);
- for ( SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin(); i != nonzeroElements.end(); i++, count++ )
- {
- uint origIndex(i->second.first);
- if ( this->b_debug )
- std::cerr << "i->1.2: " << i->second.first << " origIndex: " << origIndex << " count: " << count << " position: " << position << std::endl;
- if (count < position)
- _kstar[origIndex] += i->first;
- else
- _kstar[origIndex] += fval;
- }
- }
- }
-
-
-
- void FastMinKernel::hikComputeKVNApproximation(const NICE::VVector & _A,
- const NICE::Vector & _xstar,
- double & _norm,
- const ParameterizedFunction *_pf )
- {
- _norm = 0.0;
- uint dim ( 0 );
- for (Vector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++, dim++)
- {
- double fval = *i;
- uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
- if ( nrZeroIndices == this->ui_n ) {
-
- continue;
- }
- uint position;
-
-
-
- this->X_sorted.findFirstLargerInDimension(dim, fval, position);
- bool posIsZero ( position == 0 );
- if ( !posIsZero )
- {
- position--;
- }
-
- double firstPart(0.0);
-
-
- if ( !posIsZero && ((position-nrZeroIndices) < this->ui_n) )
- firstPart = (_A[dim][position-nrZeroIndices]);
- double secondPart( 0.0);
- if ( _pf != NULL )
- fval = _pf->f ( dim, fval );
- fval = fval * fval;
- if ( !posIsZero )
- secondPart = fval * (this->ui_n-nrZeroIndices-(position+1));
- else
- secondPart = fval * (this->ui_n-nrZeroIndices);
-
- _norm += firstPart + secondPart;
- }
- }
- void FastMinKernel::hikComputeKVNApproximationFast(const double *_Tlookup,
- const Quantization * _q,
- const NICE::Vector & _xstar,
- double & _norm
- ) const
- {
- _norm = 0.0;
-
- uint dim ( 0 );
- for ( NICE::Vector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++, dim++ )
- {
- double v = *i;
-
-
- uint qBin = _q->quantize( v, dim );
- _norm += _Tlookup[dim*_q->getNumberOfBins() + qBin];
- }
- }
- void FastMinKernel::hikComputeKernelVector( const NICE::Vector & _xstar,
- NICE::Vector & _kstar) const
- {
-
- _kstar.resize(this->ui_n);
- _kstar.set(0.0);
-
- uint dim ( 0 );
- for (NICE::Vector::const_iterator i = _xstar.begin(); i != _xstar.end(); i++, dim++)
- {
- double fval = *i;
- uint nrZeroIndices = this->X_sorted.getNumberOfZeroElementsPerDimension(dim);
- if ( nrZeroIndices == this->ui_n ) {
-
- continue;
- }
- uint position;
-
-
-
- this->X_sorted.findFirstLargerInDimension(dim, fval, position);
-
-
- const multimap< double, SortedVectorSparse<double>::dataelement> & nonzeroElements = this->X_sorted.getFeatureValues(dim).nonzeroElements();
-
- uint count(nrZeroIndices);
- for ( SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin(); i != nonzeroElements.end(); i++, count++ )
- {
- uint origIndex(i->second.first);
- if (count < position)
- _kstar[origIndex] += i->first;
- else
- _kstar[origIndex] += fval;
- }
- }
- }
- void FastMinKernel::restore ( std::istream & _is,
- int _format )
- {
- bool b_restoreVerbose ( false );
- if ( _is.good() )
- {
- if ( b_restoreVerbose )
- std::cerr << " restore FastMinKernel" << std::endl;
- std::string tmp;
- _is >> tmp;
- if ( ! this->isStartTag( tmp, "FastMinKernel" ) )
- {
- std::cerr << " WARNING - attempt to restore FastMinKernel, but start flag " << tmp << " does not match! Aborting... " << std::endl;
- throw;
- }
- _is.precision (numeric_limits<double>::digits10 + 1);
- bool b_endOfBlock ( false ) ;
- while ( !b_endOfBlock )
- {
- _is >> tmp;
- if ( this->isEndTag( tmp, "FastMinKernel" ) )
- {
- b_endOfBlock = true;
- continue;
- }
- tmp = this->removeStartTag ( tmp );
- if ( b_restoreVerbose )
- std::cerr << " currently restore section " << tmp << " in FastMinKernel" << std::endl;
- if ( tmp.compare("ui_n") == 0 )
- {
- _is >> this->ui_n;
- _is >> tmp;
- tmp = this->removeEndTag ( tmp );
- }
- else if ( tmp.compare("ui_d") == 0 )
- {
- _is >> this->ui_d;
- _is >> tmp;
- tmp = this->removeEndTag ( tmp );
- }
- else if ( tmp.compare("d_noise") == 0 )
- {
- _is >> this->d_noise;
- _is >> tmp;
- tmp = this->removeEndTag ( tmp );
- }
- else if ( tmp.compare("approxScheme") == 0 )
- {
- int approxSchemeInt;
- _is >> approxSchemeInt;
- setApproximationScheme(approxSchemeInt);
- _is >> tmp;
- tmp = this->removeEndTag ( tmp );
- }
- else if ( tmp.compare("X_sorted") == 0 )
- {
- this->X_sorted.restore(_is,_format);
- _is >> tmp;
- tmp = this->removeEndTag ( tmp );
- }
- else
- {
- std::cerr << "WARNING -- unexpected FastMinKernel object -- " << tmp << " -- for restoration... aborting" << std::endl;
- throw;
- }
- }
- }
- else
- {
- std::cerr << "FastMinKernel::restore -- InStream not initialized - restoring not possible!" << std::endl;
- }
- }
- void FastMinKernel::store ( std::ostream & _os,
- int _format
- ) const
- {
- if (_os.good())
- {
-
- _os << this->createStartTag( "FastMinKernel" ) << std::endl;
- _os.precision (numeric_limits<double>::digits10 + 1);
- _os << this->createStartTag( "ui_n" ) << std::endl;
- _os << this->ui_n << std::endl;
- _os << this->createEndTag( "ui_n" ) << std::endl;
- _os << this->createStartTag( "ui_d" ) << std::endl;
- _os << this->ui_d << std::endl;
- _os << this->createEndTag( "ui_d" ) << std::endl;
- _os << this->createStartTag( "d_noise" ) << std::endl;
- _os << this->d_noise << std::endl;
- _os << this->createEndTag( "d_noise" ) << std::endl;
- _os << this->createStartTag( "approxScheme" ) << std::endl;
- _os << this->approxScheme << std::endl;
- _os << this->createEndTag( "approxScheme" ) << std::endl;
- _os << this->createStartTag( "X_sorted" ) << std::endl;
-
- this->X_sorted.store(_os, _format);
- _os << this->createEndTag( "X_sorted" ) << std::endl;
-
- _os << this->createEndTag( "FastMinKernel" ) << std::endl;
- }
- else
- {
- std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
- }
- }
- void FastMinKernel::clear ()
- {
- std::cerr << "FastMinKernel clear-function called" << std::endl;
- }
- void FastMinKernel::addExample( const NICE::SparseVector * _example,
- const double & _label,
- const bool & _performOptimizationAfterIncrement
- )
- {
-
- this->addExample ( _example );
- }
- void FastMinKernel::addMultipleExamples( const std::vector< const NICE::SparseVector * > & _newExamples,
- const NICE::Vector & _newLabels,
- const bool & _performOptimizationAfterIncrement
- )
- {
-
- this->addMultipleExamples ( _newExamples );
- }
- void FastMinKernel::addExample( const NICE::SparseVector * _example,
- const NICE::ParameterizedFunction *_pf
- )
- {
- this->X_sorted.add_feature( *_example, _pf );
- this->ui_n++;
- }
- void FastMinKernel::addMultipleExamples( const std::vector< const NICE::SparseVector * > & _newExamples,
- const NICE::ParameterizedFunction *_pf
- )
- {
- for ( std::vector< const NICE::SparseVector * >::const_iterator exIt = _newExamples.begin();
- exIt != _newExamples.end();
- exIt++ )
- {
- this->X_sorted.add_feature( **exIt, _pf );
- this->ui_n++;
- }
- }
|