GMHIKernelRaw.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. // sort along each dimension
  88. for (uint d = 0; d < this->num_dimension; d++)
  89. {
  90. uint nnz = this->nnz_per_dimension[d];
  91. if ( nnz > 1 )
  92. std::sort( this->examples_raw[d], this->examples_raw[d] + nnz );
  93. }
  94. // pre-allocate the A and B matrices
  95. this->table_A = new double *[this->num_dimension];
  96. this->table_B = new double *[this->num_dimension];
  97. for (uint i = 0; i < this->num_dimension; i++)
  98. {
  99. uint nnz = this->nnz_per_dimension[i];
  100. if (nnz>0) {
  101. this->table_A[i] = new double [ nnz ];
  102. this->table_B[i] = new double [ nnz ];
  103. } else {
  104. this->table_A[i] = NULL;
  105. this->table_B[i] = NULL;
  106. }
  107. }
  108. }
  109. /** multiply with a vector: A*x = y */
  110. void GMHIKernelRaw::multiply (NICE::Vector & _y, const NICE::Vector & _x) const
  111. {
  112. // STEP 1: initialize tables A and B
  113. for (uint dim = 0; dim < this->num_dimension; dim++)
  114. {
  115. double alpha_sum = 0.0;
  116. double alpha_times_x_sum = 0.0;
  117. uint nnz = nnz_per_dimension[dim];
  118. // loop through all elements in sorted order
  119. sparseVectorElement *training_values_in_dim = examples_raw[dim];
  120. for ( uint cntNonzeroFeat = 0; cntNonzeroFeat < nnz; cntNonzeroFeat++, training_values_in_dim++ )
  121. {
  122. // index of the feature
  123. int index = training_values_in_dim->example_index;
  124. // element of the feature
  125. double elem = training_values_in_dim->value;
  126. alpha_times_x_sum += _x[index] * elem;
  127. this->table_A[dim][cntNonzeroFeat] = alpha_times_x_sum;
  128. alpha_sum += _x[index];
  129. this->table_B[dim][cntNonzeroFeat] = alpha_sum;
  130. cntNonzeroFeat++;
  131. }
  132. }
  133. _y.resize( this->num_examples );
  134. _y.set(0.0);
  135. for (uint dim = 0; dim < this->num_dimension; dim++)
  136. {
  137. uint nnz = this->nnz_per_dimension[dim];
  138. if ( nnz == this->num_examples ) {
  139. // all values are zero in this dimension :) and we can simply ignore the feature
  140. continue;
  141. }
  142. sparseVectorElement *training_values_in_dim = examples_raw[dim];
  143. for ( uint cntNonzeroFeat = 0; cntNonzeroFeat < nnz; cntNonzeroFeat++, training_values_in_dim++ )
  144. {
  145. uint feat = training_values_in_dim->example_index;
  146. uint inversePosition = cntNonzeroFeat;
  147. double fval = training_values_in_dim->value;
  148. double firstPart( this->table_A[dim][inversePosition] );
  149. double secondPart( this->table_B[dim][this->num_examples-1-nnz] - this->table_B[dim][inversePosition]);
  150. _y[cntNonzeroFeat] += firstPart + fval * secondPart;
  151. }
  152. }
  153. for (uint feat = 0; feat < this->num_examples; feat++)
  154. {
  155. _y[feat] += this->d_noise * _x[feat];
  156. }
  157. }
  158. /** get the number of rows in A */
  159. uint GMHIKernelRaw::rows () const
  160. {
  161. // return the number of examples
  162. return num_examples;
  163. }
  164. /** get the number of columns in A */
  165. uint GMHIKernelRaw::cols () const
  166. {
  167. // return the number of examples
  168. return num_examples;
  169. }