gmm_diag_bones.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_diag
  16. //! @{
  17. namespace gmm_priv
  18. {
  19. template<typename eT>
  20. class gmm_diag
  21. {
  22. public:
  23. arma_aligned const Mat<eT> means;
  24. arma_aligned const Mat<eT> dcovs;
  25. arma_aligned const Row<eT> hefts;
  26. //
  27. //
  28. inline ~gmm_diag();
  29. inline gmm_diag();
  30. inline gmm_diag(const gmm_diag& x);
  31. inline gmm_diag& operator=(const gmm_diag& x);
  32. inline explicit gmm_diag(const gmm_full<eT>& x);
  33. inline gmm_diag& operator=(const gmm_full<eT>& x);
  34. inline gmm_diag(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 Base<eT,T2>& in_dcovs, 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_dcovs(const Base<eT,T1>& in_dcovs);
  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. template<typename T1>
  75. inline
  76. bool
  77. kmeans_wrapper
  78. (
  79. Mat<eT>& user_means,
  80. const Base<eT,T1>& data,
  81. const uword n_gaus,
  82. const gmm_seed_mode& seed_mode,
  83. const uword km_iter,
  84. const bool print_mode
  85. );
  86. //
  87. protected:
  88. arma_aligned Mat<eT> inv_dcovs;
  89. arma_aligned Row<eT> log_det_etc;
  90. arma_aligned Row<eT> log_hefts;
  91. arma_aligned Col<eT> mah_aux;
  92. //
  93. inline void init(const gmm_diag& x);
  94. inline void init(const gmm_full<eT>& x);
  95. inline void init(const uword in_n_dim, const uword in_n_gaus);
  96. inline void init_constants();
  97. inline umat internal_gen_boundaries(const uword N) const;
  98. inline eT internal_scalar_log_p(const eT* x ) const;
  99. inline eT internal_scalar_log_p(const eT* x, const uword gaus_id) const;
  100. inline Row<eT> internal_vec_log_p(const Mat<eT>& X ) const;
  101. inline Row<eT> internal_vec_log_p(const Mat<eT>& X, const uword gaus_id) const;
  102. inline eT internal_sum_log_p(const Mat<eT>& X ) const;
  103. inline eT internal_sum_log_p(const Mat<eT>& X, const uword gaus_id) const;
  104. inline eT internal_avg_log_p(const Mat<eT>& X ) const;
  105. inline eT internal_avg_log_p(const Mat<eT>& X, const uword gaus_id) const;
  106. inline uword internal_scalar_assign(const Mat<eT>& X, const gmm_dist_mode& dist_mode) const;
  107. inline void internal_vec_assign(urowvec& out, const Mat<eT>& X, const gmm_dist_mode& dist_mode) const;
  108. inline void internal_raw_hist(urowvec& hist, const Mat<eT>& X, const gmm_dist_mode& dist_mode) const;
  109. //
  110. template<uword dist_id> inline void generate_initial_means(const Mat<eT>& X, const gmm_seed_mode& seed);
  111. template<uword dist_id> inline void generate_initial_params(const Mat<eT>& X, const eT var_floor);
  112. template<uword dist_id> inline bool km_iterate(const Mat<eT>& X, const uword max_iter, const bool verbose, const char* signature);
  113. //
  114. inline bool em_iterate(const Mat<eT>& X, const uword max_iter, const eT var_floor, const bool verbose);
  115. inline void em_update_params(const Mat<eT>& X, const umat& boundaries, field< Mat<eT> >& t_acc_means, field< Mat<eT> >& t_acc_dcovs, field< Col<eT> >& t_acc_norm_lhoods, field< Col<eT> >& t_gaus_log_lhoods, Col<eT>& t_progress_log_lhoods);
  116. inline void em_generate_acc(const Mat<eT>& X, const uword start_index, const uword end_index, Mat<eT>& acc_means, Mat<eT>& acc_dcovs, Col<eT>& acc_norm_lhoods, Col<eT>& gaus_log_lhoods, eT& progress_log_lhood) const;
  117. inline void em_fix_params(const eT var_floor);
  118. };
  119. }
  120. typedef gmm_priv::gmm_diag<double> gmm_diag;
  121. typedef gmm_priv::gmm_diag<float> fgmm_diag;
  122. //! @}