AccumDiagGmm Class Reference

#include <mle-diag-gmm.h>

Collaboration diagram for AccumDiagGmm:

Public Member Functions

 AccumDiagGmm ()
 
 AccumDiagGmm (const DiagGmm &gmm, GmmFlagsType flags)
 
 AccumDiagGmm (const AccumDiagGmm &other)
 
void Read (std::istream &in_stream, bool binary, bool add)
 
void Write (std::ostream &out_stream, bool binary) const
 
void Resize (int32 num_gauss, int32 dim, GmmFlagsType flags)
 Allocates memory for accumulators. More...
 
void Resize (const DiagGmm &gmm, GmmFlagsType flags)
 Calls ResizeAccumulators with arguments based on gmm. More...
 
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 AccumulateFromDiag (const DiagGmm &gmm, const VectorBase< BaseFloat > &data, BaseFloat frame_posterior)
 Accumulate for all components given a diagonal-covariance GMM. More...
 
BaseFloat AccumulateFromDiagMultiThreaded (const DiagGmm &gmm, const MatrixBase< BaseFloat > &data, const VectorBase< BaseFloat > &frame_weights, int32 num_threads)
 This does the same job as AccumulateFromDiag, but using multiple threads. More...
 
void AddStatsForComponent (int32 comp_id, double occ, const VectorBase< double > &x_stats, const VectorBase< double > &x2_stats)
 Increment the stats for this component by the specified amount (not all parts may be taken, depending on flags). More...
 
void Add (double scale, const AccumDiagGmm &acc)
 Increment with stats from this other accumulator (times scale) More...
 
void SmoothStats (BaseFloat tau)
 Smooths the accumulated counts by adding 'tau' extra frames. More...
 
void SmoothWithAccum (BaseFloat tau, const AccumDiagGmm &src_acc)
 Smooths the accumulated counts using some other accumulator. More...
 
void SmoothWithModel (BaseFloat tau, const DiagGmm &src_gmm)
 Smooths the accumulated counts using the parameters of a given model. More...
 
GmmFlagsType Flags () const
 
const VectorBase< double > & occupancy () const
 
const MatrixBase< double > & mean_accumulator () const
 
const MatrixBase< double > & variance_accumulator () const
 
void AssertEqual (const AccumDiagGmm &other)
 

Private Attributes

int32 dim_
 
int32 num_comp_
 
GmmFlagsType flags_
 Flags corresponding to the accumulators that are stored. More...
 
Vector< double > occupancy_
 
Matrix< double > mean_accumulator_
 
Matrix< double > variance_accumulator_
 

Detailed Description

Definition at line 106 of file mle-diag-gmm.h.

Constructor & Destructor Documentation

◆ AccumDiagGmm() [1/3]

AccumDiagGmm ( )
inline

Definition at line 108 of file mle-diag-gmm.h.

108 : dim_(0), num_comp_(0), flags_(0) { }
GmmFlagsType flags_
Flags corresponding to the accumulators that are stored.
Definition: mle-diag-gmm.h:193

◆ AccumDiagGmm() [2/3]

AccumDiagGmm ( const DiagGmm gmm,
GmmFlagsType  flags 
)
inlineexplicit

Definition at line 109 of file mle-diag-gmm.h.

109  {
110  Resize(gmm, flags);
111  }
void Resize(int32 num_gauss, int32 dim, GmmFlagsType flags)
Allocates memory for accumulators.

◆ AccumDiagGmm() [3/3]

AccumDiagGmm ( const AccumDiagGmm other)
explicit

Definition at line 255 of file mle-diag-gmm.cc.

256  : dim_(other.dim_), num_comp_(other.num_comp_),
257  flags_(other.flags_), occupancy_(other.occupancy_),
258  mean_accumulator_(other.mean_accumulator_),
259  variance_accumulator_(other.variance_accumulator_) {}
Matrix< double > mean_accumulator_
Definition: mle-diag-gmm.h:196
GmmFlagsType flags_
Flags corresponding to the accumulators that are stored.
Definition: mle-diag-gmm.h:193
Vector< double > occupancy_
Definition: mle-diag-gmm.h:195
Matrix< double > variance_accumulator_
Definition: mle-diag-gmm.h:197

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 140 of file mle-diag-gmm.cc.

