FullGmmNormal Class Reference

Definition for Gaussian Mixture Model with full covariances in normal mode: where the parameters are stored as means and variances (instead of the exponential form that the FullGmm class is stored as). More...

#include <full-gmm-normal.h>

Collaboration diagram for FullGmmNormal:

Public Member Functions

 FullGmmNormal ()
 Empty constructor. More...
 
 FullGmmNormal (const FullGmm &gmm)
 
void Resize (int32 nMix, int32 dim)
 Resizes arrays to this dim. Does not initialize data. More...
 
void CopyFromFullGmm (const FullGmm &fullgmm)
 Copies from given FullGmm. More...
 
void CopyToFullGmm (FullGmm *fullgmm, GmmFlagsType flags=kGmmAll)
 Copies to FullGmm. More...
 
void Rand (MatrixBase< BaseFloat > *feats)
 Generates random features from the model. More...
 
 KALDI_DISALLOW_COPY_AND_ASSIGN (FullGmmNormal)
 

Public Attributes

Vector< double > weights_
 weights (not log). More...
 
Matrix< double > means_
 Means. More...
 
std::vector< SpMatrix< double > > vars_
 covariances More...
 

Detailed Description

Definition for Gaussian Mixture Model with full covariances in normal mode: where the parameters are stored as means and variances (instead of the exponential form that the FullGmm class is stored as).

This class will be used in the update (since the update formulas are for the standard parameterization) and then copied to the exponential form of the FullGmm class. The FullGmmNormal class will not be used anywhere else, and should not have any extra methods that are not needed.

Definition at line 45 of file full-gmm-normal.h.

Constructor & Destructor Documentation

◆ FullGmmNormal() [1/2]

FullGmmNormal ( )
inline

Empty constructor.

Definition at line 48 of file full-gmm-normal.h.

48 { }

◆ FullGmmNormal() [2/2]

FullGmmNormal ( const FullGmm gmm)
inlineexplicit

Definition at line 50 of file full-gmm-normal.h.

References FullGmmNormal::CopyFromFullGmm(), FullGmmNormal::CopyToFullGmm(), kaldi::kGmmAll, FullGmmNormal::Rand(), and FullGmmNormal::Resize().

50  {
51  CopyFromFullGmm(gmm);
52  }
void CopyFromFullGmm(const FullGmm &fullgmm)
Copies from given FullGmm.

Member Function Documentation

◆ CopyFromFullGmm()

void CopyFromFullGmm ( const FullGmm fullgmm)

Copies from given FullGmm.

resize the variables to fit the gmm

copy weights

we need to split the natural components for each gaussian

Definition at line 53 of file full-gmm-normal.cc.

References VectorBase< Real >::CopyFromVec(), FullGmm::Dim(), rnnlm::i, FullGmm::inv_covars_, FullGmmNormal::means_, FullGmm::means_invcovars_, FullGmm::NumGauss(), FullGmmNormal::Resize(), MatrixBase< Real >::Row(), FullGmmNormal::vars_, FullGmmNormal::weights_, and FullGmm::weights_.

Referenced by FullGmmNormal::FullGmmNormal().

53  {
55  size_t dim = fullgmm.Dim();
56  size_t num_gauss = fullgmm.NumGauss();
57  Resize(num_gauss, dim);
58 
60  weights_.CopyFromVec(fullgmm.weights_);
61 
63  Vector<double> mean_times_invcovar(dim);
64 
65  for (size_t i = 0; i < num_gauss; i++) {
66  // copy and invert (inverse) covariance matrix
67  vars_[i].CopyFromSp(fullgmm.inv_covars_[i]);
68  vars_[i].InvertDouble();
69 
70  // multiply the (mean x icov) by (cov) to get the means back
71  mean_times_invcovar.CopyFromVec(fullgmm.means_invcovars_.Row(i));
72  (means_.Row(i)).AddSpVec(1.0, vars_[i], mean_times_invcovar, 0.0);
73  }
74 }
void Resize(int32 nMix, int32 dim)
Resizes arrays to this dim. Does not initialize data.
std::vector< SpMatrix< double > > vars_
covariances
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
Vector< double > weights_
weights (not log).
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
Matrix< double > means_
Means.

◆ CopyToFullGmm()

void CopyToFullGmm ( FullGmm fullgmm,
GmmFlagsType  flags = kGmmAll 
)

Copies to FullGmm.

Definition at line 76 of file full-gmm-normal.cc.

References VectorBase< Real >::AddSpVec(), FullGmm::Dim(), VectorBase< Real >::Dim(), rnnlm::i, FullGmm::inv_covars_, KALDI_ASSERT, kaldi::kGmmMeans, kaldi::kGmmVariances, kaldi::kGmmWeights, FullGmmNormal::means_, FullGmm::means_invcovars_, MatrixBase< Real >::NumCols(), FullGmm::NumGauss(), MatrixBase< Real >::Row(), FullGmm::valid_gconsts_, FullGmmNormal::vars_, FullGmmNormal::weights_, and FullGmm::weights_.

Referenced by Sgmm2Project::ApplyProjection(), FullGmmNormal::FullGmmNormal(), kaldi::MleFullGmmUpdate(), and UnitTestEstimateFullGmm().

