MLPACK
1.0.7
|
A Gaussian Mixture Model (GMM). More...
Public Member Functions | |
GMM () | |
Create an empty Gaussian Mixture Model, with zero gaussians. More... | |
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. More... | |
GMM (const size_t gaussians, const size_t dimensionality, FittingType &fitter) | |
Create a GMM with the given number of Gaussians, each of which have the specified dimensionality. More... | |
GMM (const std::vector< arma::vec > &means, const std::vector< arma::mat > &covariances, const arma::vec &weights) | |
Create a GMM with the given means, covariances, and weights. More... | |
GMM (const std::vector< arma::vec > &means, const std::vector< arma::mat > &covariances, const arma::vec &weights, FittingType &fitter) | |
Create a GMM with the given means, covariances, and weights, and use the given initialized FittingType class. More... | |
template<typename OtherFittingType > | |
GMM (const GMM< OtherFittingType > &other) | |
Copy constructor for GMMs which use different fitting types. More... | |
GMM (const GMM &other) | |
Copy constructor for GMMs using the same fitting type. More... | |
void | Classify (const arma::mat &observations, arma::Col< size_t > &labels) const |
Classify the given observations as being from an individual component in this GMM. More... | |
const std::vector< arma::mat > & | Covariances () const |
Return a const reference to the vector of covariance matrices (sigma). More... | |
std::vector< arma::mat > & | Covariances () |
Return a reference to the vector of covariance matrices (sigma). More... | |
size_t | Dimensionality () const |
Return the dimensionality of the model. More... | |
size_t & | Dimensionality () |
Modify the dimensionality of the model. More... | |
double | Estimate (const arma::mat &observations, const size_t trials=1) |
Estimate the probability distribution directly from the given observations, using the given algorithm in the FittingType class to fit the data. More... | |
double | Estimate (const arma::mat &observations, const arma::vec &probabilities, const size_t trials=1) |
Estimate the probability distribution directly from the given observations, taking into account the probability of each observation actually being from this distribution, and using the given algorithm in the FittingType class to fit the data. More... | |
const FittingType & | Fitter () const |
Return a const reference to the fitting type. More... | |
FittingType & | Fitter () |
Return a reference to the fitting type. More... | |
size_t | Gaussians () const |
Return the number of gaussians in the model. More... | |
size_t & | Gaussians () |
Modify the number of gaussians in the model. More... | |
void | Load (const std::string &filename) |
Load a GMM from an XML file. More... | |
const std::vector< arma::vec > & | Means () const |
Return a const reference to the vector of means (mu). More... | |
std::vector< arma::vec > & | Means () |
Return a reference to the vector of means (mu). More... | |
template<typename OtherFittingType > | |
GMM & | operator= (const GMM< OtherFittingType > &other) |
Copy operator for GMMs which use different fitting types. More... | |
GMM & | operator= (const GMM &other) |
Copy operator for GMMs which use the same fitting type. More... | |
double | Probability (const arma::vec &observation) const |
Return the probability that the given observation came from this distribution. More... | |
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 distribution. More... | |
arma::vec | Random () const |
Return a randomly generated observation according to the probability distribution defined by this object. More... | |
void | Save (const std::string &filename) const |
Save a GMM to an XML file. More... | |
const arma::vec & | Weights () const |
Return a const reference to the a priori weights of each Gaussian. More... | |
arma::vec & | Weights () |
Return a reference to the a priori weights of each Gaussian. More... | |
Private Member Functions | |
double | LogLikelihood (const arma::mat &dataPoints, const std::vector< arma::vec > &means, const std::vector< arma::mat > &covars, const arma::vec &weights) const |
This function computes the loglikelihood of the given model. More... | |
Private Attributes | |
std::vector< arma::mat > | covariances |
Vector of covariances; one for each Gaussian. More... | |
size_t | dimensionality |
The dimensionality of the model. More... | |
FittingType & | fitter |
Reference to the fitting object we should use. More... | |
size_t | gaussians |
The number of Gaussians in the model. More... | |
FittingType | localFitter |
Locally-stored fitting object; in case the user did not pass one. More... | |
std::vector< arma::vec > | means |
Vector of means; one for each Gaussian. More... | |
arma::vec | weights |
Vector of a priori weights for each Gaussian. More... | |
A Gaussian Mixture Model (GMM).
This class uses maximum likelihood loss functions to estimate the parameters of the GMM on a given dataset via the given fitting mechanism, defined by the FittingType template parameter. The GMM can be trained using normal data, or data with probabilities of being from this GMM (see GMM::Estimate() for more information).
The FittingType template class must provide a way for the GMM to train on data. It must provide the following two functions:
These functions should produce a trained GMM from the given observations and probabilities. These may modify the size of the model (by increasing the size of the mean and covariance vectors as well as the weight vectors), but the method should expect that these vectors are already set to the size of the GMM as specified in the constructor.
For a sample implementation, see the EMFit class; this class uses the EM algorithm to train a GMM, and is the default fitting type.
The GMM, once trained, can be used to generate random points from the distribution and estimate the probability of points being from the distribution. The parameters of the GMM can be obtained through the accessors and mutators.
Example use:
|
inline |
Create an empty Gaussian Mixture Model, with zero gaussians.
Definition at line 107 of file gmm.hpp.
References mlpack::Log::Debug.
|
inline |
|
inline |
Create a GMM with the given number of Gaussians, each of which have the specified dimensionality.
Also, pass in an initialized FittingType class; this is useful in cases where the FittingType class needs to store some state.
gaussians | Number of Gaussians in this GMM. |
dimensionality | Dimensionality of each Gaussian. |
fitter | Initialized fitting mechanism. |
|
inline |
|
inline |
Create a GMM with the given means, covariances, and weights, and use the given initialized FittingType class.
This is useful in cases where the FittingType class needs to store some state.
means | Means of the model. |
covariances | Covariances of the model. |
weights | Weights of the model. |
mlpack::gmm::GMM< FittingType >::GMM | ( | const GMM< OtherFittingType > & | other | ) |
Copy constructor for GMMs which use different fitting types.
mlpack::gmm::GMM< FittingType >::GMM | ( | const GMM< FittingType > & | other | ) |
Copy constructor for GMMs using the same fitting type.
This also copies the fitter.
void mlpack::gmm::GMM< FittingType >::Classify | ( | const arma::mat & | observations, |
arma::Col< size_t > & | labels | ||
) | const |
Classify the given observations as being from an individual component in this GMM.
The resultant classifications are stored in the 'labels' object, and each label will be between 0 and (Gaussians() - 1). Supposing that a point was classified with label 2, and that our GMM object was called 'gmm', one could access the relevant Gaussian distribution as follows:
observations | List of observations to classify. |
labels | Object which will be filled with labels. |
|
inline |
Return a const reference to the vector of covariance matrices (sigma).
Definition at line 251 of file gmm.hpp.
References mlpack::gmm::GMM< FittingType >::covariances.
|
inline |
Return a reference to the vector of covariance matrices (sigma).
Definition at line 253 of file gmm.hpp.
References mlpack::gmm::GMM< FittingType >::covariances.
|
inline |
Return the dimensionality of the model.
Definition at line 240 of file gmm.hpp.
References mlpack::gmm::GMM< FittingType >::dimensionality.
|
inline |
Modify the dimensionality of the model.
Careful! You will have to update each mean and covariance matrix yourself.
Definition at line 243 of file gmm.hpp.
References mlpack::gmm::GMM< FittingType >::dimensionality.
double mlpack::gmm::GMM< FittingType >::Estimate | ( | const arma::mat & | observations, |
const size_t | trials = 1 |
||
) |
Estimate the probability distribution directly from the given observations, using the given algorithm in the FittingType class to fit the data.
The fitting will be performed 'trials' times; from these trials, the model with the greatest log-likelihood will be selected. By default, only one trial is performed. The log-likelihood of the best fitting is returned.
FittingType | The type of fitting method which should be used (EMFit<> is suggested). |
observations | Observations of the model. |
trials | Number of trials to perform; the model in these trials with the greatest log-likelihood will be selected. |
double mlpack::gmm::GMM< FittingType >::Estimate | ( | const arma::mat & | observations, |
const arma::vec & | probabilities, | ||
const size_t | trials = 1 |
||
) |
Estimate the probability distribution directly from the given observations, taking into account the probability of each observation actually being from this distribution, and using the given algorithm in the FittingType class to fit the data.
The fitting will be performed 'trials' times; from these trials, the model with the greatest log-likelihood will be selected. By default, only one trial is performed. The log-likelihood of the best fitting is returned.
observations | Observations of the model. |
probabilities | Probability of each observation being from this distribution. |
trials | Number of trials to perform; the model in these trials with the greatest log-likelihood will be selected. |
|
inline |
Return a const reference to the fitting type.
Definition at line 261 of file gmm.hpp.
References mlpack::gmm::GMM< FittingType >::fitter.
|
inline |
Return a reference to the fitting type.
Definition at line 263 of file gmm.hpp.
References mlpack::gmm::GMM< FittingType >::fitter.
|
inline |
Return the number of gaussians in the model.
Definition at line 234 of file gmm.hpp.
References mlpack::gmm::GMM< FittingType >::gaussians.
|
inline |
Modify the number of gaussians in the model.
Careful! You will have to resize the means, covariances, and weights yourself.
Definition at line 237 of file gmm.hpp.
References mlpack::gmm::GMM< FittingType >::gaussians.
void mlpack::gmm::GMM< FittingType >::Load | ( | const std::string & | filename | ) |
|
private |
This function computes the loglikelihood of the given model.
This function is used by GMM::Estimate().
dataPoints | Observations to calculate the likelihood for. |
means | Means of the given mixture model. |
covars | Covariances of the given mixture model. |
weights | Weights of the given mixture model. |
|
inline |
Return a const reference to the vector of means (mu).
Definition at line 246 of file gmm.hpp.
References mlpack::gmm::GMM< FittingType >::means.
|
inline |
Return a reference to the vector of means (mu).
Definition at line 248 of file gmm.hpp.
References mlpack::gmm::GMM< FittingType >::means.
GMM& mlpack::gmm::GMM< FittingType >::operator= | ( | const GMM< OtherFittingType > & | other | ) |
Copy operator for GMMs which use different fitting types.
GMM& mlpack::gmm::GMM< FittingType >::operator= | ( | const GMM< FittingType > & | other | ) |
Copy operator for GMMs which use the same fitting type.
This also copies the fitter.
double mlpack::gmm::GMM< FittingType >::Probability | ( | const arma::vec & | observation | ) | const |
Return the probability that the given observation came from this distribution.
observation | Observation to evaluate the probability of. |
double mlpack::gmm::GMM< FittingType >::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 distribution.
observation | Observation to evaluate the probability of. |
component | Index of the component of the GMM to be considered. |
arma::vec mlpack::gmm::GMM< FittingType >::Random | ( | ) | const |
Return a randomly generated observation according to the probability distribution defined by this object.
void mlpack::gmm::GMM< FittingType >::Save | ( | const std::string & | filename | ) | const |
Save a GMM to an XML file.
filename | Name of XML file to write to. |
|
inline |
Return a const reference to the a priori weights of each Gaussian.
Definition at line 256 of file gmm.hpp.
References mlpack::gmm::GMM< FittingType >::weights.
|
inline |
Return a reference to the a priori weights of each Gaussian.
Definition at line 258 of file gmm.hpp.
References mlpack::gmm::GMM< FittingType >::weights.
|
private |
Vector of covariances; one for each Gaussian.
Definition at line 99 of file gmm.hpp.
Referenced by mlpack::gmm::GMM< FittingType >::Covariances().
|
private |
The dimensionality of the model.
Definition at line 95 of file gmm.hpp.
Referenced by mlpack::gmm::GMM< FittingType >::Dimensionality().
|
private |
Reference to the fitting object we should use.
Definition at line 368 of file gmm.hpp.
Referenced by mlpack::gmm::GMM< FittingType >::Fitter().
|
private |
The number of Gaussians in the model.
Definition at line 93 of file gmm.hpp.
Referenced by mlpack::gmm::GMM< FittingType >::Gaussians().
|
private |
|
private |
Vector of means; one for each Gaussian.
Definition at line 97 of file gmm.hpp.
Referenced by mlpack::gmm::GMM< FittingType >::Means().
|
private |
Vector of a priori weights for each Gaussian.
Definition at line 101 of file gmm.hpp.
Referenced by mlpack::gmm::GMM< FittingType >::Weights().