GPLikelihoodApprox.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * @file GPLikelihoodApprox.h
  3. * @brief GP likelihood approximation as a cost function (Interface)
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 02/09/2012
  6. */
  7. #ifndef _NICE_GPLIKELIHOODAPPROXINCLUDE
  8. #define _NICE_GPLIKELIHOODAPPROXINCLUDE
  9. // STL includes
  10. #include <map>
  11. // NICE-core includes
  12. #include <core/algebra/EigValues.h>
  13. #include <core/algebra/IterativeLinearSolver.h>
  14. //
  15. #include <core/basics/Config.h>
  16. #include <core/optimization/blackbox/CostFunction.h>
  17. //
  18. #include <core/vector/VectorT.h>
  19. // gp-hik-core includes
  20. #include "gp-hik-core/FastMinKernel.h"
  21. #include "gp-hik-core/ImplicitKernelMatrix.h"
  22. #include "gp-hik-core/parameterizedFunctions/ParameterizedFunction.h"
  23. namespace NICE {
  24. /**
  25. * @class GPLikelihoodApprox
  26. * @brief GP likelihood approximation as a cost function
  27. * @author Erik Rodner, Alexander Freytag
  28. */
  29. class GPLikelihoodApprox : public OPTIMIZATION::CostFunction
  30. {
  31. protected:
  32. /** method computing eigenvalues */
  33. EigValues *eig;
  34. /** method for solving linear equation systems */
  35. IterativeLinearSolver *linsolver;
  36. /** object providing fast calculations */
  37. ImplicitKernelMatrix *ikm;
  38. /** set of binary label vectors */
  39. std::map<uint, Vector> binaryLabels;
  40. /** number of classes */
  41. uint nrOfClasses;
  42. /** To define how fine the approximation of the squared frobenius norm will be*/
  43. int nrOfEigenvaluesToConsider;
  44. //! only for debugging purposes, printing some statistics
  45. void calculateLikelihood ( double _mypara,
  46. const FeatureMatrix & _f,
  47. const std::map< uint, NICE::Vector > & _yset,
  48. double _noise,
  49. double _lambdaMax );
  50. //! last alpha vectors computed (from previous IL-step)
  51. std::map<uint, NICE::Vector> * initialAlphaGuess;
  52. //! alpha vectors of the best solution
  53. std::map<uint, Vector> min_alphas;
  54. //! minimal value of the likelihood
  55. double min_nlikelihood;
  56. //! best hyperparameter vector
  57. Vector min_parameter;
  58. //! function value pairs already visited
  59. std::map<unsigned long, double> alreadyVisited;
  60. //! to check whether the current solution of our optimization routine is too small
  61. double parameterLowerBound;
  62. //! to check whether the current solution of our optimization routine is too large
  63. double parameterUpperBound;
  64. //! Just for debugging to verify wheter the likelihood approximation is useful at all
  65. bool verifyApproximation;
  66. /** verbose flag */
  67. bool verbose;
  68. /** debug flag for several outputs useful for debugging*/
  69. bool debug;
  70. public:
  71. // ------ constructors and destructors ------
  72. /** simple constructor */
  73. GPLikelihoodApprox( const std::map<uint, Vector> & _binaryLabels,
  74. ImplicitKernelMatrix *_ikm,
  75. IterativeLinearSolver *_linsolver,
  76. EigValues *_eig,
  77. bool _verifyApproximation = false,
  78. int _nrOfEigenvaluesToConsider = 1
  79. );
  80. /** simple destructor */
  81. virtual ~GPLikelihoodApprox();
  82. // ------ main methods ------
  83. /**
  84. * @brief Compute alpha vectors for given hyperparameters
  85. *
  86. * @param x vector with specified hyperparameters to evaluate their likelihood
  87. *
  88. * @return void
  89. */
  90. void computeAlphaDirect(const OPTIMIZATION::matrix_type & _x,
  91. const NICE::Vector & _eigenValues
  92. );
  93. /**
  94. * @brief Evaluate the likelihood for given hyperparameters
  95. *
  96. * @param x vector with specified hyperparameters to evaluate their likelihood
  97. *
  98. * @return likelihood
  99. */
  100. virtual double evaluate(const OPTIMIZATION::matrix_type & x);
  101. // ------ get and set methods ------
  102. const NICE::Vector & getBestParameters () const { return min_parameter; };
  103. const std::map<uint, Vector> & getBestAlphas () const;
  104. void setParameterLowerBound(const double & _parameterLowerBound);
  105. void setParameterUpperBound(const double & _parameterUpperBound);
  106. void setInitialAlphaGuess(std::map<uint, NICE::Vector> * _initialAlphaGuess);
  107. void setBinaryLabels(const std::map<uint, Vector> & _binaryLabels);
  108. void setVerbose( const bool & _verbose );
  109. void setDebug( const bool & _debug );
  110. bool getVerbose ( ) { return verbose; } ;
  111. bool getDebug ( ) { return debug; } ;
  112. };
  113. }
  114. #endif