GMHIKernelRaw.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. uint example_index = 0;
  72. for (std::vector< const NICE::SparseVector * >::const_iterator i = _examples.begin();
  73. i != _examples.end(); i++, example_index++)
  74. {
  75. const NICE::SparseVector *x = *i;
  76. for ( NICE::SparseVector::const_iterator j = x->begin(); j != x->end(); j++ )
  77. {
  78. uint index = j->first;
  79. double value = j->second;
  80. examples_raw_increment[index]->value = value;
  81. examples_raw_increment[index]->example_index = example_index;
  82. // move to the next element
  83. examples_raw_increment[index]++;
  84. this->nnz_per_dimension[index]++;
  85. }
  86. }
  87. delete [] examples_raw_increment;
  88. // sort along each dimension
  89. for (uint d = 0; d < this->num_dimension; d++)
  90. {
  91. uint nnz = this->nnz_per_dimension[d];
  92. if ( nnz > 1 )
  93. std::sort( this->examples_raw[d], this->examples_raw[d] + nnz );
  94. }
  95. // pre-allocate the A and B matrices
  96. this->table_A = allocateTable();
  97. this->table_A = new double *[this->num_dimension];
  98. this->table_B = new double *[this->num_dimension];
  99. for (uint i = 0; i < this->num_dimension; i++)
  100. {
  101. uint nnz = this->nnz_per_dimension[i];
  102. if (nnz>0) {
  103. this->table_A[i] = new double [ nnz ];
  104. this->table_B[i] = new double [ nnz ];
  105. } else {
  106. this->table_A[i] = NULL;
  107. this->table_B[i] = NULL;
  108. }
  109. }
  110. }
  111. double **GMHIKernelRaw::allocateTable() const
  112. {
  113. double **table;
  114. table = 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. table[i] = new double [ nnz ];
  120. } else {
  121. table[i] = NULL;
  122. }
  123. }
  124. return table;
  125. }
  126. void GMHIKernelRaw::copyTable(double **src, double **dst) const
  127. {
  128. for (uint i = 0; i < this->num_dimension; i++)
  129. {
  130. uint nnz = this->nnz_per_dimension[i];
  131. if (nnz>0) {
  132. for (uint j = 0; j < nnz; j++)
  133. dst[i][j] = src[i][j];
  134. } else {
  135. dst[i] = NULL;
  136. }
  137. }
  138. }
  139. /** multiply with a vector: A*x = y */
  140. void GMHIKernelRaw::multiply (NICE::Vector & _y, const NICE::Vector & _x) const
  141. {
  142. // STEP 1: initialize tables A and B
  143. for (uint dim = 0; dim < this->num_dimension; dim++)
  144. {
  145. double alpha_sum = 0.0;
  146. double alpha_times_x_sum = 0.0;
  147. uint nnz = nnz_per_dimension[dim];
  148. // loop through all elements in sorted order
  149. sparseVectorElement *training_values_in_dim = examples_raw[dim];
  150. for ( uint cntNonzeroFeat = 0; cntNonzeroFeat < nnz; cntNonzeroFeat++, training_values_in_dim++ )
  151. {
  152. // index of the feature
  153. int index = training_values_in_dim->example_index;
  154. // element of the feature
  155. double elem = training_values_in_dim->value;
  156. alpha_times_x_sum += _x[index] * elem;
  157. this->table_A[dim][cntNonzeroFeat] = alpha_times_x_sum;
  158. alpha_sum += _x[index];
  159. this->table_B[dim][cntNonzeroFeat] = alpha_sum;
  160. }
  161. }
  162. _y.resize( this->num_examples );
  163. _y.set(0.0);
  164. for (uint dim = 0; dim < this->num_dimension; dim++)
  165. {
  166. uint nnz = this->nnz_per_dimension[dim];
  167. uint nz = this->num_examples - nnz;
  168. if ( nnz == 0 ) {
  169. // all values are zero in this dimension :) and we can simply ignore the feature
  170. continue;
  171. }
  172. sparseVectorElement *training_values_in_dim = examples_raw[dim];
  173. for ( uint cntNonzeroFeat = 0; cntNonzeroFeat < nnz; cntNonzeroFeat++, training_values_in_dim++ )
  174. {
  175. uint feat = training_values_in_dim->example_index;
  176. uint inversePosition = cntNonzeroFeat;
  177. double fval = training_values_in_dim->value;
  178. double firstPart = this->table_A[dim][inversePosition];
  179. double secondPart = this->table_B[dim][nnz-1] - this->table_B[dim][inversePosition];
  180. _y[feat] += firstPart + fval * secondPart;
  181. }
  182. }
  183. for (uint feat = 0; feat < this->num_examples; feat++)
  184. _y[feat] += this->d_noise * _x[feat];
  185. }
  186. /** get the number of rows in A */
  187. uint GMHIKernelRaw::rows () const
  188. {
  189. // return the number of examples
  190. return num_examples;
  191. }
  192. /** get the number of columns in A */
  193. uint GMHIKernelRaw::cols () const
  194. {
  195. // return the number of examples
  196. return num_examples;
  197. }
  198. double **GMHIKernelRaw::getTableA() const
  199. {
  200. double **t = allocateTable();
  201. copyTable(this->table_A, t);
  202. return t;
  203. }
  204. double **GMHIKernelRaw::getTableB() const
  205. {
  206. double **t = allocateTable();
  207. copyTable(this->table_B, t);
  208. return t;
  209. }
  210. uint *GMHIKernelRaw::getNNZPerDimension() const
  211. {
  212. uint *v = new uint[this->num_dimension];
  213. for (uint i = 0; i < this->num_dimension; i++)
  214. v[i] = this->nnz_per_dimension[i];
  215. return v;
  216. }