GMHIKernelRaw.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /**
  2. * @file GMHIKernelRaw.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 "GMHIKernelRaw.h"
  11. using namespace NICE;
  12. using namespace std;
  13. GMHIKernelRaw::GMHIKernelRaw( const std::vector< const NICE::SparseVector *> &_examples, const double _d_noise )
  14. {
  15. this->examples_raw = NULL;
  16. this->nnz_per_dimension = NULL;
  17. this->table_A = NULL;
  18. this->table_B = NULL;
  19. this->d_noise = _d_noise;
  20. initData(_examples);
  21. }
  22. GMHIKernelRaw::~GMHIKernelRaw()
  23. {
  24. cleanupData();
  25. }
  26. void GMHIKernelRaw::cleanupData()
  27. {
  28. if ( this->examples_raw != NULL ) {
  29. for ( uint d = 0; d < this->num_dimension; d++ )
  30. if (examples_raw[d] != NULL)
  31. delete [] examples_raw[d];
  32. delete [] this->examples_raw;
  33. this->examples_raw = NULL;
  34. }
  35. if ( this->nnz_per_dimension != NULL ) {
  36. delete [] this->nnz_per_dimension;
  37. this->nnz_per_dimension = NULL;
  38. }
  39. if ( this->table_A != NULL ) {
  40. for ( uint d = 0; d < this->num_dimension; d++ )
  41. if (table_A[d] != NULL)
  42. delete [] table_A[d];
  43. delete [] this->table_A;
  44. this->table_A = NULL;
  45. }
  46. if ( this->table_B != NULL ) {
  47. for ( uint d = 0; d < this->num_dimension; d++ )
  48. if (table_B[d] != NULL)
  49. delete [] table_B[d];
  50. delete [] this->table_B;
  51. this->table_B = NULL;
  52. }
  53. }
  54. void GMHIKernelRaw::initData ( const std::vector< const NICE::SparseVector *> &_examples )
  55. {
  56. if (_examples.size() == 0 )
  57. fthrow(Exception, "No examples given for learning");
  58. cleanupData();
  59. this->num_dimension = _examples[0]->getDim();
  60. this->examples_raw = new sparseVectorElement *[num_dimension];
  61. this->nnz_per_dimension = new uint [num_dimension];
  62. this->num_examples = _examples.size();
  63. // waste memory and allocate a non-sparse data block
  64. sparseVectorElement **examples_raw_increment = new sparseVectorElement *[num_dimension];
  65. for (uint d = 0; d < this->num_dimension; d++)
  66. {
  67. this->examples_raw[d] = new sparseVectorElement [ this->num_examples ];
  68. examples_raw_increment[d] = this->examples_raw[d];
  69. this->nnz_per_dimension[d] = 0;
  70. }
  71. // additionally allocate a Vector with as many entries as examples
  72. // this vector will contain the L1 norm values of all examples + noise
  73. // thereby, it represents the diagonal entries of our kernel matrix for
  74. // the special case of minimum kernel
  75. this->diagonalElements.resize ( this->num_examples );
  76. this->diagonalElements.set ( this->d_noise );
  77. uint example_index = 0;
  78. NICE::Vector::iterator itDiagEl = this->diagonalElements.begin();
  79. // minor pre-allocation
  80. uint index;
  81. double value;
  82. double l1norm;
  83. for ( std::vector< const NICE::SparseVector * >::const_iterator i = _examples.begin();
  84. i != _examples.end();
  85. i++, example_index++, itDiagEl++
  86. )
  87. {
  88. l1norm = 0.0;
  89. const NICE::SparseVector *x = *i;
  90. for ( NICE::SparseVector::const_iterator j = x->begin(); j != x->end(); j++ )
  91. {
  92. index = j->first;
  93. value = j->second;
  94. examples_raw_increment[index]->value = value;
  95. examples_raw_increment[index]->example_index = example_index;
  96. // move to the next element
  97. examples_raw_increment[index]++;
  98. this->nnz_per_dimension[index]++;
  99. l1norm = l1norm + value;
  100. }
  101. *itDiagEl = *itDiagEl + l1norm;
  102. }
  103. delete [] examples_raw_increment;
  104. // sort along each dimension
  105. for (uint d = 0; d < this->num_dimension; d++)
  106. {
  107. uint nnz = this->nnz_per_dimension[d];
  108. if ( nnz > 1 )
  109. std::sort( this->examples_raw[d], this->examples_raw[d] + nnz );
  110. }
  111. // pre-allocate the A and B matrices
  112. this->table_A = allocateTable();
  113. this->table_B = allocateTable();
  114. }
  115. double **GMHIKernelRaw::allocateTable() const
  116. {
  117. double **table;
  118. table = new double *[this->num_dimension];
  119. for (uint i = 0; i < this->num_dimension; i++)
  120. {
  121. uint nnz = this->nnz_per_dimension[i];
  122. if (nnz>0) {
  123. table[i] = new double [ nnz ];
  124. } else {
  125. table[i] = NULL;
  126. }
  127. }
  128. return table;
  129. }
  130. void GMHIKernelRaw::copyTable(double **src, double **dst) const
  131. {
  132. for (uint i = 0; i < this->num_dimension; i++)
  133. {
  134. uint nnz = this->nnz_per_dimension[i];
  135. if (nnz>0) {
  136. for (uint j = 0; j < nnz; j++)
  137. dst[i][j] = src[i][j];
  138. } else {
  139. dst[i] = NULL;
  140. }
  141. }
  142. }
  143. void GMHIKernelRaw::updateTables ( const NICE::Vector _x ) const
  144. {
  145. for (uint dim = 0; dim < this->num_dimension; dim++)
  146. {
  147. double alpha_sum = 0.0;
  148. double alpha_times_x_sum = 0.0;
  149. uint nnz = nnz_per_dimension[dim];
  150. // loop through all elements in sorted order
  151. sparseVectorElement *training_values_in_dim = examples_raw[dim];
  152. for ( uint cntNonzeroFeat = 0; cntNonzeroFeat < nnz; cntNonzeroFeat++, training_values_in_dim++ )
  153. {
  154. // index of the feature
  155. int index = training_values_in_dim->example_index;
  156. // element of the feature
  157. double elem = training_values_in_dim->value;
  158. alpha_times_x_sum += _x[index] * elem;
  159. this->table_A[dim][cntNonzeroFeat] = alpha_times_x_sum;
  160. alpha_sum += _x[index];
  161. this->table_B[dim][cntNonzeroFeat] = alpha_sum;
  162. }
  163. }
  164. }
  165. /** multiply with a vector: A*x = y */
  166. void GMHIKernelRaw::multiply (NICE::Vector & _y, const NICE::Vector & _x) const
  167. {
  168. // STEP 1: initialize tables A and B
  169. updateTables(_x);
  170. _y.resize( this->num_examples );
  171. _y.set(0.0);
  172. for (uint dim = 0; dim < this->num_dimension; dim++)
  173. {
  174. uint nnz = this->nnz_per_dimension[dim];
  175. uint nz = this->num_examples - nnz;
  176. if ( nnz == 0 ) {
  177. // all values are zero in this dimension :) and we can simply ignore the feature
  178. continue;
  179. }
  180. sparseVectorElement *training_values_in_dim = examples_raw[dim];
  181. for ( uint cntNonzeroFeat = 0; cntNonzeroFeat < nnz; cntNonzeroFeat++, training_values_in_dim++ )
  182. {
  183. uint feat = training_values_in_dim->example_index;
  184. uint inversePosition = cntNonzeroFeat;
  185. double fval = training_values_in_dim->value;
  186. double firstPart = this->table_A[dim][inversePosition];
  187. double secondPart = this->table_B[dim][nnz-1] - this->table_B[dim][inversePosition];
  188. _y[feat] += firstPart + fval * secondPart;
  189. }
  190. }
  191. for (uint feat = 0; feat < this->num_examples; feat++)
  192. _y[feat] += this->d_noise * _x[feat];
  193. }
  194. /** get the number of rows in A */
  195. uint GMHIKernelRaw::rows () const
  196. {
  197. // return the number of examples
  198. return num_examples;
  199. }
  200. /** get the number of columns in A */
  201. uint GMHIKernelRaw::cols () const
  202. {
  203. // return the number of examples
  204. return num_examples;
  205. }
  206. double **GMHIKernelRaw::getTableA() const
  207. {
  208. double **t = allocateTable();
  209. copyTable(this->table_A, t);
  210. return t;
  211. }
  212. double **GMHIKernelRaw::getTableB() const
  213. {
  214. double **t = allocateTable();
  215. copyTable(this->table_B, t);
  216. return t;
  217. }
  218. uint *GMHIKernelRaw::getNNZPerDimension() const
  219. {
  220. uint *v = new uint[this->num_dimension];
  221. for (uint i = 0; i < this->num_dimension; i++)
  222. v[i] = this->nnz_per_dimension[i];
  223. return v;
  224. }
  225. void NICE::GMHIKernelRaw::getDiagonalElements( NICE::Vector & _diagonalElements) const
  226. {
  227. _diagonalElements = this->diagonalElements;
  228. }