FMKGPHyperparameterOptimization.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /**
  2. * @file FMKGPHyperparameterOptimization.h
  3. * @brief Heart of the framework to set up everything, perform optimization, classification, and variance prediction (Interface)
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 01/02/2012
  6. */
  7. #ifndef _NICE_FMKGPHYPERPARAMETEROPTIMIZATIONINCLUDE
  8. #define _NICE_FMKGPHYPERPARAMETEROPTIMIZATIONINCLUDE
  9. // STL includes
  10. #include <vector>
  11. #include <set>
  12. #include <map>
  13. // NICE-core includes
  14. #include <core/algebra/EigValues.h>
  15. #include <core/algebra/IterativeLinearSolver.h>
  16. #include <core/basics/Config.h>
  17. #include <core/basics/Persistent.h>
  18. #include <core/vector/VectorT.h>
  19. #ifdef NICE_USELIB_MATIO
  20. #include <core/matlabAccess/MatFileIO.h>
  21. #endif
  22. // gp-hik-core includes
  23. #include "FastMinKernel.h"
  24. #include "GPLikelihoodApprox.h"
  25. #include "IKMLinearCombination.h"
  26. #include "Quantization.h"
  27. #include "gp-hik-core/parameterizedFunctions/ParameterizedFunction.h"
  28. namespace NICE {
  29. /**
  30. * @class FMKGPHyperparameterOptimization
  31. * @brief Heart of the framework to set up everything, perform optimization, classification, and variance prediction
  32. * @author Erik Rodner, Alexander Freytag
  33. */
  34. class FMKGPHyperparameterOptimization : NICE::Persistent
  35. {
  36. protected:
  37. enum {
  38. OPT_GREEDY = 0,
  39. OPT_DOWNHILLSIMPLEX,
  40. OPT_NONE,
  41. OPT_NUMBEROFMETHODS
  42. };
  43. /** optimization method used */
  44. int optimizationMethod;
  45. /** the parameterized function we use within the minimum kernel */
  46. ParameterizedFunction *pf;
  47. /** method computing eigenvalues */
  48. EigValues *eig;
  49. /** method for solving linear equation systems */
  50. IterativeLinearSolver *linsolver;
  51. /** object which stores our sorted data and provides fast hik functions */
  52. FastMinKernel *fmk;
  53. /** object which stores our quantization object */
  54. Quantization *q;
  55. /** verbose flag */
  56. bool verbose;
  57. /** verbose flag for time measurement outputs */
  58. bool verboseTime;
  59. /** debug flag for several outputs useful for debugging*/
  60. bool debug;
  61. /** optimization parameters */
  62. double parameterUpperBound;
  63. double parameterLowerBound;
  64. double parameterStepSize;
  65. int ils_max_iterations;
  66. int downhillSimplexMaxIterations;
  67. double downhillSimplexTimeLimit;
  68. double downhillSimplexParamTol;
  69. /** whether to compute the likelihood with the usual method */
  70. bool verifyApproximation;
  71. /** number of Eigenvalues to consider in the approximation of |K|_F */
  72. int nrOfEigenvaluesToConsider;
  73. /** number of Eigenvalues to consider in the fine approximation of the predictive variance */
  74. int nrOfEigenvaluesToConsiderForVarApprox;
  75. typedef VVector PrecomputedType;
  76. /** precomputed arrays and lookup tables */
  77. std::map< int, PrecomputedType > precomputedA;
  78. std::map< int, PrecomputedType > precomputedB;
  79. std::map< int, double * > precomputedT;
  80. PrecomputedType precomputedAForVarEst;
  81. double * precomputedTForVarEst;
  82. //! optimize noise with the GP likelihood
  83. bool optimizeNoise;
  84. //! k largest eigenvalues of the kernel matrix (k == nrOfEigenvaluesToConsider)
  85. NICE::Vector eigenMax;
  86. //! eigenvectors corresponding to k largest eigenvalues (k == nrOfEigenvaluesToConsider) -- format: nxk
  87. NICE::Matrix eigenMaxVectors;
  88. //! needed for optimization and variance approximation
  89. IKMLinearCombination * ikmsum;
  90. //! storing the labels is needed for Incremental Learning (re-optimization)
  91. NICE::Vector labels;
  92. //! calculate binary label vectors using a multi-class label vector
  93. int prepareBinaryLabels ( std::map<int, NICE::Vector> & binaryLabels, const NICE::Vector & y , std::set<int> & myClasses);
  94. //! prepare the GPLike object for given binary labels and already given ikmsum-object
  95. inline void setupGPLikelihoodApprox( GPLikelihoodApprox * & gplike, const std::map<int, NICE::Vector> & binaryLabels, uint & parameterVectorSize);
  96. //! update eigenvectors and eigenvalues for given ikmsum-objects and a method to compute eigenvalues
  97. inline void updateEigenDecomposition( const int & i_noEigenValues );
  98. //! core of the optimize-functions
  99. inline void performOptimization( GPLikelihoodApprox & gplike, const uint & parameterVectorSize);
  100. //! apply the optimized transformation values to the underlying features
  101. inline void transformFeaturesWithOptimalParameters(const GPLikelihoodApprox & gplike, const uint & parameterVectorSize);
  102. //! build the resulting matrices A and B as well as lookup tables T for fast evaluations using the optimized parameter settings
  103. inline void computeMatricesAndLUTs( const GPLikelihoodApprox & gplike);
  104. //! store the class number of the positive class (i.e., larger class no), only used in binary settings
  105. int binaryLabelPositive;
  106. //! store the class number of the negative class (i.e., smaller class no), only used in binary settings
  107. int binaryLabelNegative;
  108. //! contains all class numbers of the currently known classes
  109. std::set<int> knownClasses;
  110. public:
  111. FMKGPHyperparameterOptimization();
  112. /**
  113. * @brief standard constructor
  114. *
  115. * @param pf pointer to a parameterized function used within the minimum kernel min(f(x_i), f(x_j)) (will not be deleted)
  116. * @param noise GP label noise
  117. * @param fmk pointer to a pre-initialized structure (will be deleted)
  118. */
  119. FMKGPHyperparameterOptimization( const Config *conf, ParameterizedFunction *pf, FastMinKernel *fmk = NULL, const std::string & confSection = "GPHIKClassifier" );
  120. /** simple destructor */
  121. virtual ~FMKGPHyperparameterOptimization();
  122. // get and set methods
  123. void setParameterUpperBound(const double & _parameterUpperBound);
  124. void setParameterLowerBound(const double & _parameterLowerBound);
  125. std::set<int> getKnownClassNumbers ( ) const;
  126. //high level methods
  127. void initialize( const Config *conf, ParameterizedFunction *pf, FastMinKernel *fmk = NULL, const std::string & confSection = "GPHIKClassifier" );
  128. #ifdef NICE_USELIB_MATIO
  129. /**
  130. * @brief Perform hyperparameter optimization
  131. *
  132. * @param data MATLAB data structure, like a feature matrix loaded from ImageNet
  133. * @param y label vector (arbitrary), will be converted into a binary label vector
  134. * @param positives set of positive examples (indices)
  135. * @param negatives set of negative examples (indices)
  136. */
  137. void optimizeBinary ( const sparse_t & data, const NICE::Vector & y, const std::set<int> & positives, const std::set<int> & negatives, double noise );
  138. /**
  139. * @brief Perform hyperparameter optimization for GP multi-class or binary problems
  140. *
  141. * @param data MATLAB data structure, like a feature matrix loaded from ImageNet
  142. * @param y label vector with multi-class labels
  143. * @param examples mapping of example index to new index
  144. */
  145. void optimize ( const sparse_t & data, const NICE::Vector & y, const std::map<int, int> & examples, double noise );
  146. #endif
  147. /**
  148. * @brief Perform hyperparameter optimization (multi-class or binary) assuming an already initialized fmk object
  149. *
  150. * @param y label vector (multi-class as well as binary labels supported)
  151. */
  152. void optimize ( const NICE::Vector & y );
  153. /**
  154. * @brief Perform hyperparameter optimization (multi-class or binary) assuming an already initialized fmk object
  155. *
  156. * @param binLabels vector of binary label vectors (1,-1) and corresponding class no.
  157. */
  158. void optimize ( std::map<int, NICE::Vector> & binaryLabels );
  159. /**
  160. * @brief Compute the necessary variables for appxorimations of predictive variance (LUTs), assuming an already initialized fmk object
  161. * @author Alexander Freytag
  162. * @date 11-04-2012 (dd-mm-yyyy)
  163. */
  164. void prepareVarianceApproximationRough();
  165. /**
  166. * @brief Compute the necessary variables for fine appxorimations of predictive variance (EVs), assuming an already initialized fmk object
  167. * @author Alexander Freytag
  168. * @date 11-04-2012 (dd-mm-yyyy)
  169. */
  170. void prepareVarianceApproximationFine();
  171. /**
  172. * @brief classify an example
  173. *
  174. * @param x input example (sparse vector)
  175. * @param scores scores for each class number
  176. *
  177. * @return class number achieving the best score
  178. */
  179. int classify ( const NICE::SparseVector & x, SparseVector & scores ) const;
  180. /**
  181. * @brief classify an example that is given as non-sparse vector
  182. * NOTE: whenever possible, you should sparse vectors to obtain significantly smaller computation times
  183. *
  184. * @date 18-06-2013 (dd-mm-yyyy)
  185. * @author Alexander Freytag
  186. *
  187. * @param x input example (non-sparse vector)
  188. * @param scores scores for each class number
  189. *
  190. * @return class number achieving the best score
  191. */
  192. int classify ( const NICE::Vector & x, SparseVector & scores ) const;
  193. //////////////////////////////////////////
  194. // variance computation: sparse inputs
  195. //////////////////////////////////////////
  196. /**
  197. * @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
  198. * @author Alexander Freytag
  199. * @date 10-04-2012 (dd-mm-yyyy)
  200. * @param x input example
  201. * @param predVariance contains the approximation of the predictive variance
  202. *
  203. */
  204. void computePredictiveVarianceApproximateRough(const NICE::SparseVector & x, double & predVariance ) const;
  205. /**
  206. * @brief compute predictive variance for a given test example using a fine approximation (k eigenvalues and eigenvectors to approximate the quadratic term)
  207. * @author Alexander Freytag
  208. * @date 18-04-2012 (dd-mm-yyyy)
  209. * @param x input example
  210. * @param predVariance contains the approximation of the predictive variance
  211. *
  212. */
  213. void computePredictiveVarianceApproximateFine(const NICE::SparseVector & x, double & predVariance ) const;
  214. /**
  215. * @brief compute exact predictive variance for a given test example using ILS methods (exact, but more time consuming than approx versions)
  216. * @author Alexander Freytag
  217. * @date 10-04-2012 (dd-mm-yyyy)
  218. * @param x input example
  219. * @param predVariance contains the approximation of the predictive variance
  220. *
  221. */
  222. void computePredictiveVarianceExact(const NICE::SparseVector & x, double & predVariance ) const;
  223. //////////////////////////////////////////
  224. // variance computation: non-sparse inputs
  225. //////////////////////////////////////////
  226. /**
  227. * @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
  228. * @author Alexander Freytag
  229. * @date 19-12-2013 (dd-mm-yyyy)
  230. * @param x input example
  231. * @param predVariance contains the approximation of the predictive variance
  232. *
  233. */
  234. void computePredictiveVarianceApproximateRough(const NICE::Vector & x, double & predVariance ) const;
  235. /**
  236. * @brief compute predictive variance for a given test example using a fine approximation (k eigenvalues and eigenvectors to approximate the quadratic term)
  237. * @author Alexander Freytag
  238. * @date 19-12-2013 (dd-mm-yyyy)
  239. * @param x input example
  240. * @param predVariance contains the approximation of the predictive variance
  241. *
  242. */
  243. void computePredictiveVarianceApproximateFine(const NICE::Vector & x, double & predVariance ) const;
  244. /**
  245. * @brief compute exact predictive variance for a given test example using ILS methods (exact, but more time consuming than approx versions)
  246. * @author Alexander Freytag
  247. * @date 19-12-2013 (dd-mm-yyyy)
  248. * @param x input example
  249. * @param predVariance contains the approximation of the predictive variance
  250. *
  251. */
  252. void computePredictiveVarianceExact(const NICE::Vector & x, double & predVariance ) const;
  253. /** Persistent interface */
  254. void restore ( std::istream & is, int format = 0 );
  255. void store ( std::ostream & os, int format = 0 ) const;
  256. void clear ( ) ;
  257. };
  258. }
  259. #endif