123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- /**
- * @file FMKGPHyperparameterOptimization.h
- * @brief Heart of the framework to set up everything, perform optimization, classification, and variance prediction (Interface)
- * @author Erik Rodner, Alexander Freytag
- * @date 01/02/2012
- */
- #ifndef _NICE_FMKGPHYPERPARAMETEROPTIMIZATIONINCLUDE
- #define _NICE_FMKGPHYPERPARAMETEROPTIMIZATIONINCLUDE
- // STL includes
- #include <vector>
- #include <set>
- #include <map>
- // NICE-core includes
- #include <core/algebra/EigValues.h>
- #include <core/algebra/IterativeLinearSolver.h>
- #include <core/basics/Config.h>
- #include <core/basics/Persistent.h>
- #include <core/vector/VectorT.h>
- #ifdef NICE_USELIB_MATIO
- #include <core/matlabAccess/MatFileIO.h>
- #endif
- // gp-hik-core includes
- #include "gp-hik-core/FastMinKernel.h"
- #include "gp-hik-core/GPLikelihoodApprox.h"
- #include "gp-hik-core/IKMLinearCombination.h"
- #include "gp-hik-core/OnlineLearnable.h"
- #include "gp-hik-core/Quantization.h"
- #include "gp-hik-core/parameterizedFunctions/ParameterizedFunction.h"
- namespace NICE {
-
- /**
- * @class FMKGPHyperparameterOptimization
- * @brief Heart of the framework to set up everything, perform optimization, classification, and variance prediction
- * @author Erik Rodner, Alexander Freytag
- */
-
- class FMKGPHyperparameterOptimization : public NICE::Persistent, public NICE::OnlineLearnable
- {
- protected:
- enum {
- OPT_GREEDY = 0,
- OPT_DOWNHILLSIMPLEX,
- OPT_NONE,
- OPT_NUMBEROFMETHODS
- };
- /** optimization method used */
- int optimizationMethod;
- /** the parameterized function we use within the minimum kernel */
- ParameterizedFunction *pf;
- /** method computing eigenvalues */
- EigValues *eig;
- /** method for solving linear equation systems */
- IterativeLinearSolver *linsolver;
- /** object which stores our sorted data and provides fast hik functions */
- FastMinKernel *fmk;
- /** object which stores our quantization object */
- Quantization *q;
- /** verbose flag */
- bool verbose;
- /** verbose flag for time measurement outputs */
- bool verboseTime;
- /** debug flag for several outputs useful for debugging*/
- bool debug;
- /** optimization parameters */
- double parameterUpperBound;
- double parameterLowerBound;
- double parameterStepSize;
- int ils_max_iterations;
- int downhillSimplexMaxIterations;
- double downhillSimplexTimeLimit;
- double downhillSimplexParamTol;
- /** whether to compute the likelihood with the usual method */
- bool verifyApproximation;
-
- /** number of Eigenvalues to consider in the approximation of |K|_F */
- int nrOfEigenvaluesToConsider;
-
- /** number of Eigenvalues to consider in the fine approximation of the predictive variance */
- int nrOfEigenvaluesToConsiderForVarApprox;
- typedef VVector PrecomputedType;
- /** precomputed arrays and lookup tables */
- std::map< int, PrecomputedType > precomputedA;
- std::map< int, PrecomputedType > precomputedB;
- std::map< int, double * > precomputedT;
- PrecomputedType precomputedAForVarEst;
- double * precomputedTForVarEst;
- //! optimize noise with the GP likelihood
- bool optimizeNoise;
-
- //! k largest eigenvalues of the kernel matrix (k == nrOfEigenvaluesToConsider)
- NICE::Vector eigenMax;
- //! eigenvectors corresponding to k largest eigenvalues (k == nrOfEigenvaluesToConsider) -- format: nxk
- NICE::Matrix eigenMaxVectors;
-
- //! needed for optimization and variance approximation
- IKMLinearCombination * ikmsum;
-
- //! storing the labels is needed for Incremental Learning (re-optimization)
- NICE::Vector labels;
-
- //! calculate binary label vectors using a multi-class label vector
- int prepareBinaryLabels ( std::map<int, NICE::Vector> & binaryLabels, const NICE::Vector & y , std::set<int> & myClasses);
-
- //! prepare the GPLike object for given binary labels and already given ikmsum-object
- inline void setupGPLikelihoodApprox( GPLikelihoodApprox * & gplike, const std::map<int, NICE::Vector> & binaryLabels, uint & parameterVectorSize);
-
- //! update eigenvectors and eigenvalues for given ikmsum-objects and a method to compute eigenvalues
- inline void updateEigenDecomposition( const int & i_noEigenValues );
-
- //! core of the optimize-functions
- inline void performOptimization( GPLikelihoodApprox & gplike, const uint & parameterVectorSize);
-
- //! apply the optimized transformation values to the underlying features
- inline void transformFeaturesWithOptimalParameters(const GPLikelihoodApprox & gplike, const uint & parameterVectorSize);
-
- //! build the resulting matrices A and B as well as lookup tables T for fast evaluations using the optimized parameter settings
- inline void computeMatricesAndLUTs( const GPLikelihoodApprox & gplike);
-
-
- //! store the class number of the positive class (i.e., larger class no), only used in binary settings
- int binaryLabelPositive;
- //! store the class number of the negative class (i.e., smaller class no), only used in binary settings
- int binaryLabelNegative;
-
- //! contains all class numbers of the currently known classes
- std::set<int> knownClasses;
-
- bool b_usePreviousAlphas;
-
- //! we store the alpha vectors for good initializations in the IL setting
- std::map<int, NICE::Vector> lastAlphas;
- //! Update matrices (A, B, LUTs) and optionally find optimal parameters after adding a new example.
- void updateAfterSingleIncrement (
- const NICE::SparseVector & x,
- const std::set<int> newClasses,
- const bool & performOptimizationAfterIncrement = false
- );
- //! Update matrices (A, B, LUTs) and optionally find optimal parameters after adding multiple examples.
- void updateAfterMultipleIncrements (
- const std::vector<const NICE::SparseVector*> & x,
- const std::set<int> newClasses,
- const bool & performOptimizationAfterIncrement = false
- );
-
- public:
-
- FMKGPHyperparameterOptimization();
-
- /**
- * @brief standard constructor
- *
- * @param pf pointer to a parameterized function used within the minimum kernel min(f(x_i), f(x_j)) (will not be deleted)
- * @param noise GP label noise
- * @param fmk pointer to a pre-initialized structure (will be deleted)
- */
- FMKGPHyperparameterOptimization( const Config *conf, ParameterizedFunction *pf, FastMinKernel *fmk = NULL, const std::string & confSection = "GPHIKClassifier" );
-
- /** simple destructor */
- virtual ~FMKGPHyperparameterOptimization();
-
- ///////////////////// ///////////////////// /////////////////////
- // GET / SET
- ///////////////////// ///////////////////// /////////////////////
- void setParameterUpperBound(const double & _parameterUpperBound);
- void setParameterLowerBound(const double & _parameterLowerBound);
-
- std::set<int> getKnownClassNumbers ( ) const;
-
- ///////////////////// ///////////////////// /////////////////////
- // CLASSIFIER STUFF
- ///////////////////// ///////////////////// /////////////////////
-
- void initialize( const Config *conf, ParameterizedFunction *pf, FastMinKernel *fmk = NULL, const std::string & confSection = "GPHIKClassifier" );
-
- #ifdef NICE_USELIB_MATIO
- /**
- * @brief Perform hyperparameter optimization
- *
- * @param data MATLAB data structure, like a feature matrix loaded from ImageNet
- * @param y label vector (arbitrary), will be converted into a binary label vector
- * @param positives set of positive examples (indices)
- * @param negatives set of negative examples (indices)
- */
- void optimizeBinary ( const sparse_t & data, const NICE::Vector & y, const std::set<int> & positives, const std::set<int> & negatives, double noise );
- /**
- * @brief Perform hyperparameter optimization for GP multi-class or binary problems
- *
- * @param data MATLAB data structure, like a feature matrix loaded from ImageNet
- * @param y label vector with multi-class labels
- * @param examples mapping of example index to new index
- */
- void optimize ( const sparse_t & data, const NICE::Vector & y, const std::map<int, int> & examples, double noise );
- #endif
- /**
- * @brief Perform hyperparameter optimization (multi-class or binary) assuming an already initialized fmk object
- *
- * @param y label vector (multi-class as well as binary labels supported)
- */
- void optimize ( const NICE::Vector & y );
-
- /**
- * @brief Perform hyperparameter optimization (multi-class or binary) assuming an already initialized fmk object
- *
- * @param binLabels vector of binary label vectors (1,-1) and corresponding class no.
- */
- void optimize ( std::map<int, NICE::Vector> & binaryLabels );
-
- /**
- * @brief Compute the necessary variables for appxorimations of predictive variance (LUTs), assuming an already initialized fmk object
- * @author Alexander Freytag
- * @date 11-04-2012 (dd-mm-yyyy)
- */
- void prepareVarianceApproximationRough();
-
- /**
- * @brief Compute the necessary variables for fine appxorimations of predictive variance (EVs), assuming an already initialized fmk object
- * @author Alexander Freytag
- * @date 11-04-2012 (dd-mm-yyyy)
- */
- void prepareVarianceApproximationFine();
-
- /**
- * @brief classify an example
- *
- * @param x input example (sparse vector)
- * @param scores scores for each class number
- *
- * @return class number achieving the best score
- */
- int classify ( const NICE::SparseVector & x, SparseVector & scores ) const;
-
- /**
- * @brief classify an example that is given as non-sparse vector
- * NOTE: whenever possible, you should sparse vectors to obtain significantly smaller computation times
- *
- * @date 18-06-2013 (dd-mm-yyyy)
- * @author Alexander Freytag
- *
- * @param x input example (non-sparse vector)
- * @param scores scores for each class number
- *
- * @return class number achieving the best score
- */
- int classify ( const NICE::Vector & x, SparseVector & scores ) const;
- //////////////////////////////////////////
- // variance computation: sparse inputs
- //////////////////////////////////////////
-
- /**
- * @brief compute predictive variance for a given test example using a rough approximation: k_{**} - k_*^T (K+\sigma I)^{-1} k_* <= k_{**} - |k_*|^2 * 1 / \lambda_max(K + \sigma I), where we approximate |k_*|^2 by neglecting the mixed terms
- * @author Alexander Freytag
- * @date 10-04-2012 (dd-mm-yyyy)
- * @param x input example
- * @param predVariance contains the approximation of the predictive variance
- *
- */
- void computePredictiveVarianceApproximateRough(const NICE::SparseVector & x, double & predVariance ) const;
-
- /**
- * @brief compute predictive variance for a given test example using a fine approximation (k eigenvalues and eigenvectors to approximate the quadratic term)
- * @author Alexander Freytag
- * @date 18-04-2012 (dd-mm-yyyy)
- * @param x input example
- * @param predVariance contains the approximation of the predictive variance
- *
- */
- void computePredictiveVarianceApproximateFine(const NICE::SparseVector & x, double & predVariance ) const;
-
- /**
- * @brief compute exact predictive variance for a given test example using ILS methods (exact, but more time consuming than approx versions)
- * @author Alexander Freytag
- * @date 10-04-2012 (dd-mm-yyyy)
- * @param x input example
- * @param predVariance contains the approximation of the predictive variance
- *
- */
- void computePredictiveVarianceExact(const NICE::SparseVector & x, double & predVariance ) const;
-
-
- //////////////////////////////////////////
- // variance computation: non-sparse inputs
- //////////////////////////////////////////
-
- /**
- * @brief compute predictive variance for a given test example using a rough approximation: k_{**} - k_*^T (K+\sigma I)^{-1} k_* <= k_{**} - |k_*|^2 * 1 / \lambda_max(K + \sigma I), where we approximate |k_*|^2 by neglecting the mixed terms
- * @author Alexander Freytag
- * @date 19-12-2013 (dd-mm-yyyy)
- * @param x input example
- * @param predVariance contains the approximation of the predictive variance
- *
- */
- void computePredictiveVarianceApproximateRough(const NICE::Vector & x, double & predVariance ) const;
-
-
- /**
- * @brief compute predictive variance for a given test example using a fine approximation (k eigenvalues and eigenvectors to approximate the quadratic term)
- * @author Alexander Freytag
- * @date 19-12-2013 (dd-mm-yyyy)
- * @param x input example
- * @param predVariance contains the approximation of the predictive variance
- *
- */
- void computePredictiveVarianceApproximateFine(const NICE::Vector & x, double & predVariance ) const;
-
-
- /**
- * @brief compute exact predictive variance for a given test example using ILS methods (exact, but more time consuming than approx versions)
- * @author Alexander Freytag
- * @date 19-12-2013 (dd-mm-yyyy)
- * @param x input example
- * @param predVariance contains the approximation of the predictive variance
- *
- */
- void computePredictiveVarianceExact(const NICE::Vector & x, double & predVariance ) const;
-
-
-
-
-
- ///////////////////// INTERFACE PERSISTENT /////////////////////
- // interface specific methods for store and restore
- ///////////////////// INTERFACE PERSISTENT /////////////////////
-
- void restore ( std::istream & is, int format = 0 );
- void store ( std::ostream & os, int format = 0 ) const;
- void clear ( ) ;
-
- ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
- // interface specific methods for incremental extensions
- ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
-
- virtual void addExample( const NICE::SparseVector * example,
- const double & label,
- const bool & performOptimizationAfterIncrement = true
- );
-
- virtual void addMultipleExamples( const std::vector< const NICE::SparseVector * > & newExamples,
- const NICE::Vector & newLabels,
- const bool & performOptimizationAfterIncrement = true
- );
- };
- }
- #endif
|