mlpack 3.4.2
Loading...
Searching...
No Matches
gmm.hpp
Go to the documentation of this file.
1
13#ifndef MLPACK_METHODS_MOG_MOG_EM_HPP
14#define MLPACK_METHODS_MOG_MOG_EM_HPP
15
16#include <mlpack/prereqs.hpp>
17
18// This is the default fitting method class.
19#include "em_fit.hpp"
20
21namespace mlpack {
22namespace gmm {
23
78class GMM
79{
80 private:
82 size_t gaussians;
84 size_t dimensionality;
85
87 std::vector<distribution::GaussianDistribution> dists;
88
90 arma::vec weights;
91
92 public:
96 GMM() :
97 gaussians(0),
98 dimensionality(0)
99 {
100 // Warn the user. They probably don't want to do this. If this constructor
101 // is being used (because it is required by some template classes), the user
102 // should know that it is potentially dangerous.
103 Log::Debug << "GMM::GMM(): no parameters given; Estimate() may fail "
104 << "unless parameters are set." << std::endl;
105 }
106
114 GMM(const size_t gaussians, const size_t dimensionality);
115
122 GMM(const std::vector<distribution::GaussianDistribution> & dists,
123 const arma::vec& weights) :
124 gaussians(dists.size()),
125 dimensionality((!dists.empty()) ? dists[0].Mean().n_elem : 0),
126 dists(dists),
127 weights(weights) { /* Nothing to do. */ }
128
130 GMM(const GMM& other);
131
133 GMM& operator=(const GMM& other);
134
136 size_t Gaussians() const { return gaussians; }
138 size_t Dimensionality() const { return dimensionality; }
139
146 return dists[i]; }
152 distribution::GaussianDistribution& Component(size_t i) { return dists[i]; }
153
155 const arma::vec& Weights() const { return weights; }
157 arma::vec& Weights() { return weights; }
158
165 double Probability(const arma::vec& observation) const;
166
173 double LogProbability(const arma::vec& observation) const;
174
182 double Probability(const arma::vec& observation,
183 const size_t component) const;
184
192 double LogProbability(const arma::vec& observation,
193 const size_t component) const;
200 arma::vec Random() const;
201
225 template<typename FittingType = EMFit<>>
226 double Train(const arma::mat& observations,
227 const size_t trials = 1,
228 const bool useExistingModel = false,
229 FittingType fitter = FittingType());
230
256 template<typename FittingType = EMFit<>>
257 double Train(const arma::mat& observations,
258 const arma::vec& probabilities,
259 const size_t trials = 1,
260 const bool useExistingModel = false,
261 FittingType fitter = FittingType());
262
279 void Classify(const arma::mat& observations,
280 arma::Row<size_t>& labels) const;
281
285 template<typename Archive>
286 void serialize(Archive& ar, const unsigned int /* version */);
287
288 private:
298 double LogLikelihood(
299 const arma::mat& dataPoints,
300 const std::vector<distribution::GaussianDistribution>& distsL,
301 const arma::vec& weights) const;
302};
303
304} // namespace gmm
305} // namespace mlpack
306
307// Include implementation.
308#include "gmm_impl.hpp"
309
310#endif
static MLPACK_EXPORT util::NullOutStream Debug
MLPACK_EXPORT is required for global variables, so that they are properly exported by the Windows com...
Definition: log.hpp:79
A single multivariate Gaussian distribution.
A Gaussian Mixture Model (GMM).
Definition: gmm.hpp:79
GMM()
Create an empty Gaussian Mixture Model, with zero gaussians.
Definition: gmm.hpp:96
GMM(const size_t gaussians, const size_t dimensionality)
Create a GMM with the given number of Gaussians, each of which have the specified dimensionality.
size_t Gaussians() const
Return the number of gaussians in the model.
Definition: gmm.hpp:136
arma::vec Random() const
Return a randomly generated observation according to the probability distribution defined by this obj...
const distribution::GaussianDistribution & Component(size_t i) const
Return a const reference to a component distribution.
Definition: gmm.hpp:145
distribution::GaussianDistribution & Component(size_t i)
Return a reference to a component distribution.
Definition: gmm.hpp:152
void Classify(const arma::mat &observations, arma::Row< size_t > &labels) const
Classify the given observations as being from an individual component in this GMM.
arma::vec & Weights()
Return a reference to the a priori weights of each Gaussian.
Definition: gmm.hpp:157
double LogProbability(const arma::vec &observation, const size_t component) const
Return the log probability that the given observation came from the given Gaussian component in this ...
const arma::vec & Weights() const
Return a const reference to the a priori weights of each Gaussian.
Definition: gmm.hpp:155
double Train(const arma::mat &observations, const arma::vec &probabilities, const size_t trials=1, const bool useExistingModel=false, FittingType fitter=FittingType())
Estimate the probability distribution directly from the given observations, taking into account the p...
GMM(const std::vector< distribution::GaussianDistribution > &dists, const arma::vec &weights)
Create a GMM with the given dists and weights.
Definition: gmm.hpp:122
double LogProbability(const arma::vec &observation) const
Return the log probability that the given observation came from this distribution.
size_t Dimensionality() const
Return the dimensionality of the model.
Definition: gmm.hpp:138
GMM & operator=(const GMM &other)
Copy operator for GMMs.
GMM(const GMM &other)
Copy constructor for GMMs.
double Probability(const arma::vec &observation) const
Return the probability that the given observation came from this distribution.
double Train(const arma::mat &observations, const size_t trials=1, const bool useExistingModel=false, FittingType fitter=FittingType())
Estimate the probability distribution directly from the given observations, using the given algorithm...
void serialize(Archive &ar, const unsigned int)
Serialize the GMM.
double Probability(const arma::vec &observation, const size_t component) const
Return the probability that the given observation came from the given Gaussian component in this dist...
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.