AccumFullGmm Class Reference

Class for computing the maximum-likelihood estimates of the parameters of a Gaussian mixture model. More...

#include <mle-full-gmm.h>

Collaboration diagram for AccumFullGmm:

Public Member Functions

 AccumFullGmm ()
 
 AccumFullGmm (int32 num_comp, int32 dim, GmmFlagsType flags)
 
 AccumFullGmm (const FullGmm &gmm, GmmFlagsType flags)
 
 AccumFullGmm (const AccumFullGmm &other)
 
void Read (std::istream &in_stream, bool binary, bool add)
 
void Write (std::ostream &out_stream, bool binary) const
 
void Resize (int32 num_components, int32 dim, GmmFlagsType flags)
 Allocates memory for accumulators. More...
 
void Resize (const FullGmm &gmm, GmmFlagsType flags)
 Calls Resize with arguments based on gmm_ptr_. More...
 
void ResizeVarAccumulator (int32 num_comp, int32 dim)
 
int32 NumGauss () const
 Returns the number of mixture components. More...
 
int32 Dim () const
 Returns the dimensionality of the feature vectors. More...
 
void SetZero (GmmFlagsType flags)
 
void Scale (BaseFloat f, GmmFlagsType flags)
 
void AccumulateForComponent (const VectorBase< BaseFloat > &data, int32 comp_index, BaseFloat weight)
 Accumulate for a single component, given the posterior. More...
 
void AccumulateFromPosteriors (const VectorBase< BaseFloat > &data, const VectorBase< BaseFloat > &gauss_posteriors)
 Accumulate for all components, given the posteriors. More...
 
BaseFloat AccumulateFromFull (const FullGmm &gmm, const VectorBase< BaseFloat > &data, BaseFloat frame_posterior)
 Accumulate for all components given a full-covariance GMM. More...
 
BaseFloat AccumulateFromDiag (const DiagGmm &gmm, const VectorBase< BaseFloat > &data, BaseFloat frame_posterior)
 Accumulate for all components given a diagonal-covariance GMM. More...
 
GmmFlagsType Flags () const
 Accessors. More...
 
const Vector< double > & occupancy () const
 
const Matrix< double > & mean_accumulator () const
 
const std::vector< SpMatrix< double > > & covariance_accumulator () const
 

Private Attributes

int32 dim_
 
int32 num_comp_
 
GmmFlagsType flags_
 
Vector< double > occupancy_
 
Matrix< double > mean_accumulator_
 
std::vector< SpMatrix< double > > covariance_accumulator_
 

Detailed Description

Class for computing the maximum-likelihood estimates of the parameters of a Gaussian mixture model.

Definition at line 74 of file mle-full-gmm.h.

Constructor & Destructor Documentation

◆ AccumFullGmm() [1/4]

AccumFullGmm ( )
inline

Definition at line 76 of file mle-full-gmm.h.

76 : dim_(0), num_comp_(0), flags_(0) { }
GmmFlagsType flags_
Definition: mle-full-gmm.h:134

◆ AccumFullGmm() [2/4]

AccumFullGmm ( int32  num_comp,
int32  dim,
GmmFlagsType  flags 
)
inline

Definition at line 77 of file mle-full-gmm.h.

77  :
78  dim_(0), num_comp_(0), flags_(0) {
79  Resize(num_comp, dim, flags);
80  }
GmmFlagsType flags_
Definition: mle-full-gmm.h:134
void Resize(int32 num_components, int32 dim, GmmFlagsType flags)
Allocates memory for accumulators.
Definition: mle-full-gmm.cc:37

◆ AccumFullGmm() [3/4]

AccumFullGmm ( const FullGmm gmm,
GmmFlagsType  flags 
)
inlineexplicit

Definition at line 81 of file mle-full-gmm.h.

81  {
82  Resize(gmm, flags);
83  }
void Resize(int32 num_components, int32 dim, GmmFlagsType flags)
Allocates memory for accumulators.
Definition: mle-full-gmm.cc:37

◆ AccumFullGmm() [4/4]

AccumFullGmm ( const AccumFullGmm other)
explicit

Definition at line 31 of file mle-full-gmm.cc.