References VectorBase< Real >::ApplyPow(), VectorBase< Real >::Dim(), AccumDiagGmm::Dim(), AccumDiagGmm::flags_, KALDI_ASSERT, kaldi::kGmmMeans, kaldi::kGmmVariances, AccumDiagGmm::mean_accumulator_, AccumDiagGmm::NumGauss(), AccumDiagGmm::occupancy_, MatrixBase< Real >::Row(), and AccumDiagGmm::variance_accumulator_.

Referenced by main(), and TestComponentAcc().

141  {
142  if (flags_ & kGmmMeans)
143  KALDI_ASSERT(data.Dim() == Dim());
144  double wt = static_cast<double>(weight);
145  KALDI_ASSERT(comp_index < NumGauss());
146  // accumulate
147  occupancy_(comp_index) += wt;
148  if (flags_ & kGmmMeans) {
149  Vector<double> data_d(data); // Copy with type-conversion
150  mean_accumulator_.Row(comp_index).AddVec(wt, data_d);
151  if (flags_ & kGmmVariances) {
152  data_d.ApplyPow(2.0);
153  variance_accumulator_.Row(comp_index).AddVec(wt, data_d);
154  }
155  }
156 }
Matrix< double > mean_accumulator_
Definition: mle-diag-gmm.h:196
GmmFlagsType flags_
Flags corresponding to the accumulators that are stored.
Definition: mle-diag-gmm.h:193
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
Vector< double > occupancy_
Definition: mle-diag-gmm.h:195
int32 Dim() const
Returns the dimensionality of the feature vectors.
Definition: mle-diag-gmm.h:126
Matrix< double > variance_accumulator_
Definition: mle-diag-gmm.h:197
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32 NumGauss() const
Returns the number of mixture components.
Definition: mle-diag-gmm.h:124

◆ 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 191 of file mle-diag-gmm.cc.

References AccumDiagGmm::AccumulateFromPosteriors(), DiagGmm::ComponentPosteriors(), VectorBase< Real >::Dim(), DiagGmm::Dim(), AccumDiagGmm::Dim(), KALDI_ASSERT, DiagGmm::NumGauss(), and AccumDiagGmm::NumGauss().

Referenced by main(), test_flags_driven_update(), TestComponentAcc(), kaldi::UnitTestDiagGmmGenerate(), UnitTestEstimateDiagGmm(), and kaldi::UnitTestEstimateMmieDiagGmm().

193  {
194  KALDI_ASSERT(gmm.NumGauss() == NumGauss());
195  KALDI_ASSERT(gmm.Dim() == Dim());
196  KALDI_ASSERT(static_cast<int32>(data.Dim()) == Dim());
197 
198  Vector<BaseFloat> posteriors(NumGauss());
199  BaseFloat log_like = gmm.ComponentPosteriors(data, &posteriors);
200  posteriors.Scale(frame_posterior);
201 
202  AccumulateFromPosteriors(data, posteriors);
203  return log_like;
204 }
float BaseFloat
Definition: kaldi-types.h:29
int32 Dim() const
Returns the dimensionality of the feature vectors.
Definition: mle-diag-gmm.h:126
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void AccumulateFromPosteriors(const VectorBase< BaseFloat > &data, const VectorBase< BaseFloat > &gauss_posteriors)
Accumulate for all components, given the posteriors.
int32 NumGauss() const
Returns the number of mixture components.
Definition: mle-diag-gmm.h:124

◆ AccumulateFromDiagMultiThreaded()

BaseFloat AccumulateFromDiagMultiThreaded ( const DiagGmm gmm,
const MatrixBase< BaseFloat > &  data,
const VectorBase< BaseFloat > &  frame_weights,
int32  num_threads 
)

This does the same job as AccumulateFromDiag, but using multiple threads.

Returns sum of (log-likelihood times frame weight) over all frames.

