gmm_full_bones.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2008-2016 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. //! \addtogroup gmm_full
  16. //! @{
  17. namespace gmm_priv
  18. {
  19. template<typename eT>
  20. class gmm_full
  21. {
  22. public:
  23. arma_aligned const Mat <eT> means;
  24. arma_aligned const Cube<eT> fcovs;
  25. arma_aligned const Row <eT> hefts;
  26. //
  27. //
  28. inline ~gmm_full();
  29. inline gmm_full();
  30. inline gmm_full(const gmm_full& x);
  31. inline gmm_full& operator=(const gmm_full& x);
  32. inline explicit gmm_full(const gmm_diag<eT>& x);
  33. inline gmm_full& operator=(const gmm_diag<eT>& x);
  34. inline gmm_full(const uword in_n_dims, const uword in_n_gaus);
  35. inline void reset(const uword in_n_dims, const uword in_n_gaus);
  36. inline void reset();
  37. template<typename T1, typename T2, typename T3>
  38. inline void set_params(const Base<eT,T1>& in_means, const BaseCube<eT,T2>& in_fcovs, const Base<eT,T3>& in_hefts);
  39. template<typename T1> inline void set_means(const Base <eT,T1>& in_means);
  40. template<typename T1> inline void set_fcovs(const BaseCube<eT,T1>& in_fcovs);
  41. template<typename T1> inline void set_hefts(const Base <eT,T1>& in_hefts);
  42. inline uword n_dims() const;
  43. inline uword n_gaus() const;
  44. inline bool load(const std::string name);
  45. inline bool save(const std::string name) const;
  46. inline Col<eT> generate() const;
  47. inline Mat<eT> generate(const uword N) const;
  48. template<typename T1> inline eT log_p(const T1& expr, const gmm_empty_arg& junk1 = gmm_empty_arg(), typename enable_if<((is_arma_type<T1>::value) && (resolves_to_colvector<T1>::value == true ))>::result* junk2 = 0) const;
  49. template<typename T1> inline eT log_p(const T1& expr, const uword gaus_id, typename enable_if<((is_arma_type<T1>::value) && (resolves_to_colvector<T1>::value == true ))>::result* junk2 = 0) const;
  50. template<typename T1> inline Row<eT> log_p(const T1& expr, const gmm_empty_arg& junk1 = gmm_empty_arg(), typename enable_if<((is_arma_type<T1>::value) && (resolves_to_colvector<T1>::value == false))>::result* junk2 = 0) const;
  51. template<typename T1> inline Row<eT> log_p(const T1& expr, const uword gaus_id, typename enable_if<((is_arma_type<T1>::value) && (resolves_to_colvector<T1>::value == false))>::result* junk2 = 0) const;
  52. template<typename T1> inline eT sum_log_p(const Base<eT,T1>& expr) const;
  53. template<typename T1> inline eT sum_log_p(const Base<eT,T1>& expr, const uword gaus_id) const;
  54. template<typename T1> inline eT avg_log_p(const Base<eT,T1>& expr) const;
  55. template<typename T1> inline eT avg_log_p(const Base<eT,T1>& expr, const uword gaus_id) const;
  56. template<typename T1> inline uword assign(const T1& expr, const gmm_dist_mode& dist, typename enable_if<((is_arma_type<T1>::value) && (resolves_to_colvector<T1>::value == true ))>::result* junk = 0) const;
  57. template<typename T1> inline urowvec assign(const T1& expr, const gmm_dist_mode& dist, typename enable_if<((is_arma_type<T1>::value) && (resolves_to_colvector<T1>::value == false))>::result* junk = 0) const;
  58. template<typename T1> inline urowvec raw_hist(const Base<eT,T1>& expr, const gmm_dist_mode& dist_mode) const;
  59. template<typename T1> inline Row<eT> norm_hist(const Base<eT,T1>& expr, const gmm_dist_mode& dist_mode) const;
  60. template<typename T1>
  61. inline
  62. bool
  63. learn
  64. (
  65. const Base<eT,T1>& data,
  66. const uword n_gaus,
  67. const gmm_dist_mode& dist_mode,
  68. const gmm_seed_mode& seed_mode,
  69. const uword km_iter,
  70. const uword em_iter,
  71. const eT var_floor,
  72. const bool print_mode
  73. );
  74. //
  75. protected:
  76. arma_aligned Cube<eT> inv_fcovs;
  77. arma_aligned Row<eT> log_det_etc;
  78. arma_aligned Row<eT> log_hefts;
  79. arma_aligned Col<eT> mah_aux;
  80. arma_aligned Cube<eT> chol_fcovs;
  81. //
  82. inline void init(const gmm_full& x);
  83. inline void init(const gmm_diag<eT>& x);
  84. inline void init(const uword in_n_dim, const uword in_n_gaus);
  85. inline void init_constants(const bool calc_chol = true);
  86. inline umat internal_gen_boundaries(const uword N) const;
  87. inline eT internal_scalar_log_p(const eT* x ) const;
  88. inline eT internal_scalar_log_p(const eT* x, const uword gaus_id) const;
  89. inline Row<eT> internal_vec_log_p(const Mat<eT>& X ) const;
  90. inline Row<eT> internal_vec_log_p(const Mat<eT>& X, const uword gaus_id) const;
  91. inline eT internal_sum_log_p(const Mat<eT>& X ) const;
  92. inline eT internal_sum_log_p(const Mat<eT>& X, const uword gaus_id) const;
  93. inline eT internal_avg_log_p(const Mat<eT>& X ) const;
  94. inline eT internal_avg_log_p(const Mat<eT>& X, const uword gaus_id) const;
  95. inline uword internal_scalar_assign(const Mat<eT>& X, const gmm_dist_mode& dist_mode) const;
  96. inline void internal_vec_assign(urowvec& out, const Mat<eT>& X, const gmm_dist_mode& dist_mode) const;
  97. inline void internal_raw_hist(urowvec& hist, const Mat<eT>& X, const gmm_dist_mode& dist_mode) const;
  98. //
  99. template<uword dist_id> inline void generate_initial_means(const Mat<eT>& X, const gmm_seed_mode& seed);
  100. template<uword dist_id> inline void generate_initial_params(const Mat<eT>& X, const eT var_floor);
  101. template<uword dist_id> inline bool km_iterate(const Mat<eT>& X, const uword max_iter, const bool verbose);
  102. //
  103. inline bool em_iterate(const Mat<eT>& X, const uword max_iter, const eT var_floor, const bool verbose);
  104. inline void em_update_params(const Mat<eT>& X, const umat& boundaries, field< Mat<eT> >& t_acc_means, field< Cube<eT> >& t_acc_fcovs, field< Col<eT> >& t_acc_norm_lhoods, field< Col<eT> >& t_gaus_log_lhoods, Col<eT>& t_progress_log_lhoods, const eT var_floor);
  105. inline void em_generate_acc(const Mat<eT>& X, const uword start_index, const uword end_index, Mat<eT>& acc_means, Cube<eT>& acc_fcovs, Col<eT>& acc_norm_lhoods, Col<eT>& gaus_log_lhoods, eT& progress_log_lhood) const;
  106. inline void em_fix_params(const eT var_floor);
  107. };
  108. }
  109. typedef gmm_priv::gmm_full<double> gmm_full;
  110. typedef gmm_priv::gmm_full<float> fgmm_full;
  111. //! @}