GeneralizedIntersectionKernelFunction.tcc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * @file GeneralizedIntersectionKernelFunction.cpp
  3. * @brief The generalized intersection kernel function as distance measure between two histograms interpreted as vectors (Implementation)
  4. * @author Alexander Freytag
  5. * @date 08-12-2011 (dd-mm-yyyy)
  6. */
  7. #include <gp-hik-core/SortedVectorSparse.h>
  8. #include "GeneralizedIntersectionKernelFunction.h"
  9. #include <math.h>
  10. using namespace NICE;
  11. template <typename T>
  12. GeneralizedIntersectionKernelFunction<T>::GeneralizedIntersectionKernelFunction()
  13. {
  14. exponent = 1.0;
  15. }
  16. template <typename T>
  17. GeneralizedIntersectionKernelFunction<T>::GeneralizedIntersectionKernelFunction(const double & _exponent)
  18. {
  19. exponent = _exponent;
  20. }
  21. template <typename T>
  22. GeneralizedIntersectionKernelFunction<T>::~GeneralizedIntersectionKernelFunction()
  23. {
  24. }
  25. template <typename T>
  26. void GeneralizedIntersectionKernelFunction<T>::set_exponent(const double & _exponent)
  27. {
  28. exponent = _exponent;
  29. }
  30. template <typename T>
  31. double GeneralizedIntersectionKernelFunction<T>::get_exponent()
  32. {
  33. return exponent;
  34. }
  35. template <typename T>
  36. double GeneralizedIntersectionKernelFunction<T>::measureDistance ( const std::vector<T> & a, const std::vector<T> & b )
  37. {
  38. int size( (int) a.size());
  39. if ((int) b.size() < size)
  40. size = (int) b.size();
  41. double distance(0.0);
  42. for (int i = 0; i < size; i++)
  43. {
  44. if ( a[i] < b[i])
  45. distance += pow((double) a[i],exponent);
  46. else
  47. distance += pow((double) b[i],exponent);
  48. }
  49. return distance;
  50. }
  51. template <typename T>
  52. NICE::Matrix GeneralizedIntersectionKernelFunction<T>::computeKernelMatrix ( const std::vector<std::vector<T> > & X )
  53. {
  54. NICE::Matrix K;
  55. K.resize((int) X.size(), (int) X.size());
  56. for (int i = 0; i < (int) X.size(); i++)
  57. {
  58. for (int j = i; j < (int) X.size(); j++)
  59. {
  60. //Kernel matrix has to be symmetric
  61. K(i,j) = measureDistance(X[i],X[j]);
  62. K(j,i) = measureDistance(X[i],X[j]);
  63. }
  64. }
  65. return K;
  66. }
  67. template <typename T>
  68. NICE::Matrix GeneralizedIntersectionKernelFunction<T>::computeKernelMatrix ( const std::vector<std::vector<T> > & X , const double & noise)
  69. {
  70. NICE::Matrix K(computeKernelMatrix(X));
  71. for (int i = 0; i < (int) X.size(); i++)
  72. K(i,i) += noise;
  73. return K;
  74. }
  75. template <typename T>
  76. NICE::Matrix GeneralizedIntersectionKernelFunction<T>::computeKernelMatrix ( const NICE::FeatureMatrixT<T> & X , const double & noise)
  77. {
  78. NICE::Matrix K;
  79. K.resize(X.get_n(), X.get_n());
  80. //run over every dimension and add the corresponding min-values to the entries in the kernel matrix
  81. for (int dim = 0; dim < X.get_d(); dim++)
  82. {
  83. const std::multimap< double, typename SortedVectorSparse<double>::dataelement> & nonzeroElements = X.getFeatureValues(dim).nonzeroElements();
  84. //compute the min-values (similarities) between every pair in this dimension, zero elements do not influence this
  85. SortedVectorSparse<double>::const_elementpointer it1 = nonzeroElements.begin();
  86. for (; it1 != nonzeroElements.end(); it1++)
  87. {
  88. int i(it1->second.first);
  89. SortedVectorSparse<double>::const_elementpointer it2 = it1;
  90. for (; it2 != nonzeroElements.end(); it2++)
  91. {
  92. int j(it2->second.first);
  93. double val(pow(std::min(it1->second.second, it2->second.second),exponent));
  94. K(i,j) += val;
  95. //kernel-matrix has to be symmetric, but avoid adding twice the value to the main-diagonal
  96. if ( i != j)
  97. K(j,i) += val;
  98. } // for-j-loop
  99. } // for-i-loop
  100. }//dim-loop
  101. //add noise on the main diagonal
  102. for (int i = 0; i < (int) X.get_n(); i++)
  103. K(i,i) += noise;
  104. return K;
  105. }
  106. template <typename T>
  107. std::vector<double> GeneralizedIntersectionKernelFunction<T>::computeKernelVector ( const std::vector<std::vector<T> > & X , const std::vector<T> & xstar)
  108. {
  109. std::vector<double> kstar;
  110. kstar.resize((int) X.size());
  111. for (int i = 0; i < (int) X.size(); i++)
  112. {
  113. kstar[i] = measureDistance(X[i], xstar);
  114. }
  115. return kstar;
  116. }
  117. template <typename T>
  118. void GeneralizedIntersectionKernelFunction<T>::sayYourName()
  119. {
  120. std::cerr << "I'm the Generalized Intersection Kernel." << std::endl;
  121. }