FastMinKernel.h 21 KB

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