GMHIKernel.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * @file GMHIKernel.cpp
  3. * @brief Fast multiplication with histogram intersection kernel matrices (Implementation)
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 01/02/2012
  6. */
  7. #include <iostream>
  8. #include <core/vector/VVector.h>
  9. #include <core/basics/Timer.h>
  10. #include "GMHIKernel.h"
  11. using namespace NICE;
  12. using namespace std;
  13. GMHIKernel::GMHIKernel( FastMinKernel *_fmk, ParameterizedFunction *_pf, const Quantization *_q )
  14. {
  15. this->fmk = _fmk;
  16. this->q = _q;
  17. this->pf = _pf;
  18. verbose = false;
  19. useOldPreparation = false;
  20. }
  21. GMHIKernel::~GMHIKernel()
  22. {
  23. }
  24. /** multiply with a vector: A*x = y */
  25. void GMHIKernel::multiply (NICE::Vector & y, const NICE::Vector & x) const
  26. {
  27. //do we want to use any quantization at all?
  28. if (q != NULL)
  29. {
  30. double *T;
  31. if (useOldPreparation)
  32. {
  33. NICE::VVector A;
  34. NICE::VVector B;
  35. // prepare to calculate sum_i x_i K(x,x_i)
  36. fmk->hik_prepare_alpha_multiplications(x, A, B);
  37. T = fmk->hik_prepare_alpha_multiplications_fast(A, B, *q, pf);
  38. }
  39. else
  40. {
  41. T = fmk->hikPrepareLookupTable(x, *q, pf );
  42. }
  43. fmk->hik_kernel_multiply_fast ( T, *q, x, y );
  44. delete [] T;
  45. }
  46. else //no quantization
  47. {
  48. NICE::VVector A;
  49. NICE::VVector B;
  50. // prepare to calculate sum_i x_i K(x,x_i)
  51. fmk->hik_prepare_alpha_multiplications(x, A, B);
  52. if (verbose)
  53. {
  54. int sizeOfDouble (sizeof(double));
  55. int sizeOfA(0);
  56. int sizeOfB(0);
  57. for (uint i = 0; i < A.size(); i++)
  58. {
  59. sizeOfA += A[i].size();
  60. }
  61. for (uint i = 0; i < B.size(); i++)
  62. {
  63. sizeOfB += B[i].size();
  64. }
  65. sizeOfA*=sizeOfDouble;
  66. sizeOfB*=sizeOfDouble;
  67. std::cerr << "multiplySparse: sizeof(A) + sizeof(B): " << sizeOfA + sizeOfB << std::endl;
  68. }
  69. // y = K * x
  70. //we only need x as input argument to add x*noise to beta
  71. //all necessary information for the "real" multiplication is already stored in y
  72. fmk->hik_kernel_multiply(A, B, x, y);
  73. }
  74. }
  75. /** get the number of rows in A */
  76. uint GMHIKernel::rows () const
  77. {
  78. // return the number of examples
  79. return fmk->get_n();
  80. }
  81. /** get the number of columns in A */
  82. uint GMHIKernel::cols () const
  83. {
  84. // return the number of examples
  85. return fmk->get_n();
  86. }
  87. /** set verbose-flag needed for output of size of A and B */
  88. void GMHIKernel::setVerbose ( const bool& _verbose )
  89. {
  90. verbose = _verbose;
  91. }
  92. void GMHIKernel::setUseOldPreparation( const bool & _useOldPreparation)
  93. {
  94. useOldPreparation = _useOldPreparation;
  95. }
  96. uint GMHIKernel::getNumParameters() const
  97. {
  98. if ( pf == NULL )
  99. return 0;
  100. else
  101. return pf->parameters().size();
  102. }
  103. void GMHIKernel::getParameters(Vector & parameters) const
  104. {
  105. if ( pf == NULL )
  106. parameters.clear();
  107. else {
  108. parameters.resize( pf->parameters().size() );
  109. parameters = pf->parameters();
  110. }
  111. }
  112. void GMHIKernel::setParameters(const Vector & parameters)
  113. {
  114. if ( pf == NULL && parameters.size() > 0 )
  115. fthrow(Exception, "Unable to set parameters of a non-parameterized GMHIKernel object");
  116. pf->parameters() = parameters;
  117. fmk->applyFunctionToFeatureMatrix( pf );
  118. // only for debugging with small matrices: fmk->featureMatrix().print();
  119. }
  120. void GMHIKernel::getDiagonalElements ( Vector & diagonalElements ) const
  121. {
  122. fmk->featureMatrix().hikDiagonalElements(diagonalElements);
  123. // add sigma^2 I
  124. diagonalElements += fmk->getNoise();
  125. }
  126. void GMHIKernel::getFirstDiagonalElement ( double & diagonalElement ) const
  127. {
  128. Vector diagonalElements;
  129. fmk->featureMatrix().hikDiagonalElements(diagonalElements);
  130. diagonalElement = diagonalElements[0];
  131. // add sigma^2 I
  132. diagonalElement += fmk->getNoise();
  133. }
  134. bool GMHIKernel::outOfBounds(const Vector & parameters) const
  135. {
  136. if ( pf == NULL && parameters.size() > 0 )
  137. fthrow(Exception, "Unable to check the bounds of a parameter without any parameterization");
  138. Vector uB = pf->getParameterUpperBounds();
  139. Vector lB = pf->getParameterLowerBounds();
  140. if ( uB.size() != parameters.size() || lB.size() != parameters.size() )
  141. fthrow(Exception, "Dimension of lower/upper bound vector " << lB.size() << " and " << uB.size() << " does not match the size of the parameter vector " << parameters.size() << ".");
  142. for ( uint i = 0 ; i < parameters.size() ; i++ )
  143. if ( (parameters[i] < lB[i]) || (parameters[i] > uB[i]) )
  144. {
  145. if (verbose)
  146. std::cerr << "Parameter " << i << " is out of bounds: " << lB[i] << " <= " << parameters[i] << " <= " << uB[i] << std::endl;
  147. return true;
  148. }
  149. return false;
  150. }
  151. Vector GMHIKernel::getParameterLowerBounds() const
  152. {
  153. if ( pf == NULL )
  154. fthrow(Exception, "Unable to get the bounds without any parameterization");
  155. return pf->getParameterLowerBounds();
  156. }
  157. Vector GMHIKernel::getParameterUpperBounds() const
  158. {
  159. if ( pf == NULL )
  160. fthrow(Exception, "Unable to get the bounds without any parameterization");
  161. return pf->getParameterUpperBounds();
  162. }
  163. double GMHIKernel::approxFrobNorm() const
  164. {
  165. return this->fmk->getFrobNormApprox();
  166. }
  167. void GMHIKernel::setApproximationScheme(const int & _approxScheme)
  168. {
  169. this->fmk->setApproximationScheme(_approxScheme);
  170. }
  171. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  172. // interface specific methods for incremental extensions
  173. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  174. void GMHIKernel::addExample( const NICE::SparseVector * example,
  175. const double & label,
  176. const bool & performOptimizationAfterIncrement
  177. )
  178. {
  179. //nothing has to be done here, the fmk-object got new examples already in outer struct (FMKGPHyperparameterOptimization)
  180. }
  181. void GMHIKernel::addMultipleExamples( const std::vector< const NICE::SparseVector * > & newExamples,
  182. const NICE::Vector & newLabels,
  183. const bool & performOptimizationAfterIncrement
  184. )
  185. {
  186. //nothing has to be done here, the fmk-object got new examples already in outer struct (FMKGPHyperparameterOptimization)
  187. }