76  {
77  KALDI_ASSERT(weights_.Dim() == fullgmm->weights_.Dim()
78  && means_.NumCols() == fullgmm->Dim());
79 
80  FullGmmNormal oldg(*fullgmm);
81 
82  if (flags & kGmmWeights)
83  fullgmm->weights_.CopyFromVec(weights_);
84 
85  size_t num_comp = fullgmm->NumGauss(), dim = fullgmm->Dim();
86  for (size_t i = 0; i < num_comp; i++) {
87  if (flags & kGmmVariances) {
88  fullgmm->inv_covars_[i].CopyFromSp(vars_[i]);
89  fullgmm->inv_covars_[i].InvertDouble();
90 
91  // update the mean-related natural part with old mean, if necessary
92  if (!(flags & kGmmMeans)) {
93  Vector<BaseFloat> mean_times_inv(dim);
94  Vector<BaseFloat> mhelp(oldg.means_.Row(i));
95  mean_times_inv.AddSpVec(1.0, fullgmm->inv_covars_[i], mhelp, 0.0f);
96  fullgmm->means_invcovars_.Row(i).CopyFromVec(mean_times_inv);
97  }
98  }
99 
100  if (flags & kGmmMeans) {
101  Vector<BaseFloat> mean_times_inv(dim), mean(means_.Row(i));
102  mean_times_inv.AddSpVec(1.0, fullgmm->inv_covars_[i], mean, 0.0f);
103  fullgmm->means_invcovars_.Row(i).CopyFromVec(mean_times_inv);
104  }
105  }
106 
107  fullgmm->valid_gconsts_ = false;
108 }
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
std::vector< SpMatrix< double > > vars_
covariances
Vector< double > weights_
weights (not log).
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
FullGmmNormal()
Empty constructor.
Matrix< double > means_
Means.

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( FullGmmNormal  )

◆ Rand()

void Rand ( MatrixBase< BaseFloat > *  feats)

Generates random features from the model.

Definition at line 110 of file full-gmm-normal.cc.

References VectorBase< Real >::AddTpVec(), VectorBase< Real >::CopyFromVec(), rnnlm::i, KALDI_ASSERT, kaldi::kNoTrans, FullGmmNormal::means_, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), VectorBase< Real >::RandCategorical(), MatrixBase< Real >::Row(), VectorBase< Real >::SetRandn(), FullGmmNormal::vars_, and FullGmmNormal::weights_.

Referenced by FullGmmNormal::FullGmmNormal(), UnitTestFullGmmEst(), and kaldi::UnitTestIvectorExtractor().

110  {
111  int32 dim = means_.NumCols(), num_frames = feats->NumRows(),
112  num_gauss = means_.NumRows();
113  KALDI_ASSERT(feats->NumCols() == dim);
114  std::vector<TpMatrix<BaseFloat> > sqrt_var(num_gauss);
115  for (int32 i = 0; i < num_gauss; i++) {
116  sqrt_var[i].Resize(dim);
117  sqrt_var[i].Cholesky(SpMatrix<BaseFloat>(vars_[i]));
118  }
119  Vector<BaseFloat> rand(dim);
120  for (int32 t = 0; t < num_frames; t++) {
121  int32 i = weights_.RandCategorical(); // index with prob propto weights_[i].
122  SubVector<BaseFloat> frame(*feats, t);
123  frame.CopyFromVec(means_.Row(i));
124  rand.SetRandn();
125  frame.AddTpVec(1.0, sqrt_var[i], kNoTrans, rand, 1.0);
126  }
127 }
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
kaldi::int32 int32
std::vector< SpMatrix< double > > vars_
covariances
Vector< double > weights_
weights (not log).
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
MatrixIndexT RandCategorical() const
This function returns a random index into this vector, chosen with probability proportional to the co...
Matrix< double > means_
Means.

◆ Resize()

void Resize ( int32  nMix,
int32  dim 
)

Resizes arrays to this dim. Does not initialize data.

Definition at line 33 of file full-gmm-normal.cc.

References VectorBase< Real >::Dim(), rnnlm::i, KALDI_ASSERT, FullGmmNormal::means_, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), Vector< Real >::Resize(), Matrix< Real >::Resize(), FullGmmNormal::vars_, and FullGmmNormal::weights_.

Referenced by FullGmmNormal::CopyFromFullGmm(), and FullGmmNormal::FullGmmNormal().

33  {
34  KALDI_ASSERT(nmix > 0 && dim > 0);
35 
36  if (weights_.Dim() != nmix)
37  weights_.Resize(nmix);
38 
39  if (means_.NumRows() != nmix ||
40  means_.NumCols() != dim)
41  means_.Resize(nmix, dim);
42 
43  if (vars_.size() != nmix)
44  vars_.resize(nmix);
45  for (int32 i = 0; i < nmix; i++) {
46  if (vars_[i].NumRows() != nmix ||
47  vars_[i].NumCols() != dim) {
48  vars_[i].Resize(dim);
49  }
50  }
51 }
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
kaldi::int32 int32
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
std::vector< SpMatrix< double > > vars_
covariances
Vector< double > weights_
weights (not log).
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).
Matrix< double > means_
Means.

Member Data Documentation

◆ means_

◆ vars_

◆ weights_


The documentation for this class was generated from the following files: