KernelLinCombNormalized.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @file KernelLinCombNormalized.cpp
  3. * @brief Interface for the popular exponential mercer kernels
  4. * @author Erik Rodner
  5. * @date 10/24/2007
  6. */
  7. #include <iostream>
  8. #include <math.h>
  9. #include "KernelLinCombNormalized.h"
  10. using namespace OBJREC;
  11. using namespace std;
  12. using namespace NICE;
  13. KernelLinCombNormalized::KernelLinCombNormalized( uint num, bool _exponential )
  14. : alpha ( num ), exponential(_exponential)
  15. {
  16. for ( uint i = 0 ; i < num ; i++ )
  17. alpha[i] = - log((double)num);
  18. }
  19. KernelLinCombNormalized::KernelLinCombNormalized ( const KernelLinCombNormalized & src )
  20. {
  21. alpha.resize ( src.alpha.size() );
  22. alpha = src.alpha;
  23. exponential = src.exponential;
  24. }
  25. KernelLinCombNormalized::~KernelLinCombNormalized()
  26. {
  27. }
  28. KernelLinCombNormalized *KernelLinCombNormalized::clone(void) const
  29. {
  30. return new KernelLinCombNormalized ( *this );
  31. }
  32. double KernelLinCombNormalized::K (const NICE::Vector & x, const NICE::Vector & y) const
  33. {
  34. fthrow(Exception, "Unable to calculate kernel values directly.");
  35. }
  36. void KernelLinCombNormalized::updateKernelData ( KernelData *kernelData ) const
  37. {
  38. NICE::Matrix & kernelMatrix = kernelData->getKernelMatrix();
  39. double norm = 0.0;
  40. for ( uint k = 0 ; k < alpha.size(); k++ )
  41. norm += exp(alpha[k]);
  42. for ( uint k = 0 ; k < alpha.size(); k++ )
  43. {
  44. const NICE::Matrix & cachedMatrix = kernelData->getCachedMatrix ( k );
  45. if ( k == 0 ) {
  46. kernelMatrix.resize ( cachedMatrix.rows(), cachedMatrix.cols() );
  47. kernelMatrix.set(0.0);
  48. }
  49. kernelMatrix += exp(alpha[k])/norm * cachedMatrix;
  50. }
  51. if ( exponential ) {
  52. for ( uint i = 0 ; i < kernelMatrix.rows(); i++ )
  53. for ( uint j = 0 ; j < kernelMatrix.cols(); j++ )
  54. kernelMatrix(i,j) = exp(- kernelMatrix(i,j));
  55. }
  56. }
  57. void KernelLinCombNormalized::getKernelJacobi ( size_t parameter, const NICE::Vector & parameters, const KernelData *kernelData, NICE::Matrix & jacobiMatrix ) const
  58. {
  59. // double _gammasq = exp(2*parameters[0]);
  60. const NICE::Matrix & kernelMatrix = kernelData->getKernelMatrix();
  61. if ( parameter >= alpha.size() )
  62. fthrow(Exception, "Parameter index exceeds parameter vector size" );
  63. jacobiMatrix.resize ( kernelMatrix.rows(), kernelMatrix.cols() );
  64. #if 0
  65. if ( exponential ) {
  66. for ( uint i = 0 ; i < jacobiMatrix.rows(); i++ )
  67. for ( uint j = 0 ; j < jacobiMatrix.cols(); j++ )
  68. jacobiMatrix(i,j) = - kernelMatrixParameter(i,j) * exp(alpha[parameter]) * kernelMatrix(i,j);
  69. } else {
  70. jacobiMatrix = exp(alpha[parameter]) * kernelMatrixParameter;
  71. }
  72. #endif
  73. // derivation of \sum_k exp(alpha_k) / (sum_j exp(alpha_j)) * K_k = \sum_k exp(\alpha_k) / norm * K_k
  74. //
  75. // \sum_k (k==parameter) ...
  76. double norm = 0.0;
  77. for ( uint k = 0 ; k < alpha.size(); k++ )
  78. norm += exp(alpha[k]);
  79. Matrix tmp ( kernelMatrix.rows(), kernelMatrix.cols() );
  80. tmp.set(0.0);
  81. for ( uint k = 0 ; k < alpha.size(); k++ )
  82. {
  83. const Matrix & kernelMatrixSingle = kernelData->getCachedMatrix( k );
  84. for ( uint i = 0 ; i < tmp.rows(); i++ )
  85. for ( uint j = 0 ; j < tmp.cols(); j++ )
  86. if ( k == parameter ) {
  87. tmp(i,j) += (exp(alpha[k])/norm) * ( 1 - exp(alpha[k])/norm ) * kernelMatrixSingle(i,j);
  88. } else {
  89. tmp(i,j) -= (exp(alpha[k])/norm) * (exp(alpha[parameter])/norm) * kernelMatrixSingle(i,j);
  90. }
  91. }
  92. if ( exponential ) {
  93. for ( uint i = 0 ; i < jacobiMatrix.rows(); i++ )
  94. for ( uint j = 0 ; j < jacobiMatrix.cols(); j++ )
  95. jacobiMatrix(i,j) = - tmp(i,j) * kernelMatrix(i,j);
  96. } else {
  97. jacobiMatrix = tmp;
  98. }
  99. }
  100. void KernelLinCombNormalized::setParameters( const NICE::Vector & newParameters )
  101. {
  102. alpha = newParameters;
  103. }
  104. void KernelLinCombNormalized::getParameters( NICE::Vector & newParameters ) const
  105. {
  106. newParameters.resize ( alpha.size() );
  107. newParameters = alpha;
  108. }