Definition at line 538 of file mle-diag-gmm.cc.

Referenced by kaldi::TrainOneIter(), and UnitTestEstimateDiagGmm().

542  {
543 
544  double tot_like = 0.0;
545  AccumulateMultiThreadedClass accumulator(gmm, data, frame_weights,
546  this, &tot_like);
547  {
548  // Note: everything happens in the constructor and destructor of
549  // the object created below.
550  MultiThreader<AccumulateMultiThreadedClass> threader(num_threads,
551  accumulator);
552  // we need to make sure it's destroyed before we access the
553  // value of tot_like.
554  }
555  return tot_like;
556 }

◆ AccumulateFromPosteriors()

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

Accumulate for all components, given the posteriors.

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

References VectorBase< Real >::AddVec(), MatrixBase< Real >::AddVecVec(), VectorBase< Real >::ApplyPow(), VectorBase< Real >::Dim(), AccumDiagGmm::Dim(), AccumDiagGmm::flags_, KALDI_ASSERT, kaldi::kGmmMeans, kaldi::kGmmVariances, AccumDiagGmm::mean_accumulator_, AccumDiagGmm::NumGauss(), AccumDiagGmm::occupancy_, and AccumDiagGmm::variance_accumulator_.

Referenced by AccumAmDiagGmm::AccumulateForGmmTwofeats(), AccumDiagGmm::AccumulateFromDiag(), and main().

173  {
174  if (flags_ & kGmmMeans)
175  KALDI_ASSERT(static_cast<int32>(data.Dim()) == Dim());
176  KALDI_ASSERT(static_cast<int32>(posteriors.Dim()) == NumGauss());
177  Vector<double> post_d(posteriors); // Copy with type-conversion
178 
179  // accumulate
180  occupancy_.AddVec(1.0, post_d);
181  if (flags_ & kGmmMeans) {
182  Vector<double> data_d(data); // Copy with type-conversion
183  mean_accumulator_.AddVecVec(1.0, post_d, data_d);
184  if (flags_ & kGmmVariances) {
185  data_d.ApplyPow(2.0);
186  variance_accumulator_.AddVecVec(1.0, post_d, data_d);
187  }
188  }
189 }
Matrix< double > mean_accumulator_
Definition: mle-diag-gmm.h:196
GmmFlagsType flags_
Flags corresponding to the accumulators that are stored.
Definition: mle-diag-gmm.h:193
Vector< double > occupancy_
Definition: mle-diag-gmm.h:195
int32 Dim() const
Returns the dimensionality of the feature vectors.
Definition: mle-diag-gmm.h:126
Matrix< double > variance_accumulator_
Definition: mle-diag-gmm.h:197
#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) ...
int32 NumGauss() const
Returns the number of mixture components.
Definition: mle-diag-gmm.h:124

◆ Add()

void Add ( double  scale,
const AccumDiagGmm acc 
)

Increment with stats from this other accumulator (times scale)

Definition at line 399 of file mle-diag-gmm.cc.

References MatrixBase< Real >::AddMat(), VectorBase< Real >::AddVec(), AccumDiagGmm::flags_, kaldi::kGmmMeans, kaldi::kGmmVariances, AccumDiagGmm::mean_accumulator_, AccumDiagGmm::occupancy_, and AccumDiagGmm::variance_accumulator_.

399  {
400  // The functions called here will crash if the dimensions etc.
401  // or the flags don't match.
402  occupancy_.AddVec(scale, acc.occupancy_);
403  if (flags_ & kGmmMeans)
404  mean_accumulator_.AddMat(scale, acc.mean_accumulator_);
405  if (flags_ & kGmmVariances)
406  variance_accumulator_.AddMat(scale, acc.variance_accumulator_);
407 }
void AddMat(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transA=kNoTrans)
*this += alpha * M [or M^T]
Matrix< double > mean_accumulator_
Definition: mle-diag-gmm.h:196
GmmFlagsType flags_
Flags corresponding to the accumulators that are stored.
Definition: mle-diag-gmm.h:193
Vector< double > occupancy_
Definition: mle-diag-gmm.h:195
Matrix< double > variance_accumulator_
Definition: mle-diag-gmm.h:197
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...

