GMHIKernel.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 ( this->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, this->q, pf);
  38. }
  39. else
  40. {
  41. T = fmk->hikPrepareLookupTable(x, this->q, pf );
  42. }
  43. fmk->hik_kernel_multiply_fast ( T, this->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 ( this->pf == NULL )
  99. return 0;
  100. else
  101. return this->pf->parameters().size();
  102. }
  103. void GMHIKernel::getParameters( NICE::Vector & parameters ) const
  104. {
  105. if ( this->pf == NULL )
  106. parameters.clear();
  107. else {
  108. parameters.resize( this->pf->parameters().size() );
  109. parameters = this->pf->parameters();
  110. }
  111. }
  112. void GMHIKernel::setParameters( const NICE::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. }
  119. void GMHIKernel::getDiagonalElements ( Vector & diagonalElements ) const
  120. {
  121. fmk->featureMatrix().hikDiagonalElements(diagonalElements);
  122. // add sigma^2 I
  123. diagonalElements += fmk->getNoise();
  124. }
  125. void GMHIKernel::getFirstDiagonalElement ( double & diagonalElement ) const
  126. {
  127. Vector diagonalElements;
  128. fmk->featureMatrix().hikDiagonalElements(diagonalElements);
  129. diagonalElement = diagonalElements[0];
  130. // add sigma^2 I
  131. diagonalElement += fmk->getNoise();
  132. }
  133. bool GMHIKernel::outOfBounds(const Vector & parameters) const
  134. {
  135. if ( pf == NULL && parameters.size() > 0 )
  136. fthrow(Exception, "Unable to check the bounds of a parameter without any parameterization");
  137. Vector uB = pf->getParameterUpperBounds();
  138. Vector lB = pf->getParameterLowerBounds();
  139. if ( uB.size() != parameters.size() || lB.size() != parameters.size() )
  140. fthrow(Exception, "Dimension of lower/upper bound vector " << lB.size() << " and " << uB.size() << " does not match the size of the parameter vector " << parameters.size() << ".");
  141. for ( uint i = 0 ; i < parameters.size() ; i++ )
  142. if ( (parameters[i] < lB[i]) || (parameters[i] > uB[i]) )
  143. {
  144. if (verbose)
  145. std::cerr << "Parameter " << i << " is out of bounds: " << lB[i] << " <= " << parameters[i] << " <= " << uB[i] << std::endl;
  146. return true;
  147. }
  148. return false;
  149. }
  150. Vector GMHIKernel::getParameterLowerBounds() const
  151. {
  152. if ( pf == NULL )
  153. fthrow(Exception, "Unable to get the bounds without any parameterization");
  154. return pf->getParameterLowerBounds();
  155. }
  156. Vector GMHIKernel::getParameterUpperBounds() const
  157. {
  158. if ( pf == NULL )
  159. fthrow(Exception, "Unable to get the bounds without any parameterization");
  160. return pf->getParameterUpperBounds();
  161. }
  162. double GMHIKernel::approxFrobNorm() const
  163. {
  164. return this->fmk->getFrobNormApprox();
  165. }
  166. void GMHIKernel::setApproximationScheme(const int & _approxScheme)
  167. {
  168. this->fmk->setApproximationScheme(_approxScheme);
  169. }
  170. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  171. // interface specific methods for incremental extensions
  172. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  173. void GMHIKernel::addExample( const NICE::SparseVector * example,
  174. const double & label,
  175. const bool & performOptimizationAfterIncrement
  176. )
  177. {
  178. //nothing has to be done here, the fmk-object got new examples already in outer struct (FMKGPHyperparameterOptimization)
  179. }
  180. void GMHIKernel::addMultipleExamples( const std::vector< const NICE::SparseVector * > & newExamples,
  181. const NICE::Vector & newLabels,
  182. const bool & performOptimizationAfterIncrement
  183. )
  184. {
  185. //nothing has to be done here, the fmk-object got new examples already in outer struct (FMKGPHyperparameterOptimization)
  186. }