1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528 |
- /**
- * @file FMKGPHyperparameterOptimization.cpp
- * @brief Heart of the framework to set up everything, perform optimization, classification, and variance prediction (Implementation)
- * @author Erik Rodner, Alexander Freytag
- * @date 01/02/2012
- */
- // STL includes
- #include <iostream>
- #include <map>
- // NICE-core includes
- #include <core/algebra/ILSConjugateGradients.h>
- #include <core/algebra/ILSConjugateGradientsLanczos.h>
- #include <core/algebra/ILSSymmLqLanczos.h>
- #include <core/algebra/ILSMinResLanczos.h>
- #include <core/algebra/ILSPlainGradient.h>
- #include <core/algebra/EigValuesTRLAN.h>
- #include <core/algebra/CholeskyRobust.h>
- //
- #include <core/basics/Timer.h>
- #include <core/basics/ResourceStatistics.h>
- #include <core/basics/Exception.h>
- //
- #include <core/vector/Algorithms.h>
- #include <core/vector/Eigen.h>
- //
- #include <core/optimization/blackbox/DownhillSimplexOptimizer.h>
- // gp-hik-core includes
- #include "FMKGPHyperparameterOptimization.h"
- #include "FastMinKernel.h"
- #include "GMHIKernel.h"
- #include "IKMNoise.h"
- using namespace NICE;
- using namespace std;
- FMKGPHyperparameterOptimization::FMKGPHyperparameterOptimization()
- {
- // initialize pointer variables
- pf = NULL;
- eig = NULL;
- linsolver = NULL;
- fmk = NULL;
- q = NULL;
- precomputedTForVarEst = NULL;
- ikmsum = NULL;
-
- // initialize boolean flags
- verbose = false;
- verboseTime = false;
- debug = false;
-
- //stupid unneeded default values
- binaryLabelPositive = -1;
- binaryLabelNegative = -2;
- }
- FMKGPHyperparameterOptimization::FMKGPHyperparameterOptimization ( const Config *_conf, ParameterizedFunction *_pf, FastMinKernel *_fmk, const string & _confSection )
- {
- // initialize pointer variables
- pf = NULL;
- eig = NULL;
- linsolver = NULL;
- fmk = NULL;
- q = NULL;
- precomputedTForVarEst = NULL;
- ikmsum = NULL;
-
- //stupid unneeded default values
- binaryLabelPositive = -1;
- binaryLabelNegative = -2;
- knownClasses.clear();
- //TODO
- if ( _fmk == NULL )
- this->initialize ( _conf, _pf ); //then the confSection is also the default value
- //TODO not needed anymore, only for backword compatibility
- // else if ( _confSection.compare ( "HIKGP" ) == 0 )
- // this->initialize ( _conf, _pf, _fmk );
- else
- this->initialize ( _conf, _pf, _fmk, _confSection );
- }
- FMKGPHyperparameterOptimization::~FMKGPHyperparameterOptimization()
- {
- //pf will delete from outer program
- if ( this->eig != NULL )
- delete this->eig;
- if ( this->linsolver != NULL )
- delete this->linsolver;
- if ( this->fmk != NULL )
- delete this->fmk;
- if ( this->q != NULL )
- delete this->q;
- for ( uint i = 0 ; i < precomputedT.size(); i++ )
- delete [] ( precomputedT[i] );
- if ( precomputedTForVarEst != NULL )
- delete precomputedTForVarEst;
- if ( ikmsum != NULL )
- delete ikmsum;
- }
- void FMKGPHyperparameterOptimization::initialize ( const Config *_conf, ParameterizedFunction *_pf, FastMinKernel *_fmk, const std::string & _confSection )
- {
- if ( this->fmk != NULL )
- {
- std::cerr << "fmk deleted" << std::endl;
- delete this->fmk;
- }
- if ( _fmk != NULL )
- {
- std::cerr << "fmk copied" << std::endl;
- this->fmk = _fmk;
- }
-
- this->pf = _pf;
- std::cerr << "pf copied" << std::endl;
-
-
- std::cerr << "------------" << std::endl;
- std::cerr << "| set-up |" << std::endl;
- std::cerr << "------------" << std::endl;
- this->eig = new EVArnoldi ( _conf->gB ( _confSection, "eig_verbose", false ) /* verbose flag */, 10 );
- // this->eig = new EigValuesTRLAN();
- // My time measurements show that both methods use equal time, a comparision
- // of their numerical performance has not been done yet
- this->parameterUpperBound = _conf->gD ( _confSection, "parameter_upper_bound", 2.5 );
- this->parameterLowerBound = _conf->gD ( _confSection, "parameter_lower_bound", 1.0 );
- this->parameterStepSize = _conf->gD ( _confSection, "parameter_step_size", 0.1 );
- this->verifyApproximation = _conf->gB ( _confSection, "verify_approximation", false );
- this->nrOfEigenvaluesToConsider = _conf->gI ( _confSection, "nrOfEigenvaluesToConsider", 1 );
- this->nrOfEigenvaluesToConsiderForVarApprox = _conf->gI ( _confSection, "nrOfEigenvaluesToConsiderForVarApprox", 2 );
- this->verbose = _conf->gB ( _confSection, "verbose", false );
- this->verboseTime = _conf->gB ( _confSection, "verboseTime", false );
- this->debug = _conf->gB ( _confSection, "debug", false );
- bool useQuantization = _conf->gB ( _confSection, "use_quantization", false );
- std::cerr << "_confSection: " << _confSection << std::endl;
- std::cerr << "use_quantization: " << useQuantization << std::endl;
- if ( _conf->gB ( _confSection, "use_quantization", false ) ) {
- int numBins = _conf->gI ( _confSection, "num_bins", 100 );
- if ( verbose )
- cerr << "FMKGPHyperparameterOptimization: quantization initialized with " << numBins << " bins." << endl;
- this->q = new Quantization ( numBins );
- } else {
- this->q = NULL;
- }
- bool ils_verbose = _conf->gB ( _confSection, "ils_verbose", false );
- ils_max_iterations = _conf->gI ( _confSection, "ils_max_iterations", 1000 );
- if ( verbose )
- cerr << "FMKGPHyperparameterOptimization: maximum number of iterations is " << ils_max_iterations << endl;
- double ils_min_delta = _conf->gD ( _confSection, "ils_min_delta", 1e-7 );
- double ils_min_residual = _conf->gD ( _confSection, "ils_min_residual", 1e-7/*1e-2 */ );
- string ils_method = _conf->gS ( _confSection, "ils_method", "CG" );
- if ( ils_method.compare ( "CG" ) == 0 )
- {
- if ( verbose )
- std::cerr << "We use CG with " << ils_max_iterations << " iterations, " << ils_min_delta << " as min delta, and " << ils_min_residual << " as min res " << std::endl;
- this->linsolver = new ILSConjugateGradients ( ils_verbose , ils_max_iterations, ils_min_delta, ils_min_residual );
- if ( verbose )
- cerr << "FMKGPHyperparameterOptimization: using ILS ConjugateGradients" << endl;
- }
- else if ( ils_method.compare ( "CGL" ) == 0 )
- {
- this->linsolver = new ILSConjugateGradientsLanczos ( ils_verbose , ils_max_iterations );
- if ( verbose )
- cerr << "FMKGPHyperparameterOptimization: using ILS ConjugateGradients (Lanczos)" << endl;
- }
- else if ( ils_method.compare ( "SYMMLQ" ) == 0 )
- {
- this->linsolver = new ILSSymmLqLanczos ( ils_verbose , ils_max_iterations );
- if ( verbose )
- cerr << "FMKGPHyperparameterOptimization: using ILS SYMMLQ" << endl;
- }
- else if ( ils_method.compare ( "MINRES" ) == 0 )
- {
- this->linsolver = new ILSMinResLanczos ( ils_verbose , ils_max_iterations );
- if ( verbose )
- cerr << "FMKGPHyperparameterOptimization: using ILS MINRES" << endl;
- }
- else
- {
- cerr << "FMKGPHyperparameterOptimization: " << _confSection << ":ils_method (" << ils_method << ") does not match any type (CG,CGL,SYMMLQ,MINRES), I will use CG" << endl;
- this->linsolver = new ILSConjugateGradients ( ils_verbose , ils_max_iterations, ils_min_delta, ils_min_residual );
- }
-
- string optimizationMethod_s = _conf->gS ( _confSection, "optimization_method", "greedy" );
- if ( optimizationMethod_s == "greedy" )
- optimizationMethod = OPT_GREEDY;
- else if ( optimizationMethod_s == "downhillsimplex" )
- optimizationMethod = OPT_DOWNHILLSIMPLEX;
- else if ( optimizationMethod_s == "none" )
- optimizationMethod = OPT_NONE;
- else
- fthrow ( Exception, "Optimization method " << optimizationMethod_s << " is not known." );
- if ( verbose )
- cerr << "Using optimization method: " << optimizationMethod_s << endl;
- downhillSimplexMaxIterations = _conf->gI ( _confSection, "downhillsimplex_max_iterations", 20 );
- // do not run longer than a day :)
- downhillSimplexTimeLimit = _conf->gD ( _confSection, "downhillsimplex_time_limit", 24 * 60 * 60 );
- downhillSimplexParamTol = _conf->gD ( _confSection, "downhillsimplex_delta", 0.01 );
- optimizeNoise = _conf->gB ( _confSection, "optimize_noise", false );
- if ( verbose )
- cerr << "Optimize noise: " << ( optimizeNoise ? "on" : "off" ) << endl;
-
- std::cerr << "------------" << std::endl;
- std::cerr << "| start |" << std::endl;
- std::cerr << "------------" << std::endl;
- }
- // get and set methods
- void FMKGPHyperparameterOptimization::setParameterUpperBound ( const double & _parameterUpperBound )
- {
- parameterUpperBound = _parameterUpperBound;
- }
- void FMKGPHyperparameterOptimization::setParameterLowerBound ( const double & _parameterLowerBound )
- {
- parameterLowerBound = _parameterLowerBound;
- }
- std::set<int> FMKGPHyperparameterOptimization::getKnownClassNumbers ( ) const
- {
- return this->knownClasses;
- }
- //high level methods
- void FMKGPHyperparameterOptimization::setupGPLikelihoodApprox ( GPLikelihoodApprox * & gplike, const std::map<int, NICE::Vector> & binaryLabels, uint & parameterVectorSize )
- {
- gplike = new GPLikelihoodApprox ( binaryLabels, ikmsum, linsolver, eig, verifyApproximation, nrOfEigenvaluesToConsider );
- gplike->setDebug( debug );
- gplike->setVerbose( verbose );
- parameterVectorSize = ikmsum->getNumParameters();
- }
- void FMKGPHyperparameterOptimization::updateEigenDecomposition( const int & i_noEigenValues )
- {
- //compute the largest eigenvalue of K + noise
- try
- {
- eig->getEigenvalues ( *ikmsum, eigenMax, eigenMaxVectors, i_noEigenValues );
- }
- catch ( char const* exceptionMsg)
- {
- std::cerr << exceptionMsg << std::endl;
- throw("Problem in calculating Eigendecomposition of kernel matrix. Abort program...");
- }
-
- // EigenValue computation does not necessarily extract them in decreasing order.
- // Therefore: sort eigenvalues decreasingly!
- NICE::VectorT< int > ewPermutation;
- eigenMax.sortDescend ( ewPermutation );
-
-
- }
- void FMKGPHyperparameterOptimization::performOptimization ( GPLikelihoodApprox & gplike, const uint & parameterVectorSize )
- {
- if (verbose)
- std::cerr << "perform optimization" << std::endl;
-
- if ( optimizationMethod == OPT_GREEDY )
- {
- if ( verbose )
- std::cerr << "OPT_GREEDY!!! " << std::endl;
-
- // simple greedy strategy
- if ( ikmsum->getNumParameters() != 1 )
- fthrow ( Exception, "Reduce size of the parameter vector or use downhill simplex!" );
- NICE::Vector lB = ikmsum->getParameterLowerBounds();
- NICE::Vector uB = ikmsum->getParameterUpperBounds();
-
- if ( verbose )
- cerr << "lower bound " << lB << " upper bound " << uB << " parameterStepSize: " << parameterStepSize << endl;
-
- NICE::Vector tmp = gplike.getBestParameters( );
-
- for ( double mypara = lB[0]; mypara <= uB[0]; mypara += this->parameterStepSize )
- {
- OPTIMIZATION::matrix_type hyperp ( 1, 1, mypara );
- gplike.evaluate ( hyperp );
- }
- }
- else if ( optimizationMethod == OPT_DOWNHILLSIMPLEX )
- {
- //standard as before, normal optimization
- if ( verbose )
- std::cerr << "DOWNHILLSIMPLEX WITHOUT BALANCED LEARNING!!! " << std::endl;
- // downhill simplex strategy
- OPTIMIZATION::DownhillSimplexOptimizer optimizer;
- OPTIMIZATION::matrix_type initialParams ( parameterVectorSize, 1 );
- NICE::Vector currentParameters;
- ikmsum->getParameters ( currentParameters );
- for ( uint i = 0 ; i < parameterVectorSize; i++ )
- initialParams(i,0) = currentParameters[ i ];
- if ( verbose )
- std::cerr << "Initial parameters: " << initialParams << std::endl;
- //the scales object does not really matter in the actual implementation of Downhill Simplex
- //OPTIMIZATION::matrix_type scales ( parameterVectorSize, 1);
- //cales.Set(1.0);
-
- OPTIMIZATION::SimpleOptProblem optProblem ( &gplike, initialParams, initialParams /* scales*/ );
- // cerr << "OPT: " << mypara << " " << nlikelihood << " " << logdet << " " << dataterm << endl;
- optimizer.setMaxNumIter ( true, downhillSimplexMaxIterations );
- optimizer.setTimeLimit ( true, downhillSimplexTimeLimit );
- optimizer.setParamTol ( true, downhillSimplexParamTol );
- optimizer.optimizeProb ( optProblem );
- }
- else if ( optimizationMethod == OPT_NONE )
- {
- if ( verbose )
- std::cerr << "NO OPTIMIZATION!!! " << std::endl;
- // without optimization
- if ( optimizeNoise )
- fthrow ( Exception, "Deactivate optimize_noise!" );
-
- if ( verbose )
- std::cerr << "Optimization is deactivated!" << std::endl;
-
- double value (1.0);
- if ( this->parameterLowerBound == this->parameterUpperBound)
- value = this->parameterLowerBound;
- pf->setParameterLowerBounds ( NICE::Vector ( 1, value ) );
- pf->setParameterUpperBounds ( NICE::Vector ( 1, value ) );
- // we use the standard value
- OPTIMIZATION::matrix_type hyperp ( 1, 1, value );
- gplike.setParameterLowerBound ( value );
- gplike.setParameterUpperBound ( value );
- //we do not need to compute the likelihood here - we are only interested in directly obtaining alpha vectors
- gplike.computeAlphaDirect( hyperp, eigenMax );
- }
- if ( verbose )
- std::cerr << "Optimal hyperparameter was: " << gplike.getBestParameters() << std::endl;
-
- std::map<int, Vector> bestAlphas = gplike.getBestAlphas();
- std::cerr << "length of alpha vectors: " << bestAlphas.size() << std::endl;
- std::cerr << "alpha vector: " << bestAlphas.begin()->second << std::endl;
- }
- void FMKGPHyperparameterOptimization::transformFeaturesWithOptimalParameters ( const GPLikelihoodApprox & gplike, const uint & parameterVectorSize )
- {
- // transform all features with the "optimal" parameter
- ikmsum->setParameters ( gplike.getBestParameters() );
- }
- void FMKGPHyperparameterOptimization::computeMatricesAndLUTs ( const GPLikelihoodApprox & gplike )
- {
- precomputedA.clear();
- precomputedB.clear();
- for ( std::map<int, NICE::Vector>::const_iterator i = gplike.getBestAlphas().begin(); i != gplike.getBestAlphas().end(); i++ )
- {
- PrecomputedType A;
- PrecomputedType B;
- fmk->hik_prepare_alpha_multiplications ( i->second, A, B );
- A.setIoUntilEndOfFile ( false );
- B.setIoUntilEndOfFile ( false );
- precomputedA[ i->first ] = A;
- precomputedB[ i->first ] = B;
- if ( q != NULL )
- {
- double *T = fmk->hik_prepare_alpha_multiplications_fast ( A, B, *q, pf );
- //just to be sure that we do not waste space here
- if ( precomputedT[ i->first ] != NULL )
- delete precomputedT[ i->first ];
-
- precomputedT[ i->first ] = T;
- }
-
- //TODO update the variance-related matrices as well here - currently it is done before in the outer method!!!
- }
- }
- #ifdef NICE_USELIB_MATIO
- void FMKGPHyperparameterOptimization::optimizeBinary ( const sparse_t & data, const NICE::Vector & yl, const std::set<int> & positives, const std::set<int> & negatives, double noise )
- {
- map<int, int> examples;
- Vector y ( yl.size() );
- int ind = 0;
- for ( uint i = 0 ; i < yl.size(); i++ )
- {
- if ( positives.find ( i ) != positives.end() ) {
- y[ examples.size() ] = 1.0;
- examples.insert ( pair<int, int> ( i, ind ) );
- ind++;
- } else if ( negatives.find ( i ) != negatives.end() ) {
- y[ examples.size() ] = -1.0;
- examples.insert ( pair<int, int> ( i, ind ) );
- ind++;
- }
- }
- y.resize ( examples.size() );
- cerr << "Examples: " << examples.size() << endl;
- optimize ( data, y, examples, noise );
- }
- void FMKGPHyperparameterOptimization::optimize ( const sparse_t & data, const NICE::Vector & y, const std::map<int, int> & examples, double noise )
- {
- Timer t;
- t.start();
- cerr << "Initializing data structure ..." << std::endl;
- if ( fmk != NULL ) delete fmk;
- fmk = new FastMinKernel ( data, noise, examples );
- t.stop();
- if (verboseTime)
- std::cerr << "Time used for initializing the FastMinKernel structure: " << t.getLast() << std::endl;
-
- optimize ( y );
- }
- #endif
- int FMKGPHyperparameterOptimization::prepareBinaryLabels ( std::map<int, NICE::Vector> & binaryLabels, const NICE::Vector & y , std::set<int> & myClasses )
- {
- myClasses.clear();
-
- // determine which classes we have in our label vector
- // -> MATLAB: myClasses = unique(y);
- for ( NICE::Vector::const_iterator it = y.begin(); it != y.end(); it++ )
- {
- if ( myClasses.find ( *it ) == myClasses.end() )
- {
- myClasses.insert ( *it );
- }
- }
- //count how many different classes appear in our data
- int nrOfClasses = myClasses.size();
- binaryLabels.clear();
- //compute the corresponding binary label vectors
- if ( nrOfClasses > 2 )
- {
- //resize every labelVector and set all entries to -1.0
- for ( set<int>::const_iterator k = myClasses.begin(); k != myClasses.end(); k++ )
- {
- binaryLabels[ *k ].resize ( y.size() );
- binaryLabels[ *k ].set ( -1.0 );
- }
- // now look on every example and set the entry of its corresponding label vector to 1.0
- // proper existance should not be a problem
- for ( int i = 0 ; i < ( int ) y.size(); i++ )
- binaryLabels[ y[i] ][i] = 1.0;
- }
- else if ( nrOfClasses == 2 )
- {
- //binary setting -- prepare two binary label vectors with opposite signs
- NICE::Vector yb ( y );
- binaryLabelNegative = *(myClasses.begin());
- std::set<int>::const_iterator classIt = myClasses.begin(); classIt++;
- binaryLabelPositive = *classIt;
-
- if ( verbose )
- std::cerr << "positiveClass : " << binaryLabelPositive << " negativeClass: " << binaryLabelNegative << std::endl;
- for ( uint i = 0 ; i < yb.size() ; i++ )
- yb[i] = ( y[i] == binaryLabelNegative ) ? -1.0 : 1.0;
-
- binaryLabels[ binaryLabelPositive ] = yb;
-
- //NOTE
- //uncomment the following, if you want to perform real binary computations with 2 classes
- // //we only need one vector, which already contains +1 and -1, so we need only one computation too
- // binaryLabels[ negativeClass ] = yb;
- // binaryLabels[ negativeClass ] *= -1.0;
-
- // std::cerr << "binaryLabels.size(): " << binaryLabels.size() << std::endl;
-
- // binaryLabels[ 0 ] = yb;
- // binaryLabels[ 0 ] *= -1.0;
-
-
- //comment the following, if you want to do a real binary computation. It should be senseless, but let's see...
-
- //we do NOT do real binary computation, but an implicite one with only a single object
- nrOfClasses--;
- std::set<int>::iterator it = myClasses.begin(); it++;
- // myClasses.erase(it);
- }
- else //OCC setting
- {
- //we set the labels to 1, independent of the previously given class number
- //however, the original class numbers are stored and returned in classification
- Vector yNew ( y.size(), 1 );
- myClasses.clear();
- myClasses.insert ( 1 );
- //we have to indicate, that we are in an OCC setting
- nrOfClasses--;
- }
- return nrOfClasses;
- }
- void FMKGPHyperparameterOptimization::optimize ( const NICE::Vector & y )
- {
- if ( fmk == NULL )
- fthrow ( Exception, "FastMinKernel object was not initialized!" );
- this->labels = y;
-
- std::map<int, NICE::Vector> binaryLabels;
- prepareBinaryLabels ( binaryLabels, y , knownClasses );
-
- //now call the main function :)
- this->optimize(binaryLabels);
- }
-
- void FMKGPHyperparameterOptimization::optimize ( std::map<int, NICE::Vector> & binaryLabels )
- {
- Timer t;
- t.start();
-
- //how many different classes do we have right now?
- int nrOfClasses = binaryLabels.size();
-
- if (verbose)
- {
- std::cerr << "Initial noise level: " << fmk->getNoise() << endl;
- std::cerr << "Number of classes (=1 means we have a binary setting):" << nrOfClasses << std::endl;
- std::cerr << "Effective number of classes (neglecting classes without positive examples): " << knownClasses.size() << std::endl;
- }
- // combine standard model and noise model
- Timer t1;
- t1.start();
- //setup the kernel combination
- ikmsum = new IKMLinearCombination ();
- if ( verbose )
- {
- std::cerr << "binaryLabels.size(): " << binaryLabels.size() << std::endl;
- }
- //First model: noise
- ikmsum->addModel ( new IKMNoise ( fmk->get_n(), fmk->getNoise(), optimizeNoise ) );
-
- // set pretty low built-in noise, because we explicitely add the noise with the IKMNoise
- fmk->setNoise ( 0.0 );
- //NOTE The GMHIKernel is always the last model which is added (this is necessary for easy store and restore functionality)
- ikmsum->addModel ( new GMHIKernel ( fmk, pf, NULL /* no quantization */ ) );
- t1.stop();
- if (verboseTime)
- std::cerr << "Time used for setting up the ikm-objects: " << t1.getLast() << std::endl;
- GPLikelihoodApprox * gplike;
- uint parameterVectorSize;
- t1.start();
- this->setupGPLikelihoodApprox ( gplike, binaryLabels, parameterVectorSize );
- t1.stop();
-
- if (verboseTime)
- std::cerr << "Time used for setting up the gplike-objects: " << t1.getLast() << std::endl;
- if (verbose)
- {
- std::cerr << "parameterVectorSize: " << parameterVectorSize << std::endl;
- }
- t1.start();
- this->updateEigenDecomposition( this->nrOfEigenvaluesToConsider );
- t1.stop();
- if (verboseTime)
- std::cerr << "Time used for setting up the eigenvectors-objects: " << t1.getLast() << std::endl;
- if ( verbose )
- std::cerr << "resulting eigenvalues for first class: " << eigenMax[0] << std::endl;
- t1.start();
- this->performOptimization ( *gplike, parameterVectorSize );
- t1.stop();
- if (verboseTime)
- std::cerr << "Time used for performing the optimization: " << t1.getLast() << std::endl;
- if ( verbose )
- cerr << "Preparing classification ..." << endl;
- t1.start();
- this->transformFeaturesWithOptimalParameters ( *gplike, parameterVectorSize );
- t1.stop();
- if (verboseTime)
- std::cerr << "Time used for transforming features with optimal parameters: " << t1.getLast() << std::endl;
- t1.start();
- this->computeMatricesAndLUTs ( *gplike );
- t1.stop();
- if (verboseTime)
- std::cerr << "Time used for setting up the A'nB -objects: " << t1.getLast() << std::endl;
- t.stop();
- ResourceStatistics rs;
- std::cerr << "Time used for learning: " << t.getLast() << std::endl;
- long maxMemory;
- rs.getMaximumMemory ( maxMemory );
- std::cerr << "Maximum memory used: " << maxMemory << " KB" << std::endl;
- //don't waste memory
- delete gplike;
- }
- void FMKGPHyperparameterOptimization::prepareVarianceApproximationRough()
- {
- PrecomputedType AVar;
- fmk->hikPrepareKVNApproximation ( AVar );
- precomputedAForVarEst = AVar;
- precomputedAForVarEst.setIoUntilEndOfFile ( false );
- if ( q != NULL )
- {
- double *T = fmk->hikPrepareLookupTableForKVNApproximation ( *q, pf );
- precomputedTForVarEst = T;
- }
- }
- void FMKGPHyperparameterOptimization::prepareVarianceApproximationFine()
- {
- this->updateEigenDecomposition( this->nrOfEigenvaluesToConsiderForVarApprox );
- }
- int FMKGPHyperparameterOptimization::classify ( const NICE::SparseVector & xstar, NICE::SparseVector & scores ) const
- {
- // loop through all classes
- if ( precomputedA.size() == 0 )
- {
- fthrow ( Exception, "The precomputation vector is zero...have you trained this classifier?" );
- }
- uint maxClassNo = 0;
- for ( std::map<int, PrecomputedType>::const_iterator i = precomputedA.begin() ; i != precomputedA.end(); i++ )
- {
- uint classno = i->first;
- maxClassNo = std::max ( maxClassNo, classno );
- double beta;
- if ( q != NULL ) {
- map<int, double *>::const_iterator j = precomputedT.find ( classno );
- double *T = j->second;
- fmk->hik_kernel_sum_fast ( T, *q, xstar, beta );
- } else {
- const PrecomputedType & A = i->second;
- std::map<int, PrecomputedType>::const_iterator j = precomputedB.find ( classno );
- const PrecomputedType & B = j->second;
- // fmk->hik_kernel_sum ( A, B, xstar, beta ); if A, B are of type Matrix
- // Giving the transformation pf as an additional
- // argument is necessary due to the following reason:
- // FeatureMatrixT is sorted according to the original values, therefore,
- // searching for upper and lower bounds ( findFirst... functions ) require original feature
- // values as inputs. However, for calculation we need the transformed features values.
- fmk->hik_kernel_sum ( A, B, xstar, beta, pf );
- }
- scores[ classno ] = beta;
- }
- scores.setDim ( maxClassNo + 1 );
-
- if ( precomputedA.size() > 1 )
- { // multi-class classification
- return scores.maxElement();
- }
- else
- { // binary setting
- scores[binaryLabelNegative] = -scores[binaryLabelPositive];
- return scores[ binaryLabelPositive ] <= 0.0 ? binaryLabelNegative : binaryLabelPositive;
- }
- }
- int FMKGPHyperparameterOptimization::classify ( const NICE::Vector & xstar, NICE::SparseVector & scores ) const
- {
- // loop through all classes
- if ( precomputedA.size() == 0 )
- {
- fthrow ( Exception, "The precomputation vector is zero...have you trained this classifier?" );
- }
- uint maxClassNo = 0;
- for ( std::map<int, PrecomputedType>::const_iterator i = precomputedA.begin() ; i != precomputedA.end(); i++ )
- {
- uint classno = i->first;
- maxClassNo = std::max ( maxClassNo, classno );
- double beta;
- if ( q != NULL ) {
- std::map<int, double *>::const_iterator j = precomputedT.find ( classno );
- double *T = j->second;
- fmk->hik_kernel_sum_fast ( T, *q, xstar, beta );
- } else {
- const PrecomputedType & A = i->second;
- std::map<int, PrecomputedType>::const_iterator j = precomputedB.find ( classno );
- const PrecomputedType & B = j->second;
- // fmk->hik_kernel_sum ( A, B, xstar, beta ); if A, B are of type Matrix
- // Giving the transformation pf as an additional
- // argument is necessary due to the following reason:
- // FeatureMatrixT is sorted according to the original values, therefore,
- // searching for upper and lower bounds ( findFirst... functions ) require original feature
- // values as inputs. However, for calculation we need the transformed features values.
- fmk->hik_kernel_sum ( A, B, xstar, beta, pf );
- }
- scores[ classno ] = beta;
- }
- scores.setDim ( maxClassNo + 1 );
-
- if ( precomputedA.size() > 1 )
- { // multi-class classification
- return scores.maxElement();
- }
- else
- { // binary setting
-
- scores[binaryLabelNegative] = -scores[binaryLabelPositive];
- return scores[ binaryLabelPositive ] <= 0.0 ? binaryLabelNegative : binaryLabelPositive;
- }
- }
- //////////////////////////////////////////
- // variance computation: sparse inputs
- //////////////////////////////////////////
- void FMKGPHyperparameterOptimization::computePredictiveVarianceApproximateRough ( const NICE::SparseVector & x, double & predVariance ) const
- {
- // security check!
- if ( pf == NULL )
- fthrow ( Exception, "pf is NULL...have you prepared the uncertainty prediction? Aborting..." );
-
- // ---------------- compute the first term --------------------
- double kSelf ( 0.0 );
- for ( NICE::SparseVector::const_iterator it = x.begin(); it != x.end(); it++ )
- {
- kSelf += pf->f ( 0, it->second );
- // if weighted dimensions:
- //kSelf += pf->f(it->first,it->second);
- }
-
- // ---------------- compute the approximation of the second term --------------------
- double normKStar;
- if ( q != NULL )
- {
- if ( precomputedTForVarEst == NULL )
- {
- fthrow ( Exception, "The precomputed LUT for uncertainty prediction is NULL...have you prepared the uncertainty prediction? Aborting..." );
- }
- fmk->hikComputeKVNApproximationFast ( precomputedTForVarEst, *q, x, normKStar );
- }
- else
- {
- if ( precomputedAForVarEst.size () == 0 )
- {
- fthrow ( Exception, "The precomputedAForVarEst is empty...have you trained this classifer? Aborting..." );
- }
- fmk->hikComputeKVNApproximation ( precomputedAForVarEst, x, normKStar, pf );
- }
- predVariance = kSelf - ( 1.0 / eigenMax[0] )* normKStar;
- }
- void FMKGPHyperparameterOptimization::computePredictiveVarianceApproximateFine ( const NICE::SparseVector & x, double & predVariance ) const
- {
- // security check!
- if ( eigenMaxVectors.rows() == 0 )
- {
- fthrow ( Exception, "eigenMaxVectors is empty...have you trained this classifer? Aborting..." );
- }
-
- // ---------------- compute the first term --------------------
- // Timer t;
- // t.start();
- double kSelf ( 0.0 );
- for ( NICE::SparseVector::const_iterator it = x.begin(); it != x.end(); it++ )
- {
- kSelf += pf->f ( 0, it->second );
- // if weighted dimensions:
- //kSelf += pf->f(it->first,it->second);
- }
- // ---------------- compute the approximation of the second term --------------------
- // t.stop();
- // std::cerr << "ApproxFine -- time for first term: " << t.getLast() << std::endl;
- // t.start();
- NICE::Vector kStar;
- fmk->hikComputeKernelVector ( x, kStar );
- /* t.stop();
- std::cerr << "ApproxFine -- time for kernel vector: " << t.getLast() << std::endl;*/
-
- // NICE::Vector multiplicationResults; // will contain nrOfEigenvaluesToConsiderForVarApprox many entries
- // multiplicationResults.multiply ( *eigenMaxVectorIt, kStar, true/* transpose */ );
- NICE::Vector multiplicationResults( nrOfEigenvaluesToConsiderForVarApprox-1, 0.0 );
- //ok, there seems to be a nasty thing in computing multiplicationResults.multiply ( *eigenMaxVectorIt, kStar, true/* transpose */ );
- //wherefor it takes aeons...
- //so we compute it by ourselves
-
-
- // for ( uint tmpI = 0; tmpI < kStar.size(); tmpI++)
- NICE::Matrix::const_iterator eigenVecIt = eigenMaxVectors.begin();
- // double kStarI ( kStar[tmpI] );
- for ( int tmpJ = 0; tmpJ < nrOfEigenvaluesToConsiderForVarApprox-1; tmpJ++)
- {
- for ( NICE::Vector::const_iterator kStarIt = kStar.begin(); kStarIt != kStar.end(); kStarIt++,eigenVecIt++)
- {
- multiplicationResults[tmpJ] += (*kStarIt) * (*eigenVecIt);//eigenMaxVectors(tmpI,tmpJ);
- }
- }
-
- double projectionLength ( 0.0 );
- double currentSecondTerm ( 0.0 );
- double sumOfProjectionLengths ( 0.0 );
- int cnt ( 0 );
- NICE::Vector::const_iterator it = multiplicationResults.begin();
- while ( cnt < ( nrOfEigenvaluesToConsiderForVarApprox - 1 ) )
- {
- projectionLength = ( *it );
- currentSecondTerm += ( 1.0 / eigenMax[cnt] ) * pow ( projectionLength, 2 );
- sumOfProjectionLengths += pow ( projectionLength, 2 );
-
- it++;
- cnt++;
- }
-
-
- double normKStar ( pow ( kStar.normL2 (), 2 ) );
- currentSecondTerm += ( 1.0 / eigenMax[nrOfEigenvaluesToConsiderForVarApprox-1] ) * ( normKStar - sumOfProjectionLengths );
-
- if ( ( normKStar - sumOfProjectionLengths ) < 0 )
- {
- std::cerr << "Attention: normKStar - sumOfProjectionLengths is smaller than zero -- strange!" << std::endl;
- }
- predVariance = kSelf - currentSecondTerm;
- }
- void FMKGPHyperparameterOptimization::computePredictiveVarianceExact ( const NICE::SparseVector & x, double & predVariance ) const
- {
- // security check!
- if ( ikmsum->getNumberOfModels() == 0 )
- {
- fthrow ( Exception, "ikmsum is empty... have you trained this classifer? Aborting..." );
- }
-
- Timer t;
- // t.start();
- // ---------------- compute the first term --------------------
- double kSelf ( 0.0 );
- for ( NICE::SparseVector::const_iterator it = x.begin(); it != x.end(); it++ )
- {
- kSelf += pf->f ( 0, it->second );
- // if weighted dimensions:
- //kSelf += pf->f(it->first,it->second);
- }
- // ---------------- compute the second term --------------------
- // t.stop();
- // std::cerr << "ApproxExact -- time for first term: " << t.getLast() << std::endl;
- // t.start();
- NICE::Vector kStar;
- fmk->hikComputeKernelVector ( x, kStar );
- // t.stop();
- // std::cerr << "ApproxExact -- time for kernel vector: " << t.getLast() << std::endl;
- //
-
- //now run the ILS method
- NICE::Vector diagonalElements;
- ikmsum->getDiagonalElements ( diagonalElements );
- // t.start();
- // init simple jacobi pre-conditioning
- ILSConjugateGradients *linsolver_cg = dynamic_cast<ILSConjugateGradients *> ( linsolver );
- //perform pre-conditioning
- if ( linsolver_cg != NULL )
- linsolver_cg->setJacobiPreconditioner ( diagonalElements );
-
- NICE::Vector beta;
-
- /** About finding a good initial solution (see also GPLikelihoodApproximation)
- * K~ = K + sigma^2 I
- *
- * K~ \approx lambda_max v v^T
- * \lambda_max v v^T * alpha = k_* | multiply with v^T from left
- * => \lambda_max v^T alpha = v^T k_*
- * => alpha = k_* / lambda_max could be a good initial start
- * If we put everything in the first equation this gives us
- * v = k_*
- * This reduces the number of iterations by 5 or 8
- */
- beta = (kStar * (1.0 / eigenMax[0]) );
- /* t.stop();
- std::cerr << "ApproxExact -- time for preconditioning etc: " << t.getLast() << std::endl;
-
- t.start();*/
- // t.start();
- linsolver->solveLin ( *ikmsum, kStar, beta );
- // t.stop();
- // t.stop();
- // t.stop();
- // std::cerr << "ApproxExact -- time for lin solve: " << t.getLast() << std::endl;
- beta *= kStar;
-
- double currentSecondTerm( beta.Sum() );
- predVariance = kSelf - currentSecondTerm;
- }
- //////////////////////////////////////////
- // variance computation: non-sparse inputs
- //////////////////////////////////////////
-
- void FMKGPHyperparameterOptimization::computePredictiveVarianceApproximateRough ( const NICE::Vector & x, double & predVariance ) const
- {
- // security check!
- if ( pf == NULL )
- fthrow ( Exception, "pf is NULL...have you prepared the uncertainty prediction? Aborting..." );
-
- // ---------------- compute the first term --------------------
- double kSelf ( 0.0 );
- int dim ( 0 );
- for ( NICE::Vector::const_iterator it = x.begin(); it != x.end(); it++, dim++ )
- {
- kSelf += pf->f ( 0, *it );
- // if weighted dimensions:
- //kSelf += pf->f(dim,*it);
- }
-
- // ---------------- compute the approximation of the second term --------------------
- double normKStar;
- if ( q != NULL )
- {
- if ( precomputedTForVarEst == NULL )
- {
- fthrow ( Exception, "The precomputed LUT for uncertainty prediction is NULL...have you prepared the uncertainty prediction? Aborting..." );
- }
- fmk->hikComputeKVNApproximationFast ( precomputedTForVarEst, *q, x, normKStar );
- }
- else
- {
- if ( precomputedAForVarEst.size () == 0 )
- {
- fthrow ( Exception, "The precomputedAForVarEst is empty...have you trained this classifer? Aborting..." );
- }
- fmk->hikComputeKVNApproximation ( precomputedAForVarEst, x, normKStar, pf );
- }
- predVariance = kSelf - ( 1.0 / eigenMax[0] )* normKStar;
- }
- void FMKGPHyperparameterOptimization::computePredictiveVarianceApproximateFine ( const NICE::Vector & x, double & predVariance ) const
- {
- // security check!
- if ( eigenMaxVectors.rows() == 0 )
- {
- fthrow ( Exception, "eigenMaxVectors is empty...have you trained this classifer? Aborting..." );
- }
-
- // ---------------- compute the first term --------------------
- // Timer t;
- // t.start();
- double kSelf ( 0.0 );
- int dim ( 0 );
- for ( NICE::Vector::const_iterator it = x.begin(); it != x.end(); it++, dim++ )
- {
- kSelf += pf->f ( 0, *it );
- // if weighted dimensions:
- //kSelf += pf->f(dim,*it);
- }
- // ---------------- compute the approximation of the second term --------------------
- // t.stop();
- // std::cerr << "ApproxFine -- time for first term: " << t.getLast() << std::endl;
- // t.start();
- NICE::Vector kStar;
- fmk->hikComputeKernelVector ( x, kStar );
- /* t.stop();
- std::cerr << "ApproxFine -- time for kernel vector: " << t.getLast() << std::endl;*/
-
- // NICE::Vector multiplicationResults; // will contain nrOfEigenvaluesToConsiderForVarApprox many entries
- // multiplicationResults.multiply ( *eigenMaxVectorIt, kStar, true/* transpose */ );
- NICE::Vector multiplicationResults( nrOfEigenvaluesToConsiderForVarApprox-1, 0.0 );
- //ok, there seems to be a nasty thing in computing multiplicationResults.multiply ( *eigenMaxVectorIt, kStar, true/* transpose */ );
- //wherefor it takes aeons...
- //so we compute it by ourselves
-
-
- // for ( uint tmpI = 0; tmpI < kStar.size(); tmpI++)
- NICE::Matrix::const_iterator eigenVecIt = eigenMaxVectors.begin();
- // double kStarI ( kStar[tmpI] );
- for ( int tmpJ = 0; tmpJ < nrOfEigenvaluesToConsiderForVarApprox-1; tmpJ++)
- {
- for ( NICE::Vector::const_iterator kStarIt = kStar.begin(); kStarIt != kStar.end(); kStarIt++,eigenVecIt++)
- {
- multiplicationResults[tmpJ] += (*kStarIt) * (*eigenVecIt);//eigenMaxVectors(tmpI,tmpJ);
- }
- }
- double projectionLength ( 0.0 );
- double currentSecondTerm ( 0.0 );
- double sumOfProjectionLengths ( 0.0 );
- int cnt ( 0 );
- NICE::Vector::const_iterator it = multiplicationResults.begin();
- while ( cnt < ( nrOfEigenvaluesToConsiderForVarApprox - 1 ) )
- {
- projectionLength = ( *it );
- currentSecondTerm += ( 1.0 / eigenMax[cnt] ) * pow ( projectionLength, 2 );
- sumOfProjectionLengths += pow ( projectionLength, 2 );
-
- it++;
- cnt++;
- }
-
-
- double normKStar ( pow ( kStar.normL2 (), 2 ) );
- currentSecondTerm += ( 1.0 / eigenMax[nrOfEigenvaluesToConsiderForVarApprox-1] ) * ( normKStar - sumOfProjectionLengths );
-
- if ( ( normKStar - sumOfProjectionLengths ) < 0 )
- {
- std::cerr << "Attention: normKStar - sumOfProjectionLengths is smaller than zero -- strange!" << std::endl;
- }
- predVariance = kSelf - currentSecondTerm;
- }
- void FMKGPHyperparameterOptimization::computePredictiveVarianceExact ( const NICE::Vector & x, double & predVariance ) const
- {
- if ( ikmsum->getNumberOfModels() == 0 )
- {
- fthrow ( Exception, "ikmsum is empty... have you trained this classifer? Aborting..." );
- }
-
- Timer t;
- // t.start();
- // ---------------- compute the first term --------------------
- double kSelf ( 0.0 );
- int dim ( 0 );
- for ( NICE::Vector::const_iterator it = x.begin(); it != x.end(); it++, dim++ )
- {
- kSelf += pf->f ( 0, *it );
- // if weighted dimensions:
- //kSelf += pf->f(dim,*it);
- }
- // ---------------- compute the second term --------------------
- // t.stop();
- // std::cerr << "ApproxExact -- time for first term: " << t.getLast() << std::endl;
- // t.start();
- NICE::Vector kStar;
- fmk->hikComputeKernelVector ( x, kStar );
- // t.stop();
- // std::cerr << "ApproxExact -- time for kernel vector: " << t.getLast() << std::endl;
- //
- //now run the ILS method
- NICE::Vector diagonalElements;
- ikmsum->getDiagonalElements ( diagonalElements );
- // t.start();
- // init simple jacobi pre-conditioning
- ILSConjugateGradients *linsolver_cg = dynamic_cast<ILSConjugateGradients *> ( linsolver );
- //perform pre-conditioning
- if ( linsolver_cg != NULL )
- linsolver_cg->setJacobiPreconditioner ( diagonalElements );
-
- NICE::Vector beta;
-
- /** About finding a good initial solution (see also GPLikelihoodApproximation)
- * K~ = K + sigma^2 I
- *
- * K~ \approx lambda_max v v^T
- * \lambda_max v v^T * alpha = k_* | multiply with v^T from left
- * => \lambda_max v^T alpha = v^T k_*
- * => alpha = k_* / lambda_max could be a good initial start
- * If we put everything in the first equation this gives us
- * v = k_*
- * This reduces the number of iterations by 5 or 8
- */
- beta = (kStar * (1.0 / eigenMax[0]) );
- /* t.stop();
- std::cerr << "ApproxExact -- time for preconditioning etc: " << t.getLast() << std::endl;
-
- t.start();*/
- // t.start();
- linsolver->solveLin ( *ikmsum, kStar, beta );
- // t.stop();
- // t.stop();
- // t.stop();
- // std::cerr << "ApproxExact -- time for lin solve: " << t.getLast() << std::endl;
- beta *= kStar;
-
- double currentSecondTerm( beta.Sum() );
- predVariance = kSelf - currentSecondTerm;
- }
- // ---------------------- STORE AND RESTORE FUNCTIONS ----------------------
- void FMKGPHyperparameterOptimization::restore ( std::istream & is, int format )
- {
- bool b_restoreVerbose ( false );
- if ( is.good() )
- {
- if ( b_restoreVerbose )
- std::cerr << " in FMKGP restore" << std::endl;
- //load the underlying data
- if (fmk != NULL)
- delete fmk;
- if ( b_restoreVerbose )
- std::cerr << " create FMK" << std::endl;
- fmk = new FastMinKernel;
- if ( b_restoreVerbose )
- std::cerr << " restore FMK" << std::endl;
- fmk->restore(is,format);
- if ( b_restoreVerbose )
- std::cerr << "fmk->restore done " << std::endl;
-
- is.precision ( numeric_limits<double>::digits10 + 1 );
- string tmp;
- is >> tmp; //class name
-
- is >> tmp; //precomputedA:
- is >> tmp; //size:
- int preCompSize ( 0 );
- is >> preCompSize;
- precomputedA.clear();
- if ( b_restoreVerbose )
- std::cerr << "restore precomputedA with size: " << preCompSize << std::endl;
- for ( int i = 0; i < preCompSize; i++ )
- {
- int nr;
- is >> nr;
- PrecomputedType pct;
- pct.setIoUntilEndOfFile ( false );
- pct.restore ( is, format );
- precomputedA.insert ( std::pair<int, PrecomputedType> ( nr, pct ) );
- }
-
- is >> tmp; //precomputedB:
- is >> tmp; //size:
- is >> preCompSize;
- precomputedB.clear();
- if ( b_restoreVerbose )
- std::cerr << "restore precomputedB with size: " << preCompSize << std::endl;
- for ( int i = 0; i < preCompSize; i++ )
- {
- int nr;
- is >> nr;
- PrecomputedType pct;
- pct.setIoUntilEndOfFile ( false );
- pct.restore ( is, format );
- precomputedB.insert ( std::pair<int, PrecomputedType> ( nr, pct ) );
- }
-
- is >> tmp; //precomputedT: std::cerr << " content of tmp: " << tmp << std::endl;
- is >> tmp; //size: std::cerr << " content of tmp: " << tmp << std::endl;
-
- int precomputedTSize ( 0 );
- is >> precomputedTSize;
- precomputedT.clear();
-
- if ( b_restoreVerbose )
- std::cerr << "restore precomputedT with size: " << precomputedTSize << std::endl;
- if ( precomputedTSize > 0 )
- {
- if ( b_restoreVerbose )
- std::cerr << " restore precomputedT" << std::endl;
- is >> tmp;
- int sizeOfLUT;
- is >> sizeOfLUT;
-
- for (int i = 0; i < precomputedTSize; i++)
- {
- is >> tmp;
- int index;
- is >> index;
- double * array = new double [ sizeOfLUT];
- for ( int i = 0; i < sizeOfLUT; i++ )
- {
- is >> array[i];
- }
- precomputedT.insert ( std::pair<int, double*> ( index, array ) );
- }
- }
- else
- {
- if ( b_restoreVerbose )
- std::cerr << " skip restoring precomputedT" << std::endl;
- }
- //now restore the things we need for the variance computation
- is >> tmp;
- if ( b_restoreVerbose )
- std::cerr << " content of tmp: " << tmp << std::endl;
- int sizeOfAForVarEst;
- is >> sizeOfAForVarEst;
-
- if ( b_restoreVerbose )
- std::cerr << "restore precomputedAForVarEst with size: " << sizeOfAForVarEst << std::endl;
-
- if (sizeOfAForVarEst > 0)
- {
- precomputedAForVarEst.clear();
-
- precomputedAForVarEst.setIoUntilEndOfFile ( false );
- std::cerr << "restore precomputedAForVarEst" << std::endl;
- precomputedAForVarEst.restore ( is, format );
- }
- is >> tmp; //precomputedTForVarEst
- if ( b_restoreVerbose )
- std::cerr << "content of tmp: " << tmp << std::endl;
- is >> tmp; // NOTNULL or NULL
- if ( b_restoreVerbose )
- std::cerr << "content of tmp: " << tmp << std::endl;
- if (tmp.compare("NOTNULL") == 0)
- {
- if ( b_restoreVerbose )
- std::cerr << "restore precomputedTForVarEst" << std::endl;
-
- int sizeOfLUT;
- is >> sizeOfLUT;
- precomputedTForVarEst = new double [ sizeOfLUT ];
- for ( int i = 0; i < sizeOfLUT; i++ )
- {
- is >> precomputedTForVarEst[i];
- }
- }
- else
- {
- if ( b_restoreVerbose )
- std::cerr << "skip restoring of precomputedTForVarEst" << std::endl;
- if (precomputedTForVarEst != NULL)
- delete precomputedTForVarEst;
- }
-
- if ( b_restoreVerbose )
- std::cerr << "restore eigenMax and eigenMaxVectors " << std::endl;
-
- //restore eigenvalues and eigenvectors
- is >> tmp; //"eigenmax:"
- if ( b_restoreVerbose )
- std::cerr << "content of tmp: " << tmp << std::endl;
- is >> eigenMax;
- if ( b_restoreVerbose )
- std::cerr << "loaded the following eigenMax: " << eigenMax << std::endl;
- is >> tmp; //"eigenMaxVectors:"
- if ( b_restoreVerbose )
- std::cerr << "content of tmp: " << tmp << std::endl;
- is >> eigenMaxVectors;
- if ( b_restoreVerbose )
- std::cerr << "loaded the following eigenMaxVectors: " << eigenMaxVectors << std::endl;
-
- if ( b_restoreVerbose )
- std::cerr << " create ikmsum object" << std::endl;
-
- if ( ikmsum == NULL )
- {
- ikmsum = new IKMLinearCombination ();
- if ( b_restoreVerbose )
- std::cerr << "ikmsum object created" << std::endl;
- }
- else
- {
- if ( b_restoreVerbose )
- std::cerr << "ikmsum object already existing" << std::endl;
- }
-
- is >> tmp; //"numberOfModels:"
- if ( b_restoreVerbose )
- std::cerr << "content of tmp: " << tmp << std::endl;
- int nrOfModels ( 0 );
- is >> nrOfModels;
- if ( b_restoreVerbose )
- std::cerr << "number of models to add in total: " << nrOfModels << std::endl;
-
- if ( b_restoreVerbose )
- std::cerr << " restore IKMNoise " << std::endl;
-
- //the first one is always our noise-model
- IKMNoise * ikmnoise = new IKMNoise ();
- ikmnoise->restore ( is, format );
-
- if ( b_restoreVerbose )
- std::cerr << " add ikmnoise to ikmsum object " << std::endl;
- ikmsum->addModel ( ikmnoise );
- //NOTE are there any more models you added? then add them here respectively in the correct order
- //.....
- //the last one is the GHIK - which we do not have to restore, but simply reset it
- if ( b_restoreVerbose )
- std::cerr << " add GMHIKernel" << std::endl;
- ikmsum->addModel ( new GMHIKernel ( fmk, this->pf, this->q ) );
-
- if ( b_restoreVerbose )
- std::cerr << " restore positive and negative label" << std::endl;
-
- //restore the class numbers for binary settings (if mc-settings, these values will be negative by default)
- is >> tmp; // "binaryLabelPositive: "
- if ( b_restoreVerbose )
- std::cerr << " content of tmp: " << tmp << std::endl;
- is >> binaryLabelPositive;
- if ( b_restoreVerbose )
- std::cerr << " content of binaryLabelPositive: " << binaryLabelPositive << std::endl;
- is >> tmp; // " binaryLabelNegative: "
- if ( b_restoreVerbose )
- std::cerr << " content of tmp: " << tmp << std::endl;
- is >> binaryLabelNegative;
- if ( b_restoreVerbose )
- std::cerr << " content of binaryLabelNegative: " << binaryLabelNegative << std::endl;
-
- is >> tmp; // "labels: "
- if ( b_restoreVerbose )
- std::cerr << " content of tmp: " << tmp << std::endl;
- is >> this->labels;
- if ( b_restoreVerbose )
- std::cerr << " restored labels: " << labels << std::endl;
- knownClasses.clear();
-
- if ( b_restoreVerbose )
- std::cerr << " fill known classes object " << std::endl;
-
- if ( precomputedA.size() == 1)
- {
- knownClasses.insert( binaryLabelPositive );
- knownClasses.insert( binaryLabelNegative );
- if ( b_restoreVerbose )
- std::cerr << " binary setting - added corresp. two class numbers" << std::endl;
- }
- else
- {
- for ( std::map<int, PrecomputedType>::const_iterator itA = precomputedA.begin(); itA != precomputedA.end(); itA++)
- knownClasses.insert ( itA->first );
- if ( b_restoreVerbose )
- std::cerr << " multi class setting - added corresp. multiple class numbers" << std::endl;
- }
- }
- else
- {
- std::cerr << "InStream not initialized - restoring not possible!" << std::endl;
- }
- }
- void FMKGPHyperparameterOptimization::store ( std::ostream & os, int format ) const
- {
- if ( os.good() )
- {
- fmk->store ( os, format );
- os.precision ( numeric_limits<double>::digits10 + 1 );
- os << "FMKGPHyperparameterOptimization" << std::endl;
- //we only have to store the things we computed, since the remaining settings come with the config file afterwards
-
- os << "precomputedA: size: " << precomputedA.size() << std::endl;
- std::map< int, PrecomputedType >::const_iterator preCompIt = precomputedA.begin();
- for ( uint i = 0; i < precomputedA.size(); i++ )
- {
- os << preCompIt->first << std::endl;
- ( preCompIt->second ).store ( os, format );
- preCompIt++;
- }
-
- os << "precomputedB: size: " << precomputedB.size() << std::endl;
- preCompIt = precomputedB.begin();
- for ( uint i = 0; i < precomputedB.size(); i++ )
- {
- os << preCompIt->first << std::endl;
- ( preCompIt->second ).store ( os, format );
- preCompIt++;
- }
-
-
- os << "precomputedT: size: " << precomputedT.size() << std::endl;
- if ( precomputedT.size() > 0 )
- {
- int sizeOfLUT ( 0 );
- if ( q != NULL )
- sizeOfLUT = q->size() * this->fmk->get_d();
- os << "SizeOfLUTs: " << sizeOfLUT << std::endl;
- for ( std::map< int, double * >::const_iterator it = precomputedT.begin(); it != precomputedT.end(); it++ )
- {
- os << "index: " << it->first << std::endl;
- for ( int i = 0; i < sizeOfLUT; i++ )
- {
- os << ( it->second ) [i] << " ";
- }
- os << std::endl;
- }
- }
- //now store the things needed for the variance estimation
-
- os << "precomputedAForVarEst.size(): "<< precomputedAForVarEst.size() << std::endl;
-
- if (precomputedAForVarEst.size() > 0)
- {
- precomputedAForVarEst.store ( os, format );
- os << std::endl;
- }
-
- if ( precomputedTForVarEst != NULL )
- {
- os << "precomputedTForVarEst NOTNULL" << std::endl;
- int sizeOfLUT ( 0 );
- if ( q != NULL )
- sizeOfLUT = q->size() * this->fmk->get_d();
-
- os << sizeOfLUT << std::endl;
- for ( int i = 0; i < sizeOfLUT; i++ )
- {
- os << precomputedTForVarEst[i] << " ";
- }
- os << std::endl;
- }
- else
- {
- os << "precomputedTForVarEst NULL" << std::endl;
- }
-
- //store the eigenvalues and eigenvectors
- os << "eigenMax" << std::endl;
- os << eigenMax << std::endl;
- os << "eigenMaxVectors" << std::endl;
- os << eigenMaxVectors << std::endl;
- //store the ikmsum object
- os << "numberOfModels: " << ikmsum->getNumberOfModels() << std::endl;
- for ( int j = 0; j < ikmsum->getNumberOfModels() - 1; j++ )
- {
- ( ikmsum->getModel ( j ) )->store ( os, format );
- }
-
- //store the class numbers for binary settings (if mc-settings, these values will be negative by default)
- os << "binaryLabelPositive: " << binaryLabelPositive << " binaryLabelNegative: " << binaryLabelNegative << std::endl;
-
- os << "labels: " << this->labels << std::endl;
- }
- else
- {
- std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
- }
- }
- void FMKGPHyperparameterOptimization::clear ( ) {};
|