32  : dim_(other.dim_), num_comp_(other.num_comp_),
33  flags_(other.flags_), occupancy_(other.occupancy_),
34  mean_accumulator_(other.mean_accumulator_),
35  covariance_accumulator_(other.covariance_accumulator_) {}
std::vector< SpMatrix< double > > covariance_accumulator_
Definition: mle-full-gmm.h:138
Vector< double > occupancy_
Definition: mle-full-gmm.h:136
GmmFlagsType flags_
Definition: mle-full-gmm.h:134
Matrix< double > mean_accumulator_
Definition: mle-full-gmm.h:137

Member Function Documentation

◆ AccumulateForComponent()

void AccumulateForComponent ( const VectorBase< BaseFloat > &  data,
int32  comp_index,
BaseFloat  weight 
)

Accumulate for a single component, given the posterior.

Definition at line 96 of file mle-full-gmm.cc.

References AccumFullGmm::covariance_accumulator_, VectorBase< Real >::Dim(), AccumFullGmm::Dim(), AccumFullGmm::flags_, KALDI_ASSERT, kaldi::kGmmMeans, kaldi::kGmmVariances, AccumFullGmm::mean_accumulator_, AccumFullGmm::occupancy_, and MatrixBase< Real >::Row().

Referenced by kaldi::AccumulateForUtterance(), main(), and TestComponentAcc().

97  {
98  KALDI_ASSERT(data.Dim() == Dim());
99  double wt = static_cast<double>(weight);
100 
101  // accumulate
102  occupancy_(comp_index) += wt;
103  if (flags_ & kGmmMeans) {
104  Vector<double> data_d(data); // Copy with type-conversion
105  mean_accumulator_.Row(comp_index).AddVec(wt, data_d);
106  if (flags_ & kGmmVariances) {
107  covariance_accumulator_[comp_index].AddVec2(wt, data_d);
108  }
109  }
110 }
std::vector< SpMatrix< double > > covariance_accumulator_
Definition: mle-full-gmm.h:138
Vector< double > occupancy_
Definition: mle-full-gmm.h:136
int32 Dim() const
Returns the dimensionality of the feature vectors.
Definition: mle-full-gmm.h:99
GmmFlagsType flags_
Definition: mle-full-gmm.h:134
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
Matrix< double > mean_accumulator_
Definition: mle-full-gmm.h:137

◆ AccumulateFromDiag()

BaseFloat AccumulateFromDiag ( const DiagGmm gmm,
const VectorBase< BaseFloat > &  data,
BaseFloat  frame_posterior 
)

Accumulate for all components given a diagonal-covariance GMM.

Computes posteriors and returns log-likelihood

Definition at line 157 of file mle-full-gmm.cc.

References AccumFullGmm::AccumulateFromPosteriors(), DiagGmm::ComponentPosteriors(), DiagGmm::Dim(), AccumFullGmm::Dim(), KALDI_ASSERT, DiagGmm::NumGauss(), and AccumFullGmm::NumGauss().

Referenced by kaldi::AccumulateForUtterance().

158  {
159  KALDI_ASSERT(gmm.NumGauss() == NumGauss());
160  KALDI_ASSERT(gmm.Dim() == Dim());
161 
162  Vector<BaseFloat> component_posterior(NumGauss());
163 
164  BaseFloat log_like = gmm.ComponentPosteriors(data, &component_posterior);
165  component_posterior.Scale(frame_posterior);
166 
167  AccumulateFromPosteriors(data, component_posterior);
168  return log_like;
169 }
int32 NumGauss() const
Returns the number of mixture components.
Definition: mle-full-gmm.h:97
int32 Dim() const
Returns the dimensionality of the feature vectors.
Definition: mle-full-gmm.h:99
float BaseFloat
Definition: kaldi-types.h:29
void AccumulateFromPosteriors(const VectorBase< BaseFloat > &data, const VectorBase< BaseFloat > &gauss_posteriors)
Accumulate for all components, given the posteriors.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ AccumulateFromFull()

BaseFloat AccumulateFromFull ( const FullGmm gmm,
const VectorBase< BaseFloat > &  data,
BaseFloat  frame_posterior 
)

Accumulate for all components given a full-covariance GMM.

Computes posteriors and returns log-likelihood

Definition at line 143 of file mle-full-gmm.cc.

References AccumFullGmm::AccumulateFromPosteriors(), FullGmm::ComponentPosteriors(), FullGmm::Dim(), AccumFullGmm::Dim(), KALDI_ASSERT, FullGmm::NumGauss(), and AccumFullGmm::NumGauss().

Referenced by main(), test_flags_driven_update(), TestComponentAcc(), UnitTestEstimateFullGmm(), and UnitTestFullGmmEst().

144  {
145  KALDI_ASSERT(gmm.NumGauss() == NumGauss());
146  KALDI_ASSERT(gmm.Dim() == Dim());
147 
148  Vector<BaseFloat> component_posterior(NumGauss());
149 
150  BaseFloat log_like = gmm.ComponentPosteriors(data, &component_posterior);
151  component_posterior.Scale(frame_posterior);
152 
153  AccumulateFromPosteriors(data, component_posterior);
154  return log_like;
155 }
int32 NumGauss() const
Returns the number of mixture components.
Definition: mle-full-gmm.h:97
int32 Dim() const
Returns the dimensionality of the feature vectors.
Definition: mle-full-gmm.h:99
float BaseFloat
Definition: kaldi-types.h:29
void AccumulateFromPosteriors(const VectorBase< BaseFloat > &data, const VectorBase< BaseFloat > &gauss_posteriors)
Accumulate for all components, given the posteriors.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ AccumulateFromPosteriors()

void AccumulateFromPosteriors ( const VectorBase< BaseFloat > &  data,
const VectorBase< BaseFloat > &  gauss_posteriors 
)

Accumulate for all components, given the posteriors.

Definition at line 112 of file mle-full-gmm.cc.

References VectorBase< Real >::AddVec(), SpMatrix< Real >::AddVec2(), MatrixBase< Real >::AddVecVec(), VectorBase< Real >::CopyFromVec(), AccumFullGmm::covariance_accumulator_, VectorBase< Real >::Dim(), AccumFullGmm::Dim(), AccumFullGmm::flags_, rnnlm::i, KALDI_ASSERT, kaldi::kGmmMeans, kaldi::kGmmVariances, AccumFullGmm::mean_accumulator_, AccumFullGmm::NumGauss(), AccumFullGmm::occupancy_, and MatrixBase< Real >::Row().

Referenced by AccumFullGmm::AccumulateFromDiag(), and AccumFullGmm::AccumulateFromFull().

114  {
115  KALDI_ASSERT(gauss_posteriors.Dim() == NumGauss());
116  KALDI_ASSERT(data.Dim() == Dim());
117  Vector<double> data_d(data.Dim());
118  data_d.CopyFromVec(data);
119  Vector<double> post_d(gauss_posteriors.Dim());
120  post_d.CopyFromVec(gauss_posteriors);
121 
122  occupancy_.AddVec(1.0, post_d);
123  if (flags_ & (kGmmMeans|kGmmVariances)) { // mean stats.
124  if (static_cast<int32>(post_d.Norm(0.0)*2.0) > post_d.Dim()) {
125  // If we're not very sparse... note: zero-norm is number of
126  // nonzero elements.
127  mean_accumulator_.AddVecVec(1.0, post_d, data_d);
128  } else {
129  for (int32 i = 0; i < post_d.Dim(); i++)
130  if (post_d(i) != 0.0)
131  mean_accumulator_.Row(i).AddVec(post_d(i), data_d);
132  }
133  if (flags_ & kGmmVariances) {
134  SpMatrix<double> data_sq_d(data_d.Dim());
135  data_sq_d.AddVec2(1.0, data_d);
136  for (int32 mix = 0; mix < NumGauss(); mix++)
137  if (post_d(mix) != 0.0)
138  covariance_accumulator_[mix].AddSp(post_d(mix), data_sq_d);
139  }
140  }
141 }
int32 NumGauss() const
Returns the number of mixture components.
Definition: mle-full-gmm.h:97
std::vector< SpMatrix< double > > covariance_accumulator_
Definition: mle-full-gmm.h:138
Vector< double > occupancy_
Definition: mle-full-gmm.h:136
int32 Dim() const
Returns the dimensionality of the feature vectors.
Definition: mle-full-gmm.h:99
kaldi::int32 int32
GmmFlagsType flags_
Definition: mle-full-gmm.h:134
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
void AddVecVec(const Real alpha, const VectorBase< OtherReal > &a, const VectorBase< OtherReal > &b)
*this += alpha * a * b^T
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...
Matrix< double > mean_accumulator_
Definition: mle-full-gmm.h:137

◆ covariance_accumulator()

const std::vector<SpMatrix<double> >& covariance_accumulator ( ) const
inline

Definition at line 129 of file mle-full-gmm.h.

Referenced by FmllrDiagGmmAccs::FmllrDiagGmmAccs(), kaldi::MleFullGmmUpdate(), and kaldi::MlObjective().

129 { return covariance_accumulator_; }
std::vector< SpMatrix< double > > covariance_accumulator_
Definition: mle-full-gmm.h:138

◆ Dim()

◆ Flags()

GmmFlagsType Flags ( ) const
inline

Accessors.

Definition at line 126 of file mle-full-gmm.h.

Referenced by kaldi::MleFullGmmUpdate(), kaldi::MlObjective(), AccumFullGmm::Read(), and test_io().

126 { return flags_; }
GmmFlagsType flags_
Definition: mle-full-gmm.h:134

◆ mean_accumulator()

const Matrix<double>& mean_accumulator ( ) const
inline

Definition at line 128 of file mle-full-gmm.h.

Referenced by FmllrDiagGmmAccs::FmllrDiagGmmAccs(), kaldi::MleFullGmmUpdate(), and kaldi::MlObjective().

128 { return mean_accumulator_; }
Matrix< double > mean_accumulator_
Definition: mle-full-gmm.h:137

◆ NumGauss()

int32 NumGauss ( ) const
inline

◆ occupancy()

const Vector<double>& occupancy ( ) const
inline

Definition at line 127 of file mle-full-gmm.h.

Referenced by FmllrDiagGmmAccs::FmllrDiagGmmAccs(), kaldi::MleFullGmmUpdate(), and kaldi::MlObjective().

127 { return occupancy_; }
Vector< double > occupancy_
Definition: mle-full-gmm.h:136

◆ Read()

void Read ( std::istream &  in_stream,
bool  binary,
bool  add 
)

Definition at line 171 of file mle-full-gmm.cc.

References MatrixBase< Real >::AddMat(), VectorBase< Real >::AddVec(), SpMatrix< Real >::AddVec2(), AccumFullGmm::covariance_accumulator_, AccumFullGmm::Dim(), kaldi::ExpectToken(), AccumFullGmm::Flags(), kaldi::GmmFlagsToString(), rnnlm::i, KALDI_ASSERT, KALDI_ERR, AccumFullGmm::mean_accumulator_, AccumFullGmm::NumGauss(), AccumFullGmm::occupancy_, PackedMatrix< Real >::Read(), Vector< Real >::Read(), Matrix< Real >::Read(), kaldi::ReadBasicType(), kaldi::ReadToken(), AccumFullGmm::Resize(), MatrixBase< Real >::Row(), VectorBase< Real >::SetZero(), and MatrixBase< Real >::SetZero().

Referenced by main(), and test_io().

171  {
172  int32 dimension, num_components;
173  GmmFlagsType flags;
174  std::string token;
175 
176  ExpectToken(in_stream, binary, "<GMMACCS>");
177  ExpectToken(in_stream, binary, "<VECSIZE>");
178  ReadBasicType(in_stream, binary, &dimension);
179  ExpectToken(in_stream, binary, "<NUMCOMPONENTS>");
180  ReadBasicType(in_stream, binary, &num_components);
181  KALDI_ASSERT(dimension > 0 && num_components > 0);
182  ExpectToken(in_stream, binary, "<FLAGS>");
183  ReadBasicType(in_stream, binary, &flags);
184 
185  if (add) {
186  if ((NumGauss() != 0 || Dim() != 0 || Flags() != 0)) {
187  if (num_components != NumGauss() || dimension != Dim()
188  || flags != Flags())
189  KALDI_ERR << "MlEstimatediagGmm::Read, dimension or flags mismatch, "
190  << NumGauss() << ", " << Dim() << ", "
191  << GmmFlagsToString(Flags()) << " vs. " << num_components << ", "
192  << dimension << ", " << flags;
193  } else {
194  Resize(num_components, dimension, flags);
195  }
196  } else {
197  Resize(num_components, dimension, flags);
198  }
199 
200  // these are needed for demangling the variances.
201  Vector<double> tmp_occs;
202  Matrix<double> tmp_means;
203 
204  ReadToken(in_stream, binary, &token);
205  while (token != "</GMMACCS>") {
206  if (token == "<OCCUPANCY>") {
207  tmp_occs.Read(in_stream, binary, false);
208  if (!add) occupancy_.SetZero();
209  occupancy_.AddVec(1.0, tmp_occs);
210  } else if (token == "<MEANACCS>") {
211  tmp_means.Read(in_stream, binary, false);
212  if (!add) mean_accumulator_.SetZero();
213  mean_accumulator_.AddMat(1.0, tmp_means);
214  } else if (token == "<FULLVARACCS>") {
215  for (int32 i = 0; i < num_components; i++) {
216  SpMatrix<double> tmp_acc;
217  tmp_acc.Read(in_stream, binary, add);
218  if (tmp_occs(i) != 0) tmp_acc.AddVec2(1.0 / tmp_occs(i), tmp_means.Row(
219  i));
220  if (!add) covariance_accumulator_[i].SetZero();
221  covariance_accumulator_[i].AddSp(1.0, tmp_acc);
222  }
223  } else {
224  KALDI_ERR << "Unexpected token '" << token << "' in model file ";
225  }
226  ReadToken(in_stream, binary, &token);
227  }
228 }
int32 NumGauss() const
Returns the number of mixture components.
Definition: mle-full-gmm.h:97
std::vector< SpMatrix< double > > covariance_accumulator_
Definition: mle-full-gmm.h:138
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
void AddMat(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transA=kNoTrans)
*this += alpha * M [or M^T]
Vector< double > occupancy_
Definition: mle-full-gmm.h:136
int32 Dim() const
Returns the dimensionality of the feature vectors.
Definition: mle-full-gmm.h:99
kaldi::int32 int32
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
Definition: io-funcs.cc:154
uint16 GmmFlagsType
Bitwise OR of the above flags.
Definition: model-common.h:35
GmmFlagsType Flags() const
Accessors.
Definition: mle-full-gmm.h:126
void Resize(int32 num_components, int32 dim, GmmFlagsType flags)
Allocates memory for accumulators.
Definition: mle-full-gmm.cc:37
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
Definition: io-funcs.cc:191
#define KALDI_ERR
Definition: kaldi-error.h:147
void SetZero()
Sets matrix to zero.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::string GmmFlagsToString(GmmFlagsType flags)
Convert GMM flags to string.
Definition: model-common.cc:43
void SetZero()
Set vector to all zeros.
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...
Matrix< double > mean_accumulator_
Definition: mle-full-gmm.h:137

◆ Resize() [1/2]

void Resize ( int32  num_components,
int32  dim,
GmmFlagsType  flags 
)

Allocates memory for accumulators.

Definition at line 37 of file mle-full-gmm.cc.

References kaldi::AugmentGmmFlags(), AccumFullGmm::covariance_accumulator_, AccumFullGmm::dim_, AccumFullGmm::flags_, kaldi::kGmmMeans, kaldi::kGmmVariances, AccumFullGmm::mean_accumulator_, AccumFullGmm::num_comp_, AccumFullGmm::occupancy_, Vector< Real >::Resize(), Matrix< Real >::Resize(), and AccumFullGmm::ResizeVarAccumulator().

Referenced by main(), AccumFullGmm::Read(), test_flags_driven_update(), test_io(), TestComponentAcc(), and UnitTestEstimateFullGmm().

37  {
38  num_comp_ = num_comp;
39  dim_ = dim;
40  flags_ = AugmentGmmFlags(flags);
41  occupancy_.Resize(num_comp);
42  if (flags_ & kGmmMeans)
43  mean_accumulator_.Resize(num_comp, dim);
44  else
46 
47  if (flags_ & kGmmVariances)
48  ResizeVarAccumulator(num_comp, dim);
49  else
51 }
GmmFlagsType AugmentGmmFlags(GmmFlagsType f)
Returns "augmented" version of flags: e.g.
Definition: model-common.cc:52
std::vector< SpMatrix< double > > covariance_accumulator_
Definition: mle-full-gmm.h:138
void ResizeVarAccumulator(int32 num_comp, int32 dim)
Definition: mle-full-gmm.cc:53
Vector< double > occupancy_
Definition: mle-full-gmm.h:136
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
GmmFlagsType flags_
Definition: mle-full-gmm.h:134
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 > mean_accumulator_
Definition: mle-full-gmm.h:137

