GMHIKernel.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. /** multiply with a vector: A_subset * x = y */
  76. void GMHIKernel::multiply (const PartialGenericMatrix::SetType & rowSet, const PartialGenericMatrix::SetType & columnSet, NICE::Vector & y, const NICE::Vector & x) const
  77. {
  78. if (q != NULL) {
  79. fthrow(NICE::Exception, "not implemented with quantization")
  80. } else {
  81. NICE::VVector A;
  82. NICE::VVector B;
  83. // prepare to calculate sum_i x_i K(x,x_i)
  84. fmk->hik_prepare_alpha_multiplications_subset(x, A, B, columnSet);
  85. // y = K * x
  86. //we only need x as input argument to add x*noise to beta
  87. //all necessary information for the "real" multiplication is already stored in y
  88. fmk->hik_kernel_multiply_subset(A, B, x, rowSet, columnSet, y);
  89. }
  90. }
  91. /** get the number of rows in A */
  92. uint GMHIKernel::rows () const
  93. {
  94. // return the number of examples
  95. return fmk->get_n();
  96. }
  97. /** get the number of columns in A */
  98. uint GMHIKernel::cols () const
  99. {
  100. // return the number of examples
  101. return fmk->get_n();
  102. }
  103. /** set verbose-flag needed for output of size of A and B */
  104. void GMHIKernel::setVerbose ( const bool& _verbose )
  105. {
  106. verbose = _verbose;
  107. }
  108. void GMHIKernel::setUseOldPreparation( const bool & _useOldPreparation)
  109. {
  110. useOldPreparation = _useOldPreparation;
  111. }
  112. uint GMHIKernel::getNumParameters() const
  113. {
  114. if ( pf == NULL )
  115. return 0;
  116. else
  117. return pf->parameters().size();
  118. }
  119. void GMHIKernel::getParameters(Vector & parameters) const
  120. {
  121. if ( pf == NULL )
  122. parameters.clear();
  123. else {
  124. parameters.resize( pf->parameters().size() );
  125. parameters = pf->parameters();
  126. }
  127. }
  128. void GMHIKernel::setParameters(const Vector & parameters)
  129. {
  130. if ( pf == NULL && parameters.size() > 0 )
  131. fthrow(Exception, "Unable to set parameters of a non-parameterized GMHIKernel object");
  132. pf->parameters() = parameters;
  133. fmk->applyFunctionToFeatureMatrix( pf );
  134. // only for debugging with small matrices: fmk->featureMatrix().print();
  135. }
  136. void GMHIKernel::getDiagonalElements ( Vector & diagonalElements ) const
  137. {
  138. fmk->featureMatrix().hikDiagonalElements(diagonalElements);
  139. // add sigma^2 I
  140. diagonalElements += fmk->getNoise();
  141. }
  142. double GMHIKernel::getDiagonalElement ( uint i ) const
  143. {
  144. Vector diagonalElements;
  145. getDiagonalElements(diagonalElements);
  146. return diagonalElements[i];
  147. }
  148. void GMHIKernel::getFirstDiagonalElement ( double & diagonalElement ) const
  149. {
  150. Vector diagonalElements;
  151. fmk->featureMatrix().hikDiagonalElements(diagonalElements);
  152. diagonalElement = diagonalElements[0];
  153. // add sigma^2 I
  154. diagonalElement += fmk->getNoise();
  155. }
  156. bool GMHIKernel::outOfBounds(const Vector & parameters) const
  157. {
  158. if ( pf == NULL && parameters.size() > 0 )
  159. fthrow(Exception, "Unable to check the bounds of a parameter without any parameterization");
  160. Vector uB = pf->getParameterUpperBounds();
  161. Vector lB = pf->getParameterLowerBounds();
  162. if ( uB.size() != parameters.size() || lB.size() != parameters.size() )
  163. fthrow(Exception, "Dimension of lower/upper bound vector " << lB.size() << " and " << uB.size() << " does not match the size of the parameter vector " << parameters.size() << ".");
  164. for ( uint i = 0 ; i < parameters.size() ; i++ )
  165. if ( (parameters[i] < lB[i]) || (parameters[i] > uB[i]) )
  166. {
  167. if (verbose)
  168. std::cerr << "Parameter " << i << " is out of bounds: " << lB[i] << " <= " << parameters[i] << " <= " << uB[i] << std::endl;
  169. return true;
  170. }
  171. return false;
  172. }
  173. Vector GMHIKernel::getParameterLowerBounds() const
  174. {
  175. if ( pf == NULL )
  176. fthrow(Exception, "Unable to get the bounds without any parameterization");
  177. return pf->getParameterLowerBounds();
  178. }
  179. Vector GMHIKernel::getParameterUpperBounds() const
  180. {
  181. if ( pf == NULL )
  182. fthrow(Exception, "Unable to get the bounds without any parameterization");
  183. return pf->getParameterUpperBounds();
  184. }
  185. double GMHIKernel::approxFrobNorm() const
  186. {
  187. return this->fmk->getFrobNormApprox();
  188. }
  189. void GMHIKernel::setApproximationScheme(const int & _approxScheme)
  190. {
  191. this->fmk->setApproximationScheme(_approxScheme);
  192. }
  193. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  194. // interface specific methods for incremental extensions
  195. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  196. void GMHIKernel::addExample( const NICE::SparseVector * example,
  197. const double & label,
  198. const bool & performOptimizationAfterIncrement
  199. )
  200. {
  201. //nothing has to be done here, the fmk-object got new examples already in outer struct (FMKGPHyperparameterOptimization)
  202. }
  203. void GMHIKernel::addMultipleExamples( const std::vector< const NICE::SparseVector * > & newExamples,
  204. const NICE::Vector & newLabels,
  205. const bool & performOptimizationAfterIncrement
  206. )
  207. {
  208. //nothing has to be done here, the fmk-object got new examples already in outer struct (FMKGPHyperparameterOptimization)
  209. }