GMHIKernelRaw.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. initData(_examples);
  20. this->d_noise = _d_noise;
  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_A = new double *[this->num_dimension];
  114. this->table_B = new double *[this->num_dimension];
  115. for (uint i = 0; i < this->num_dimension; i++)
  116. {
  117. uint nnz = this->nnz_per_dimension[i];
  118. if (nnz>0) {
  119. this->table_A[i] = new double [ nnz ];
  120. this->table_B[i] = new double [ nnz ];
  121. } else {
  122. this->table_A[i] = NULL;
  123. this->table_B[i] = NULL;
  124. }
  125. }
  126. }
  127. double **GMHIKernelRaw::allocateTable() const
  128. {
  129. double **table;
  130. table = new double *[this->num_dimension];
  131. for (uint i = 0; i < this->num_dimension; i++)
  132. {
  133. uint nnz = this->nnz_per_dimension[i];
  134. if (nnz>0) {
  135. table[i] = new double [ nnz ];
  136. } else {
  137. table[i] = NULL;
  138. }
  139. }
  140. return table;
  141. }
  142. void GMHIKernelRaw::copyTable(double **src, double **dst) const
  143. {
  144. for (uint i = 0; i < this->num_dimension; i++)
  145. {
  146. uint nnz = this->nnz_per_dimension[i];
  147. if (nnz>0) {
  148. for (uint j = 0; j < nnz; j++)
  149. dst[i][j] = src[i][j];
  150. } else {
  151. dst[i] = NULL;
  152. }
  153. }
  154. }
  155. void GMHIKernelRaw::updateTables ( const NICE::Vector _x ) const
  156. {
  157. for (uint dim = 0; dim < this->num_dimension; dim++)
  158. {
  159. double alpha_sum = 0.0;
  160. double alpha_times_x_sum = 0.0;
  161. uint nnz = nnz_per_dimension[dim];
  162. // loop through all elements in sorted order
  163. sparseVectorElement *training_values_in_dim = examples_raw[dim];
  164. for ( uint cntNonzeroFeat = 0; cntNonzeroFeat < nnz; cntNonzeroFeat++, training_values_in_dim++ )
  165. {
  166. // index of the feature
  167. int index = training_values_in_dim->example_index;
  168. // element of the feature
  169. double elem = training_values_in_dim->value;
  170. alpha_times_x_sum += _x[index] * elem;
  171. this->table_A[dim][cntNonzeroFeat] = alpha_times_x_sum;
  172. alpha_sum += _x[index];
  173. this->table_B[dim][cntNonzeroFeat] = alpha_sum;
  174. }
  175. }
  176. }
  177. /** multiply with a vector: A*x = y */
  178. void GMHIKernelRaw::multiply (NICE::Vector & _y, const NICE::Vector & _x) const
  179. {
  180. // STEP 1: initialize tables A and B
  181. updateTables(_x);
  182. _y.resize( this->num_examples );
  183. _y.set(0.0);
  184. for (uint dim = 0; dim < this->num_dimension; dim++)
  185. {
  186. uint nnz = this->nnz_per_dimension[dim];
  187. uint nz = this->num_examples - nnz;
  188. if ( nnz == 0 ) {
  189. // all values are zero in this dimension :) and we can simply ignore the feature
  190. continue;
  191. }
  192. sparseVectorElement *training_values_in_dim = examples_raw[dim];
  193. for ( uint cntNonzeroFeat = 0; cntNonzeroFeat < nnz; cntNonzeroFeat++, training_values_in_dim++ )
  194. {
  195. uint feat = training_values_in_dim->example_index;
  196. uint inversePosition = cntNonzeroFeat;
  197. double fval = training_values_in_dim->value;
  198. double firstPart = this->table_A[dim][inversePosition];
  199. double secondPart = this->table_B[dim][nnz-1] - this->table_B[dim][inversePosition];
  200. _y[feat] += firstPart + fval * secondPart;
  201. }
  202. }
  203. for (uint feat = 0; feat < this->num_examples; feat++)
  204. _y[feat] += this->d_noise * _x[feat];
  205. }
  206. /** get the number of rows in A */
  207. uint GMHIKernelRaw::rows () const
  208. {
  209. // return the number of examples
  210. return num_examples;
  211. }
  212. /** get the number of columns in A */
  213. uint GMHIKernelRaw::cols () const
  214. {
  215. // return the number of examples
  216. return num_examples;
  217. }
  218. double **GMHIKernelRaw::getTableA() const
  219. {
  220. double **t = allocateTable();
  221. copyTable(this->table_A, t);
  222. return t;
  223. }
  224. double **GMHIKernelRaw::getTableB() const
  225. {
  226. double **t = allocateTable();
  227. copyTable(this->table_B, t);
  228. return t;
  229. }
  230. uint *GMHIKernelRaw::getNNZPerDimension() const
  231. {
  232. uint *v = new uint[this->num_dimension];
  233. for (uint i = 0; i < this->num_dimension; i++)
  234. v[i] = this->nnz_per_dimension[i];
  235. return v;
  236. }
  237. void NICE::GMHIKernelRaw::getDiagonalElements( NICE::Vector & _diagonalElements) const
  238. {
  239. _diagonalElements = this->diagonalElements;
  240. }