FMKGPHyperparameterOptimization.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /**
  2. * @file FMKGPHyperparameterOptimization.h
  3. * @brief Heart of the framework to set up everything, perform optimization, classification, and variance prediction (Interface)
  4. * @author Alexander Freytag, Erik Rodner
  5. * @date 01-02-2012 (dd-mm-yyyy)
  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 "gp-hik-core/FastMinKernel.h"
  24. #include "gp-hik-core/GPLikelihoodApprox.h"
  25. #include "gp-hik-core/IKMLinearCombination.h"
  26. #include "gp-hik-core/OnlineLearnable.h"
  27. #include "gp-hik-core/Quantization.h"
  28. #include "gp-hik-core/parameterizedFunctions/ParameterizedFunction.h"
  29. namespace NICE {
  30. /**
  31. * @class FMKGPHyperparameterOptimization
  32. * @brief Heart of the framework to set up everything, perform optimization, classification, and variance prediction
  33. * @author Alexander Freytag, Erik Rodner
  34. */
  35. class FMKGPHyperparameterOptimization : public NICE::Persistent, public NICE::OnlineLearnable
  36. {
  37. protected:
  38. /////////////////////////
  39. /////////////////////////
  40. // PROTECTED VARIABLES //
  41. /////////////////////////
  42. /////////////////////////
  43. ///////////////////////////////////
  44. // output/debug related settings //
  45. ///////////////////////////////////
  46. /** verbose flag */
  47. bool verbose;
  48. /** verbose flag for time measurement outputs */
  49. bool verboseTime;
  50. /** debug flag for several outputs useful for debugging*/
  51. bool debug;
  52. //////////////////////////////////////
  53. // classification related variables //
  54. //////////////////////////////////////
  55. /** per default, we perform classification, if not stated otherwise */
  56. bool b_performRegression;
  57. /** object storing sorted data and providing fast hik methods */
  58. NICE::FastMinKernel *fmk;
  59. /** object performing feature quantization */
  60. NICE::Quantization *q;
  61. /** the parameterized function we use within the minimum kernel */
  62. NICE::ParameterizedFunction *pf;
  63. /** method for solving linear equation systems - needed to compute K^-1 \times y */
  64. IterativeLinearSolver *linsolver;
  65. /** Max. number of iterations the iterative linear solver is allowed to run */
  66. int ils_max_iterations;
  67. /** Simple type definition for precomputation matrices used for fast classification */
  68. typedef VVector PrecomputedType;
  69. /** precomputed arrays A (1 per class) needed for classification without quantization */
  70. std::map< int, PrecomputedType > precomputedA;
  71. /** precomputed arrays B (1 per class) needed for classification without quantization */
  72. std::map< int, PrecomputedType > precomputedB;
  73. /** precomputed LUTs (1 per class) needed for classification with quantization */
  74. std::map< int, double * > precomputedT;
  75. //! storing the labels is needed for Incremental Learning (re-optimization)
  76. NICE::Vector labels;
  77. //! store the class number of the positive class (i.e., larger class no), only used in binary settings
  78. int binaryLabelPositive;
  79. //! store the class number of the negative class (i.e., smaller class no), only used in binary settings
  80. int binaryLabelNegative;
  81. //! contains all class numbers of the currently known classes
  82. std::set<int> knownClasses;
  83. //! container for multiple kernel matrices (e.g., a data-containing kernel matrix (GMHIKernel) and a noise matrix (IKMNoise) )
  84. NICE::IKMLinearCombination * ikmsum;
  85. /////////////////////////////////////
  86. // optimization related parameters //
  87. /////////////////////////////////////
  88. enum {
  89. OPT_GREEDY = 0,
  90. OPT_DOWNHILLSIMPLEX,
  91. OPT_NONE
  92. };
  93. /** specify the optimization method used (see corresponding enum) */
  94. int optimizationMethod;
  95. //! whether or not to optimize noise with the GP likelihood
  96. bool optimizeNoise;
  97. /** upper bound for hyper parameters to optimize */
  98. double parameterUpperBound;
  99. /** lower bound for hyper parameters to optimize */
  100. double parameterLowerBound;
  101. // specific to greedy optimization
  102. /** step size used in grid based greedy optimization technique */
  103. double parameterStepSize;
  104. // specific to downhill simplex optimization
  105. /** Max. number of iterations the downhill simplex optimizer is allowed to run */
  106. int downhillSimplexMaxIterations;
  107. /** Max. time the downhill simplex optimizer is allowed to run */
  108. double downhillSimplexTimeLimit;
  109. /** Max. number of iterations the iterative linear solver is allowed to run */
  110. double downhillSimplexParamTol;
  111. // likelihood computation related variables
  112. /** whether to compute the exact likelihood by computing the exact kernel matrix (not recommended - only for debugging/comparison purpose) */
  113. bool verifyApproximation;
  114. /** method computing eigenvalues and eigenvectors*/
  115. NICE::EigValues *eig;
  116. /** number of Eigenvalues to consider in the approximation of |K|_F used for approximating the likelihood */
  117. int nrOfEigenvaluesToConsider;
  118. //! k largest eigenvalues of the kernel matrix (k == nrOfEigenvaluesToConsider)
  119. NICE::Vector eigenMax;
  120. //! eigenvectors corresponding to k largest eigenvalues (k == nrOfEigenvaluesToConsider) -- format: nxk
  121. NICE::Matrix eigenMaxVectors;
  122. ////////////////////////////////////////////
  123. // variance computation related variables //
  124. ////////////////////////////////////////////
  125. /** number of Eigenvalues to consider in the fine approximation of the predictive variance (fine approximation only) */
  126. int nrOfEigenvaluesToConsiderForVarApprox;
  127. /** precomputed array needed for rough variance approximation without quantization */
  128. PrecomputedType precomputedAForVarEst;
  129. /** precomputed LUT needed for rough variance approximation with quantization */
  130. double * precomputedTForVarEst;
  131. /////////////////////////////////////////////////////
  132. // online / incremental learning related variables //
  133. /////////////////////////////////////////////////////
  134. /** whether or not to use previous alpha solutions as initialization after adding new examples*/
  135. bool b_usePreviousAlphas;
  136. //! store alpha vectors for good initializations in the IL setting, if activated
  137. std::map<int, NICE::Vector> previousAlphas;
  138. /////////////////////////
  139. /////////////////////////
  140. // PROTECTED METHODS //
  141. /////////////////////////
  142. /////////////////////////
  143. /**
  144. * @brief calculate binary label vectors using a multi-class label vector
  145. * @author Alexander Freytag
  146. */
  147. int prepareBinaryLabels ( std::map<int, NICE::Vector> & binaryLabels, const NICE::Vector & y , std::set<int> & myClasses);
  148. /**
  149. * @brief prepare the GPLike object for given binary labels and already given ikmsum-object
  150. * @author Alexander Freytag
  151. */
  152. inline void setupGPLikelihoodApprox( GPLikelihoodApprox * & gplike, const std::map<int, NICE::Vector> & binaryLabels, uint & parameterVectorSize);
  153. /**
  154. * @brief update eigenvectors and eigenvalues for given ikmsum-objects and a method to compute eigenvalues
  155. * @author Alexander Freytag
  156. */
  157. inline void updateEigenDecomposition( const int & i_noEigenValues );
  158. /**
  159. * @brief core of the optimize-functions
  160. * @author Alexander Freytag
  161. */
  162. inline void performOptimization( GPLikelihoodApprox & gplike, const uint & parameterVectorSize);
  163. /**
  164. * @brief apply the optimized transformation values to the underlying features
  165. * @author Alexander Freytag
  166. */
  167. inline void transformFeaturesWithOptimalParameters(const GPLikelihoodApprox & gplike, const uint & parameterVectorSize);
  168. /**
  169. * @brief build the resulting matrices A and B as well as lookup tables T for fast evaluations using the optimized parameter settings
  170. * @author Alexander Freytag
  171. */
  172. inline void computeMatricesAndLUTs( const GPLikelihoodApprox & gplike);
  173. /**
  174. * @brief Update matrices (A, B, LUTs) and optionally find optimal parameters after adding (a) new example(s).
  175. * @author Alexander Freytag
  176. */
  177. void updateAfterIncrement (
  178. const std::set<int> newClasses,
  179. const bool & performOptimizationAfterIncrement = false
  180. );
  181. public:
  182. /**
  183. * @brief simple constructor
  184. * @author Alexander Freytag
  185. */
  186. FMKGPHyperparameterOptimization( const bool & b_performRegression = false);
  187. /**
  188. * @brief standard constructor
  189. *
  190. * @param pf pointer to a parameterized function used within the minimum kernel min(f(x_i), f(x_j)) (will not be deleted)
  191. * @param noise GP label noise
  192. * @param fmk pointer to a pre-initialized structure (will be deleted)
  193. */
  194. FMKGPHyperparameterOptimization( const Config *conf, ParameterizedFunction *pf, FastMinKernel *fmk = NULL, const std::string & confSection = "GPHIKClassifier" );
  195. /**
  196. * @brief standard destructor
  197. * @author Alexander Freytag
  198. */
  199. virtual ~FMKGPHyperparameterOptimization();
  200. ///////////////////// ///////////////////// /////////////////////
  201. // GET / SET
  202. ///////////////////// ///////////////////// /////////////////////
  203. /**
  204. * @brief Set lower bound for hyper parameters to optimize
  205. * @author Alexander Freytag
  206. */
  207. void setParameterUpperBound(const double & _parameterUpperBound);
  208. /**
  209. * @brief Set upper bound for hyper parameters to optimize
  210. * @author Alexander Freytag
  211. */
  212. void setParameterLowerBound(const double & _parameterLowerBound);
  213. /**
  214. * @brief Get the currently known class numbers
  215. * @author Alexander Freytag
  216. */
  217. std::set<int> getKnownClassNumbers ( ) const;
  218. ///////////////////// ///////////////////// /////////////////////
  219. // CLASSIFIER STUFF
  220. ///////////////////// ///////////////////// /////////////////////
  221. /**
  222. * @brief Set variables and parameters to default or config-specified values
  223. * @author Alexander Freytag
  224. */
  225. void initialize( const Config *conf, ParameterizedFunction *pf, FastMinKernel *fmk = NULL, const std::string & confSection = "GPHIKClassifier" );
  226. #ifdef NICE_USELIB_MATIO
  227. /**
  228. * @brief Perform hyperparameter optimization
  229. * @author Alexander Freytag
  230. *
  231. * @param data MATLAB data structure, like a feature matrix loaded from ImageNet
  232. * @param y label vector (arbitrary), will be converted into a binary label vector
  233. * @param positives set of positive examples (indices)
  234. * @param negatives set of negative examples (indices)
  235. */
  236. void optimizeBinary ( const sparse_t & data, const NICE::Vector & y, const std::set<int> & positives, const std::set<int> & negatives, double noise );
  237. /**
  238. * @brief Perform hyperparameter optimization for GP multi-class or binary problems
  239. * @author Alexander Freytag
  240. *
  241. * @param data MATLAB data structure, like a feature matrix loaded from ImageNet
  242. * @param y label vector with multi-class labels
  243. * @param examples mapping of example index to new index
  244. */
  245. void optimize ( const sparse_t & data, const NICE::Vector & y, const std::map<int, int> & examples, double noise );
  246. #endif
  247. /**
  248. * @brief Perform hyperparameter optimization (multi-class or binary) assuming an already initialized fmk object
  249. * @author Alexander Freytag
  250. *
  251. * @param y label vector (multi-class as well as binary labels supported)
  252. */
  253. void optimize ( const NICE::Vector & y );
  254. /**
  255. * @brief Perform hyperparameter optimization (multi-class or binary) assuming an already initialized fmk object
  256. *
  257. * @param binLabels vector of binary label vectors (1,-1) and corresponding class no.
  258. */
  259. void optimize ( std::map<int, NICE::Vector> & binaryLabels );
  260. /**
  261. * @brief Compute the necessary variables for appxorimations of predictive variance (LUTs), assuming an already initialized fmk object
  262. * @author Alexander Freytag
  263. * @date 11-04-2012 (dd-mm-yyyy)
  264. */
  265. void prepareVarianceApproximationRough();
  266. /**
  267. * @brief Compute the necessary variables for fine appxorimations of predictive variance (EVs), assuming an already initialized fmk object
  268. * @author Alexander Freytag
  269. * @date 11-04-2012 (dd-mm-yyyy)
  270. */
  271. void prepareVarianceApproximationFine();
  272. /**
  273. * @brief classify an example
  274. *
  275. * @param x input example (sparse vector)
  276. * @param scores scores for each class number
  277. *
  278. * @return class number achieving the best score
  279. */
  280. int classify ( const NICE::SparseVector & x, SparseVector & scores ) const;
  281. /**
  282. * @brief classify an example that is given as non-sparse vector
  283. * NOTE: whenever possible, you should sparse vectors to obtain significantly smaller computation times
  284. *
  285. * @date 18-06-2013 (dd-mm-yyyy)
  286. * @author Alexander Freytag
  287. *
  288. * @param x input example (non-sparse vector)
  289. * @param scores scores for each class number
  290. *
  291. * @return class number achieving the best score
  292. */
  293. int classify ( const NICE::Vector & x, SparseVector & scores ) const;
  294. //////////////////////////////////////////
  295. // variance computation: sparse inputs
  296. //////////////////////////////////////////
  297. /**
  298. * @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
  299. * @author Alexander Freytag
  300. * @date 10-04-2012 (dd-mm-yyyy)
  301. * @param x input example
  302. * @param predVariance contains the approximation of the predictive variance
  303. *
  304. */
  305. void computePredictiveVarianceApproximateRough(const NICE::SparseVector & x, double & predVariance ) const;
  306. /**
  307. * @brief compute predictive variance for a given test example using a fine approximation (k eigenvalues and eigenvectors to approximate the quadratic term)
  308. * @author Alexander Freytag
  309. * @date 18-04-2012 (dd-mm-yyyy)
  310. * @param x input example
  311. * @param predVariance contains the approximation of the predictive variance
  312. *
  313. */
  314. void computePredictiveVarianceApproximateFine(const NICE::SparseVector & x, double & predVariance ) const;
  315. /**
  316. * @brief compute exact predictive variance for a given test example using ILS methods (exact, but more time consuming than approx versions)
  317. * @author Alexander Freytag
  318. * @date 10-04-2012 (dd-mm-yyyy)
  319. * @param x input example
  320. * @param predVariance contains the approximation of the predictive variance
  321. *
  322. */
  323. void computePredictiveVarianceExact(const NICE::SparseVector & x, double & predVariance ) const;
  324. //////////////////////////////////////////
  325. // variance computation: non-sparse inputs
  326. //////////////////////////////////////////
  327. /**
  328. * @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
  329. * @author Alexander Freytag
  330. * @date 19-12-2013 (dd-mm-yyyy)
  331. * @param x input example
  332. * @param predVariance contains the approximation of the predictive variance
  333. *
  334. */
  335. void computePredictiveVarianceApproximateRough(const NICE::Vector & x, double & predVariance ) const;
  336. /**
  337. * @brief compute predictive variance for a given test example using a fine approximation (k eigenvalues and eigenvectors to approximate the quadratic term)
  338. * @author Alexander Freytag
  339. * @date 19-12-2013 (dd-mm-yyyy)
  340. * @param x input example
  341. * @param predVariance contains the approximation of the predictive variance
  342. *
  343. */
  344. void computePredictiveVarianceApproximateFine(const NICE::Vector & x, double & predVariance ) const;
  345. /**
  346. * @brief compute exact predictive variance for a given test example using ILS methods (exact, but more time consuming than approx versions)
  347. * @author Alexander Freytag
  348. * @date 19-12-2013 (dd-mm-yyyy)
  349. * @param x input example
  350. * @param predVariance contains the approximation of the predictive variance
  351. *
  352. */
  353. void computePredictiveVarianceExact(const NICE::Vector & x, double & predVariance ) const;
  354. ///////////////////// INTERFACE PERSISTENT /////////////////////
  355. // interface specific methods for store and restore
  356. ///////////////////// INTERFACE PERSISTENT /////////////////////
  357. /**
  358. * @brief Load current object from external file (stream)
  359. * @author Alexander Freytag
  360. */
  361. void restore ( std::istream & is, int format = 0 );
  362. /**
  363. * @brief Save current object to external file (stream)
  364. * @author Alexander Freytag
  365. */
  366. void store ( std::ostream & os, int format = 0 ) const;
  367. /**
  368. * @brief Clear current object
  369. * @author Alexander Freytag
  370. */
  371. void clear ( ) ;
  372. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  373. // interface specific methods for incremental extensions
  374. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  375. /**
  376. * @brief add a new example
  377. * @author Alexander Freytag
  378. */
  379. virtual void addExample( const NICE::SparseVector * example,
  380. const double & label,
  381. const bool & performOptimizationAfterIncrement = true
  382. );
  383. /**
  384. * @brief add several new examples
  385. * @author Alexander Freytag
  386. */
  387. virtual void addMultipleExamples( const std::vector< const NICE::SparseVector * > & newExamples,
  388. const NICE::Vector & newLabels,
  389. const bool & performOptimizationAfterIncrement = true
  390. );
  391. };
  392. }
  393. #endif