◆ Resize() [2/2]

void Resize ( const FullGmm gmm,
GmmFlagsType  flags 
)
inline

Calls Resize with arguments based on gmm_ptr_.

Definition at line 141 of file mle-full-gmm.h.

References FullGmm::Dim(), kaldi::MleFullGmmUpdate(), kaldi::MlObjective(), and FullGmm::NumGauss().

141  {
142  Resize(gmm.NumGauss(), gmm.Dim(), flags);
143 }
void Resize(int32 num_components, int32 dim, GmmFlagsType flags)
Allocates memory for accumulators.
Definition: mle-full-gmm.cc:37

◆ ResizeVarAccumulator()

void ResizeVarAccumulator ( int32  num_comp,
int32  dim 
)

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

References AccumFullGmm::covariance_accumulator_, rnnlm::i, and KALDI_ASSERT.

Referenced by AccumFullGmm::Resize().

53  {
54  KALDI_ASSERT(num_comp > 0 && dim > 0);
55  if (covariance_accumulator_.size() != static_cast<size_t>(num_comp))
56  covariance_accumulator_.resize(num_comp);
57  for (int32 i = 0; i < num_comp; i++) {
58  if (covariance_accumulator_[i].NumRows() != dim)
59  covariance_accumulator_[i].Resize(dim);
60  }
61 }
std::vector< SpMatrix< double > > covariance_accumulator_
Definition: mle-full-gmm.h:138
kaldi::int32 int32
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Scale()

void Scale ( BaseFloat  f,
GmmFlagsType  flags 
)

Definition at line 79 of file mle-full-gmm.cc.

References AccumFullGmm::covariance_accumulator_, rnnlm::d, AccumFullGmm::flags_, rnnlm::i, KALDI_ERR, kaldi::kGmmMeans, kaldi::kGmmVariances, kaldi::kGmmWeights, AccumFullGmm::mean_accumulator_, AccumFullGmm::occupancy_, MatrixBase< Real >::Scale(), and VectorBase< Real >::Scale().

Referenced by test_io().

79  {
80  if (flags & ~flags_)
81  KALDI_ERR << "Flags in argument do not match the active accumulators";
82 
83  double d = static_cast<double>(f);
84  if (flags & kGmmWeights)
85  occupancy_.Scale(d);
86 
87  if (flags & kGmmMeans)
89 
90  if (flags & kGmmVariances) {
91  for (int32 i = 0, end = covariance_accumulator_.size(); i < end; i++)
93  }
94 }
std::vector< SpMatrix< double > > covariance_accumulator_
Definition: mle-full-gmm.h:138
Vector< double > occupancy_
Definition: mle-full-gmm.h:136
kaldi::int32 int32
GmmFlagsType flags_
Definition: mle-full-gmm.h:134
void Scale(Real alpha)
Multiply each element with a scalar value.
#define KALDI_ERR
Definition: kaldi-error.h:147
void Scale(Real alpha)
Multiplies all elements by this constant.
Matrix< double > mean_accumulator_
Definition: mle-full-gmm.h:137
void Scale(BaseFloat f, GmmFlagsType flags)
Definition: mle-full-gmm.cc:79

◆ SetZero()

void SetZero ( GmmFlagsType  flags)

Definition at line 63 of file mle-full-gmm.cc.

References AccumFullGmm::covariance_accumulator_, AccumFullGmm::flags_, rnnlm::i, KALDI_ERR, kaldi::kGmmMeans, kaldi::kGmmVariances, kaldi::kGmmWeights, AccumFullGmm::mean_accumulator_, AccumFullGmm::occupancy_, VectorBase< Real >::SetZero(), and MatrixBase< Real >::SetZero().

Referenced by test_flags_driven_update(), TestComponentAcc(), and UnitTestEstimateFullGmm().

