All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
FullGmm Class Reference

Definition for Gaussian Mixture Model with full covariances. More...

#include <full-gmm.h>

Collaboration diagram for FullGmm:

Public Member Functions

 FullGmm ()
 Empty constructor. More...
 
 FullGmm (const FullGmm &gmm)
 
 FullGmm (int32 nMix, int32 dim)
 
void Resize (int32 nMix, int32 dim)
 Resizes arrays to this dim. Does not initialize data. More...
 
int32 NumGauss () const
 Returns the number of mixture components in the GMM. More...
 
int32 Dim () const
 Returns the dimensionality of the Gaussian mean vectors. More...
 
void CopyFromFullGmm (const FullGmm &fullgmm)
 Copies from given FullGmm. More...
 
void CopyFromDiagGmm (const DiagGmm &diaggmm)
 Copies from given DiagGmm. More...
 
BaseFloat LogLikelihood (const VectorBase< BaseFloat > &data) const
 Returns the log-likelihood of a data point (vector) given the GMM. More...
 
void LogLikelihoods (const VectorBase< BaseFloat > &data, Vector< BaseFloat > *loglikes) const
 Outputs the per-component contributions to the log-likelihood. More...
 
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. More...
 
BaseFloat GaussianSelection (const VectorBase< BaseFloat > &data, int32 num_gselect, std::vector< int32 > *output) const
 Get gaussian selection information for one frame. More...
 
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. More...
 
BaseFloat ComponentPosteriors (const VectorBase< BaseFloat > &data, VectorBase< BaseFloat > *posterior) const
 Computes the posterior probabilities of all Gaussian components given a data point. More...
 
BaseFloat ComponentLogLikelihood (const VectorBase< BaseFloat > &data, int32 comp_id) const
 Computes the contribution log-likelihood of a data point from a single Gaussian component. More...
 
int32 ComputeGconsts ()
 Sets the gconsts. More...
 
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) More...
 
void Perturb (float perturb_factor)
 Perturbs the component means with a random vector multiplied by the pertrub factor. More...
 
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) More...
 
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); this version only considers merging pairs in "preselect_pairs" (or their descendants after merging). More...
 
void Write (std::ostream &os, bool binary) const
 
void Read (std::istream &is, bool binary)
 
void Interpolate (BaseFloat rho, const FullGmm &source, GmmFlagsType flags=kGmmAll)
 this = rho x source + (1-rho) x this More...
 
const Vector< BaseFloat > & gconsts () const
 Const accessors. More...
 
const Vector< BaseFloat > & weights () const
 
const Matrix< BaseFloat > & means_invcovars () const
 
const std::vector< SpMatrix
< BaseFloat > > & 
inv_covars () const
 
Matrix< BaseFloat > & means_invcovars ()
 Non-const accessors. More...
 
std::vector< SpMatrix
< BaseFloat > > & 
inv_covars ()
 
template<class Real >
void SetWeights (const Vector< Real > &w)
 Mutators for both float or double. More...
 
template<class Real >
void SetMeans (const Matrix< Real > &m)
 Use SetMeans to update only the Gaussian means (and not variances) More...
 
template<class Real >
void SetInvCovarsAndMeans (const std::vector< SpMatrix< Real > > &invcovars, const Matrix< Real > &means)
 Use SetInvCovarsAndMeans if updating both means and (inverse) covariances. More...
 
template<class Real >
void SetInvCovarsAndMeansInvCovars (const std::vector< SpMatrix< Real > > &invcovars, const Matrix< Real > &means_invcovars)
 Use this if setting both, in the class's native format. More...
 
template<class Real >
void SetInvCovars (const std::vector< SpMatrix< Real > > &v)
 Set the (inverse) covariances and recompute means_invcovars_. More...
 
template<class Real >
void GetCovars (std::vector< SpMatrix< Real > > *v) const
 Accessor for covariances. More...
 
template<class Real >
void GetMeans (Matrix< Real > *m) const
 Accessor for means. More...
 
template<class Real >
void GetCovarsAndMeans (std::vector< SpMatrix< Real > > *covars, Matrix< Real > *means) const
 Accessor for covariances and means. More...
 
void RemoveComponent (int32 gauss, bool renorm_weights)
 Mutators for single component, supports float or double Removes single component from model. More...
 
void RemoveComponents (const std::vector< int32 > &gauss, bool renorm_weights)
 Removes multiple components from model; "gauss" must not have dups. More...
 
template<class Real >
void GetComponentMean (int32 gauss, VectorBase< Real > *out) const
 Accessor for component mean. More...
 

Private Member Functions

void ResizeInvCovars (int32 nMix, int32 dim)
 Resizes arrays to this dim. Does not initialize data. More...
 
BaseFloat MergedComponentsLogdet (BaseFloat w1, BaseFloat w2, const VectorBase< BaseFloat > &f1, const VectorBase< BaseFloat > &f2, const SpMatrix< BaseFloat > &s1, const SpMatrix< BaseFloat > &s2) const
 
const FullGmmoperator= (const FullGmm &other)
 

Private Attributes

