GPHIKRegression.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. // output/debug related settings
  36. /** verbose flag for useful output*/
  37. bool verbose;
  38. /** debug flag for several outputs useful for debugging*/
  39. bool debug;
  40. // general specifications
  41. /** Header in configfile where variable settings are stored */
  42. std::string confSection;
  43. /** Configuration file specifying variable settings */
  44. NICE::Config *confCopy;
  45. // internal objects
  46. /** Main object doing all the jobs: training, regression, optimization, ... */
  47. NICE::FMKGPHyperparameterOptimization *gphyper;
  48. /** Possibility for transforming feature values, parameters can be optimized */
  49. NICE::ParameterizedFunction *pf;
  50. /** Gaussian label noise for model regularization */
  51. double noise;
  52. enum VarianceApproximation{
  53. APPROXIMATE_ROUGH,
  54. APPROXIMATE_FINE,
  55. EXACT,
  56. NONE
  57. };
  58. /** Which technique for variance approximations shall be used */
  59. VarianceApproximation varianceApproximation;
  60. /**compute the uncertainty prediction during regression?*/
  61. bool uncertaintyPredictionForRegression;
  62. /////////////////////////
  63. /////////////////////////
  64. // PROTECTED METHODS //
  65. /////////////////////////
  66. /////////////////////////
  67. /**
  68. * @brief Setup internal variables and objects used
  69. * @author Alexander Freytag
  70. * @param conf Config file to specify variable settings
  71. * @param s_confSection
  72. */
  73. void init(const NICE::Config *conf, const std::string & s_confSection);
  74. public:
  75. /**
  76. * @brief standard constructor
  77. * @author Alexander Freytag
  78. */
  79. GPHIKRegression( const NICE::Config *conf = NULL, const std::string & s_confSection = "GPHIKRegression" );
  80. /**
  81. * @brief simple destructor
  82. * @author Alexander Freytag
  83. */
  84. ~GPHIKRegression();
  85. ///////////////////// ///////////////////// /////////////////////
  86. // GET / SET
  87. ///////////////////// ///////////////////// /////////////////////
  88. ///////////////////// ///////////////////// /////////////////////
  89. // REGRESSION STUFF
  90. ///////////////////// ///////////////////// /////////////////////
  91. /**
  92. * @brief Estimate output of a given example with the previously learnt model
  93. * @date 15-01-2014 (dd-mm-yyyy)
  94. * @author Alexander Freytag
  95. * @param example (SparseVector) for which regression shall be performed, given in a sparse representation
  96. * @param result (double) regression result
  97. */
  98. void estimate ( const NICE::SparseVector * example, double & result ) const;
  99. /**
  100. * @brief Estimate output of a given example with the previously learnt model
  101. ** @date 15-01-2014 (dd-mm-yyyy)
  102. * @author Alexander Freytag
  103. * @param example (SparseVector) for which regression shall be performed, given in a sparse representation
  104. * @param result (double) regression result
  105. * @param uncertainty (double*) predictive variance of the regression result, if computed
  106. */
  107. void estimate ( const NICE::SparseVector * example, double & result, double & uncertainty ) const;
  108. /**
  109. * @brief Estimate output of a given example with the previously learnt model
  110. * NOTE: whenever possible, you should the sparse version to obtain significantly smaller computation times*
  111. * @date 15-01-2014 (dd-mm-yyyy)
  112. * @author Alexander Freytag
  113. * @param example (non-sparse Vector) for which regression shall be performed, given in a non-sparse representation
  114. * @param result (double) regression result
  115. */
  116. void estimate ( const NICE::Vector * example, double & result ) const;
  117. /**
  118. * @brief Estimate output of a given example with the previously learnt model
  119. * NOTE: whenever possible, you should the sparse version to obtain significantly smaller computation times
  120. * @date 15-01-2014 (dd-mm-yyyy)
  121. * @author Alexander Freytag
  122. * @param example (non-sparse Vector) for which regression shall be performed, given in a non-sparse representation
  123. * @param result (double)regression result
  124. * @param uncertainty (double*) predictive variance of the regression result, if computed
  125. */
  126. void estimate ( const NICE::Vector * example, double & result, double & uncertainty ) const;
  127. /**
  128. * @brief train this regression method using a given set of examples and corresponding labels
  129. * @date 15-01-2014 (dd-mm-yyyy)
  130. * @author Alexander Freytag
  131. * @param examples (std::vector< NICE::SparseVector *>) training data given in a sparse representation
  132. * @param labels (Vector) labels
  133. */
  134. void train ( const std::vector< const NICE::SparseVector *> & examples, const NICE::Vector & labels );
  135. /**
  136. * @brief Clone regression object
  137. * @author Alexander Freytag
  138. */
  139. GPHIKRegression *clone () const;
  140. /**
  141. * @brief prediction of regression uncertainty
  142. * @date 15-01-2014 (dd-mm-yyyy)
  143. * @author Alexander Freytag
  144. * @param examples example for which the regression uncertainty shall be predicted, given in a sparse representation
  145. * @param uncertainty contains the resulting regression uncertainty
  146. */
  147. void predictUncertainty( const NICE::SparseVector * example, double & uncertainty ) const;
  148. /**
  149. * @brief prediction of regression uncertainty
  150. * @date 15-01-2014 (dd-mm-yyyy)
  151. * @author Alexander Freytag
  152. * @param examples example for which the regression uncertainty shall be predicted, given in a non-sparse representation
  153. * @param uncertainty contains the resulting regression uncertainty
  154. */
  155. void predictUncertainty( const NICE::Vector * example, double & uncertainty ) const;
  156. ///////////////////// INTERFACE PERSISTENT /////////////////////
  157. // interface specific methods for store and restore
  158. ///////////////////// INTERFACE PERSISTENT /////////////////////
  159. /**
  160. * @brief Load regression object from external file (stream)
  161. * @author Alexander Freytag
  162. */
  163. void restore ( std::istream & is, int format = 0 );
  164. /**
  165. * @brief Save regression object to external file (stream)
  166. * @author Alexander Freytag
  167. */
  168. void store ( std::ostream & os, int format = 0 ) const;
  169. /**
  170. * @brief Clear regression object
  171. * @author Alexander Freytag
  172. */
  173. void clear ();
  174. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  175. // interface specific methods for incremental extensions
  176. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  177. /**
  178. * @brief add a new example
  179. * @author Alexander Freytag
  180. */
  181. virtual void addExample( const NICE::SparseVector * example,
  182. const double & label,
  183. const bool & performOptimizationAfterIncrement = true
  184. );
  185. /**
  186. * @brief add several new examples
  187. * @author Alexander Freytag
  188. */
  189. virtual void addMultipleExamples( const std::vector< const NICE::SparseVector * > & newExamples,
  190. const NICE::Vector & newLabels,
  191. const bool & performOptimizationAfterIncrement = true
  192. );
  193. };
  194. }
  195. #endif