GMHIKernelRaw.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 )
  14. {
  15. initData(_examples);
  16. }
  17. GMHIKernelRaw::~GMHIKernelRaw()
  18. {
  19. }
  20. void GMHIKernelRaw::initData ( const std::vector< const NICE::SparseVector *> &_examples )
  21. {
  22. if (_examples.size() == 0 )
  23. fthrow(Exception, "No examples given for learning");
  24. // TODO: clean up data if it exists
  25. this->num_dimension = _examples[0]->getDim();
  26. this->examples_raw = new sparseVectorElement *[num_dimension];
  27. this->nnz_per_dimension = new uint [num_dimension];
  28. this->num_examples = _examples.size();
  29. // waste memory and allocate a non-sparse data block
  30. sparseVectorElement **examples_raw_increment = new sparseVectorElement *[num_dimension];
  31. for (uint d = 0; d < num_dimension; d++)
  32. {
  33. this->examples_raw[d] = new sparseVectorElement [ this->num_dimension ];
  34. examples_raw_increment[d] = this->examples_raw[d];
  35. this->nnz_per_dimension[d] = 0;
  36. }
  37. uint example_index = 0;
  38. for (std::vector< const NICE::SparseVector * >::const_iterator i = _examples.begin();
  39. i != _examples.end(); i++, example_index++)
  40. {
  41. const NICE::SparseVector *x = *i;
  42. for ( NICE::SparseVector::const_iterator j = x->begin(); j != x->end(); j++ )
  43. {
  44. uint index = j->first;
  45. double value = j->second;
  46. examples_raw_increment[index]->value = value;
  47. examples_raw_increment[index]->example_index = example_index;
  48. // move to the next element
  49. examples_raw_increment[index]++;
  50. this->nnz_per_dimension[index]++;
  51. }
  52. }
  53. // sort along each dimension
  54. for (uint d = 0; d < this->num_dimension; d++)
  55. {
  56. std::sort( this->examples_raw[d], this->examples_raw[d] + this->nnz_per_dimension[d] );
  57. }
  58. }
  59. /** multiply with a vector: A*x = y */
  60. void GMHIKernelRaw::multiply (NICE::Vector & y, const NICE::Vector & x) const
  61. {
  62. /*
  63. NICE::VVector A;
  64. NICE::VVector B;
  65. // prepare to calculate sum_i x_i K(x,x_i)
  66. fmk->hik_prepare_alpha_multiplications(x, A, B);
  67. fmk->hik_kernel_multiply(A, B, x, y);
  68. */
  69. }
  70. /** get the number of rows in A */
  71. uint GMHIKernelRaw::rows () const
  72. {
  73. // return the number of examples
  74. return num_examples;
  75. }
  76. /** get the number of columns in A */
  77. uint GMHIKernelRaw::cols () const
  78. {
  79. // return the number of examples
  80. return num_examples;
  81. }