◆ AddStatsForComponent()

void AddStatsForComponent ( int32  comp_id,
double  occ,
const VectorBase< double > &  x_stats,
const VectorBase< double > &  x2_stats 
)

Increment the stats for this component by the specified amount (not all parts may be taken, depending on flags).

Note: x_stats and x2_stats are assumed to already be multiplied by "occ"

Definition at line 158 of file mle-diag-gmm.cc.

References AccumDiagGmm::flags_, KALDI_ASSERT, kaldi::kGmmMeans, kaldi::kGmmVariances, AccumDiagGmm::mean_accumulator_, AccumDiagGmm::NumGauss(), AccumDiagGmm::occupancy_, MatrixBase< Real >::Row(), and AccumDiagGmm::variance_accumulator_.

Referenced by kaldi::DiagGmmToStats(), kaldi::GetStatsDerivative(), and kaldi::IsmoothStatsDiagGmm().

161  {
162  KALDI_ASSERT(g < NumGauss());
163  occupancy_(g) += occ;
164  if (flags_ & kGmmMeans)
165  mean_accumulator_.Row(g).AddVec(1.0, x_stats);
166  if (flags_ & kGmmVariances)
167  variance_accumulator_.Row(g).AddVec(1.0, x2_stats);
168 }
Matrix< double > mean_accumulator_
Definition: mle-diag-gmm.h:196
GmmFlagsType flags_
Flags corresponding to the accumulators that are stored.
Definition: mle-diag-gmm.h:193
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
Vector< double > occupancy_
Definition: mle-diag-gmm.h:195
Matrix< double > variance_accumulator_
Definition: mle-diag-gmm.h:197
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32 NumGauss() const
Returns the number of mixture components.
Definition: mle-diag-gmm.h:124

◆ AssertEqual()

void AssertEqual ( const AccumDiagGmm other)

Definition at line 558 of file mle-diag-gmm.cc.

References VectorBase< Real >::ApproxEqual(), MatrixBase< Real >::ApproxEqual(), AccumDiagGmm::dim_, AccumDiagGmm::flags_, KALDI_ASSERT, AccumDiagGmm::mean_accumulator_, AccumDiagGmm::num_comp_, AccumDiagGmm::occupancy_, and AccumDiagGmm::variance_accumulator_.

Referenced by UnitTestEstimateDiagGmm().

558  {
559  KALDI_ASSERT(dim_ == other.dim_ && num_comp_ == other.num_comp_ &&
560  flags_ == other.flags_);
561  KALDI_ASSERT(occupancy_.ApproxEqual(other.occupancy_));
562  KALDI_ASSERT(mean_accumulator_.ApproxEqual(other.mean_accumulator_));
563  KALDI_ASSERT(variance_accumulator_.ApproxEqual(other.variance_accumulator_));
564 }
bool ApproxEqual(const VectorBase< Real > &other, float tol=0.01) const
Returns true if ((*this)-other).Norm(2.0) <= tol * (*this).Norm(2.0).
bool ApproxEqual(const MatrixBase< Real > &other, float tol=0.01) const
Returns true if ((*this)-other).FrobeniusNorm() <= tol * (*this).FrobeniusNorm(). ...
Matrix< double > mean_accumulator_
Definition: mle-diag-gmm.h:196
GmmFlagsType flags_
Flags corresponding to the accumulators that are stored.
Definition: mle-diag-gmm.h:193
Vector< double > occupancy_
Definition: mle-diag-gmm.h:195
Matrix< double > variance_accumulator_
Definition: mle-diag-gmm.h:197
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Dim()

◆ Flags()

◆ mean_accumulator()

◆ NumGauss()

◆ occupancy()

◆ Read()

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

Definition at line 33 of file mle-diag-gmm.cc.

