FastMinKernel.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /**
  2. * @file FastMinKernel.h
  3. * @brief Efficient GPs with HIK for classification by regression (Interface)
  4. * @author Alexander Freytag
  5. * @date 06-12-2011 (dd-mm-yyyy)
  6. */
  7. #ifndef FASTMINKERNELINCLUDE
  8. #define FASTMINKERNELINCLUDE
  9. #include <iostream>
  10. #include <core/vector/MatrixT.h>
  11. #include <core/vector/SparseVectorT.h>
  12. #include <core/vector/VVector.h>
  13. #include <core/basics/Exception.h>
  14. #include "core/basics/Persistent.h"
  15. #include "FeatureMatrixT.h"
  16. #include "Quantization.h"
  17. #include "gp-hik-core/parameterizedFunctions/ParameterizedFunction.h"
  18. namespace NICE {
  19. /**
  20. * @class FastMinKernel
  21. * @brief Efficient GPs with HIK for classification by regression
  22. * @author Alexander Freytag
  23. */
  24. /** interface to FastMinKernel implementation*/
  25. class FastMinKernel : NICE::Persistent
  26. {
  27. protected:
  28. /** number of examples */
  29. int n;
  30. /** dimension of feature vectors */
  31. int d;
  32. /** noise added to the diagonal of the kernel matrix */
  33. double noise;
  34. /** sorted matrix of features (sorted along each dimension) */
  35. NICE::FeatureMatrixT<double> X_sorted;
  36. //! verbose flag for output after calling the restore-function
  37. bool verbose;
  38. //! debug flag for output during debugging
  39. bool debug;
  40. /**
  41. * @brief Set number of examples
  42. * @author Alexander Freytag
  43. * @date 07-12-2011 (dd-mm-yyyy)
  44. */
  45. void set_n(const int & _n){n = _n;};
  46. /**
  47. * @brief Set number of dimensions
  48. * @author Alexander Freytag
  49. * @date 07-12-2011 (dd-mm-yyyy)
  50. */
  51. void set_d(const int & _d){d = _d;};
  52. /**
  53. * @brief Prepare the efficient HIK-computations part 1: order the features in each dimension and save the permutation. Pay attention: X is of dim n x d, where as X_sorted is of dimensionality d x n!
  54. * @author Alexander Freytag
  55. * @date 07-12-2011 (dd-mm-yyyy)
  56. */
  57. void hik_prepare_kernel_multiplications(const std::vector<std::vector<double> > & X, NICE::FeatureMatrixT<double> & X_sorted, const int & _dim = -1);
  58. void hik_prepare_kernel_multiplications ( const std::vector< NICE::SparseVector * > & X, NICE::FeatureMatrixT<double> & X_sorted, const bool & dimensionsOverExamples, const int & _dim = -1);
  59. void randomPermutation(NICE::Vector & permutation, const std::vector<int> & oldIndices, const int & newSize) const;
  60. enum ApproximationScheme{ MEDIAN = 0, EXPECTATION=1};
  61. ApproximationScheme approxScheme;
  62. public:
  63. //------------------------------------------------------
  64. // several constructors and destructors
  65. //------------------------------------------------------
  66. /**
  67. * @brief dummy constructor
  68. * @author Alexander Freytag
  69. * @date 20-04-2012 (dd-mm-yyyy)
  70. */
  71. FastMinKernel();
  72. /**
  73. * @brief initialize with some data
  74. * @author Alexander Freytag
  75. * @date 06-12-2011 (dd-mm-yyyy)
  76. */
  77. FastMinKernel( const std::vector<std::vector<double> > & X, const double noise , const bool _debug = false, const int & _dim = -1);
  78. /**
  79. * @brief Just another sparse data structure
  80. *
  81. * @param X vector of sparse vector pointers
  82. * @param noise GP noise
  83. */
  84. FastMinKernel( const std::vector< SparseVector * > & X, const double noise, const bool _debug = false, const bool & dimensionsOverExamples=false, const int & _dim = -1);
  85. #ifdef NICE_USELIB_MATIO
  86. /**
  87. * @brief intialize with some data given in a matlab-sparse struct and restricted with an example index
  88. *
  89. * @param X matlab-struct containing the feature vectors
  90. * @param noise additional noise variance of the labels
  91. * @param examples set of indices to include
  92. */
  93. FastMinKernel ( const sparse_t & X, const double noise, const std::map<int, int> & examples, const bool _debug = false , const int & _dim = -1);
  94. #endif
  95. /**
  96. * @brief Default destructor
  97. * @author Alexander Freytag
  98. * @date 06-12-2011 (dd-mm-yyyy)
  99. */
  100. ~FastMinKernel();
  101. //------------------------------------------------------
  102. // several get and set methods including access operators
  103. //------------------------------------------------------
  104. void setApproximationScheme(const ApproximationScheme & _approxScheme = MEDIAN) {approxScheme = _approxScheme;};
  105. virtual void setApproximationScheme(const int & _approxScheme = 0);
  106. /**
  107. * @brief Get number of examples
  108. * @author Alexander Freytag
  109. * @date 07-12-2011 (dd-mm-yyyy)
  110. */
  111. int get_n() const {return n;};
  112. /**
  113. * @brief Get number of dimensions
  114. * @author Alexander Freytag
  115. * @date 07-12-2011 (dd-mm-yyyy)
  116. */
  117. int get_d() const {return d;};
  118. /**
  119. * @brief Computes the ratio of sparsity across the matrix
  120. * @author Alexander Freytag
  121. * @date 11-01-2012 (dd-mm-yyyy)
  122. */
  123. double getSparsityRatio(){return X_sorted.computeSparsityRatio();};
  124. /** set verbose flag used for restore-functionality*/
  125. void setVerbose( const bool & _verbose);
  126. bool getVerbose( ) const;
  127. /** set debug flag used for debug output*/
  128. void setDebug( const bool & _debug);
  129. bool getDebug( ) const;
  130. //------------------------------------------------------
  131. // high level methods
  132. //------------------------------------------------------
  133. /**
  134. * @brief apply a parameterized function to the feature matrix
  135. * @author Alexander Freytag
  136. * @date 04-05-2012 (dd-mm-yyyy)
  137. *
  138. * @param pf the parameterized function (optional), if not given, nothing will be done
  139. */
  140. void applyFunctionToFeatureMatrix ( const NICE::ParameterizedFunction *pf = NULL );
  141. /**
  142. * @brief Prepare the efficient HIK-computations part 2: calculate the partial sum for each dimension. Explicitely exploiting sparsity!!! Pay attention: X_sorted is of dimensionality d x n!
  143. * @author Alexander Freytag
  144. * @date 17-01-2012 (dd-mm-yyyy)
  145. */
  146. void hik_prepare_alpha_multiplications(const NICE::Vector & alpha, NICE::VVector & A, NICE::VVector & B) const;
  147. /**
  148. * @brief Computing K*alpha with the minimum kernel trick, explicitely exploiting sparsity!!!
  149. * @author Alexander Freytag
  150. * @date 17-01-2012 (dd-mm-yyyy)
  151. */
  152. void hik_kernel_multiply(const NICE::VVector & A, const NICE::VVector & B, const NICE::Vector & alpha, NICE::Vector & beta) const;
  153. void hik_kernel_multiply_fast(const double *Tlookup, const Quantization & q, const NICE::Vector & alpha, NICE::Vector & beta) const;
  154. /**
  155. * @brief Computing k_{*}*alpha using the minimum kernel trick and exploiting sparsity of the feature vector given
  156. *
  157. * @author Alexander Freytag
  158. * @date 20-01-2012 (dd-mm-yyyy)
  159. * @param A pre-computation matrix (VVector) (use the prepare method)
  160. * @param B pre-computation matrix (VVector)
  161. * @param xstar new feature vector (SparseVector)
  162. * @param beta result of the scalar product
  163. * @param pf optional feature transformation
  164. */
  165. void hik_kernel_sum(const NICE::VVector & A, const NICE::VVector & B, const NICE::SparseVector & xstar, double & beta, const ParameterizedFunction *pf = NULL ) const;
  166. /**
  167. * @brief compute beta = k_*^T * alpha by using a large lookup table created by hik_prepare_alpha_multiplications_fast
  168. * @author Erik Rodner
  169. *
  170. * @param Tlookup large lookup table calculated by hik_prepare_alpha_multiplications_fast
  171. * @param q Quantization object
  172. * @param xstar feature vector (indirect k_*)
  173. * @param beta result of the calculation
  174. */
  175. void hik_kernel_sum_fast(const double* Tlookup, const Quantization & q, const NICE::Vector & xstar, double & beta) const;
  176. void hik_kernel_sum_fast(const double *Tlookup, const Quantization & q, const NICE::SparseVector & xstar, double & beta) const;
  177. /**
  178. * @brief compute lookup table for HIK calculation using quantized signals and prepare for K*alpha or k_*^T * alpha computations
  179. * @author Erik Rodner
  180. *
  181. * @param alpha coefficient vector
  182. * @param A pre-calculation array computed by hik_prepare_alpha_multiplications
  183. * @param B pre-calculation array computed by hik_prepare_alpha_multiplications
  184. * @param q Quantization
  185. *
  186. * @return C standard vector representing a q.size()*n double matrix and the lookup table T. Elements can be accessed with
  187. * T[dim*q.size() + j], where j is a bin entry corresponding to quantization q.
  188. */
  189. double *hik_prepare_alpha_multiplications_fast(const NICE::VVector & A, const NICE::VVector & B, const Quantization & q, const ParameterizedFunction *pf = NULL ) const;
  190. /**
  191. * @brief compute lookup table for HIK calculation using quantized signals and prepare for K*alpha or k_*^T * alpha computations
  192. * @author Alexander Freytag
  193. *
  194. * @param alpha coefficient vector
  195. * @param q Quantization
  196. * @param pf ParameterizedFunction to change the original feature values
  197. *
  198. * @return C standard vector representing a q.size()*n double matrix and the lookup table T. Elements can be accessed with
  199. * T[dim*q.size() + j], where j is a bin entry corresponding to quantization q.
  200. */
  201. double* hikPrepareLookupTable(const NICE::Vector & alpha, const Quantization & q, const ParameterizedFunction *pf = NULL) const;
  202. /**
  203. * @brief update the lookup table for HIK calculation using quantized signals and prepare for K*alpha or k_*^T * alpha computations
  204. * @author Alexander Freytag
  205. *
  206. * @param T previously computed LUT, that will be changed
  207. * @param alphaNew new value of alpha at index idx
  208. * @param alphaOld old value of alpha at index idx
  209. * @param idx index in which alpha changed
  210. * @param q Quantization
  211. * @param pf ParameterizedFunction to change the original feature values
  212. */
  213. void hikUpdateLookupTable(double * T, const double & alphaNew, const double & alphaOld, const int & idx, const Quantization & q, const ParameterizedFunction *pf ) const;
  214. /**
  215. * @brief return a reference to the sorted feature matrix
  216. */
  217. FeatureMatrix & featureMatrix(void) { return X_sorted; };
  218. const FeatureMatrix & featureMatrix(void) const { return X_sorted; };
  219. /**
  220. * @brief solve the linear system K*alpha = y with the minimum kernel trick based on the algorithm of Wu (Wu10_AFD)
  221. * @note method converges slowly for large scale problems and even for normal scale :(
  222. * @author Paul Bodesheim
  223. *
  224. * @param y right hand side of linear system
  225. * @param alpha final solution of the linear system
  226. * @param q Quantization
  227. * @param pf ParameterizedFunction to change the original feature values
  228. * @param useRandomSubsets true, if the order of examples in each iteration should be randomly sampled
  229. * @param maxIterations maximum number of iterations
  230. * @param sizeOfRandomSubset nr of Elements that should be randomly considered in each iteration (max: y.size())
  231. * @param minDelta minimum difference between two solutions alpha_t and alpha_{t+1} (convergence criterion)
  232. *
  233. * @return C standard vector representing a q.size()*n double matrix and the lookup table T. Elements can be accessed with
  234. * T[dim*q.size() + j], where j is a bin entry corresponding to quantization q.
  235. **/
  236. double *solveLin(const NICE::Vector & y, NICE::Vector & alpha, const Quantization & q, const ParameterizedFunction *pf = NULL, const bool & useRandomSubsets = true, uint maxIterations = 10000, const int & _sizeOfRandomSubset = (-1), double minDelta = 1e-7, bool timeAnalysis = false) const;
  237. //! set the noise parameter
  238. void setNoise ( double noise ) { this->noise = noise; }
  239. //! get the current noise parameter
  240. double getNoise (void) const { return noise; }
  241. double getFrobNormApprox();
  242. /**
  243. * @brief Prepare the efficient HIK-computations for the squared kernel vector |k_*|^2 : calculate the partial squared sums for each dimension.
  244. * @author Alexander Freytag
  245. * @date 10-04-2012 (dd-mm-yyyy)
  246. */
  247. void hikPrepareKVNApproximation(NICE::VVector & A) const;
  248. /**
  249. * @brief Compute lookup table for HIK calculation of |k_*|^2 assuming quantized test samples. You have to run hikPrepareSquaredKernelVector before
  250. * @author Alexander Freytag
  251. * @date 10-04-2012 (dd-mm-yyyy)
  252. *
  253. * @param A pre-calculation array computed by hikPrepareSquaredKernelVector
  254. * @param q Quantization
  255. * @param pf Parameterized Function to efficiently apply a function to the underlying data
  256. *
  257. * @return C standard vector representing a q.size()*d double matrix and the lookup table T. Elements can be accessed with
  258. * T[dim*q.size() + j], where j is a bin entry corresponding to quantization q.
  259. */
  260. double * hikPrepareKVNApproximationFast(NICE::VVector & A, const Quantization & q, const ParameterizedFunction *pf = NULL ) const;
  261. /**
  262. * @brief Compute lookup table for HIK calculation of |k_*|^2 assuming quantized test samples ( equals hikPrepareSquaredKernelVector + hikPrepareSquaredKernelVectorFast, but is faster). Approximation does not considere mixed terms between dimensions.
  263. * @author Alexander Freytag
  264. * @date 10-04-2012 (dd-mm-yyyy)
  265. *
  266. * @param q Quantization
  267. * @param pf ParameterizedFunction to change the original feature values
  268. *
  269. * @return C standard vector representing a q.size()*d double matrix and the lookup table T. Elements can be accessed with
  270. * T[dim*q.size() + j], where j is a bin entry corresponding to quantization q.
  271. */
  272. double* hikPrepareLookupTableForKVNApproximation(const Quantization & q, const ParameterizedFunction *pf = NULL) const;
  273. /**
  274. * @brief Approximate norm = |k_*|^2 using the minimum kernel trick and exploiting sparsity of the given feature vector. Approximation does not considere mixed terms between dimensions.
  275. * @author Alexander Freytag
  276. * @date 10-04-2012 (dd-mm-yyyy)
  277. *
  278. * @param A pre-computation matrix (VVector) (use the prepare method)
  279. * @param xstar new feature vector (SparseVector)
  280. * @param norm result of the squared norm approximation
  281. * @param pf optional feature transformation
  282. */
  283. void hikComputeKVNApproximation(const NICE::VVector & A, const NICE::SparseVector & xstar, double & norm, const ParameterizedFunction *pf = NULL ) ;
  284. /**
  285. * @brief Approximate norm = |k_*|^2 using a large lookup table created by hikPrepareSquaredKernelVector and hikPrepareSquaredKernelVectorFast or directly using hikPrepareLookupTableForSquaredKernelVector. Approximation does not considere mixed terms between dimensions.
  286. * @author Alexander Freytag
  287. * @date 10-04-2012 (dd-mm-yyyy)
  288. *
  289. * @param Tlookup large lookup table
  290. * @param q Quantization object
  291. * @param xstar feature vector (indirect k_*)
  292. * @param norm result of the calculation
  293. */
  294. void hikComputeKVNApproximationFast(const double *Tlookup, const Quantization & q, const NICE::SparseVector & xstar, double & norm ) const;
  295. /**
  296. * @brief Compute the kernel vector k_* between training examples and test example. Runtime. O(n \times D). Exploiting sparsity
  297. * @author Alexander Freytag
  298. * @date 13-04-2012 (dd-mm-yyyy)
  299. *
  300. * @param xstar feature vector
  301. * @param kstar kernel vector
  302. */
  303. void hikComputeKernelVector( const NICE::SparseVector & xstar, NICE::Vector & kstar) const;
  304. /** Persistent interface */
  305. virtual void restore ( std::istream & is, int format = 0 );
  306. virtual void store ( std::ostream & os, int format = 0 ) const;
  307. virtual void clear ();
  308. // ----------------- INCREMENTAL LEARNING METHODS -----------------------
  309. /**
  310. * @brief Add a new example to the feature-storage. You have to update the corresponding variables explicitely after that.
  311. * @author Alexander Freytag
  312. * @date 25-04-2012 (dd-mm-yyyy)
  313. *
  314. * @param _v new feature vector
  315. */
  316. void addExample(const NICE::SparseVector & _v, const ParameterizedFunction *pf = NULL);
  317. /**
  318. * @brief Add a new example to the feature-storage. You have to update the corresponding variables explicitely after that.
  319. * @author Alexander Freytag
  320. * @date 25-04-2012 (dd-mm-yyyy)
  321. *
  322. * @param _v new feature vector
  323. */
  324. void addExample(const std::vector<double> & _v, const ParameterizedFunction *pf = NULL);
  325. /**
  326. * @brief Updates A and B matrices for fast kernel multiplications and kernel sums. You need to compute the new alpha value and run addExample first!
  327. * @author Alexander Freytag
  328. * @date 25-04-2012 (dd-mm-yyyy)
  329. *
  330. * @param _v new feature vector
  331. * @param alpha new alpha value for the corresponding feature
  332. * @param A precomputed matrix A which will be updated accordingly
  333. * @param B precomputed matrix B which will be updated accordingly
  334. * @param pf optional feature transformation
  335. */
  336. void updatePreparationForAlphaMultiplications(const NICE::SparseVector & _v, const double & alpha, NICE::VVector & A, NICE::VVector & B, const ParameterizedFunction *pf = NULL) const;
  337. /**
  338. * @brief Updates LUT T for very fast kernel multiplications and kernel sums. You need to compute the new alpha value and run addExample first!
  339. * @author Alexander Freytag
  340. * @date 26-04-2012 (dd-mm-yyyy)
  341. *
  342. * @param _v new feature vector
  343. * @param alpha new alpha value for the corresponding feature
  344. * @param T precomputed lookup table, which will be updated
  345. * @param q quantization object to quantize possible test samples
  346. * @param pf optional feature transformation
  347. */
  348. void updateLookupTableForAlphaMultiplications(const NICE::SparseVector & _v, const double & alpha, double * T, const Quantization & q, const ParameterizedFunction *pf = NULL) const;
  349. /**
  350. * @brief Updates matrix A for approximations of the kernel vector norm. You need to run addExample first!
  351. * @author Alexander Freytag
  352. * @date 26-04-2012 (dd-mm-yyyy)
  353. *
  354. * @param _v new feature vector
  355. * @param A precomputed matrix A which will be updated accordingly
  356. * @param pf optional feature transformation
  357. */
  358. void updatePreparationForKVNApproximation(const NICE::SparseVector & _v, NICE::VVector & A, const ParameterizedFunction *pf = NULL) const;
  359. /**
  360. * @brief Updates LUT T for fast approximations of the kernel vector norm. You need to run addExample first!
  361. * @author Alexander Freytag
  362. * @date 26-04-2012 (dd-mm-yyyy)
  363. *
  364. * @param _v new feature vector
  365. * @param T precomputed lookup table, which will be updated
  366. * @param q quantization object to quantize possible test samples
  367. * @param pf optional feature transformation
  368. */
  369. void updateLookupTableForKVNApproximation(const NICE::SparseVector & _v, double * T, const Quantization & q, const ParameterizedFunction *pf = NULL) const;
  370. };
  371. } // namespace
  372. #endif