Vector< BaseFloatgconsts_
 Equals log(weight) - 0.5 * (log det(var) + mean'*inv(var)*mean) More...
 
bool valid_gconsts_
 Recompute gconsts_ if false. More...
 
Vector< BaseFloatweights_
 weights (not log). More...
 
std::vector< SpMatrix
< BaseFloat > > 
inv_covars_
 Inverse covariances. More...
 
Matrix< BaseFloatmeans_invcovars_
 Means times inverse covariances. More...
 

Friends

class FullGmmNormal
 this makes it a little easier to modify the internals More...
 

Detailed Description

Definition for Gaussian Mixture Model with full covariances.

Definition at line 40 of file full-gmm.h.

Constructor & Destructor Documentation

FullGmm ( )
inline

Empty constructor.

Definition at line 46 of file full-gmm.h.

Referenced by FullGmm::Split().

46 : valid_gconsts_(false) {}
bool valid_gconsts_
Recompute gconsts_ if false.
Definition: full-gmm.h:200
FullGmm ( const FullGmm gmm)
inlineexplicit

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

References FullGmm::CopyFromFullGmm().

48  : valid_gconsts_(false) {
49  CopyFromFullGmm(gmm);
50  }
void CopyFromFullGmm(const FullGmm &fullgmm)
Copies from given FullGmm.
Definition: full-gmm.cc:65
bool valid_gconsts_
Recompute gconsts_ if false.
Definition: full-gmm.h:200
FullGmm ( int32  nMix,
int32  dim 
)
inline

Definition at line 52 of file full-gmm.h.

References FullGmm::Resize().

52 : valid_gconsts_(false) { Resize(nMix, dim); }
void Resize(int32 nMix, int32 dim)
Resizes arrays to this dim. Does not initialize data.
Definition: full-gmm.cc:41
bool valid_gconsts_
Recompute gconsts_ if false.
Definition: full-gmm.h:200

Member Function Documentation

BaseFloat ComponentLogLikelihood ( const VectorBase< BaseFloat > &  data,
int32  comp_id 
) const

Computes the contribution log-likelihood of a data point from a single Gaussian component.

NOTE: Currently we make no guarantees about what happens if one of the variances is zero.

Definition at line 562 of file full-gmm.cc.

References FullGmm::Dim(), VectorBase< Real >::Dim(), FullGmm::gconsts_, FullGmm::inv_covars_, KALDI_ERR, FullGmm::means_invcovars_, MatrixBase< Real >::Row(), FullGmm::valid_gconsts_, kaldi::VecSpVec(), and kaldi::VecVec().

Referenced by AmSgmm2::GaussianSelection().

563  {
564  if (!valid_gconsts_)
565  KALDI_ERR << "Must call ComputeGconsts() before computing likelihood";
566  if (data.Dim() != Dim()) {
567  KALDI_ERR << "DiagGmm::ComponentLogLikelihood, dimension "
568  << "mismatch " << (data.Dim()) << "vs. "<< (Dim());
569  }
570  BaseFloat loglike;
571 
572  // loglike = means * inv(vars) * data.
573  loglike = VecVec(means_invcovars_.Row(comp_id), data);
574  // loglike += -0.5 * tr(data*data'*inv(covar))
575  loglike -= 0.5 * VecSpVec(data, inv_covars_[comp_id], data);
576  return loglike + gconsts_(comp_id);
577 }
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: full-gmm.h:60
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
float BaseFloat
Definition: kaldi-types.h:29
bool valid_gconsts_
Recompute gconsts_ if false.
Definition: full-gmm.h:200
#define KALDI_ERR
Definition: kaldi-error.h:127
Real VecSpVec(const VectorBase< Real > &v1, const SpMatrix< Real > &M, const VectorBase< Real > &v2)
Computes v1^T * M * v2.
Definition: sp-matrix.cc:988
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
Definition: kaldi-vector.cc:36
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:62
Vector< BaseFloat > gconsts_
Equals log(weight) - 0.5 * (log det(var) + mean'*inv(var)*mean)
Definition: full-gmm.h:199
BaseFloat ComponentPosteriors ( const VectorBase< BaseFloat > &  data,
VectorBase< BaseFloat > *  posterior 
) const

Computes the posterior probabilities of all Gaussian components given a data point.

Returns the log-likehood of the data given the GMM.

Definition at line 719 of file full-gmm.cc.

References VectorBase< Real >::ApplySoftMax(), VectorBase< Real >::CopyFromVec(), KALDI_ERR, KALDI_ISINF, KALDI_ISNAN, and FullGmm::LogLikelihoods().

Referenced by IvectorExtractorStats::AccStatsForUtterance(), AccumFullGmm::AccumulateFromFull(), main(), TestComponentAcc(), kaldi::TestIvectorExtraction(), and UnitTestFullGmm().

720  {
721  if (posterior == NULL) KALDI_ERR << "NULL pointer passed as return argument.";
722  Vector<BaseFloat> loglikes;
723  LogLikelihoods(data, &loglikes);
724  BaseFloat log_sum = loglikes.ApplySoftMax();
725  if (KALDI_ISNAN(log_sum) || KALDI_ISINF(log_sum))
726  KALDI_ERR << "Invalid answer (overflow or invalid variances/features?)";
727  posterior->CopyFromVec(loglikes);
728  return log_sum;
729 }
#define KALDI_ISINF
Definition: kaldi-math.h:73
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
float BaseFloat
Definition: kaldi-types.h:29
void LogLikelihoods(const VectorBase< BaseFloat > &data, Vector< BaseFloat > *loglikes) const
Outputs the per-component contributions to the log-likelihood.
Definition: full-gmm.cc:591
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_ISNAN
Definition: kaldi-math.h:72
int32 ComputeGconsts ( )

Sets the gconsts.

Returns the number that are "invalid" e.g. because of zero weights or variances.

Definition at line 92 of file full-gmm.cc.

References FullGmm::Dim(), VectorBase< Real >::Dim(), FullGmm::gconsts_, FullGmm::inv_covars_, SpMatrix< Real >::InvertDouble(), KALDI_ASSERT, KALDI_ERR, KALDI_ISINF, KALDI_ISNAN, kaldi::Log(), SpMatrix< Real >::LogPosDefDet(), M_LOG_2PI, FullGmm::means_invcovars_, FullGmm::NumGauss(), Vector< Real >::Resize(), MatrixBase< Real >::Row(), FullGmm::valid_gconsts_, kaldi::VecSpVec(), and FullGmm::weights_.

Referenced by Sgmm2Project::ApplyProjection(), FullGmm::CopyFromDiagGmm(), kaldi::unittest::InitRandFullGmm(), FullGmm::Interpolate(), main(), FullGmm::Merge(), kaldi::MergeFullGmm(), FullGmm::MergePreselect(), kaldi::MleFullGmmUpdate(), FullGmm::Perturb(), FullGmm::Read(), FullGmm::Split(), test_flags_driven_update(), UnitTestEstimateFullGmm(), and UnitTestFullGmm().

92  {
93  int32 num_mix = NumGauss(),
94  dim = Dim();
95  KALDI_ASSERT(num_mix > 0 && dim > 0);
96  BaseFloat offset = -0.5 * M_LOG_2PI * dim; // constant term in gconst.
97  int32 num_bad = 0;
98 
99  // Resize if Gaussians have been removed during Update()
100  if (num_mix != gconsts_.Dim()) gconsts_.Resize(num_mix);
101 
102  for (int32 mix = 0; mix < num_mix; mix++) {
103  KALDI_ASSERT(weights_(mix) >= 0); // Cannot have negative weights.
104  BaseFloat gc = Log(weights_(mix)) + offset; // May be -inf if weights == 0
105  SpMatrix<BaseFloat> covar(inv_covars_[mix]);
106  covar.InvertDouble();
107  BaseFloat logdet = covar.LogPosDefDet();
108  gc -= 0.5 * (logdet + VecSpVec(means_invcovars_.Row(mix),
109  covar, means_invcovars_.Row(mix)));
110  // Note that mean_invcovars(mix)' * covar(mix) * mean_invcovars(mix, d) is
111  // really mean' * inv(covar) * mean, since mean_invcovars(mix, d) contains
112  // the inverse covariance times mean.
113  // So gc is the likelihood at zero feature value.
114 
115  if (KALDI_ISNAN(gc)) { // negative infinity is OK but NaN is not acceptable
116  KALDI_ERR << "At component" << mix
117  << ", not a number in gconst computation";
118  }
119  if (KALDI_ISINF(gc)) {
120  num_bad++;
121  // If positive infinity, make it negative infinity.
122  // Want to make sure the answer becomes -inf in the end, not NaN.
123  if (gc > 0) gc = -gc;
124  }
125  gconsts_(mix) = gc;
126  }
127 
128  valid_gconsts_ = true;
129  return num_bad;
130 }
#define M_LOG_2PI
Definition: kaldi-math.h:60
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: full-gmm.h:60
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
#define KALDI_ISINF
Definition: kaldi-math.h:73
Vector< BaseFloat > weights_
weights (not log).
Definition: full-gmm.h:201
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
float BaseFloat
Definition: kaldi-types.h:29
double Log(double x)
Definition: kaldi-math.h:100
bool valid_gconsts_
Recompute gconsts_ if false.
Definition: full-gmm.h:200
#define KALDI_ERR
Definition: kaldi-error.h:127
Real VecSpVec(const VectorBase< Real > &v1, const SpMatrix< Real > &M, const VectorBase< Real > &v2)
Computes v1^T * M * v2.
Definition: sp-matrix.cc:988
#define KALDI_ISNAN
Definition: kaldi-math.h:72
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:62
Vector< BaseFloat > gconsts_
Equals log(weight) - 0.5 * (log det(var) + mean'*inv(var)*mean)
Definition: full-gmm.h:199
void CopyFromDiagGmm ( const DiagGmm diaggmm)

Copies from given DiagGmm.

Definition at line 77 of file full-gmm.cc.

References FullGmm::ComputeGconsts(), MatrixBase< Real >::CopyFromMat(), VectorBase< Real >::CopyFromVec(), rnnlm::d, FullGmm::Dim(), DiagGmm::Dim(), DiagGmm::gconsts(), FullGmm::gconsts_, FullGmm::inv_covars_, DiagGmm::inv_vars(), FullGmm::means_invcovars_, DiagGmm::means_invvars(), FullGmm::NumGauss(), DiagGmm::NumGauss(), FullGmm::Resize(), DiagGmm::weights(), and FullGmm::weights_.

Referenced by main(), and UnitTestFullGmm().

77  {
78  Resize(diaggmm.NumGauss(), diaggmm.Dim());
79  gconsts_.CopyFromVec(diaggmm.gconsts());
80  weights_.CopyFromVec(diaggmm.weights());
81  means_invcovars_.CopyFromMat(diaggmm.means_invvars());
82  int32 ncomp = NumGauss(), dim = Dim();
83  for (int32 mix = 0; mix < ncomp; mix++) {
84  inv_covars_[mix].SetZero();
85  for (int32 d = 0; d < dim; d++) {
86  inv_covars_[mix](d, d) = diaggmm.inv_vars()(mix, d);
87  }
88  }
90 }
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: full-gmm.h:60
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
int32 ComputeGconsts()
Sets the gconsts.
Definition: full-gmm.cc:92
Vector< BaseFloat > weights_
weights (not log).
Definition: full-gmm.h:201
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
void Resize(int32 nMix, int32 dim)
Resizes arrays to this dim. Does not initialize data.
Definition: full-gmm.cc:41
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
Vector< BaseFloat > gconsts_
Equals log(weight) - 0.5 * (log det(var) + mean'*inv(var)*mean)
Definition: full-gmm.h:199
void CopyFromFullGmm ( const FullGmm fullgmm)

Copies from given FullGmm.

Definition at line 65 of file full-gmm.cc.

References MatrixBase< Real >::CopyFromMat(), VectorBase< Real >::CopyFromVec(), FullGmm::Dim(), FullGmm::gconsts_, FullGmm::inv_covars_, FullGmm::means_invcovars_, FullGmm::NumGauss(), FullGmm::Resize(), FullGmm::valid_gconsts_, and FullGmm::weights_.

Referenced by AmSgmm2::CopyFromSgmm2(), AmSgmm2::CopyGlobalsInitVecs(), FullGmm::FullGmm(), AmSgmm2::InitializeFromFullGmm(), kaldi::MergeFullGmm(), FullGmm::Split(), test_flags_driven_update(), test_io(), and UnitTestFullGmm().

65  {
66  Resize(fullgmm.NumGauss(), fullgmm.Dim());
67  gconsts_.CopyFromVec(fullgmm.gconsts_);
68  weights_.CopyFromVec(fullgmm.weights_);
69  means_invcovars_.CopyFromMat(fullgmm.means_invcovars_);
70  int32 ncomp = NumGauss();
71  for (int32 mix = 0; mix < ncomp; mix++) {
72  inv_covars_[mix].CopyFromSp(fullgmm.inv_covars_[mix]);
73  }
74  valid_gconsts_ = fullgmm.valid_gconsts_;
75 }
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
Vector< BaseFloat > weights_
weights (not log).
Definition: full-gmm.h:201
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
void Resize(int32 nMix, int32 dim)
Resizes arrays to this dim. Does not initialize data.
Definition: full-gmm.cc:41
bool valid_gconsts_
Recompute gconsts_ if false.
Definition: full-gmm.h:200
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
Vector< BaseFloat > gconsts_
Equals log(weight) - 0.5 * (log det(var) + mean'*inv(var)*mean)
Definition: full-gmm.h:199
int32 Dim ( ) const
inline

Returns the dimensionality of the Gaussian mean vectors.

Definition at line 60 of file full-gmm.h.

References FullGmm::means_invcovars_, and MatrixBase< Real >::NumCols().

Referenced by AccumFullGmm::AccumulateFromFull(), AmSgmm2::Check(), FullGmm::ComponentLogLikelihood(), AmSgmm2::ComputeDerivedVars(), kaldi::ComputeFeatureNormalizingTransform(), FullGmm::ComputeGconsts(), Sgmm2Project::ComputeLdaStats(), FullGmm::CopyFromDiagGmm(), FullGmmNormal::CopyFromFullGmm(), FullGmm::CopyFromFullGmm(), DiagGmm::CopyFromFullGmm(), FullGmmNormal::CopyToFullGmm(), FullGmm::GetComponentMean(), FullGmm::GetCovars(), FullGmm::GetCovarsAndMeans(), GetLogLikeTest(), FullGmm::GetMeans(), AmSgmm2::InitializeCovars(), AmSgmm2::InitializeFromFullGmm(), AmSgmm2::InitializeMw(), AmSgmm2::InitializeNu(), FullGmm::Interpolate(), DiagGmm::Interpolate(), FullGmm::LogLikelihoods(), FullGmm::LogLikelihoodsPreselect(), main(), FullGmm::Merge(), kaldi::MergeFullGmm(), FullGmm::MergePreselect(), kaldi::MleFullGmmUpdate(), kaldi::MlObjective(), FullGmm::Perturb(), AccumFullGmm::Resize(), FullGmm::SetInvCovars(), FullGmm::SetInvCovarsAndMeans(), FullGmm::Split(), test_flags_driven_update(), test_io(), TestComponentAcc(), UnitTestEstimateFullGmm(), and UnitTestFullGmm().

60 { return means_invcovars_.NumCols(); }
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
BaseFloat GaussianSelection ( const VectorBase< BaseFloat > &  data,
int32  num_gselect,
std::vector< int32 > *  output 
) const

Get gaussian selection information for one frame.

Returns log-like for this frame. Output is the best "num_gselect" indices, sorted from best to worst likelihood. If "num_gselect" > NumGauss(), sets it to NumGauss().

Definition at line 637 of file full-gmm.cc.

References VectorBase< Real >::Data(), rnnlm::j, KALDI_ASSERT, kaldi::kUndefined, kaldi::LogAdd(), FullGmm::LogLikelihoods(), and FullGmm::NumGauss().

Referenced by main().

639  {
640  int32 num_gauss = NumGauss();
641  Vector<BaseFloat> loglikes(num_gauss, kUndefined);
642  output->clear();
643  this->LogLikelihoods(data, &loglikes);
644 
645  BaseFloat thresh;
646  if (num_gselect < num_gauss) {
647  Vector<BaseFloat> loglikes_copy(loglikes);
648  BaseFloat *ptr = loglikes_copy.Data();
649  std::nth_element(ptr, ptr+num_gauss-num_gselect, ptr+num_gauss);
650  thresh = ptr[num_gauss-num_gselect];
651  } else {
652  thresh = -std::numeric_limits<BaseFloat>::infinity();
653  }
654  BaseFloat tot_loglike = -std::numeric_limits<BaseFloat>::infinity();
655  std::vector<std::pair<BaseFloat, int32> > pairs;
656  for (int32 p = 0; p < num_gauss; p++) {
657  if (loglikes(p) >= thresh) {
658  pairs.push_back(std::make_pair(loglikes(p), p));
659  }
660  }
661  std::sort(pairs.begin(), pairs.end(),
662  std::greater<std::pair<BaseFloat, int32> >());
663  for (int32 j = 0;
664  j < num_gselect && j < static_cast<int32>(pairs.size());
665  j++) {
666  output->push_back(pairs[j].second);
667  tot_loglike = LogAdd(tot_loglike, pairs[j].first);
668  }
669  KALDI_ASSERT(!output->empty());
670  return tot_loglike;
671 }
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
float BaseFloat
Definition: kaldi-types.h:29
void LogLikelihoods(const VectorBase< BaseFloat > &data, Vector< BaseFloat > *loglikes) const
Outputs the per-component contributions to the log-likelihood.
Definition: full-gmm.cc:591
double LogAdd(double x, double y)
Definition: kaldi-math.h:184
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
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.

Returns log-like for this frame. Output is the best "num_gselect" indices that were preselected, sorted from best to worst likelihood. If "num_gselect" > NumGauss(), sets it to NumGauss().

Definition at line 674 of file full-gmm.cc.

References VectorBase< Real >::Data(), rnnlm::j, KALDI_ASSERT, KALDI_WARN, kaldi::LogAdd(), and FullGmm::LogLikelihoodsPreselect().

Referenced by main().

678  {
679  static bool warned_size = false;
680  int32 preselect_sz = preselect.size();
681  int32 this_num_gselect = std::min(num_gselect, preselect_sz);
682  if (preselect_sz <= num_gselect && !warned_size) {
683  warned_size = true;
684  KALDI_WARN << "Preselect size is less or equal to than final size, "
685  << "doing nothing: " << preselect_sz << " < " << num_gselect
686  << " [won't warn again]";
687  }
688  Vector<BaseFloat> loglikes(preselect_sz);
689  LogLikelihoodsPreselect(data, preselect, &loglikes);
690 
691  Vector<BaseFloat> loglikes_copy(loglikes);
692  BaseFloat *ptr = loglikes_copy.Data();
693  std::nth_element(ptr, ptr+preselect_sz-this_num_gselect,
694  ptr+preselect_sz);
695  BaseFloat thresh = ptr[preselect_sz-this_num_gselect];
696 
697  BaseFloat tot_loglike = -std::numeric_limits<BaseFloat>::infinity();
698  // we want the output sorted from best likelihood to worse
699  // (so we can prune further without the model)...
700  std::vector<std::pair<BaseFloat, int32> > pairs;
701  for (int32 p = 0; p < preselect_sz; p++)
702  if (loglikes(p) >= thresh)
703  pairs.push_back(std::make_pair(loglikes(p), preselect[p]));
704  std::sort(pairs.begin(), pairs.end(),
705  std::greater<std::pair<BaseFloat, int32> >());
706  output->clear();
707  for (int32 j = 0;
708  j < this_num_gselect && j < static_cast<int32>(pairs.size());
709  j++) {
710  output->push_back(pairs[j].second);
711  tot_loglike = LogAdd(tot_loglike, pairs[j].first);
712  }
713  KALDI_ASSERT(!output->empty());
714  return tot_loglike;
715 }
float BaseFloat
Definition: kaldi-types.h:29
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
#define KALDI_WARN
Definition: kaldi-error.h:130
double LogAdd(double x, double y)
Definition: kaldi-math.h:184
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
const Vector<BaseFloat>& gconsts ( ) const
inline

Const accessors.

Definition at line 143 of file full-gmm.h.

References FullGmm::gconsts_.

Referenced by DiagGmm::CopyFromFullGmm(), and kaldi::MlObjective().

143 { return gconsts_; }
Vector< BaseFloat > gconsts_
Equals log(weight) - 0.5 * (log det(var) + mean'*inv(var)*mean)
Definition: full-gmm.h:199
void GetComponentMean ( int32  gauss,
VectorBase< Real > *  out 
) const

Accessor for component mean.

Definition at line 151 of file full-gmm-inl.h.

References VectorBase< Real >::AddSpVec(), SpMatrix< Real >::CopyFromSp(), VectorBase< Real >::CopyFromVec(), FullGmm::Dim(), VectorBase< Real >::Dim(), FullGmm::inv_covars_, SpMatrix< Real >::InvertDouble(), KALDI_ASSERT, FullGmm::means_invcovars_, FullGmm::NumGauss(), MatrixBase< Real >::Row(), and VectorBase< Real >::SetZero().

Referenced by AmSgmm2::InitializeMw(), and UnitTestFullGmm().

152  {
153  KALDI_ASSERT(gauss < NumGauss() && out != NULL);
154  KALDI_ASSERT(out->Dim() == Dim());
155  out->SetZero();
156  SpMatrix<Real> covar(Dim());
157  Vector<Real> mean_times_invcovar(Dim());
158  covar.CopyFromSp(inv_covars_[gauss]);
159  covar.InvertDouble();
160  mean_times_invcovar.CopyFromVec(means_invcovars_.Row(gauss));
161  out->AddSpVec(1.0, covar, mean_times_invcovar, 0.0);
162 }
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: full-gmm.h:60
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
void GetCovars ( std::vector< SpMatrix< Real > > *  v) const

Accessor for covariances.

Definition at line 106 of file full-gmm-inl.h.

References FullGmm::Dim(), rnnlm::i, FullGmm::inv_covars_, and KALDI_ASSERT.

Referenced by test_flags_driven_update(), and UnitTestFullGmm().

106  {
107  KALDI_ASSERT(v != NULL);
108  v->resize(inv_covars_.size());
109  size_t dim = Dim();
110  for (size_t i = 0; i < inv_covars_.size(); i++) {
111  (*v)[i].Resize(dim);
112  (*v)[i].CopyFromSp(inv_covars_[i]);
113  (*v)[i].InvertDouble();
114  }
115 }
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: full-gmm.h:60
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
void GetCovarsAndMeans ( std::vector< SpMatrix< Real > > *  covars,
Matrix< Real > *  means 
) const

Accessor for covariances and means.

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

References VectorBase< Real >::CopyFromVec(), FullGmm::Dim(), rnnlm::i, FullGmm::inv_covars_, KALDI_ASSERT, FullGmm::means_invcovars_, FullGmm::NumGauss(), Matrix< Real >::Resize(), and MatrixBase< Real >::Row().

Referenced by kaldi::ComputeFeatureNormalizingTransform(), and UnitTestFullGmm().

133  {
134  KALDI_ASSERT(covars != NULL && means != NULL);
135  size_t dim = Dim();
136  size_t num_gauss = NumGauss();
137  covars->resize(num_gauss);
138  means->Resize(num_gauss, dim);
139  Vector<Real> mean_times_invcovar(Dim());
140  for (size_t i = 0; i < num_gauss; i++) {
141  (*covars)[i].Resize(dim);
142  (*covars)[i].CopyFromSp(inv_covars_[i]);
143  (*covars)[i].InvertDouble();
144  mean_times_invcovar.CopyFromVec(means_invcovars_.Row(i));
145  (means->Row(i)).AddSpVec(1.0, (*covars)[i], mean_times_invcovar, 0.0);
146  }
147 }
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: full-gmm.h:60
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
void GetMeans ( Matrix< Real > *  m) const

Accessor for means.

Definition at line 118 of file full-gmm-inl.h.

References SpMatrix< Real >::CopyFromSp(), VectorBase< Real >::CopyFromVec(), FullGmm::Dim(), rnnlm::i, FullGmm::inv_covars_, SpMatrix< Real >::InvertDouble(), KALDI_ASSERT, FullGmm::means_invcovars_, FullGmm::NumGauss(), Matrix< Real >::Resize(), and MatrixBase< Real >::Row().

Referenced by DiagGmm::CopyFromFullGmm(), GetLogLikeTest(), IvectorExtractor::IvectorExtractor(), test_flags_driven_update(), and UnitTestFullGmm().

118  {
119  KALDI_ASSERT(M != NULL);
120  M->Resize(NumGauss(), Dim());
121  SpMatrix<Real> covar(Dim());
122  Vector<Real> mean_times_invcovar(Dim());
123  for (int32 i = 0; i < NumGauss(); i++) {
124  covar.CopyFromSp(inv_covars_[i]);
125  covar.InvertDouble();
126  mean_times_invcovar.CopyFromVec(means_invcovars_.Row(i));
127  (M->Row(i)).AddSpVec(1.0, covar, mean_times_invcovar, 0.0);
128  }
129 }
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: full-gmm.h:60
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
void Interpolate ( BaseFloat  rho,
const FullGmm source,
GmmFlagsType  flags = kGmmAll 
)

this = rho x source + (1-rho) x this

Definition at line 784 of file full-gmm.cc.

References FullGmm::ComputeGconsts(), FullGmm::Dim(), rnnlm::i, KALDI_ASSERT, kaldi::kGmmMeans, kaldi::kGmmVariances, kaldi::kGmmWeights, FullGmmNormal::means_, FullGmm::NumGauss(), FullGmmNormal::vars_, and FullGmmNormal::weights_.

785  {
786  KALDI_ASSERT(NumGauss() == source.NumGauss());
787  KALDI_ASSERT(Dim() == source.Dim());
788  FullGmmNormal us(*this);
789  FullGmmNormal them(source);
790 
791  if (flags & kGmmWeights) {
792  us.weights_.Scale(1.0 - rho);
793  us.weights_.AddVec(rho, them.weights_);
794  us.weights_.Scale(1.0 / us.weights_.Sum());
795  }
796 
797  if (flags & kGmmMeans) {
798  us.means_.Scale(1.0 - rho);
799  us.means_.AddMat(rho, them.means_);
800  }
801 
802  if (flags & kGmmVariances) {
803  for (int32 i = 0; i < NumGauss(); i++) {
804  us.vars_[i].Scale(1.0 - rho);
805  us.vars_[i].AddSp(rho, them.vars_[i]);
806  }
807  }
808 
809  us.CopyToFullGmm(this);
810  ComputeGconsts();
811 }
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: full-gmm.h:60
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
int32 ComputeGconsts()
Sets the gconsts.
Definition: full-gmm.cc:92
friend class FullGmmNormal
this makes it a little easier to modify the internals
Definition: full-gmm.h:42
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
const std::vector<SpMatrix<BaseFloat> >& inv_covars ( ) const
inline

Definition at line 146 of file full-gmm.h.

References FullGmm::inv_covars_.

Referenced by DiagGmm::CopyFromFullGmm(), GetLogLikeTest(), AmSgmm2::InitializeCovars(), IvectorExtractor::IvectorExtractor(), kaldi::MergeFullGmm(), kaldi::MlObjective(), and UnitTestEstimateFullGmm().

146  {
147  return inv_covars_; }
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
std::vector<SpMatrix<BaseFloat> >& inv_covars ( )
inline

Definition at line 151 of file full-gmm.h.

References FullGmm::inv_covars_.

151 { return inv_covars_; }
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
BaseFloat LogLikelihood ( const VectorBase< BaseFloat > &  data) const

Returns the log-likelihood of a data point (vector) given the GMM.

Definition at line 582 of file full-gmm.cc.

References KALDI_ERR, KALDI_ISINF, KALDI_ISNAN, FullGmm::LogLikelihoods(), and VectorBase< Real >::LogSumExp().

Referenced by main(), test_flags_driven_update(), test_io(), TestComponentAcc(), and UnitTestFullGmm().

582  {
583  Vector<BaseFloat> loglikes;
584  LogLikelihoods(data, &loglikes);
585  BaseFloat log_sum = loglikes.LogSumExp();
586  if (KALDI_ISNAN(log_sum) || KALDI_ISINF(log_sum))
587  KALDI_ERR << "Invalid answer (overflow or invalid variances/features?)";
588  return log_sum;
589 }
#define KALDI_ISINF
Definition: kaldi-math.h:73
float BaseFloat
Definition: kaldi-types.h:29
void LogLikelihoods(const VectorBase< BaseFloat > &data, Vector< BaseFloat > *loglikes) const
Outputs the per-component contributions to the log-likelihood.
Definition: full-gmm.cc:591
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_ISNAN
Definition: kaldi-math.h:72
void LogLikelihoods ( const VectorBase< BaseFloat > &  data,
Vector< BaseFloat > *  loglikes 
) const

Outputs the per-component contributions to the log-likelihood.

Definition at line 591 of file full-gmm.cc.

References VectorBase< Real >::AddMatVec(), VectorBase< Real >::CopyFromVec(), FullGmm::Dim(), VectorBase< Real >::Dim(), FullGmm::gconsts_, FullGmm::inv_covars_, KALDI_ASSERT, kaldi::kNoTrans, kaldi::kUndefined, FullGmm::means_invcovars_, FullGmm::NumGauss(), Vector< Real >::Resize(), and kaldi::TraceSpSpLower().

Referenced by FullGmm::ComponentPosteriors(), FullGmm::GaussianSelection(), AmSgmm2::GaussianSelection(), FullGmm::LogLikelihood(), and UnitTestFullGmm().

592  {
593  loglikes->Resize(gconsts_.Dim(), kUndefined);
594  loglikes->CopyFromVec(gconsts_);
595  int32 dim = Dim();
596  KALDI_ASSERT(dim == data.Dim());
597  SpMatrix<BaseFloat> data_sq(dim); // Initialize and make zero
598  data_sq.AddVec2(1.0, data);
599  // The following enables an optimization below: TraceSpSpLower, which is
600  // just like a dot product internally.
601  data_sq.ScaleDiag(0.5);
602 
603  // loglikes += mean' * inv(covar) * data.
604  loglikes->AddMatVec(1.0, means_invcovars_, kNoTrans, data, 1.0);
605  // loglikes -= 0.5 * data'*inv(covar)*data = 0.5 * tr(data*data'*inv(covar))
606  int32 num_comp = NumGauss();
607  for (int32 mix = 0; mix < num_comp; mix++) {
608  // was: (*loglikes)(mix) -= 0.5 * TraceSpSp(data_sq, inv_covars_[mix]);
609  (*loglikes)(mix) -= TraceSpSpLower(data_sq, inv_covars_[mix]);
610  }
611 }
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: full-gmm.h:60
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
Real TraceSpSpLower(const SpMatrix< Real > &A, const SpMatrix< Real > &B)
Definition: sp-matrix.cc:1195
void AddMatVec(const Real alpha, const MatrixBase< Real > &M, const MatrixTransposeType trans, const VectorBase< Real > &v, const Real beta)
Add matrix times vector : this <– beta*this + alpha*M*v.
Definition: kaldi-vector.cc:91
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:62
Vector< BaseFloat > gconsts_
Equals log(weight) - 0.5 * (log det(var) + mean'*inv(var)*mean)
Definition: full-gmm.h:199
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.

Note: indices.size() will equal loglikes->Dim() at output. loglikes[i] will correspond to the log-likelihood of the Gaussian indexed indices[i].

Definition at line 613 of file full-gmm.cc.

References SpMatrix< Real >::AddVec2(), FullGmm::Dim(), VectorBase< Real >::Dim(), FullGmm::gconsts_, rnnlm::i, FullGmm::inv_covars_, KALDI_ASSERT, kaldi::kUndefined, FullGmm::means_invcovars_, Vector< Real >::Resize(), MatrixBase< Real >::Row(), PackedMatrix< Real >::ScaleDiag(), kaldi::TraceSpSpLower(), and kaldi::VecVec().

Referenced by FullGmm::GaussianSelectionPreselect(), main(), and UnitTestFullGmm().

615  {
616  int32 dim = Dim();
617  KALDI_ASSERT(dim == data.Dim());
618  int32 num_indices = static_cast<int32>(indices.size());
619  loglikes->Resize(num_indices, kUndefined);
620 
621  SpMatrix<BaseFloat> data_sq(dim); // Initialize and make zero
622  data_sq.AddVec2(1.0, data);
623  // The following enables an optimization below: TraceSpSpLower, which is
624  // just like a dot product internally.
625  data_sq.ScaleDiag(0.5);
626 
627  for (int32 i = 0; i < num_indices; i++) {
628  int32 idx = indices[i];
629  (*loglikes)(i) = gconsts_(idx)
630  + VecVec(means_invcovars_.Row(idx), data)
631  - TraceSpSpLower(data_sq, inv_covars_[idx]);
632  }
633 }
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: full-gmm.h:60
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
Real TraceSpSpLower(const SpMatrix< Real > &A, const SpMatrix< Real > &B)
Definition: sp-matrix.cc:1195
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
Definition: kaldi-vector.cc:36
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:62
Vector< BaseFloat > gconsts_
Equals log(weight) - 0.5 * (log det(var) + mean'*inv(var)*mean)
Definition: full-gmm.h:199
const Matrix<BaseFloat>& means_invcovars ( ) const
inline

Definition at line 145 of file full-gmm.h.

References FullGmm::means_invcovars_.

Referenced by kaldi::MergeFullGmm(), kaldi::MlObjective(), and UnitTestEstimateFullGmm().

145 { return means_invcovars_; }
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
Matrix<BaseFloat>& means_invcovars ( )
inline

Non-const accessors.

Definition at line 150 of file full-gmm.h.

References FullGmm::means_invcovars_.

150 { return means_invcovars_; }
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
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 at line 206 of file full-gmm.cc.

References VectorBase< Real >::AddVec(), kaldi::ApproxEqual(), FullGmm::ComputeGconsts(), FullGmm::Dim(), FullGmm::gconsts_, rnnlm::i, FullGmm::inv_covars_, rnnlm::j, KALDI_ASSERT, KALDI_ERR, KALDI_WARN, FullGmm::means_invcovars_, FullGmm::MergedComponentsLogdet(), FullGmm::NumGauss(), Vector< Real >::RemoveElement(), Matrix< Real >::RemoveRow(), Vector< Real >::Resize(), Matrix< Real >::Resize(), MatrixBase< Real >::Row(), VectorBase< Real >::Scale(), VectorBase< Real >::SetZero(), FullGmm::weights(), and FullGmm::weights_.

Referenced by UnitTestFullGmm().

206  {
207  if (target_components <= 0 || NumGauss() < target_components) {
208  KALDI_ERR << "Invalid argument for target number of Gaussians (="
209  << target_components << ")";
210  }
211  if (NumGauss() == target_components) {
212  KALDI_WARN << "No components merged, as target = total.";
213  return;
214  }
215 
216  int32 num_comp = NumGauss(), dim = Dim();
217 
218  if (target_components == 1) { // global mean and variance
219  Vector<BaseFloat> weights(weights_);
220  // Undo variance inversion and multiplication of mean by this
221  vector<SpMatrix<BaseFloat> > covars(num_comp);
222  Matrix<BaseFloat> means(num_comp, dim);
223  for (int32 i = 0; i < num_comp; i++) {
224  covars[i].Resize(dim);
225  covars[i].CopyFromSp(inv_covars_[i]);
226  covars[i].InvertDouble();
227  means.Row(i).AddSpVec(1.0, covars[i], means_invcovars_.Row(i), 0.0);
228  covars[i].AddVec2(1.0, means.Row(i));
229  }
230 
231  // Slightly more efficient than calling this->Resize(1, dim)
232  gconsts_.Resize(1);
233  weights_.Resize(1);
234  means_invcovars_.Resize(1, dim);
235  inv_covars_.resize(1);
236  inv_covars_[0].Resize(dim);
237  Vector<BaseFloat> tmp_mean(dim);
238 
239  for (int32 i = 0; i < num_comp; i++) {
240  weights_(0) += weights(i);
241  tmp_mean.AddVec(weights(i), means.Row(i));
242  inv_covars_[0].AddSp(weights(i), covars[i]);
243  }
244  if (!ApproxEqual(weights_(0), 1.0, 1e-6)) {
245  KALDI_WARN << "Weights sum to " << weights_(0) << ": rescaling.";
246  tmp_mean.Scale(weights_(0));
247  inv_covars_[0].Scale(weights_(0));
248  weights_(0) = 1.0;
249  }
250  inv_covars_[0].AddVec2(-1.0, tmp_mean);
251  inv_covars_[0].InvertDouble();
252  means_invcovars_.Row(0).AddSpVec(1.0, inv_covars_[0], tmp_mean, 0.0);
253  ComputeGconsts();
254  return;
255  }
256 
257  // If more than 1 merged component is required, do greedy bottom-up
258  // clustering, always picking the pair of components that lead to the smallest
259  // decrease in likelihood.
260  vector<bool> discarded_component(num_comp);
261  Vector<BaseFloat> logdet(num_comp); // logdet for each component
262  logdet.SetZero();
263  for (int32 i = 0; i < num_comp; i++) {
264  discarded_component[i] = false;
265  logdet(i) += 0.5 * inv_covars_[i].LogPosDefDet();
266  // +0.5 because var is inverted
267  }
268 
269  // Undo variance inversion and multiplication of mean by this
270  // Makes copy of means and vars for all components.
271  vector<SpMatrix<BaseFloat> > vars(num_comp);
272  Matrix<BaseFloat> means(num_comp, dim);
273  for (int32 i = 0; i < num_comp; i++) {
274  vars[i].Resize(dim);
275  vars[i].CopyFromSp(inv_covars_[i]);
276  vars[i].InvertDouble();
277  means.Row(i).AddSpVec(1.0, vars[i], means_invcovars_.Row(i), 0.0);
278 
279  // add means square to variances; get second-order stats
280  // (normalized by zero-order stats)
281  vars[i].AddVec2(1.0, means.Row(i));
282  }
283 
284  // compute change of likelihood for all combinations of components
285  SpMatrix<BaseFloat> delta_like(num_comp);
286  for (int32 i = 0; i < num_comp; i++) {
287  for (int32 j = 0; j < i; j++) {
288  BaseFloat w1 = weights_(i), w2 = weights_(j), w_sum = w1 + w2;
289  BaseFloat merged_logdet = MergedComponentsLogdet(w1, w2,
290  means.Row(i), means.Row(j), vars[i], vars[j]);
291  delta_like(i, j) = w_sum * merged_logdet
292  - w1 * logdet(i) - w2 * logdet(j);
293  }
294  }
295 
296  // Merge components with smallest impact on the loglike
297  for (int32 removed = 0; removed < num_comp - target_components; removed++) {
298  // Search for the least significant change in likelihood
299  // (maximum of negative delta_likes)
300  BaseFloat max_delta_like = -std::numeric_limits<BaseFloat>::max();
301  int32 max_i = 0, max_j = 0;
302  for (int32 i = 0; i < NumGauss(); i++) {
303  if (discarded_component[i]) continue;
304  for (int32 j = 0; j < i; j++) {
305  if (discarded_component[j]) continue;
306  if (delta_like(i, j) > max_delta_like) {
307  max_delta_like = delta_like(i, j);
308  max_i = i;
309  max_j = j;
310  }
311  }
312  }
313 
314  // make sure that different components will be merged
315  KALDI_ASSERT(max_i != max_j);
316 
317  // remember history
318  if (history != NULL) {
319  history->push_back(max_i);
320  history->push_back(max_j);
321  }
322 
323  // Merge components
324  BaseFloat w1 = weights_(max_i), w2 = weights_(max_j);
325  BaseFloat w_sum = w1 + w2;
326  // merge means
327  means.Row(max_i).AddVec(w2/w1, means.Row(max_j));
328  means.Row(max_i).Scale(w1/w_sum);
329  // merge vars
330  vars[max_i].AddSp(w2/w1, vars[max_j]);
331  vars[max_i].Scale(w1/w_sum);
332  // merge weights
333  weights_(max_i) = w_sum;
334 
335  // Update gmm for merged component
336  // copy second-order stats (normalized by zero-order stats)
337  inv_covars_[max_i].CopyFromSp(vars[max_i]);
338  // centralize
339  inv_covars_[max_i].AddVec2(-1.0, means.Row(max_i));
340  // invert
341  inv_covars_[max_i].InvertDouble();
342  // copy first-order stats (normalized by zero-order stats)
343  // and multiply by inv_vars
344  means_invcovars_.Row(max_i).AddSpVec(1.0, inv_covars_[max_i],
345  means.Row(max_i), 0.0);
346 
347  // Update logdet for merged component
348  logdet(max_i) += 0.5 * inv_covars_[max_i].LogPosDefDet();
349  // +0.5 because var is inverted
350 
351  // Label the removed component as discarded
352  discarded_component[max_j] = true;
353 
354  // Update delta_like for merged component
355  for (int32 j = 0; j < num_comp; j++) {
356  if ((j == max_i) || (discarded_component[j])) continue;
357  BaseFloat w1 = weights_(max_i), w2 = weights_(j), w_sum = w1 + w2;
358  BaseFloat merged_logdet = MergedComponentsLogdet(w1, w2,
359  means.Row(max_i), means.Row(j), vars[max_i], vars[j]);
360  delta_like(max_i, j) = w_sum * merged_logdet
361  - w1 * logdet(max_i) - w2 * logdet(j);
362  // doesn't respect lower triangular indeces,
363  // relies on implicitly performed swap of coordinates if necessary
364  }
365  }
366 
367  // Remove the consumed components
368  int32 m = 0;
369  for (int32 i = 0; i < num_comp; i++) {
370  if (discarded_component[i]) {
373  inv_covars_.erase(inv_covars_.begin() + m);
374  } else {
375  ++m;
376  }
377  }
378 
379  ComputeGconsts();
380 }
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: full-gmm.h:60
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
int32 ComputeGconsts()
Sets the gconsts.
Definition: full-gmm.cc:92
Vector< BaseFloat > weights_
weights (not log).
Definition: full-gmm.h:201
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
const Vector< BaseFloat > & weights() const
Definition: full-gmm.h:144
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
float BaseFloat
Definition: kaldi-types.h:29
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
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_WARN
Definition: kaldi-error.h:130
void Scale(Real alpha)
Multiplies all elements by this constant.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
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).
void RemoveRow(MatrixIndexT i)
Remove a specified row.
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
void RemoveElement(MatrixIndexT i)
Remove one element and shifts later elements down.
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
Definition: kaldi-math.h:262
Vector< BaseFloat > gconsts_
Equals log(weight) - 0.5 * (log det(var) + mean'*inv(var)*mean)
Definition: full-gmm.h:199
BaseFloat MergedComponentsLogdet ( BaseFloat  w1,
BaseFloat  w2,
const VectorBase< BaseFloat > &  f1,
const VectorBase< BaseFloat > &  f2,
const SpMatrix< BaseFloat > &  s1,
const SpMatrix< BaseFloat > &  s2 
) const
private

Definition at line 537 of file full-gmm.cc.

References SpMatrix< Real >::AddSp(), VectorBase< Real >::AddVec(), SpMatrix< Real >::AddVec2(), SpMatrix< Real >::CopyFromSp(), VectorBase< Real >::CopyFromVec(), VectorBase< Real >::Dim(), SpMatrix< Real >::LogPosDefDet(), PackedMatrix< Real >::Scale(), and VectorBase< Real >::Scale().

Referenced by FullGmm::Merge(), and FullGmm::MergePreselect().

542  {
543  int32 dim = f1.Dim();
544  Vector<BaseFloat> tmp_mean(dim);
545  SpMatrix<BaseFloat> tmp_var(dim);
546  BaseFloat merged_logdet = 0.0;
547 
548  BaseFloat w_sum = w1 + w2;
549  tmp_mean.CopyFromVec(f1);
550  tmp_mean.AddVec(w2/w1, f2);
551  tmp_mean.Scale(w1/w_sum);
552  tmp_var.CopyFromSp(s1);
553  tmp_var.AddSp(w2/w1, s2);
554  tmp_var.Scale(w1/w_sum);
555  tmp_var.AddVec2(-1.0, tmp_mean);
556  merged_logdet -= 0.5 * tmp_var.LogPosDefDet();
557  // -0.5 because var is not inverted
558  return merged_logdet;
559 }
float BaseFloat
Definition: kaldi-types.h:29
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:62
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); this version only considers merging pairs in "preselect_pairs" (or their descendants after merging).

This is for efficiency, for large models. Returns the delta likelihood.

Definition at line 382 of file full-gmm.cc.

References kaldi::ApproxEqual(), FullGmm::ComputeGconsts(), FullGmm::Dim(), FullGmm::gconsts_, rnnlm::i, FullGmm::inv_covars_, KALDI_ASSERT, KALDI_VLOG, KALDI_WARN, kaldi::kCopyData, FullGmm::means_invcovars_, FullGmm::MergedComponentsLogdet(), FullGmm::NumGauss(), Vector< Real >::Resize(), Matrix< Real >::Resize(), MatrixBase< Real >::Row(), VectorBase< Real >::SetZero(), FullGmm::valid_gconsts_, and FullGmm::weights_.

Referenced by main().

383  {
384  KALDI_ASSERT(!preselect.empty());
385  double ans = 0.0;
386  if (target_components <= 0 || NumGauss() < target_components) {
387  KALDI_WARN << "Invalid argument for target number of Gaussians (="
388  << target_components << "), currently "
389  << NumGauss() << ", not mixing down";
390  return 0.0;
391  }
392  if (NumGauss() == target_components) {
393  KALDI_WARN << "No components merged, as target = total.";
394  return 0.0;
395  }
396  // likelihood change (a negative or zero value), and then the pair of indices.
397  typedef pair<BaseFloat, pair<int32, int32> > QueueElem;
398  std::priority_queue<QueueElem> queue;
399 
400  int32 num_comp = NumGauss(), dim = Dim();
401 
402  // Do greedy bottom-up clustering, always picking the pair of components that
403  // lead to the smallest decrease in likelihood.
404  vector<bool> discarded_component(num_comp);
405  Vector<BaseFloat> logdet(num_comp); // logdet for each component
406  logdet.SetZero();
407  for (int32 i = 0; i < num_comp; i++) {
408  discarded_component[i] = false;
409  logdet(i) = 0.5 * inv_covars_[i].LogPosDefDet();
410  // +0.5 because var is inverted
411  }
412 
413  // Undo variance inversion and multiplication of mean by
414  // inverse variance.
415  // Makes copy of means and vars for all components.
416  vector<SpMatrix<BaseFloat> > vars(num_comp);
417  Matrix<BaseFloat> means(num_comp, dim);
418  for (int32 i = 0; i < num_comp; i++) {
419  vars[i].Resize(dim);
420  vars[i].CopyFromSp(inv_covars_[i]);
421  vars[i].InvertDouble();
422  means.Row(i).AddSpVec(1.0, vars[i], means_invcovars_.Row(i), 0.0);
423 
424  // add means square to variances; get second-order stats
425  // (normalized by zero-order stats)
426  vars[i].AddVec2(1.0, means.Row(i));
427  }
428 
429  // compute change of likelihood for all combinations of components
430  for (int32 i = 0; i < preselect.size(); i++) {
431  int32 idx1 = preselect[i].first, idx2 = preselect[i].second;
432  KALDI_ASSERT(static_cast<size_t>(idx1) < static_cast<size_t>(num_comp));
433  KALDI_ASSERT(static_cast<size_t>(idx2) < static_cast<size_t>(num_comp));
434  BaseFloat w1 = weights_(idx1), w2 = weights_(idx2), w_sum = w1 + w2;
435  BaseFloat merged_logdet = MergedComponentsLogdet(w1, w2,
436  means.Row(idx1), means.Row(idx2),
437  vars[idx1], vars[idx2]),
438  delta_like = w_sum * merged_logdet - w1 * logdet(idx1) - w2 * logdet(idx2);
439  queue.push(std::make_pair(delta_like, preselect[i]));
440  }
441 
442  vector<int32> mapping(num_comp); // map of old index to where it
443  // got merged to.
444  for (int32 i = 0; i < num_comp; i++) mapping[i] = i;
445 
446  // Merge components with smallest impact on the loglike
447  int32 removed;
448  for (removed = 0;
449  removed < num_comp - target_components && !queue.empty(); ) {
450  QueueElem qelem = queue.top();
451  queue.pop();
452  BaseFloat delta_log_like_old = qelem.first;
453  int32 idx1 = qelem.second.first, idx2 = qelem.second.second;
454  // the next 3 lines are to handle when components got merged
455  // and moved to different indices, but we still want to consider
456  // merging their descendants. [descendant = current index where
457  // their data is.]
458  while (discarded_component[idx1]) idx1 = mapping[idx1];
459  while (discarded_component[idx2]) idx2 = mapping[idx2];
460  if (idx1 == idx2) continue; // can't merge something with itself.
461 
462  BaseFloat delta_log_like;
463  { // work out delta_log_like.
464  BaseFloat w1 = weights_(idx1), w2 = weights_(idx2), w_sum = w1 + w2;
465  BaseFloat merged_logdet = MergedComponentsLogdet(w1, w2,
466  means.Row(idx1), means.Row(idx2),
467  vars[idx1], vars[idx2]);
468  delta_log_like = w_sum * merged_logdet - w1 * logdet(idx1) - w2 * logdet(idx2);
469  }
470  if (ApproxEqual(delta_log_like, delta_log_like_old) ||
471  delta_log_like > delta_log_like_old) {
472  // if the log-like change did not change, or if it actually got smaller
473  // (closer to zero, more positive), then merge the components; otherwise
474  // put it back on the queue. Note: there is no test for "freshness" --
475  // we assume nothing is fresh.
476  BaseFloat w1 = weights_(idx1), w2 = weights_(idx2);
477  BaseFloat w_sum = w1 + w2;
478  // merge means
479  means.Row(idx1).AddVec(w2/w1, means.Row(idx2));
480  means.Row(idx1).Scale(w1/w_sum);
481  // merge vars
482  vars[idx1].AddSp(w2/w1, vars[idx2]);
483  vars[idx1].Scale(w1/w_sum);
484  // merge weights
485  weights_(idx1) = w_sum;
486 
487  // Update gmm for merged component
488  // copy second-order stats (normalized by zero-order stats)
489  inv_covars_[idx1].CopyFromSp(vars[idx1]);
490  // centralize
491  inv_covars_[idx1].AddVec2(-1.0, means.Row(idx1));
492  // invert
493  inv_covars_[idx1].InvertDouble();
494  // copy first-order stats (normalized by zero-order stats)
495  // and multiply by inv_vars
496  means_invcovars_.Row(idx1).AddSpVec(1.0, inv_covars_[idx1],
497  means.Row(idx1), 0.0);
498 
499  // Update logdet for merged component
500  logdet(idx1) = 0.5 * inv_covars_[idx1].LogPosDefDet();
501  // +0.5 because var is inverted
502 
503  // Label the removed component as discarded
504  discarded_component[idx2] = true;
505  KALDI_VLOG(2) << "Delta-log-like is " << delta_log_like << " (merging "
506  << idx1 << " and " << idx2 << ")";
507  ans += delta_log_like;
508  mapping[idx2] = idx1;
509  removed++;
510  } else {
511  QueueElem new_elem(delta_log_like, std::make_pair(idx1, idx2));
512  queue.push(new_elem); // push back more accurate elem.
513  }
514  }
515 
516  // Renumber the components.
517  int32 cur_idx = 0;
518  for (int32 i = 0; i < num_comp; i++) {
519  if (mapping[i] == i) { // This component is kept, not merged into another.
520  weights_(cur_idx) = weights_(i);
521  means_invcovars_.Row(cur_idx).CopyFromVec(means_invcovars_.Row(i));
522  inv_covars_[cur_idx].CopyFromSp(inv_covars_[i]);
523  cur_idx++;
524  }
525  }
526  KALDI_ASSERT(cur_idx + removed == num_comp);
527  gconsts_.Resize(cur_idx);
528  valid_gconsts_ = false;
529  weights_.Resize(cur_idx, kCopyData);
530  means_invcovars_.Resize(cur_idx, Dim(), kCopyData);
531  inv_covars_.resize(cur_idx);
532  ComputeGconsts();
533  return ans;
534 }
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: full-gmm.h:60
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
int32 ComputeGconsts()
Sets the gconsts.
Definition: full-gmm.cc:92
Vector< BaseFloat > weights_
weights (not log).
Definition: full-gmm.h:201
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
float BaseFloat
Definition: kaldi-types.h:29
bool valid_gconsts_
Recompute gconsts_ if false.
Definition: full-gmm.h:200
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
#define KALDI_WARN
Definition: kaldi-error.h:130
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
#define KALDI_VLOG(v)
Definition: kaldi-error.h:136
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).
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
Definition: kaldi-math.h:262
Vector< BaseFloat > gconsts_
Equals log(weight) - 0.5 * (log det(var) + mean'*inv(var)*mean)
Definition: full-gmm.h:199
int32 NumGauss ( ) const
inline

Returns the number of mixture components in the GMM.

Definition at line 58 of file full-gmm.h.

References VectorBase< Real >::Dim(), and FullGmm::weights_.

Referenced by IvectorExtractorStats::AccStatsForUtterance(), AccumFullGmm::AccumulateFromFull(), AmSgmm2::Check(), AmSgmm2::ComputeDerivedVars(), kaldi::ComputeFeatureNormalizingTransform(), FullGmm::ComputeGconsts(), Sgmm2Project::ComputeLdaStats(), FullGmm::CopyFromDiagGmm(), FullGmmNormal::CopyFromFullGmm(), FullGmm::CopyFromFullGmm(), DiagGmm::CopyFromFullGmm(), FullGmmNormal::CopyToFullGmm(), FullGmm::GaussianSelection(), AmSgmm2::GaussianSelection(), FullGmm::GetComponentMean(), FullGmm::GetCovarsAndMeans(), GetLogLikeTest(), FullGmm::GetMeans(), AmSgmm2::InitializeCovars(), AmSgmm2::InitializeMw(), AmSgmm2::InitializeNu(), FullGmm::Interpolate(), DiagGmm::Interpolate(), IvectorExtractor::IvectorExtractor(), FullGmm::LogLikelihoods(), main(), FullGmm::Merge(), kaldi::MergeFullGmm(), FullGmm::MergePreselect(), kaldi::MleFullGmmUpdate(), kaldi::MlObjective(), FullGmm::Perturb(), FullGmm::RemoveComponent(), AccumFullGmm::Resize(), FullGmm::SetInvCovars(), FullGmm::SetInvCovarsAndMeans(), FullGmm::SetInvCovarsAndMeansInvCovars(), FullGmm::SetMeans(), FullGmm::Split(), test_flags_driven_update(), test_io(), TestComponentAcc(), kaldi::TestIvectorExtraction(), UnitTestEstimateFullGmm(), UnitTestFullGmm(), and FullGmm::Write().

58 { return weights_.Dim(); }
Vector< BaseFloat > weights_
weights (not log).
Definition: full-gmm.h:201
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:62
const FullGmm& operator= ( const FullGmm other)
private
void Perturb ( float  perturb_factor)

Perturbs the component means with a random vector multiplied by the pertrub factor.

Definition at line 191 of file full-gmm.cc.

References TpMatrix< Real >::Cholesky(), FullGmm::ComputeGconsts(), FullGmm::Dim(), rnnlm::i, FullGmm::inv_covars_, kaldi::kTrans, FullGmm::means_invcovars_, VectorBase< Real >::MulTp(), FullGmm::NumGauss(), MatrixBase< Real >::Row(), and VectorBase< Real >::SetRandn().

191  {
192  int32 num_comps = NumGauss(),
193  dim = Dim();
194  Vector<BaseFloat> rand_vec(dim);
195  for (int32 i = 0; i < num_comps; i++) {
196  rand_vec.SetRandn();
197  TpMatrix<BaseFloat> invcovar_l(dim);
198  invcovar_l.Cholesky(inv_covars_[i]);
199  rand_vec.MulTp(invcovar_l, kTrans);
200  means_invcovars_.Row(i).AddVec(perturb_factor, rand_vec);
201  }
202  ComputeGconsts();
203 }
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: full-gmm.h:60
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
int32 ComputeGconsts()
Sets the gconsts.
Definition: full-gmm.cc:92
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
void Read ( std::istream &  is,
bool  binary 
)

Definition at line 813 of file full-gmm.cc.

References FullGmm::ComputeGconsts(), VectorBase< Real >::Dim(), kaldi::ExpectToken(), FullGmm::gconsts_, rnnlm::i, FullGmm::inv_covars_, KALDI_ERR, FullGmm::means_invcovars_, MatrixBase< Real >::NumCols(), Vector< Real >::Read(), Matrix< Real >::Read(), kaldi::ReadToken(), FullGmm::ResizeInvCovars(), and FullGmm::weights_.

Referenced by main(), kaldi::operator>>(), AmSgmm2::Read(), and UnitTestFullGmm().

813  {
814 // ExpectToken(in_stream, binary, "<FullGMMBegin>");
815  std::string token;
816  ReadToken(in_stream, binary, &token);
817  // <FullGMMBegin> is for compatibility. Will be deleted later
818  if (token != "<FullGMMBegin>" && token != "<FullGMM>")
819  KALDI_ERR << "Expected <FullGMM>, got " << token;
820 // ExpectToken(in_stream, binary, "<GCONSTS>");
821  ReadToken(in_stream, binary, &token);
822  if (token == "<GCONSTS>") { // The gconsts are optional.
823  gconsts_.Read(in_stream, binary);
824  ExpectToken(in_stream, binary, "<WEIGHTS>");
825  } else {
826  if (token != "<WEIGHTS>")
827  KALDI_ERR << "FullGmm::Read, expected <WEIGHTS> or <GCONSTS>, got "
828  << token;
829  }
830  weights_.Read(in_stream, binary);
831  ExpectToken(in_stream, binary, "<MEANS_INVCOVARS>");
832  means_invcovars_.Read(in_stream, binary);
833  ExpectToken(in_stream, binary, "<INV_COVARS>");
834  int32 ncomp = weights_.Dim(), dim = means_invcovars_.NumCols();
835  ResizeInvCovars(ncomp, dim);
836  for (int32 i = 0; i < ncomp; i++) {
837  inv_covars_[i].Read(in_stream, binary);
838  }
839 // ExpectToken(in_stream, binary, "<FullGMMEnd>");
840  ReadToken(in_stream, binary, &token);
841  // <FullGMMEnd> is for compatibility. Will be deleted later
842  if (token != "<FullGMMEnd>" && token != "</FullGMM>")
843  KALDI_ERR << "Expected </FullGMM>, got " << token;
844 
845  ComputeGconsts(); // safer option than trusting the read gconsts
846 }
int32 ComputeGconsts()
Sets the gconsts.
Definition: full-gmm.cc:92
Vector< BaseFloat > weights_
weights (not log).
Definition: full-gmm.h:201
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
void Read(std::istream &in, bool binary, bool add=false)
read from stream.
void ResizeInvCovars(int32 nMix, int32 dim)
Resizes arrays to this dim. Does not initialize data.
Definition: full-gmm.cc:51
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:188
#define KALDI_ERR
Definition: kaldi-error.h:127
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
void Read(std::istream &in, bool binary, bool add=false)
Read function using C++ streams.
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:62
Vector< BaseFloat > gconsts_
Equals log(weight) - 0.5 * (log det(var) + mean'*inv(var)*mean)
Definition: full-gmm.h:199
void RemoveComponent ( int32  gauss,
bool  renorm_weights 
)

Mutators for single component, supports float or double Removes single component from model.

Definition at line 731 of file full-gmm.cc.

References FullGmm::gconsts_, FullGmm::inv_covars_, KALDI_ASSERT, FullGmm::means_invcovars_, FullGmm::NumGauss(), Vector< Real >::RemoveElement(), Matrix< Real >::RemoveRow(), VectorBase< Real >::Scale(), VectorBase< Real >::Sum(), FullGmm::valid_gconsts_, and FullGmm::weights_.

Referenced by FullGmm::RemoveComponents().

731  {
732  KALDI_ASSERT(gauss < NumGauss());
733 
734  weights_.RemoveElement(gauss);
735  gconsts_.RemoveElement(gauss);
737  inv_covars_.erase(inv_covars_.begin() + gauss);
738  if (renorm_weights) {
739  BaseFloat sum_weights = weights_.Sum();
740  weights_.Scale(1.0/sum_weights);
741  valid_gconsts_ = false;
742  }
743 }
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
Vector< BaseFloat > weights_
weights (not log).
Definition: full-gmm.h:201
Real Sum() const
Returns sum of the elements.
float BaseFloat
Definition: kaldi-types.h:29
bool valid_gconsts_
Recompute gconsts_ if false.
Definition: full-gmm.h:200
void Scale(Real alpha)
Multiplies all elements by this constant.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void RemoveRow(MatrixIndexT i)
Remove a specified row.
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
void RemoveElement(MatrixIndexT i)
Remove one element and shifts later elements down.
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
Vector< BaseFloat > gconsts_
Equals log(weight) - 0.5 * (log det(var) + mean'*inv(var)*mean)
Definition: full-gmm.h:199
void RemoveComponents ( const std::vector< int32 > &  gauss,
bool  renorm_weights 
)

Removes multiple components from model; "gauss" must not have dups.

Definition at line 745 of file full-gmm.cc.

References rnnlm::i, kaldi::IsSortedAndUniq(), rnnlm::j, KALDI_ASSERT, and FullGmm::RemoveComponent().

Referenced by kaldi::MleFullGmmUpdate().

745  {
746  vector<int32> gauss(gauss_in);
747  std::sort(gauss.begin(), gauss.end());
749  // If efficiency is later an issue, will code this specially (unlikely,
750  // except for quite large GMMs).
751  for (size_t i = 0; i < gauss.size(); i++) {
752  RemoveComponent(gauss[i], renorm_weights);
753  for (size_t j = i + 1; j < gauss.size(); j++)
754  gauss[j]--;
755  }
756 }
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
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
bool IsSortedAndUniq(const std::vector< T > &vec)
Returns true if the vector is sorted and contains each element only once.
Definition: stl-utils.h:63
void Resize ( int32  nMix,
int32  dim 
)

Resizes arrays to this dim. Does not initialize data.

Definition at line 41 of file full-gmm.cc.

References VectorBase< Real >::Dim(), FullGmm::gconsts_, KALDI_ASSERT, FullGmm::means_invcovars_, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), Vector< Real >::Resize(), Matrix< Real >::Resize(), FullGmm::ResizeInvCovars(), and FullGmm::weights_.

Referenced by Sgmm2Project::ApplyProjection(), FullGmm::CopyFromDiagGmm(), FullGmm::CopyFromFullGmm(), FullGmm::FullGmm(), kaldi::unittest::InitRandFullGmm(), kaldi::MergeFullGmm(), TestComponentAcc(), UnitTestEstimateFullGmm(), and UnitTestFullGmm().

41  {
42  KALDI_ASSERT(nmix > 0 && dim > 0);
43  if (gconsts_.Dim() != nmix) gconsts_.Resize(nmix);
44  if (weights_.Dim() != nmix) weights_.Resize(nmix);
45  if (means_invcovars_.NumRows() != nmix
46  || means_invcovars_.NumCols() != dim)
47  means_invcovars_.Resize(nmix, dim);
48  ResizeInvCovars(nmix, dim);
49 }
Vector< BaseFloat > weights_
weights (not log).
Definition: full-gmm.h:201
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
void ResizeInvCovars(int32 nMix, int32 dim)
Resizes arrays to this dim. Does not initialize data.
Definition: full-gmm.cc:51
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
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< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:62
Vector< BaseFloat > gconsts_
Equals log(weight) - 0.5 * (log det(var) + mean'*inv(var)*mean)
Definition: full-gmm.h:199
void ResizeInvCovars ( int32  nMix,
int32  dim 
)
private

Resizes arrays to this dim. Does not initialize data.

Definition at line 51 of file full-gmm.cc.

References rnnlm::i, FullGmm::inv_covars_, and KALDI_ASSERT.

Referenced by FullGmm::Read(), FullGmm::Resize(), and FullGmm::Split().

51  {
52  KALDI_ASSERT(nmix > 0 && dim > 0);
53  if (inv_covars_.size() != static_cast<size_t>(nmix))
54  inv_covars_.resize(nmix);
55  for (int32 i = 0; i < nmix; i++) {
56  if (inv_covars_[i].NumRows() != dim) {
57  inv_covars_[i].Resize(dim);
58  inv_covars_[i].SetUnit();
59  // must be initialized to unit for case of calling SetMeans while having
60  // covars/invcovars that are not set yet (i.e. zero)
61  }
62  }
63 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
void SetInvCovars ( const std::vector< SpMatrix< Real > > &  v)

Set the (inverse) covariances and recompute means_invcovars_.

Definition at line 83 of file full-gmm-inl.h.

References VectorBase< Real >::AddSpVec(), SpMatrix< Real >::CopyFromSp(), VectorBase< Real >::CopyFromVec(), FullGmm::Dim(), rnnlm::i, FullGmm::inv_covars_, SpMatrix< Real >::InvertDouble(), KALDI_ASSERT, FullGmm::means_invcovars_, FullGmm::NumGauss(), MatrixBase< Real >::Row(), and FullGmm::valid_gconsts_.

Referenced by test_flags_driven_update(), and UnitTestFullGmm().

83  {
84  KALDI_ASSERT(inv_covars_.size() == v.size());
85  size_t num_comp = NumGauss();
86 
87  Vector<Real> orig_mean_times_invvar(Dim());
88  Vector<Real> orig_mean(Dim());
89  Vector<Real> new_mean_times_invvar(Dim());
90  SpMatrix<Real> covar(Dim());
91 
92  for (size_t i = 0; i < num_comp; i++) {
93  orig_mean_times_invvar.CopyFromVec(means_invcovars_.Row(i));
94  covar.CopyFromSp(inv_covars_[i]);
95  covar.InvertDouble();
96  orig_mean.AddSpVec(1.0, covar, orig_mean_times_invvar, 0.0);
97  new_mean_times_invvar.AddSpVec(1.0, v[i], orig_mean, 0.0);
98  // v[i] is already inverted covar
99  means_invcovars_.Row(i).CopyFromVec(new_mean_times_invvar);
100  inv_covars_[i].CopyFromSp(v[i]);
101  }
102  valid_gconsts_ = false;
103 }
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: full-gmm.h:60
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
bool valid_gconsts_
Recompute gconsts_ if false.
Definition: full-gmm.h:200
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
void SetInvCovarsAndMeans ( const std::vector< SpMatrix< Real > > &  invcovars,
const Matrix< Real > &  means 
)

Use SetInvCovarsAndMeans if updating both means and (inverse) covariances.

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

References VectorBase< Real >::AddSpVec(), FullGmm::Dim(), rnnlm::i, FullGmm::inv_covars_, KALDI_ASSERT, FullGmm::means_invcovars_, MatrixBase< Real >::NumCols(), FullGmm::NumGauss(), MatrixBase< Real >::NumRows(), MatrixBase< Real >::Row(), and FullGmm::valid_gconsts_.

Referenced by kaldi::unittest::InitRandFullGmm(), main(), UnitTestEstimateFullGmm(), and UnitTestFullGmm().

51  {
52  KALDI_ASSERT(means_invcovars_.NumRows() == means.NumRows()
53  && means_invcovars_.NumCols() == means.NumCols()
54  && inv_covars_.size() == invcovars.size());
55 
56  size_t num_comp = NumGauss();
57  for (size_t i = 0; i < num_comp; i++) {
58  inv_covars_[i].CopyFromSp(invcovars[i]);
59  Vector<Real> mean_times_inv(Dim());
60  mean_times_inv.AddSpVec(1.0, invcovars[i], means.Row(i), 0.0);
61  means_invcovars_.Row(i).CopyFromVec(mean_times_inv);
62  }
63  valid_gconsts_ = false;
64 }
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: full-gmm.h:60
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
bool valid_gconsts_
Recompute gconsts_ if false.
Definition: full-gmm.h:200
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
void SetInvCovarsAndMeansInvCovars ( const std::vector< SpMatrix< Real > > &  invcovars,
const Matrix< Real > &  means_invcovars 
)

Use this if setting both, in the class's native format.

Definition at line 67 of file full-gmm-inl.h.

References MatrixBase< Real >::CopyFromMat(), rnnlm::i, FullGmm::inv_covars_, KALDI_ASSERT, FullGmm::means_invcovars_, MatrixBase< Real >::NumCols(), FullGmm::NumGauss(), MatrixBase< Real >::NumRows(), and FullGmm::valid_gconsts_.

Referenced by kaldi::MergeFullGmm().

69  {
72  && inv_covars_.size() == invcovars.size());
73 
74  size_t num_comp = NumGauss();
75  for (size_t i = 0; i < num_comp; i++) {
76  inv_covars_[i].CopyFromSp(invcovars[i]);
77  }
79  valid_gconsts_ = false;
80 }
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
bool valid_gconsts_
Recompute gconsts_ if false.
Definition: full-gmm.h:200
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
const Matrix< BaseFloat > & means_invcovars() const
Definition: full-gmm.h:145
void SetMeans ( const Matrix< Real > &  m)

Use SetMeans to update only the Gaussian means (and not variances)

Definition at line 38 of file full-gmm-inl.h.

References rnnlm::i, FullGmm::inv_covars_, KALDI_ASSERT, FullGmm::means_invcovars_, MatrixBase< Real >::NumCols(), FullGmm::NumGauss(), MatrixBase< Real >::NumRows(), MatrixBase< Real >::Row(), and FullGmm::valid_gconsts_.

Referenced by test_flags_driven_update(), and UnitTestFullGmm().

38  {
39  KALDI_ASSERT(means_invcovars_.NumRows() == m.NumRows()
40  && means_invcovars_.NumCols() == m.NumCols());
41  size_t num_comp = NumGauss();
42  Matrix<BaseFloat> m_bf(m);
43  for (size_t i = 0; i < num_comp; i++) {
44  means_invcovars_.Row(i).AddSpVec(1.0, inv_covars_[i], m_bf.Row(i), 0.0);
45  }
46  valid_gconsts_ = false;
47 }
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
bool valid_gconsts_
Recompute gconsts_ if false.
Definition: full-gmm.h:200
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
void SetWeights ( const Vector< Real > &  w)

Mutators for both float or double.

Set mixure weights

Definition at line 31 of file full-gmm-inl.h.

References VectorBase< Real >::CopyFromVec(), VectorBase< Real >::Dim(), KALDI_ASSERT, FullGmm::valid_gconsts_, and FullGmm::weights_.

Referenced by kaldi::unittest::InitRandFullGmm(), main(), kaldi::MergeFullGmm(), test_flags_driven_update(), UnitTestEstimateFullGmm(), and UnitTestFullGmm().

31  {
32  KALDI_ASSERT(weights_.Dim() == w.Dim());
34  valid_gconsts_ = false;
35 }
Vector< BaseFloat > weights_
weights (not log).
Definition: full-gmm.h:201
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
bool valid_gconsts_
Recompute gconsts_ if false.
Definition: full-gmm.h:200
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:62
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 at line 132 of file full-gmm.cc.

References TpMatrix< Real >::Cholesky(), FullGmm::ComputeGconsts(), FullGmm::CopyFromFullGmm(), FullGmm::Dim(), FullGmm::FullGmm(), FullGmm::gconsts_, rnnlm::i, FullGmm::inv_covars_, KALDI_WARN, kaldi::kTrans, FullGmm::means_invcovars_, VectorBase< Real >::MulTp(), FullGmm::NumGauss(), VectorBase< Real >::Range(), MatrixBase< Real >::Range(), Vector< Real >::Resize(), Matrix< Real >::Resize(), FullGmm::ResizeInvCovars(), MatrixBase< Real >::Row(), VectorBase< Real >::SetRandn(), and FullGmm::weights_.

Referenced by main(), UnitTestEstimateFullGmm(), and UnitTestFullGmm().

133  {
134  if (target_components <= NumGauss() || NumGauss() == 0) {
135  KALDI_WARN << "Cannot split from " << NumGauss() << " to "
136  << target_components << " components";
137  return;
138  }
139  int32 current_components = NumGauss(), dim = Dim();
140  FullGmm *tmp = new FullGmm();
141  tmp->CopyFromFullGmm(*this); // so we have copies of matrices...
142  // First do the resize:
143  weights_.Resize(target_components);
144  weights_.Range(0, current_components).CopyFromVec(tmp->weights_);
145  means_invcovars_.Resize(target_components, dim);
146  means_invcovars_.Range(0, current_components, 0,
147  dim).CopyFromMat(tmp->means_invcovars_);
148  ResizeInvCovars(target_components, dim);
149  for (int32 mix = 0; mix < current_components; mix++) {
150  inv_covars_[mix].CopyFromSp(tmp->inv_covars_[mix]);
151  }
152  for (int32 mix = current_components; mix < target_components; mix++) {
153  inv_covars_[mix].SetZero();
154  }
155  gconsts_.Resize(target_components);
156 
157  delete tmp;
158 
159  // future work(arnab): Use a priority queue instead?
160  while (current_components < target_components) {
161  BaseFloat max_weight = weights_(0);
162  int32 max_idx = 0;
163  for (int32 i = 1; i < current_components; i++) {
164  if (weights_(i) > max_weight) {
165  max_weight = weights_(i);
166  max_idx = i;
167  }
168  }
169 
170  // remember history
171  if (history != NULL)
172  history->push_back(max_idx);
173 
174  weights_(max_idx) /= 2;
175  weights_(current_components) = weights_(max_idx);
176  Vector<BaseFloat> rand_vec(dim);
177  rand_vec.SetRandn();
178  TpMatrix<BaseFloat> invcovar_l(dim);
179  invcovar_l.Cholesky(inv_covars_[max_idx]);
180  rand_vec.MulTp(invcovar_l, kTrans);
181  inv_covars_[current_components].CopyFromSp(inv_covars_[max_idx]);
182  means_invcovars_.Row(current_components).CopyFromVec(means_invcovars_.Row(
183  max_idx));
184  means_invcovars_.Row(current_components).AddVec(perturb_factor, rand_vec);
185  means_invcovars_.Row(max_idx).AddVec(-perturb_factor, rand_vec);
186  current_components++;
187  }
188  ComputeGconsts();
189 }
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: full-gmm.h:60
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
int32 ComputeGconsts()
Sets the gconsts.
Definition: full-gmm.cc:92
Vector< BaseFloat > weights_
weights (not log).
Definition: full-gmm.h:201
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
FullGmm()
Empty constructor.
Definition: full-gmm.h:46
float BaseFloat
Definition: kaldi-types.h:29
void ResizeInvCovars(int32 nMix, int32 dim)
Resizes arrays to this dim. Does not initialize data.
Definition: full-gmm.cc:51
SubMatrix< Real > Range(const MatrixIndexT row_offset, const MatrixIndexT num_rows, const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Return a sub-part of matrix.
Definition: kaldi-matrix.h:196
#define KALDI_WARN
Definition: kaldi-error.h:130
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).
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
SubVector< Real > Range(const MatrixIndexT o, const MatrixIndexT l)
Returns a sub-vector of a vector (a range of elements).
Definition: kaldi-vector.h:92
Vector< BaseFloat > gconsts_
Equals log(weight) - 0.5 * (log det(var) + mean'*inv(var)*mean)
Definition: full-gmm.h:199
void Write ( std::ostream &  os,
bool  binary 
) const

Definition at line 758 of file full-gmm.cc.

References FullGmm::gconsts_, rnnlm::i, FullGmm::inv_covars_, KALDI_ERR, FullGmm::means_invcovars_, FullGmm::NumGauss(), FullGmm::valid_gconsts_, FullGmm::weights_, VectorBase< Real >::Write(), MatrixBase< Real >::Write(), and kaldi::WriteToken().

Referenced by main(), kaldi::operator<<(), UnitTestFullGmm(), and AmSgmm2::Write().

758  {
759  if (!valid_gconsts_)
760  KALDI_ERR << "Must call ComputeGconsts() before writing the model.";
761  WriteToken(out_stream, binary, "<FullGMM>");
762  if (!binary) out_stream << "\n";
763  WriteToken(out_stream, binary, "<GCONSTS>");
764  gconsts_.Write(out_stream, binary);
765  WriteToken(out_stream, binary, "<WEIGHTS>");
766  weights_.Write(out_stream, binary);
767  WriteToken(out_stream, binary, "<MEANS_INVCOVARS>");
768  means_invcovars_.Write(out_stream, binary);
769  WriteToken(out_stream, binary, "<INV_COVARS>");
770  for (int32 i = 0; i < NumGauss(); i++) {
771  inv_covars_[i].Write(out_stream, binary);
772  }
773  WriteToken(out_stream, binary, "</FullGMM>");
774  if (!binary) out_stream << "\n";
775 }
void Write(std::ostream &Out, bool binary) const
Writes to C++ stream (option to write in binary).
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
Vector< BaseFloat > weights_
weights (not log).
Definition: full-gmm.h:201
bool valid_gconsts_
Recompute gconsts_ if false.
Definition: full-gmm.h:200
#define KALDI_ERR
Definition: kaldi-error.h:127
void Write(std::ostream &out, bool binary) const
write to stream.
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
std::vector< SpMatrix< BaseFloat > > inv_covars_
Inverse covariances.
Definition: full-gmm.h:202
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
Vector< BaseFloat > gconsts_
Equals log(weight) - 0.5 * (log det(var) + mean'*inv(var)*mean)
Definition: full-gmm.h:199

Friends And Related Function Documentation

friend class FullGmmNormal
friend

this makes it a little easier to modify the internals

Definition at line 42 of file full-gmm.h.

Member Data Documentation


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