References AccumDiagGmm::Dim(), kaldi::ExpectToken(), AccumDiagGmm::Flags(), kaldi::GmmFlagsToString(), KALDI_ERR, AccumDiagGmm::mean_accumulator_, AccumDiagGmm::NumGauss(), AccumDiagGmm::occupancy_, Vector< Real >::Read(), Matrix< Real >::Read(), kaldi::ReadBasicType(), kaldi::ReadToken(), AccumDiagGmm::Resize(), and AccumDiagGmm::variance_accumulator_.

Referenced by main(), test_io(), and UnitTestEstimateDiagGmm().

33  {
34  int32 dimension, num_components;
35  GmmFlagsType flags;
36  std::string token;
37 
38  ExpectToken(in_stream, binary, "<GMMACCS>");
39  ExpectToken(in_stream, binary, "<VECSIZE>");
40  ReadBasicType(in_stream, binary, &dimension);
41  ExpectToken(in_stream, binary, "<NUMCOMPONENTS>");
42  ReadBasicType(in_stream, binary, &num_components);
43  ExpectToken(in_stream, binary, "<FLAGS>");
44  ReadBasicType(in_stream, binary, &flags);
45 
46  if (add) {
47  if ((NumGauss() != 0 || Dim() != 0 || Flags() != 0)) {
48  if (num_components != NumGauss() || dimension != Dim()
49  || flags != Flags())
50  KALDI_ERR << "MlEstimatediagGmm::Read, dimension or flags mismatch, "
51  << NumGauss() << ", " << Dim() << ", "
52  << GmmFlagsToString(Flags()) << " vs. " << num_components << ", "
53  << dimension << ", " << flags << " (mixing accs from different "
54  << "models?";
55  } else {
56  Resize(num_components, dimension, flags);
57  }
58  } else {
59  Resize(num_components, dimension, flags);
60  }
61 
62  ReadToken(in_stream, binary, &token);
63  while (token != "</GMMACCS>") {
64  if (token == "<OCCUPANCY>") {
65  occupancy_.Read(in_stream, binary, add);
66  } else if (token == "<MEANACCS>") {
67  mean_accumulator_.Read(in_stream, binary, add);
68  } else if (token == "<DIAGVARACCS>") {
69  variance_accumulator_.Read(in_stream, binary, add);
70  } else {
71  KALDI_ERR << "Unexpected token '" << token << "' in model file ";
72  }
73  ReadToken(in_stream, binary, &token);
74  }
75 }
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
Matrix< double > mean_accumulator_
Definition: mle-diag-gmm.h:196
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
void Read(std::istream &in, bool binary, bool add=false)
read from stream.
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
GmmFlagsType Flags() const
Definition: mle-diag-gmm.h:182
#define KALDI_ERR
Definition: kaldi-error.h:147
Vector< double > occupancy_
Definition: mle-diag-gmm.h:195
int32 Dim() const
Returns the dimensionality of the feature vectors.
Definition: mle-diag-gmm.h:126
Matrix< double > variance_accumulator_
Definition: mle-diag-gmm.h:197
std::string GmmFlagsToString(GmmFlagsType flags)
Convert GMM flags to string.
Definition: model-common.cc:43
void Resize(int32 num_gauss, int32 dim, GmmFlagsType flags)
Allocates memory for accumulators.
void Read(std::istream &in, bool binary, bool add=false)
Read function using C++ streams.
int32 NumGauss() const
Returns the number of mixture components.
Definition: mle-diag-gmm.h:124

◆ Resize() [1/2]

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

Allocates memory for accumulators.

Definition at line 106 of file mle-diag-gmm.cc.

References kaldi::AugmentGmmFlags(), AccumDiagGmm::dim_, AccumDiagGmm::flags_, KALDI_ASSERT, kaldi::kGmmMeans, kaldi::kGmmVariances, AccumDiagGmm::mean_accumulator_, AccumDiagGmm::num_comp_, AccumDiagGmm::occupancy_, Vector< Real >::Resize(), Matrix< Real >::Resize(), and AccumDiagGmm::variance_accumulator_.

Referenced by kaldi::DiagGmmToStats(), kaldi::GetStatsDerivative(), main(), AccumDiagGmm::Read(), test_flags_driven_update(), test_io(), TestComponentAcc(), UnitTestEstimateDiagGmm(), and kaldi::UnitTestEstimateMmieDiagGmm().

