GPLikelihoodApprox.h 4.2 KB

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