34 int32 dimension, num_components;
48 if (num_components !=
NumGauss() || dimension !=
Dim()
50 KALDI_ERR <<
"MlEstimatediagGmm::Read, dimension or flags mismatch, " 53 << dimension <<
", " << flags <<
" (mixing accs from different " 56 Resize(num_components, dimension, flags);
59 Resize(num_components, dimension, flags);
63 while (token !=
"</GMMACCS>") {
64 if (token ==
"<OCCUPANCY>") {
66 }
else if (token ==
"<MEANACCS>") {
68 }
else if (token ==
"<DIAGVARACCS>") {
71 KALDI_ERR <<
"Unexpected token '" << token <<
"' in model file ";
81 WriteToken(out_stream, binary,
"<NUMCOMPONENTS>");
97 occupancy_bf.Write(out_stream, binary);
99 mean_accumulator_bf.Write(out_stream, binary);
100 WriteToken(out_stream, binary,
"<DIAGVARACCS>");
101 variance_accumulator_bf.Write(out_stream, binary);
124 KALDI_ERR <<
"Flags in argument do not match the active accumulators";
133 KALDI_ERR <<
"Flags in argument do not match the active accumulators";
134 double d =
static_cast<double>(f);
144 double wt =
static_cast<double>(weight);
200 posteriors.Scale(frame_posterior);
211 smoothing_vec.
Scale(static_cast<double>(tau));
212 smoothing_vec.
Add(1.0);
235 KALDI_WARN <<
"Could not smooth since source acc had zero occupancy.";
281 int32 *floored_elements_out,
282 int32 *floored_gaussians_out,
283 int32 *removed_gaussians_out) {
286 if (flags & ~diag_gmm_acc.
Flags())
287 KALDI_ERR <<
"Flags in argument do not match the active accumulators";
290 diag_gmm_acc.
Dim() == gmm->
Dim());
293 double occ_sum = diag_gmm_acc.
occupancy().Sum();
295 int32 elements_floored = 0, gauss_floored = 0;
305 std::vector<int32> to_remove;
306 for (
int32 i = 0;
i < num_gauss;
i++) {
310 prob = occ / occ_sum;
312 prob = 1.0 / num_gauss;
325 mean.
Scale(1.0 / occ);
333 var.
Scale(1.0 / occ);
340 var.AddVec2(1.0, old_mean);
349 elements_floored += floored;
357 static_cast<int32>(to_remove.size()) < num_gauss-1) {
359 KALDI_WARN <<
"Too little data - removing Gaussian (weight " 360 << std::fixed << prob
361 <<
", occupation count " << std::fixed << diag_gmm_acc.
occupancy()(
i)
362 <<
", vector size " << gmm->
Dim() <<
")";
363 to_remove.push_back(
i);
365 KALDI_WARN <<
"Gaussian has too little data but not removing it because" 367 " it is the last Gaussian: i = " 368 :
" remove-low-count-gaussians == false: g = ") <<
i 369 <<
", occ = " << diag_gmm_acc.
occupancy()(
i) <<
", weight = " << prob;
383 *obj_change_out = (obj_new - obj_old);
384 if (count_out) *count_out = occ_sum;
385 if (floored_elements_out) *floored_elements_out = elements_floored;
386 if (floored_gaussians_out) *floored_gaussians_out = gauss_floored;
388 if (to_remove.size() > 0) {
392 if (removed_gaussians_out != NULL) *removed_gaussians_out = to_remove.size();
394 if (gauss_floored > 0)
395 KALDI_VLOG(2) << gauss_floored <<
" variances floored in " << gauss_floored
418 if (flags & ~diag_gmm_acc.
Flags())
419 KALDI_ERR <<
"Flags in argument do not match the active accumulators";
422 diag_gmm_acc.
Dim() == gmm->
Dim());
425 double occ_sum = diag_gmm_acc.
occupancy().Sum();
435 for (
int32 i = 0;
i < num_gauss;
i++) {
459 var.
Scale(1.0 / occ);
463 var.AddVecVec(-2.0 / occ, mean_acc, mean, 1.0);
480 *obj_change_out = (obj_new - obj_old);
482 if (count_out) *count_out = occ_sum;
493 diag_gmm_(diag_gmm),
data_(data),
494 frame_weights_(frame_weights), dest_accum_(accum),
495 tot_like_ptr_(tot_like), tot_like_(0.0) { }
498 diag_gmm_(other.diag_gmm_),
data_(other.
data_),
499 frame_weights_(other.frame_weights_), dest_accum_(other.dest_accum_),
500 accum_(diag_gmm_, dest_accum_->
Flags()), tot_like_ptr_(other.tot_like_ptr_),
504 void operator () () {
505 int32 num_frames =
data_.NumRows(), num_threads = num_threads_,
506 block_size = (num_frames + num_threads - 1) / num_threads,
507 block_start = block_size * thread_id_,
508 block_end = std::min(num_frames, block_start + block_size);
510 double tot_weight = 0.0;
511 for (
int32 t = block_start; t < block_end; t++) {
512 tot_like_ += frame_weights_(t) *
513 accum_.AccumulateFromDiag(diag_gmm_,
data_.Row(t), frame_weights_(t));
514 tot_weight += frame_weights_(t);
516 KALDI_VLOG(3) <<
"Thread " << thread_id_ <<
" saw average likeliood/frame " 517 << (tot_like_ / tot_weight) <<
" over " << tot_weight
518 <<
" (weighted) frames.";
521 if (accum_.Dim() != 0) {
523 dest_accum_->Add(1.0, accum_);
524 *tot_like_ptr_ += tot_like_;
544 double tot_like = 0.0;
bool ApproxEqual(const VectorBase< Real > &other, float tol=0.01) const
Returns true if ((*this)-other).Norm(2.0) <= tol * (*this).Norm(2.0).
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
void MapDiagGmmUpdate(const MapDiagGmmOptions &config, const AccumDiagGmm &diag_gmm_acc, GmmFlagsType flags, DiagGmm *gmm, BaseFloat *obj_change_out, BaseFloat *count_out)
Maximum A Posteriori estimation of the model.
bool remove_low_count_gaussians
GmmFlagsType AugmentGmmFlags(GmmFlagsType f)
Returns "augmented" version of flags: e.g.
BaseFloat weight_tau
Tau value for the weights– this tau value is applied per state, not per Gaussian.
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.
Definition for Gaussian Mixture Model with diagonal covariances in normal mode: where the parameters ...
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Base class which provides matrix operations not involving resizing or allocation. ...
const Matrix< BaseFloat > & means_invvars() const
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...
BaseFloat min_gaussian_occupancy
Minimum count below which a Gaussian is not updated (and is removed, if remove_low_count_gaussians ==...
void MleDiagGmmUpdate(const MleDiagGmmOptions &config, const AccumDiagGmm &diag_gmm_acc, GmmFlagsType flags, DiagGmm *gmm, BaseFloat *obj_change_out, BaseFloat *count_out, int32 *floored_elements_out, int32 *floored_gaussians_out, int32 *removed_gaussians_out)
for computing the maximum-likelihood estimates of the parameters of a Gaussian mixture model...
bool ApproxEqual(const MatrixBase< Real > &other, float tol=0.01) const
Returns true if ((*this)-other).FrobeniusNorm() <= tol * (*this).FrobeniusNorm(). ...
BaseFloat MlObjective(const DiagGmm &gmm, const AccumDiagGmm &diag_gmm_acc)
Calc using the DiagGMM exponential form.
const Vector< BaseFloat > & gconsts() const
Const accessors.
void AddMat(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transA=kNoTrans)
*this += alpha * M [or M^T]
Matrix< double > mean_accumulator_
int32 ComputeGconsts()
Sets the gconsts.
const VectorBase< double > & occupancy() const
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
uint16 GmmFlagsType
Bitwise OR of the above flags.
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
double min_variance
Minimum allowed variance in any dimension (if no variance floor) It is in double since the variance i...
void Scale(BaseFloat f, GmmFlagsType flags)
void SmoothWithModel(BaseFloat tau, const DiagGmm &src_gmm)
Smooths the accumulated counts using the parameters of a given model.
const DiagGmm & diag_gmm_
GmmFlagsType flags_
Flags corresponding to the accumulators that are stored.
void GetVars(Matrix< Real > *v) const
Accessor for covariances.
void Read(std::istream &in, bool binary, bool add=false)
read from stream.
BaseFloat ComponentPosteriors(const VectorBase< BaseFloat > &data, Vector< BaseFloat > *posteriors) const
Computes the posterior probabilities of all Gaussian components given a data point.
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
const MatrixBase< BaseFloat > & data_
const MatrixBase< double > & variance_accumulator() const
void Scale(Real alpha)
Multiply each element with a scalar value.
BaseFloat AccumulateFromDiag(const DiagGmm &gmm, const VectorBase< BaseFloat > &data, BaseFloat frame_posterior)
Accumulate for all components given a diagonal-covariance GMM.
const MatrixBase< double > & mean_accumulator() const
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
GmmFlagsType Flags() const
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...
void GetMeans(Matrix< Real > *m) const
Accessor for means.
void Write(std::ostream &out_stream, bool binary) const
void RemoveComponents(const std::vector< int32 > &gauss, bool renorm_weights)
Removes multiple components from model; "gauss" must not have dups.
void SmoothStats(BaseFloat tau)
Smooths the accumulated counts by adding 'tau' extra frames.
void AccumulateForComponent(const VectorBase< BaseFloat > &data, int32 comp_index, BaseFloat weight)
Accumulate for a single component, given the posterior.
void Add(double scale, const AccumDiagGmm &acc)
Increment with stats from this other accumulator (times scale)
AccumDiagGmm * dest_accum_
Real TraceMatMat(const MatrixBase< Real > &A, const MatrixBase< Real > &B, MatrixTransposeType trans)
We need to declare this here as it will be a friend function.
Matrix< double > vars_
diagonal variance
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
int32 NumGauss() const
Returns the number of mixture components in the GMM.
MatrixIndexT Dim() const
Returns the dimension of the vector.
void SetZero()
Sets matrix to zero.
Configuration variables like variance floor, minimum occupancy, etc.
void Scale(Real alpha)
Multiplies all elements by this constant.
Vector< double > occupancy_
void Read(std::istream &in_stream, bool binary, bool add)
void SmoothWithAccum(BaseFloat tau, const AccumDiagGmm &src_acc)
Smooths the accumulated counts using some other accumulator.
void SetZero(GmmFlagsType flags)
~AccumulateMultiThreadedClass()
AccumulateMultiThreadedClass(const DiagGmm &diag_gmm, const MatrixBase< BaseFloat > &data, const VectorBase< BaseFloat > &frame_weights, AccumDiagGmm *accum, double *tot_like)
int32 Dim() const
Returns the dimensionality of the feature vectors.
void MulRowsVec(const VectorBase< Real > &scale)
Equivalent to (*this) = diag(scale) * (*this).
Vector< double > variance_floor_vector
Variance floor for each dimension [empty if not supplied].
BaseFloat min_gaussian_weight
Minimum weight below which a Gaussian is not updated (and is removed, if remove_low_count_gaussians =...
Matrix< double > means_
Means.
A class representing a vector.
void InvertElements()
Invert all elements.
Matrix< double > variance_accumulator_
#define KALDI_ASSERT(cond)
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
void ApplyPow(Real power)
Take all elements of vector to a power.
void AddVecVec(const Real alpha, const VectorBase< OtherReal > &a, const VectorBase< OtherReal > &b)
*this += alpha * a * b^T
void CopyRowFromVec(const VectorBase< Real > &v, const MatrixIndexT row)
Copy vector into specific row of matrix.
Definition for Gaussian Mixture Model with diagonal covariances.
std::string GmmFlagsToString(GmmFlagsType flags)
Convert GMM flags to string.
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...
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).
const VectorBase< BaseFloat > & frame_weights_
Vector< double > weights_
weights (not log).
AccumulateMultiThreadedClass(const AccumulateMultiThreadedClass &other)
Provides a vector abstraction class.
void Add(Real c)
Add a constant to each element of a vector.
void SetZero()
Set vector to all zeros.
void Resize(int32 num_gauss, int32 dim, GmmFlagsType flags)
Allocates memory for accumulators.
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...
void AccumulateFromPosteriors(const VectorBase< BaseFloat > &data, const VectorBase< BaseFloat > &gauss_posteriors)
Accumulate for all components, given the posteriors.
BaseFloat variance_tau
Tau value for the variances.
void Read(std::istream &in, bool binary, bool add=false)
Read function using C++ streams.
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
BaseFloat mean_tau
Tau value for the means.
void AssertEqual(const AccumDiagGmm &other)
int32 NumGauss() const
Returns the number of mixture components.
Configuration variables for Maximum A Posteriori (MAP) update.
const Matrix< BaseFloat > & inv_vars() const
void CopyToDiagGmm(DiagGmm *diaggmm, GmmFlagsType flags=kGmmAll) const
Copies to DiagGmm the requested parameters.