106  {
107  KALDI_ASSERT(num_comp > 0 && dim > 0);
108  num_comp_ = num_comp;
109  dim_ = dim;
110  flags_ = AugmentGmmFlags(flags);
111  occupancy_.Resize(num_comp);
112  if (flags_ & kGmmMeans)
113  mean_accumulator_.Resize(num_comp, dim);
114  else
116  if (flags_ & kGmmVariances)
117  variance_accumulator_.Resize(num_comp, dim);
118  else
120 }
GmmFlagsType AugmentGmmFlags(GmmFlagsType f)
Returns "augmented" version of flags: e.g.
Definition: model-common.cc:52
Matrix< double > mean_accumulator_
Definition: mle-diag-gmm.h:196
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
GmmFlagsType flags_
Flags corresponding to the accumulators that are stored.
Definition: mle-diag-gmm.h:193
Vector< double > occupancy_
Definition: mle-diag-gmm.h:195
Matrix< double > variance_accumulator_
Definition: mle-diag-gmm.h:197
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
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).

◆ Resize() [2/2]

void Resize ( const DiagGmm gmm,
GmmFlagsType  flags 
)
inline

Calls ResizeAccumulators with arguments based on gmm.

Definition at line 206 of file mle-diag-gmm.h.

References DiagGmm::Dim(), kaldi::MapDiagGmmUpdate(), kaldi::MleDiagGmmUpdate(), kaldi::MlObjective(), and DiagGmm::NumGauss().

206  {
207  Resize(gmm.NumGauss(), gmm.Dim(), flags);
208 }
void Resize(int32 num_gauss, int32 dim, GmmFlagsType flags)
Allocates memory for accumulators.

◆ Scale()

void Scale ( BaseFloat  f,
GmmFlagsType  flags 
)

Definition at line 131 of file mle-diag-gmm.cc.

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

Referenced by AccumAmDiagGmm::Scale(), and test_io().

131  {
132  if (flags & ~flags_)
133  KALDI_ERR << "Flags in argument do not match the active accumulators";
134  double d = static_cast<double>(f);
135  if (flags & kGmmWeights) occupancy_.Scale(d);
136  if (flags & kGmmMeans) mean_accumulator_.Scale(d);
137  if (flags & kGmmVariances) variance_accumulator_.Scale(d);
138 }
Matrix< double > mean_accumulator_
Definition: mle-diag-gmm.h:196
GmmFlagsType flags_
Flags corresponding to the accumulators that are stored.
Definition: mle-diag-gmm.h:193
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.
Vector< double > occupancy_
Definition: mle-diag-gmm.h:195
Matrix< double > variance_accumulator_
Definition: mle-diag-gmm.h:197

◆ SetZero()

void SetZero ( GmmFlagsType  flags)

Definition at line 122 of file mle-diag-gmm.cc.

References AccumDiagGmm::flags_, KALDI_ERR, kaldi::kGmmMeans, kaldi::kGmmVariances, kaldi::kGmmWeights, AccumDiagGmm::mean_accumulator_, AccumDiagGmm::occupancy_, VectorBase< Real >::SetZero(), MatrixBase< Real >::SetZero(), and AccumDiagGmm::variance_accumulator_.

Referenced by test_flags_driven_update(), TestComponentAcc(), UnitTestEstimateDiagGmm(), and kaldi::UnitTestEstimateMmieDiagGmm().

122  {
123  if (flags & ~flags_)
124  KALDI_ERR << "Flags in argument do not match the active accumulators";
125  if (flags & kGmmWeights) occupancy_.SetZero();
126  if (flags & kGmmMeans) mean_accumulator_.SetZero();
128 }
Matrix< double > mean_accumulator_
Definition: mle-diag-gmm.h:196
GmmFlagsType flags_
Flags corresponding to the accumulators that are stored.
Definition: mle-diag-gmm.h:193
#define KALDI_ERR
Definition: kaldi-error.h:147
void SetZero()
Sets matrix to zero.
Vector< double > occupancy_
Definition: mle-diag-gmm.h:195
Matrix< double > variance_accumulator_
Definition: mle-diag-gmm.h:197
void SetZero()
Set vector to all zeros.

