diagonal_gmm.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_METHODS_GMM_DIAGONAL_GMM_HPP
15 #define MLPACK_METHODS_GMM_DIAGONAL_GMM_HPP
16 
17 #include <mlpack/prereqs.hpp>
20 
21 // This is the default fitting method class.
22 #include "em_fit.hpp"
23 
24 // This is the default covariance matrix constraint.
25 #include "diagonal_constraint.hpp"
26 
27 namespace mlpack {
28 namespace gmm {
29 
76 {
77  private:
79  size_t gaussians;
81  size_t dimensionality;
82 
84  std::vector<distribution::DiagonalGaussianDistribution> dists;
85 
87  arma::vec weights;
88 
89  public:
94  gaussians(0),
95  dimensionality(0)
96  {
97  // Warn the user. They probably don't want to do this. If this
98  // constructor is being used (because it is required by some template
99  // classes), the user should know that it is potentially dangerous.
100  Log::Debug << "DiagonalGMM::DiagonalGMM(): no parameters given;"
101  "Estimate() may fail " << "unless parameters are set." << std::endl;
102  }
103 
111  DiagonalGMM(const size_t gaussians, const size_t dimensionality);
112 
119  DiagonalGMM(const std::vector<distribution::DiagonalGaussianDistribution>&
120  dists, const arma::vec& weights) :
121  gaussians(dists.size()),
122  dimensionality((!dists.empty()) ? dists[0].Mean().n_elem : 0),
123  dists(dists),
124  weights(weights) { /* Nothing to do. */ }
125 
127  DiagonalGMM(const DiagonalGMM& other);
128 
130  DiagonalGMM& operator=(const DiagonalGMM& other);
131 
133  size_t Gaussians() const { return gaussians; }
135  size_t Dimensionality() const { return dimensionality; }
136 
143  {
144  return dists[i];
145  }
146 
153  {
154  return dists[i];
155  }
156 
158  const arma::vec& Weights() const { return weights; }
160  arma::vec& Weights() { return weights; }
161 
168  double Probability(const arma::vec& observation) const;
169 
176  void Probability(const arma::mat& observation, arma::vec& probs) const;
177 
184  double LogProbability(const arma::vec& observation) const;
185 
192  void LogProbability(const arma::mat& observation, arma::vec& logProbs) const;
193 
201  double Probability(const arma::vec& observation,
202  const size_t component) const;
203 
211  double LogProbability(const arma::vec& observation,
212  const size_t component) const;
219  arma::vec Random() const;
220 
243  template<typename FittingType = EMFit<kmeans::KMeans<>, DiagonalConstraint,
244  distribution::DiagonalGaussianDistribution>>
245  double Train(const arma::mat& observations,
246  const size_t trials = 1,
247  const bool useExistingModel = false,
248  FittingType fitter = FittingType());
249 
275  template<typename FittingType = EMFit<kmeans::KMeans<>, DiagonalConstraint,
276  distribution::DiagonalGaussianDistribution>>
277  double Train(const arma::mat& observations,
278  const arma::vec& probabilities,
279  const size_t trials = 1,
280  const bool useExistingModel = false,
281  FittingType fitter = FittingType());
282 
300  void Classify(const arma::mat& observations,
301  arma::Row<size_t>& labels) const;
302 
306  template<typename Archive>
307  void serialize(Archive& ar, const uint32_t /* version */);
308 
309  private:
319  double LogLikelihood(
320  const arma::mat& observations,
321  const std::vector<distribution::DiagonalGaussianDistribution>& dists,
322  const arma::vec& weights) const;
323 };
324 
325 } // namespace gmm
326 } // namespace mlpack
327 
328 // Include implementation.
329 #include "diagonal_gmm_impl.hpp"
330 
331 #endif // MLPACK_METHODS_GMM_DIAGONAL_GMM_HPP
constexpr bool empty(Container const &container) noexcept
Definition: iterator.hpp:37
constexpr auto size(Container const &container) noexcept -> decltype(container.size())
Definition: iterator.hpp:29
Linear algebra utility functions, generally performed on matrices or vectors.
arma::vec & Weights()
Return a reference to the a priori weights of each Gaussian.
double Probability(const arma::vec &observation) const
Return the probability that the given observation came from this distribution.
const arma::vec & Weights() const
Return a const reference to the a priori weights of each Gaussian.
A Diagonal Gaussian Mixture Model.
The core includes that mlpack expects; standard C++ includes, Armadillo, cereal, and a few basic mlpa...
const distribution::DiagonalGaussianDistribution & Component(size_t i) const
Return a const reference to a component distribution.
arma::vec Random() const
Return a randomly generated observation according to the probability distribution defined by this obj...
DiagonalGMM & operator=(const DiagonalGMM &other)
Copy operator for DiagonalGMMs.
DiagonalGMM(const std::vector< distribution::DiagonalGaussianDistribution > &dists, const arma::vec &weights)
Create a DiagonalGMM with the given dists and weights.
distribution::DiagonalGaussianDistribution & Component(size_t i)
Return a reference to a component distribution.
void Classify(const arma::mat &observations, arma::Row< size_t > &labels) const
Classify the given observations as being from an individual component in this DiagonalGMM.
DiagonalGMM()
Create an empty Diagonal Gaussian Mixture Model, with zero gaussians.
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.
void serialize(Archive &ar, const uint32_t)
Serialize the DiagonalGMM.
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...
static util::NullOutStream Debug
Definition: log.hpp:90
size_t Gaussians() const
Return the number of Gaussians in the model.
A single multivariate Gaussian distribution with diagonal covariance.