ensmallen
mlpack
fast, flexible C++ machine learning library
em_fit.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_METHODS_GMM_EM_FIT_HPP
15 #define MLPACK_METHODS_GMM_EM_FIT_HPP
16 
17 #include <mlpack/prereqs.hpp>
20 
21 // Default clustering mechanism.
23 // Default covariance matrix constraint.
25 
26 namespace mlpack {
27 namespace gmm {
28 
42 template<typename InitialClusteringType = kmeans::KMeans<>,
43  typename CovarianceConstraintPolicy = PositiveDefiniteConstraint,
44  typename Distribution = distribution::GaussianDistribution>
45 class EMFit
46 {
47  public:
64  EMFit(const size_t maxIterations = 300,
65  const double tolerance = 1e-10,
66  InitialClusteringType clusterer = InitialClusteringType(),
67  CovarianceConstraintPolicy constraint = CovarianceConstraintPolicy());
68 
84  void Estimate(const arma::mat& observations,
85  std::vector<Distribution>& dists,
86  arma::vec& weights,
87  const bool useInitialModel = false);
88 
106  void Estimate(const arma::mat& observations,
107  const arma::vec& probabilities,
108  std::vector<Distribution>& dists,
109  arma::vec& weights,
110  const bool useInitialModel = false);
111 
113  const InitialClusteringType& Clusterer() const { return clusterer; }
115  InitialClusteringType& Clusterer() { return clusterer; }
116 
118  const CovarianceConstraintPolicy& Constraint() const { return constraint; }
120  CovarianceConstraintPolicy& Constraint() { return constraint; }
121 
123  size_t MaxIterations() const { return maxIterations; }
125  size_t& MaxIterations() { return maxIterations; }
126 
128  double Tolerance() const { return tolerance; }
130  double& Tolerance() { return tolerance; }
131 
133  template<typename Archive>
134  void serialize(Archive& ar, const unsigned int version);
135 
136  private:
147  void InitialClustering(
148  const arma::mat& observations,
149  std::vector<Distribution>& dists,
150  arma::vec& weights);
151 
162  double LogLikelihood(
163  const arma::mat& data,
164  const std::vector<Distribution>& dists,
165  const arma::vec& weights) const;
166 
177  void ArmadilloGMMWrapper(
178  const arma::mat& observations,
179  std::vector<Distribution>& dists,
180  arma::vec& weights,
181  const bool useInitialModel);
182 
184  size_t maxIterations;
186  double tolerance;
188  InitialClusteringType clusterer;
190  CovarianceConstraintPolicy constraint;
191 };
192 
193 } // namespace gmm
194 } // namespace mlpack
195 
196 // Include implementation.
197 #include "em_fit_impl.hpp"
198 
199 #endif
This class contains methods which can fit a GMM to observations using the EM algorithm.
Definition: em_fit.hpp:45
void serialize(Archive &ar, const unsigned int version)
Serialize the fitter.
.hpp
Definition: add_to_po.hpp:21
size_t MaxIterations() const
Get the maximum number of iterations of the EM algorithm.
Definition: em_fit.hpp:123
const CovarianceConstraintPolicy & Constraint() const
Get the covariance constraint policy class.
Definition: em_fit.hpp:118
The core includes that mlpack expects; standard C++ includes and Armadillo.
void Estimate(const arma::mat &observations, std::vector< Distribution > &dists, arma::vec &weights, const bool useInitialModel=false)
Fit the observations to a Gaussian mixture model (GMM) using the EM algorithm.
const InitialClusteringType & Clusterer() const
Get the clusterer.
Definition: em_fit.hpp:113
double Tolerance() const
Get the tolerance for the convergence of the EM algorithm.
Definition: em_fit.hpp:128
double & Tolerance()
Modify the tolerance for the convergence of the EM algorithm.
Definition: em_fit.hpp:130
size_t & MaxIterations()
Modify the maximum number of iterations of the EM algorithm.
Definition: em_fit.hpp:125
InitialClusteringType & Clusterer()
Modify the clusterer.
Definition: em_fit.hpp:115
EMFit(const size_t maxIterations=300, const double tolerance=1e-10, InitialClusteringType clusterer=InitialClusteringType(), CovarianceConstraintPolicy constraint=CovarianceConstraintPolicy())
Construct the EMFit object, optionally passing an InitialClusteringType object (just in case it needs...
CovarianceConstraintPolicy & Constraint()
Modify the covariance constraint policy class.
Definition: em_fit.hpp:120