◆ SmoothStats()

void SmoothStats ( BaseFloat  tau)

Smooths the accumulated counts by adding 'tau' extra frames.

An example use for this is I-smoothing for MMIE. Calls SmoothWithAccum.

Definition at line 208 of file mle-diag-gmm.cc.

References VectorBase< Real >::Add(), VectorBase< Real >::InvertElements(), AccumDiagGmm::mean_accumulator_, MatrixBase< Real >::MulRowsVec(), AccumDiagGmm::occupancy_, VectorBase< Real >::Scale(), and AccumDiagGmm::variance_accumulator_.

208  {
209  Vector<double> smoothing_vec(occupancy_);
210  smoothing_vec.InvertElements();
211  smoothing_vec.Scale(static_cast<double>(tau));
212  smoothing_vec.Add(1.0);
213  // now smoothing_vec = (tau + occ) / occ
214 
215  mean_accumulator_.MulRowsVec(smoothing_vec);
216  variance_accumulator_.MulRowsVec(smoothing_vec);
217  occupancy_.Add(static_cast<double>(tau));
218 }
Matrix< double > mean_accumulator_
Definition: mle-diag-gmm.h:196
Vector< double > occupancy_
Definition: mle-diag-gmm.h:195
void MulRowsVec(const VectorBase< Real > &scale)
Equivalent to (*this) = diag(scale) * (*this).
Matrix< double > variance_accumulator_
Definition: mle-diag-gmm.h:197
void Add(Real c)
Add a constant to each element of a vector.

◆ SmoothWithAccum()

void SmoothWithAccum ( BaseFloat  tau,
const AccumDiagGmm src_acc 
)

Smooths the accumulated counts using some other accumulator.

Performs a weighted sum of the current accumulator with the given one. An example use for this is I-smoothing for MMI and MPE. Both accumulators must have the same dimension and number of components.

Definition at line 225 of file mle-diag-gmm.cc.

References AccumDiagGmm::Dim(), AccumDiagGmm::dim_, rnnlm::i, KALDI_ASSERT, KALDI_WARN, AccumDiagGmm::mean_accumulator_, AccumDiagGmm::num_comp_, AccumDiagGmm::NumGauss(), AccumDiagGmm::occupancy_, MatrixBase< Real >::Row(), and AccumDiagGmm::variance_accumulator_.

225  {
226  KALDI_ASSERT(src_acc.NumGauss() == num_comp_ && src_acc.Dim() == dim_);
227  for (int32 i = 0; i < num_comp_; i++) {
228  if (src_acc.occupancy_(i) != 0.0) { // can only smooth if src was nonzero...
229  occupancy_(i) += tau;
230  mean_accumulator_.Row(i).AddVec(tau / src_acc.occupancy_(i),
231  src_acc.mean_accumulator_.Row(i));
232  variance_accumulator_.Row(i).AddVec(tau / src_acc.occupancy_(i),
233  src_acc.variance_accumulator_.Row(i));
234  } else
235  KALDI_WARN << "Could not smooth since source acc had zero occupancy.";
236  }
237 }
Matrix< double > mean_accumulator_
Definition: mle-diag-gmm.h:196
kaldi::int32 int32
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
#define KALDI_WARN
Definition: kaldi-error.h:150
Vector< double > occupancy_
Definition: mle-diag-gmm.h:195
Matrix< double > variance_accumulator_
Definition: mle-diag-gmm.h:197
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ SmoothWithModel()

void SmoothWithModel ( BaseFloat  tau,
const DiagGmm src_gmm 
)

Smooths the accumulated counts using the parameters of a given model.

An example use of this is MAP-adaptation. The model must have the same dimension and number of components as the current accumulator.

Definition at line 240 of file mle-diag-gmm.cc.

