GPLikelihoodApprox.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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<int, Vector> binaryLabels;
  40. /** number of classes */
  41. int 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, const FeatureMatrix & f, const std::map< int, NICE::Vector > & yset, double noise, double lambdaMax );
  46. //! last alpha vectors computed (from previous IL-step)
  47. std::map<int, NICE::Vector> * initialAlphaGuess;
  48. //! alpha vectors of the best solution
  49. std::map<int, Vector> min_alphas;
  50. //! minimal value of the likelihood
  51. double min_nlikelihood;
  52. //! best hyperparameter vector
  53. Vector min_parameter;
  54. //! function value pairs already visited
  55. std::map<unsigned long, double> alreadyVisited;
  56. //! to check whether the current solution of our optimization routine is too small
  57. double parameterLowerBound;
  58. //! to check whether the current solution of our optimization routine is too large
  59. double parameterUpperBound;
  60. //! Just for debugging to verify wheter the likelihood approximation is useful at all
  61. bool verifyApproximation;
  62. /** verbose flag */
  63. bool verbose;
  64. /** debug flag for several outputs useful for debugging*/
  65. bool debug;
  66. public:
  67. // ------ constructors and destructors ------
  68. /** simple constructor */
  69. GPLikelihoodApprox( const std::map<int, Vector> & binaryLabels,
  70. ImplicitKernelMatrix *ikm,
  71. IterativeLinearSolver *linsolver,
  72. EigValues *eig,
  73. bool verifyApproximation = false,
  74. int _nrOfEigenvaluesToConsider = 1
  75. );
  76. /** simple destructor */
  77. virtual ~GPLikelihoodApprox();
  78. // ------ main methods ------
  79. /**
  80. * @brief Compute alpha vectors for given hyperparameters
  81. *
  82. * @param x vector with specified hyperparameters to evaluate their likelihood
  83. *
  84. * @return void
  85. */
  86. void computeAlphaDirect(const OPTIMIZATION::matrix_type & x, const NICE::Vector & eigenValues);
  87. /**
  88. * @brief Evaluate the likelihood for given hyperparameters
  89. *
  90. * @param x vector with specified hyperparameters to evaluate their likelihood
  91. *
  92. * @return likelihood
  93. */
  94. virtual double evaluate(const OPTIMIZATION::matrix_type & x);
  95. // ------ get and set methods ------
  96. const NICE::Vector & getBestParameters () const { return min_parameter; };
  97. const std::map<int, Vector> & getBestAlphas () const;
  98. void setParameterLowerBound(const double & _parameterLowerBound);
  99. void setParameterUpperBound(const double & _parameterUpperBound);
  100. void setInitialAlphaGuess(std::map<int, NICE::Vector> * _initialAlphaGuess);
  101. void setBinaryLabels(const std::map<int, Vector> & _binaryLabels);
  102. void setVerbose( const bool & _verbose );
  103. void setDebug( const bool & _debug );
  104. bool getVerbose ( ) { return verbose; } ;
  105. bool getDebug ( ) { return debug; } ;
  106. };
  107. }
  108. #endif