FastMinKernel.h 23 KB

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