References VectorBase< Real >::Add(), MatrixBase< Real >::AddMat(), DiagGmm::Dim(), AccumDiagGmm::dim_, DiagGmm::GetMeans(), DiagGmm::GetVars(), KALDI_ASSERT, kaldi::kNoTrans, AccumDiagGmm::mean_accumulator_, AccumDiagGmm::num_comp_, DiagGmm::NumGauss(), AccumDiagGmm::occupancy_, and AccumDiagGmm::variance_accumulator_.

240  {
241  KALDI_ASSERT(gmm.NumGauss() == num_comp_ && gmm.Dim() == dim_);
242  Matrix<double> means(num_comp_, dim_);
243  Matrix<double> vars(num_comp_, dim_);
244  gmm.GetMeans(&means);
245  gmm.GetVars(&vars);
246 
247  mean_accumulator_.AddMat(tau, means);
248  means.ApplyPow(2.0);
249  vars.AddMat(1.0, means, kNoTrans);
250  variance_accumulator_.AddMat(tau, vars);
251 
252  occupancy_.Add(tau);
253 }
void AddMat(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transA=kNoTrans)
*this += alpha * M [or M^T]
Matrix< double > mean_accumulator_
Definition: mle-diag-gmm.h:196
Vector< double > occupancy_
Definition: mle-diag-gmm.h:195
Matrix< double > variance_accumulator_
Definition: mle-diag-gmm.h:197
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void Add(Real c)
Add a constant to each element of a vector.

◆ variance_accumulator()

◆ Write()

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

Definition at line 77 of file mle-diag-gmm.cc.

References MatrixBase< Real >::CopyFromMat(), VectorBase< Real >::Dim(), AccumDiagGmm::dim_, AccumDiagGmm::flags_, AccumDiagGmm::mean_accumulator_, AccumDiagGmm::num_comp_, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), AccumDiagGmm::occupancy_, AccumDiagGmm::variance_accumulator_, kaldi::WriteBasicType(), and kaldi::WriteToken().

Referenced by main(), test_io(), and UnitTestEstimateDiagGmm().

77  {
78  WriteToken(out_stream, binary, "<GMMACCS>");
79  WriteToken(out_stream, binary, "<VECSIZE>");
80  WriteBasicType(out_stream, binary, dim_);
81  WriteToken(out_stream, binary, "<NUMCOMPONENTS>");
82  WriteBasicType(out_stream, binary, num_comp_);
83  WriteToken(out_stream, binary, "<FLAGS>");
84  WriteBasicType(out_stream, binary, flags_);
85 
86  // convert into BaseFloat before writing things
87  Vector<BaseFloat> occupancy_bf(occupancy_.Dim());
88  Matrix<BaseFloat> mean_accumulator_bf(mean_accumulator_.NumRows(),
90  Matrix<BaseFloat> variance_accumulator_bf(variance_accumulator_.NumRows(),
92  occupancy_bf.CopyFromVec(occupancy_);
93  mean_accumulator_bf.CopyFromMat(mean_accumulator_);
94  variance_accumulator_bf.CopyFromMat(variance_accumulator_);
95 
96  WriteToken(out_stream, binary, "<OCCUPANCY>");
97  occupancy_bf.Write(out_stream, binary);
98  WriteToken(out_stream, binary, "<MEANACCS>");
99  mean_accumulator_bf.Write(out_stream, binary);
100  WriteToken(out_stream, binary, "<DIAGVARACCS>");
101  variance_accumulator_bf.Write(out_stream, binary);
102  WriteToken(out_stream, binary, "</GMMACCS>");
103 }
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
Matrix< double > mean_accumulator_
Definition: mle-diag-gmm.h:196
GmmFlagsType flags_
Flags corresponding to the accumulators that are stored.
Definition: mle-diag-gmm.h:193
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
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
Vector< double > occupancy_
Definition: mle-diag-gmm.h:195
Matrix< double > variance_accumulator_
Definition: mle-diag-gmm.h:197
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
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

Member Data Documentation

◆ dim_

◆ flags_

◆ mean_accumulator_

◆ num_comp_

◆ occupancy_

◆ variance_accumulator_


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