63  {
64  if (flags & ~flags_)
65  KALDI_ERR << "Flags in argument do not match the active accumulators";
66 
67  if (flags & kGmmWeights)
69 
70  if (flags & kGmmMeans)
72 
73  if (flags & kGmmVariances) {
74  for (int32 i = 0, end = covariance_accumulator_.size(); i < end; i++)
76  }
77 }
std::vector< SpMatrix< double > > covariance_accumulator_
Definition: mle-full-gmm.h:138
Vector< double > occupancy_
Definition: mle-full-gmm.h:136
kaldi::int32 int32
void SetZero(GmmFlagsType flags)
Definition: mle-full-gmm.cc:63
GmmFlagsType flags_
Definition: mle-full-gmm.h:134
#define KALDI_ERR
Definition: kaldi-error.h:147
void SetZero()
Sets matrix to zero.
void SetZero()
Set vector to all zeros.
Matrix< double > mean_accumulator_
Definition: mle-full-gmm.h:137

◆ Write()

void Write ( std::ostream &  out_stream,
bool  binary 
) const

Definition at line 230 of file mle-full-gmm.cc.

References SpMatrix< Real >::AddVec2(), AccumFullGmm::covariance_accumulator_, AccumFullGmm::dim_, AccumFullGmm::flags_, rnnlm::i, KALDI_ASSERT, kaldi::kGmmVariances, AccumFullGmm::mean_accumulator_, AccumFullGmm::num_comp_, AccumFullGmm::occupancy_, MatrixBase< Real >::Row(), PackedMatrix< Real >::Write(), VectorBase< Real >::Write(), MatrixBase< Real >::Write(), kaldi::WriteBasicType(), and kaldi::WriteToken().

Referenced by main(), and test_io().

230  {
231  WriteToken(out_stream, binary, "<GMMACCS>");
232  WriteToken(out_stream, binary, "<VECSIZE>");
233  WriteBasicType(out_stream, binary, dim_);
234  WriteToken(out_stream, binary, "<NUMCOMPONENTS>");
235  WriteBasicType(out_stream, binary, num_comp_);
236  WriteToken(out_stream, binary, "<FLAGS>");
237  WriteBasicType(out_stream, binary, flags_);
238 
239  Vector<BaseFloat> occupancy_bf(occupancy_);
240  WriteToken(out_stream, binary, "<OCCUPANCY>");
241  occupancy_bf.Write(out_stream, binary);
242  Matrix<BaseFloat> mean_accumulator_bf(mean_accumulator_);
243  WriteToken(out_stream, binary, "<MEANACCS>");
244  mean_accumulator_bf.Write(out_stream, binary);
245 
246  if (num_comp_ != 0) KALDI_ASSERT(((flags_ & kGmmVariances) != 0 )
247  == (covariance_accumulator_.size() != 0)); // sanity check.
248  if (covariance_accumulator_.size() != 0) {
249  WriteToken(out_stream, binary, "<FULLVARACCS>");
250  for (int32 i = 0; i < num_comp_; i++) {
251  SpMatrix<double> tmp_acc(covariance_accumulator_[i]);
252  if (occupancy_(i) != 0) tmp_acc.AddVec2(-1.0 / occupancy_(i),
254  SpMatrix<float> tmp_acc_bf(tmp_acc);
255  tmp_acc_bf.Write(out_stream, binary);
256  }
257  }
258  WriteToken(out_stream, binary, "</GMMACCS>");
259 }
std::vector< SpMatrix< double > > covariance_accumulator_
Definition: mle-full-gmm.h:138
Vector< double > occupancy_
Definition: mle-full-gmm.h:136
kaldi::int32 int32
GmmFlagsType flags_
Definition: mle-full-gmm.h:134
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
Definition: io-funcs.cc:134
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34
Matrix< double > mean_accumulator_
Definition: mle-full-gmm.h:137

Member Data Documentation

◆ covariance_accumulator_

◆ dim_

int32 dim_
private

Definition at line 132 of file mle-full-gmm.h.

Referenced by AccumFullGmm::Resize(), and AccumFullGmm::Write().

◆ flags_

◆ mean_accumulator_

◆ num_comp_

int32 num_comp_
private

Definition at line 133 of file mle-full-gmm.h.

Referenced by AccumFullGmm::Resize(), and AccumFullGmm::Write().

◆ occupancy_


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