GMHIKernelRaw.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. initData(_examples);
  16. this->d_noise = _d_noise;
  17. }
  18. GMHIKernelRaw::~GMHIKernelRaw()
  19. {
  20. }
  21. void GMHIKernelRaw::initData ( const std::vector< const NICE::SparseVector *> &_examples )
  22. {
  23. if (_examples.size() == 0 )
  24. fthrow(Exception, "No examples given for learning");
  25. // TODO: clean up data if it exists
  26. this->num_dimension = _examples[0]->getDim();
  27. this->examples_raw = new sparseVectorElement *[num_dimension];
  28. this->nnz_per_dimension = new uint [num_dimension];
  29. this->num_examples = _examples.size();
  30. // waste memory and allocate a non-sparse data block
  31. sparseVectorElement **examples_raw_increment = new sparseVectorElement *[num_dimension];
  32. for (uint d = 0; d < this->num_dimension; d++)
  33. {
  34. this->examples_raw[d] = new sparseVectorElement [ this->num_examples ];
  35. examples_raw_increment[d] = this->examples_raw[d];
  36. this->nnz_per_dimension[d] = 0;
  37. }
  38. uint example_index = 0;
  39. for (std::vector< const NICE::SparseVector * >::const_iterator i = _examples.begin();
  40. i != _examples.end(); i++, example_index++)
  41. {
  42. const NICE::SparseVector *x = *i;
  43. for ( NICE::SparseVector::const_iterator j = x->begin(); j != x->end(); j++ )
  44. {
  45. uint index = j->first;
  46. double value = j->second;
  47. examples_raw_increment[index]->value = value;
  48. examples_raw_increment[index]->example_index = example_index;
  49. // move to the next element
  50. examples_raw_increment[index]++;
  51. this->nnz_per_dimension[index]++;
  52. }
  53. }
  54. // sort along each dimension
  55. for (uint d = 0; d < this->num_dimension; d++)
  56. {
  57. uint nnz = this->nnz_per_dimension[d];
  58. if ( nnz > 1 )
  59. std::sort( this->examples_raw[d], this->examples_raw[d] + nnz );
  60. }
  61. // pre-allocate the A and B matrices
  62. this->table_A = new double *[this->num_dimension];
  63. this->table_B = new double *[this->num_dimension];
  64. for (uint i = 0; i < this->num_dimension; i++)
  65. {
  66. uint nnz = this->nnz_per_dimension[i];
  67. if (nnz>0) {
  68. this->table_A[i] = new double [ nnz ];
  69. this->table_B[i] = new double [ nnz ];
  70. } else {
  71. this->table_A[i] = NULL;
  72. this->table_B[i] = NULL;
  73. }
  74. }
  75. }
  76. /** multiply with a vector: A*x = y */
  77. void GMHIKernelRaw::multiply (NICE::Vector & _y, const NICE::Vector & _x) const
  78. {
  79. // STEP 1: initialize tables A and B
  80. for (uint dim = 0; dim < this->num_dimension; dim++)
  81. {
  82. double alpha_sum = 0.0;
  83. double alpha_times_x_sum = 0.0;
  84. uint nnz = nnz_per_dimension[dim];
  85. // loop through all elements in sorted order
  86. sparseVectorElement *training_values_in_dim = examples_raw[dim];
  87. for ( uint cntNonzeroFeat = 0; cntNonzeroFeat < nnz; cntNonzeroFeat++, training_values_in_dim++ )
  88. {
  89. // index of the feature
  90. int index = training_values_in_dim->example_index;
  91. // element of the feature
  92. double elem = training_values_in_dim->value;
  93. alpha_times_x_sum += _x[index] * elem;
  94. this->table_A[dim][cntNonzeroFeat] = alpha_times_x_sum;
  95. alpha_sum += _x[index];
  96. this->table_B[dim][cntNonzeroFeat] = alpha_sum;
  97. cntNonzeroFeat++;
  98. }
  99. }
  100. _y.resize( this->num_examples );
  101. _y.set(0.0);
  102. for (uint dim = 0; dim < this->num_dimension; dim++)
  103. {
  104. uint nnz = this->nnz_per_dimension[dim];
  105. if ( nnz == this->num_examples ) {
  106. // all values are zero in this dimension :) and we can simply ignore the feature
  107. continue;
  108. }
  109. sparseVectorElement *training_values_in_dim = examples_raw[dim];
  110. for ( uint cntNonzeroFeat = 0; cntNonzeroFeat < nnz; cntNonzeroFeat++, training_values_in_dim++ )
  111. {
  112. uint feat = training_values_in_dim->example_index;
  113. uint inversePosition = cntNonzeroFeat;
  114. double fval = training_values_in_dim->value;
  115. double firstPart( this->table_A[dim][inversePosition] );
  116. double secondPart( this->table_B[dim][this->num_examples-1-nnz] - this->table_B[dim][inversePosition]);
  117. _y[cntNonzeroFeat] += firstPart + fval * secondPart;
  118. }
  119. }
  120. for (uint feat = 0; feat < this->num_examples; feat++)
  121. {
  122. _y[feat] += this->d_noise * _x[feat];
  123. }
  124. }
  125. /** get the number of rows in A */
  126. uint GMHIKernelRaw::rows () const
  127. {
  128. // return the number of examples
  129. return num_examples;
  130. }
  131. /** get the number of columns in A */
  132. uint GMHIKernelRaw::cols () const
  133. {
  134. // return the number of examples
  135. return num_examples;
  136. }