full-gmm.h
Go to the documentation of this file.
1 // gmm/full-gmm.h
2 
3 // Copyright 2009-2011 Jan Silovsky;
4 // Saarland University (Author: Arnab Ghoshal);
5 // Microsoft Corporation
6 // 2012 Arnab Ghoshal
7 // 2013 Johns Hopkins University (author: Daniel Povey)
8 
9 // See ../../COPYING for clarification regarding multiple authors
10 //
11 // Licensed under the Apache License, Version 2.0 (the "License");
12 // you may not use this file except in compliance with the License.
13 // You may obtain a copy of the License at
14 //
15 // http://www.apache.org/licenses/LICENSE-2.0
16 //
17 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
19 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
20 // MERCHANTABLITY OR NON-INFRINGEMENT.
21 // See the Apache 2 License for the specific language governing permissions and
22 // limitations under the License.
23 
24 #ifndef KALDI_GMM_FULL_GMM_H_
25 #define KALDI_GMM_FULL_GMM_H_
26 
27 #include <utility>
28 #include <vector>
29 
30 #include "base/kaldi-common.h"
31 #include "gmm/model-common.h"
32 #include "matrix/matrix-lib.h"
33 
34 namespace kaldi {
35 
36 class DiagGmm;
37 class FullGmmNormal; // a simplified representation, see full-gmm-normal.h
38 
40 class FullGmm {
42  friend class FullGmmNormal;
43 
44  public:
46  FullGmm() : valid_gconsts_(false) {}
47 
48  explicit FullGmm(const FullGmm &gmm): valid_gconsts_(false) {
49  CopyFromFullGmm(gmm);
50  }
51 
52  FullGmm(int32 nMix, int32 dim): valid_gconsts_(false) { Resize(nMix, dim); }
53 
55  void Resize(int32 nMix, int32 dim);
56 
58  int32 NumGauss() const { return weights_.Dim(); }
60  int32 Dim() const { return means_invcovars_.NumCols(); }
61 
63  void CopyFromFullGmm(const FullGmm &fullgmm);
65  void CopyFromDiagGmm(const DiagGmm &diaggmm);
66 
69 
72  void LogLikelihoods(const VectorBase<BaseFloat> &data,
73  Vector<BaseFloat> *loglikes) const;
74 
80  const std::vector<int32> &indices,
81  Vector<BaseFloat> *loglikes) const;
82 
87  int32 num_gselect,
88  std::vector<int32> *output) const;
89 
95  const std::vector<int32> &preselect,
96  int32 num_gselect,
97  std::vector<int32> *output) const;
98 
102  VectorBase<BaseFloat> *posterior) const;
103 
108  int32 comp_id) const;
109 
113 
116  void Split(int32 target_components, float perturb_factor,
117  std::vector<int32> *history = NULL);
118 
121  void Perturb(float perturb_factor);
122 
125  void Merge(int32 target_components,
126  std::vector<int32> *history = NULL);
127 
132  BaseFloat MergePreselect(int32 target_components,
133  const std::vector<std::pair<int32, int32> > &preselect_pairs);
134 
135  void Write(std::ostream &os, bool binary) const;
136  void Read(std::istream &is, bool binary);
137 
139  void Interpolate(BaseFloat rho, const FullGmm &source,
140  GmmFlagsType flags = kGmmAll);
141 
143  const Vector<BaseFloat> &gconsts() const { return gconsts_; }
144  const Vector<BaseFloat> &weights() const { return weights_; }
146  const std::vector<SpMatrix<BaseFloat> > &inv_covars() const {
147  return inv_covars_; }
148 
151  std::vector<SpMatrix<BaseFloat> > &inv_covars() { return inv_covars_; }
152 
154  template<class Real>
155  void SetWeights(const Vector<Real> &w);
156 
158  template<class Real>
159  void SetMeans(const Matrix<Real> &m);
160 
162  template<class Real>
163  void SetInvCovarsAndMeans(const std::vector<SpMatrix<Real> > &invcovars,
164  const Matrix<Real> &means);
165 
167  template<class Real>
168  void SetInvCovarsAndMeansInvCovars(const std::vector<SpMatrix<Real> > &invcovars,
170 
172  template<class Real>
173  void SetInvCovars(const std::vector<SpMatrix<Real> > &v);
174 
176  template<class Real>
177  void GetCovars(std::vector<SpMatrix<Real> > *v) const;
179  template<class Real>
180  void GetMeans(Matrix<Real> *m) const;
182  template<class Real>
183  void GetCovarsAndMeans(std::vector< SpMatrix<Real> > *covars,
184  Matrix<Real> *means) const;
185 
188  void RemoveComponent(int32 gauss, bool renorm_weights);
189 
191  void RemoveComponents(const std::vector<int32> &gauss, bool renorm_weights);
192 
194  template<class Real>
195  void GetComponentMean(int32 gauss, VectorBase<Real> *out) const;
196 
197  private:
202  std::vector<SpMatrix<BaseFloat> > inv_covars_;
204 
206  void ResizeInvCovars(int32 nMix, int32 dim);
207 
208  // merged_components_logdet computes logdet for merged components
209  // f1, f2 are first-order stats (normalized by zero-order stats)
210  // s1, s2 are second-order stats (normalized by zero-order stats)
212  const VectorBase<BaseFloat> &f1,
213  const VectorBase<BaseFloat> &f2,
214  const SpMatrix<BaseFloat> &s1,
215  const SpMatrix<BaseFloat> &s2) const;
216 
217  const FullGmm &operator=(const FullGmm &other); // Disallow assignment.
218 };
219 
221 std::ostream &
222 operator << (std::ostream & rOut, const kaldi::FullGmm &gmm);
224 std::istream &
225 operator >> (std::istream & rIn, kaldi::FullGmm &gmm);
226 
227 } // End namespace kaldi
228 
229 #include "gmm/full-gmm-inl.h" // templated functions.
230 
231 #endif // KALDI_GMM_FULL_GMM_H_
std::ostream & operator<<(std::ostream &os, const MatrixBase< Real > &M)
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void SetWeights(const Vector< Real > &w)
Mutators for both float or double.
Definition: full-gmm-inl.h:31
Packed symetric matrix class.
Definition: matrix-common.h:62
void Merge(int32 target_components, std::vector< int32 > *history=NULL)
Merge the components and remember the order in which the components were merged (flat list of pairs) ...
Definition: full-gmm.cc:206
const std::vector< SpMatrix< BaseFloat > > & inv_covars() const
Definition: full-gmm.h:146
BaseFloat ComponentPosteriors(const VectorBase< BaseFloat > &data, VectorBase< BaseFloat > *posterior) const
Computes the posterior probabilities of all Gaussian components given a data point.
Definition: full-gmm.cc:719
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: full-gmm.h:60
Definition for Gaussian Mixture Model with full covariances in normal mode: where the parameters are ...
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
void LogLikelihoodsPreselect(const VectorBase< BaseFloat > &data, const std::vector< int32 > &indices, Vector< BaseFloat > *loglikes) const
Outputs the per-component log-likelihoods of a subset of mixture components.
Definition: full-gmm.cc:613
int32 ComputeGconsts()
Sets the gconsts.
Definition: full-gmm.cc:92
void Split(int32 target_components, float perturb_factor, std::vector< int32 > *history=NULL)
Merge the components and remember the order in which the components were merged (flat list of pairs) ...
Definition: full-gmm.cc:132
Definition for Gaussian Mixture Model with full covariances.
Definition: full-gmm.h:40
std::vector< SpMatrix< BaseFloat > > & inv_covars()
Definition: full-gmm.h:151
void SetInvCovarsAndMeans(const std::vector< SpMatrix< Real > > &invcovars, const Matrix< Real > &means)
Use SetInvCovarsAndMeans if updating both means and (inverse) covariances.
Definition: full-gmm-inl.h:50
Vector< BaseFloat > weights_
weights (not log).
Definition: full-gmm.h:201
kaldi::int32 int32
void Write(std::ostream &os, bool binary) const
Definition: full-gmm.cc:758
uint16 GmmFlagsType
Bitwise OR of the above flags.
Definition: model-common.h:35
const Vector< BaseFloat > & gconsts() const
Const accessors.
Definition: full-gmm.h:143
void GetCovars(std::vector< SpMatrix< Real > > *v) const
Accessor for covariances.
Definition: full-gmm-inl.h:106
FullGmm(const FullGmm &gmm)
Definition: full-gmm.h:48
void Perturb(float perturb_factor)
Perturbs the component means with a random vector multiplied by the pertrub factor.
Definition: full-gmm.cc:191
void RemoveComponent(int32 gauss, bool renorm_weights)
Mutators for single component, supports float or double Removes single component from model...
Definition: full-gmm.cc:731
FullGmm()
Empty constructor.
Definition: full-gmm.h:46
void CopyFromFullGmm(const FullGmm &fullgmm)
Copies from given FullGmm.
Definition: full-gmm.cc:65
void Resize(int32 nMix, int32 dim)
Resizes arrays to this dim. Does not initialize data.
Definition: full-gmm.cc:41
void ResizeInvCovars(int32 nMix, int32 dim)
Resizes arrays to this dim. Does not initialize data.
Definition: full-gmm.cc:51
void RemoveComponents(const std::vector< int32 > &gauss, bool renorm_weights)
Removes multiple components from model; "gauss" must not have dups.
Definition: full-gmm.cc:745
bool valid_gconsts_
Recompute gconsts_ if false.
Definition: full-gmm.h:200
FullGmm(int32 nMix, int32 dim)
Definition: full-gmm.h:52
BaseFloat GaussianSelectionPreselect(const VectorBase< BaseFloat > &data, const std::vector< int32 > &preselect, int32 num_gselect, std::vector< int32 > *output) const
Get gaussian selection information for one frame.
Definition: full-gmm.cc:674
BaseFloat GaussianSelection(const VectorBase< BaseFloat > &data, int32 num_gselect, std::vector< int32 > *output) const
Get gaussian selection information for one frame.
Definition: full-gmm.cc:637
BaseFloat MergedComponentsLogdet(BaseFloat w1, BaseFloat w2, const VectorBase< BaseFloat > &f1, const VectorBase< BaseFloat > &f2, const SpMatrix< BaseFloat > &s1, const SpMatrix< BaseFloat > &s2) const
Definition: full-gmm.cc:537
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
const Vector< BaseFloat > & weights() const
Definition: full-gmm.h:144
void SetInvCovarsAndMeansInvCovars(const std::vector< SpMatrix< Real > > &invcovars, const Matrix< Real > &means_invcovars)
Use this if setting both, in the class&#39;s native format.
Definition: full-gmm-inl.h:67
void Read(std::istream &is, bool binary)
Definition: full-gmm.cc:813
BaseFloat MergePreselect(int32 target_components, const std::vector< std::pair< int32, int32 > > &preselect_pairs)
Merge the components and remember the order in which the components were merged (flat list of pairs);...
Definition: full-gmm.cc:382
void GetMeans(Matrix< Real > *m) const
Accessor for means.
Definition: full-gmm-inl.h:118
std::istream & operator>>(std::istream &is, Matrix< Real > &M)
BaseFloat ComponentLogLikelihood(const VectorBase< BaseFloat > &data, int32 comp_id) const
Computes the contribution log-likelihood of a data point from a single Gaussian component.
Definition: full-gmm.cc:562
void SetInvCovars(const std::vector< SpMatrix< Real > > &v)
Set the (inverse) covariances and recompute means_invcovars_.
Definition: full-gmm-inl.h:83
A class representing a vector.
Definition: kaldi-vector.h:406
void LogLikelihoods(const VectorBase< BaseFloat > &data, Vector< BaseFloat > *loglikes) const
Outputs the per-component contributions to the log-likelihood.
Definition: full-gmm.cc:591
void CopyFromDiagGmm(const DiagGmm &diaggmm)
Copies from given DiagGmm.
Definition: full-gmm.cc:77
Definition for Gaussian Mixture Model with diagonal covariances.
Definition: diag-gmm.h:42
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
void Interpolate(BaseFloat rho, const FullGmm &source, GmmFlagsType flags=kGmmAll)
this = rho x source + (1-rho) x this
Definition: full-gmm.cc:784
const Matrix< BaseFloat > & means_invcovars() const
Definition: full-gmm.h:145
const FullGmm & operator=(const FullGmm &other)
Provides a vector abstraction class.
Definition: kaldi-vector.h:41
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
BaseFloat LogLikelihood(const VectorBase< BaseFloat > &data) const
Returns the log-likelihood of a data point (vector) given the GMM.
Definition: full-gmm.cc:582
void SetMeans(const Matrix< Real > &m)
Use SetMeans to update only the Gaussian means (and not variances)
Definition: full-gmm-inl.h:38
void GetCovarsAndMeans(std::vector< SpMatrix< Real > > *covars, Matrix< Real > *means) const
Accessor for covariances and means.
Definition: full-gmm-inl.h:132
Matrix< BaseFloat > & means_invcovars()
Non-const accessors.
Definition: full-gmm.h:150
void GetComponentMean(int32 gauss, VectorBase< Real > *out) const
Accessor for component mean.
Definition: full-gmm-inl.h:151
Vector< BaseFloat > gconsts_
Equals log(weight) - 0.5 * (log det(var) + mean&#39;*inv(var)*mean)
Definition: full-gmm.h:199