FastMinKernel.h 20 KB

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