GPHIKRegression.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * @file GPHIKRegression.h
  3. * @brief Main interface for our GP HIK regression implementation (Interface)
  4. * @author Alexander Freytag
  5. * @date 15-01-2014 (dd-mm-yyyy)
  6. */
  7. #ifndef _NICE_GPHIKREGRESSIONINCLUDE
  8. #define _NICE_GPHIKREGRESSIONINCLUDE
  9. // STL includes
  10. #include <string>
  11. #include <limits>
  12. // NICE-core includes
  13. #include <core/basics/Config.h>
  14. #include <core/basics/Persistent.h>
  15. //
  16. #include <core/vector/SparseVectorT.h>
  17. // gp-hik-core includes
  18. #include "gp-hik-core/FMKGPHyperparameterOptimization.h"
  19. #include "gp-hik-core/OnlineLearnable.h"
  20. #include "gp-hik-core/parameterizedFunctions/ParameterizedFunction.h"
  21. namespace NICE {
  22. /**
  23. * @class GPHIKRegression
  24. * @brief Main interface for our GP HIK regression implementation (Interface)
  25. * @author Alexander Freytag
  26. */
  27. class GPHIKRegression : public NICE::Persistent, public NICE::OnlineLearnable
  28. {
  29. protected:
  30. /////////////////////////
  31. /////////////////////////
  32. // PROTECTED VARIABLES //
  33. /////////////////////////
  34. /////////////////////////
  35. ///////////////////////////////////
  36. // output/debug related settings //
  37. ///////////////////////////////////
  38. /** verbose flag for useful output*/
  39. bool verbose;
  40. /** debug flag for several outputs useful for debugging*/
  41. bool debug;
  42. //////////////////////////////////////
  43. // general specifications //
  44. //////////////////////////////////////
  45. /** Header in configfile where variable settings are stored */
  46. std::string confSection;
  47. //////////////////////////////////////
  48. // classification related variables //
  49. //////////////////////////////////////
  50. /** memorize whether the classifier was already trained*/
  51. bool b_isTrained;
  52. // internal objects
  53. /** Main object doing all the jobs: training, regression, optimization, ... */
  54. NICE::FMKGPHyperparameterOptimization *gphyper;
  55. /** Gaussian label noise for model regularization */
  56. double noise;
  57. enum VarianceApproximation{
  58. APPROXIMATE_ROUGH,
  59. APPROXIMATE_FINE,
  60. EXACT,
  61. NONE
  62. };
  63. /** Which technique for variance approximations shall be used */
  64. VarianceApproximation varianceApproximation;
  65. /**compute the uncertainty prediction during regression?*/
  66. bool uncertaintyPredictionForRegression;
  67. /////////////////////////
  68. /////////////////////////
  69. // PROTECTED METHODS //
  70. /////////////////////////
  71. /////////////////////////
  72. public:
  73. /**
  74. * @brief default constructor
  75. * @author Alexander Freytag
  76. * @brief 06-02-2014 (dd-mm-yyyy)
  77. */
  78. GPHIKRegression( );
  79. /**
  80. * @brief standard constructor
  81. * @author Alexander Freytag
  82. */
  83. GPHIKRegression( const NICE::Config *conf = NULL, const std::string & s_confSection = "GPHIKRegression" );
  84. /**
  85. * @brief simple destructor
  86. * @author Alexander Freytag
  87. */
  88. ~GPHIKRegression();
  89. /**
  90. * @brief Setup internal variables and objects used
  91. * @author Alexander Freytag
  92. * @param conf Config file to specify variable settings
  93. * @param s_confSection
  94. */
  95. void initFromConfig(const NICE::Config *conf, const std::string & s_confSection);
  96. ///////////////////// ///////////////////// /////////////////////
  97. // GET / SET
  98. ///////////////////// ///////////////////// /////////////////////
  99. ///////////////////// ///////////////////// /////////////////////
  100. // REGRESSION STUFF
  101. ///////////////////// ///////////////////// /////////////////////
  102. /**
  103. * @brief Estimate output of a given example with the previously learnt model
  104. * @date 15-01-2014 (dd-mm-yyyy)
  105. * @author Alexander Freytag
  106. * @param example (SparseVector) for which regression shall be performed, given in a sparse representation
  107. * @param result (double) regression result
  108. */
  109. void estimate ( const NICE::SparseVector * example, double & result ) const;
  110. /**
  111. * @brief Estimate output of a given example with the previously learnt model
  112. ** @date 15-01-2014 (dd-mm-yyyy)
  113. * @author Alexander Freytag
  114. * @param example (SparseVector) for which regression shall be performed, given in a sparse representation
  115. * @param result (double) regression result
  116. * @param uncertainty (double*) predictive variance of the regression result, if computed
  117. */
  118. void estimate ( const NICE::SparseVector * example, double & result, double & uncertainty ) const;
  119. /**
  120. * @brief Estimate output of a given example with the previously learnt model
  121. * NOTE: whenever possible, you should the sparse version to obtain significantly smaller computation times*
  122. * @date 15-01-2014 (dd-mm-yyyy)
  123. * @author Alexander Freytag
  124. * @param example (non-sparse Vector) for which regression shall be performed, given in a non-sparse representation
  125. * @param result (double) regression result
  126. */
  127. void estimate ( const NICE::Vector * example, double & result ) const;
  128. /**
  129. * @brief Estimate output of a given example with the previously learnt model
  130. * NOTE: whenever possible, you should the sparse version to obtain significantly smaller computation times
  131. * @date 15-01-2014 (dd-mm-yyyy)
  132. * @author Alexander Freytag
  133. * @param example (non-sparse Vector) for which regression shall be performed, given in a non-sparse representation
  134. * @param result (double)regression result
  135. * @param uncertainty (double*) predictive variance of the regression result, if computed
  136. */
  137. void estimate ( const NICE::Vector * example, double & result, double & uncertainty ) const;
  138. /**
  139. * @brief train this regression method using a given set of examples and corresponding labels
  140. * @date 15-01-2014 (dd-mm-yyyy)
  141. * @author Alexander Freytag
  142. * @param examples (std::vector< NICE::SparseVector *>) training data given in a sparse representation
  143. * @param labels (Vector) labels
  144. */
  145. void train ( const std::vector< const NICE::SparseVector *> & examples, const NICE::Vector & labels );
  146. /**
  147. * @brief Clone regression object
  148. * @author Alexander Freytag
  149. */
  150. GPHIKRegression *clone () const;
  151. /**
  152. * @brief prediction of regression uncertainty
  153. * @date 15-01-2014 (dd-mm-yyyy)
  154. * @author Alexander Freytag
  155. * @param examples example for which the regression uncertainty shall be predicted, given in a sparse representation
  156. * @param uncertainty contains the resulting regression uncertainty
  157. */
  158. void predictUncertainty( const NICE::SparseVector * example, double & uncertainty ) const;
  159. /**
  160. * @brief prediction of regression uncertainty
  161. * @date 15-01-2014 (dd-mm-yyyy)
  162. * @author Alexander Freytag
  163. * @param examples example for which the regression uncertainty shall be predicted, given in a non-sparse representation
  164. * @param uncertainty contains the resulting regression uncertainty
  165. */
  166. void predictUncertainty( const NICE::Vector * example, double & uncertainty ) const;
  167. ///////////////////// INTERFACE PERSISTENT /////////////////////
  168. // interface specific methods for store and restore
  169. ///////////////////// INTERFACE PERSISTENT /////////////////////
  170. /**
  171. * @brief Load regression object from external file (stream)
  172. * @author Alexander Freytag
  173. */
  174. void restore ( std::istream & is, int format = 0 );
  175. /**
  176. * @brief Save regression object to external file (stream)
  177. * @author Alexander Freytag
  178. */
  179. void store ( std::ostream & os, int format = 0 ) const;
  180. /**
  181. * @brief Clear regression object
  182. * @author Alexander Freytag
  183. */
  184. void clear ();
  185. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  186. // interface specific methods for incremental extensions
  187. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  188. /**
  189. * @brief add a new example
  190. * @author Alexander Freytag
  191. */
  192. virtual void addExample( const NICE::SparseVector * example,
  193. const double & label,
  194. const bool & performOptimizationAfterIncrement = true
  195. );
  196. /**
  197. * @brief add several new examples
  198. * @author Alexander Freytag
  199. */
  200. virtual void addMultipleExamples( const std::vector< const NICE::SparseVector * > & newExamples,
  201. const NICE::Vector & newLabels,
  202. const bool & performOptimizationAfterIncrement = true
  203. );
  204. };
  205. }
  206. #endif