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

This does not work with multiple feature transforms. More...

#include <fmllr-diag-gmm.h>

Inheritance diagram for FmllrDiagGmmAccs:
Collaboration diagram for FmllrDiagGmmAccs:

Classes

struct  SingleFrameStats
 

Public Member Functions

 FmllrDiagGmmAccs (const FmllrOptions &opts=FmllrOptions())
 
 FmllrDiagGmmAccs (const FmllrDiagGmmAccs &other)
 
 FmllrDiagGmmAccs (int32 dim, const FmllrOptions &opts=FmllrOptions())
 
 FmllrDiagGmmAccs (const DiagGmm &gmm, const AccumFullGmm &fgmm_accs)
 
void Init (size_t dim)
 
void Read (std::istream &in, bool binary, bool add)
 
BaseFloat AccumulateForGmm (const DiagGmm &gmm, const VectorBase< BaseFloat > &data, BaseFloat weight)
 Accumulate stats for a single GMM in the model; returns log likelihood. More...
 
BaseFloat AccumulateForGmmPreselect (const DiagGmm &gmm, const std::vector< int32 > &gselect, const VectorBase< BaseFloat > &data, BaseFloat weight)
 This is like AccumulateForGmm but when you have gselect (Gaussian selection) information. More...
 
void AccumulateFromPosteriors (const DiagGmm &gmm, const VectorBase< BaseFloat > &data, const VectorBase< BaseFloat > &posteriors)
 Accumulate stats for a GMM, given supplied posteriors. More...
 
void AccumulateFromPosteriorsPreselect (const DiagGmm &gmm, const std::vector< int32 > &gselect, const VectorBase< BaseFloat > &data, const VectorBase< BaseFloat > &posteriors)
 Accumulate stats for a GMM, given supplied posteriors. More...
 
void Update (const FmllrOptions &opts, MatrixBase< BaseFloat > *fmllr_mat, BaseFloat *objf_impr, BaseFloat *count)
 Update. More...
 
- Public Member Functions inherited from AffineXformStats
 AffineXformStats ()
 
void Init (int32 dim, int32 num_gs)
 
int32 Dim () const
 
void SetZero ()
 
void CopyStats (const AffineXformStats &other)
 
void Add (const AffineXformStats &other)
 
void Write (std::ostream &out, bool binary) const
 
void Read (std::istream &in, bool binary, bool add)
 
 AffineXformStats (const AffineXformStats &other)
 

Private Member Functions

void CommitSingleFrameStats ()
 
void InitSingleFrameStats (const VectorBase< BaseFloat > &data)
 
bool DataHasChanged (const VectorBase< BaseFloat > &data) const
 

Private Attributes

SingleFrameStats single_frame_stats_
 
FmllrOptions opts_
 

Additional Inherited Members

- Public Attributes inherited from AffineXformStats
double beta_
 beta_ is the occupation count. More...
 
Matrix< double > K_
 K_ is the summed outer product of [mean times inverse variance] with [extended data], scaled by the occupation counts; dimension is dim by (dim+1) More...
 
std::vector< SpMatrix< double > > G_
 G_ is the outer product of extended-data, scaled by inverse variance, for each dimension. More...
 
int32 dim_
 dim_ is the feature dimension. More...
 

Detailed Description

This does not work with multiple feature transforms.

Definition at line 61 of file fmllr-diag-gmm.h.

Constructor & Destructor Documentation

FmllrDiagGmmAccs ( const FmllrOptions opts = FmllrOptions())
inline

Definition at line 66 of file fmllr-diag-gmm.h.

66  :
67  opts_(opts) { }
FmllrDiagGmmAccs ( const FmllrDiagGmmAccs other)
inlineexplicit

Definition at line 68 of file fmllr-diag-gmm.h.

68  :
69  AffineXformStats(other), single_frame_stats_(other.single_frame_stats_),
70  opts_(other.opts_) {}
SingleFrameStats single_frame_stats_
FmllrDiagGmmAccs ( int32  dim,
const FmllrOptions opts = FmllrOptions() 
)
inlineexplicit

Definition at line 71 of file fmllr-diag-gmm.h.

References FmllrDiagGmmAccs::Init().

71  :
72  opts_(opts) { Init(dim); }
void Init(size_t dim)
FmllrDiagGmmAccs ( const DiagGmm gmm,
const AccumFullGmm fgmm_accs 
)

Definition at line 67 of file fmllr-diag-gmm.cc.

References MatrixBase< Real >::AddVecVec(), AffineXformStats::beta_, MatrixBase< Real >::CopyColFromVec(), AccumFullGmm::covariance_accumulator(), rnnlm::d, DiagGmm::Dim(), AccumFullGmm::Dim(), AffineXformStats::G_, FmllrDiagGmmAccs::Init(), DiagGmm::inv_vars(), AffineXformStats::K_, KALDI_ASSERT, AccumFullGmm::mean_accumulator(), DiagGmm::means_invvars(), DiagGmm::NumGauss(), AccumFullGmm::NumGauss(), AccumFullGmm::occupancy(), VectorBase< Real >::Range(), MatrixBase< Real >::Range(), and MatrixBase< Real >::Row().

68  :
69  single_frame_stats_(gmm.Dim()), opts_(FmllrOptions()) {
70  KALDI_ASSERT(gmm.NumGauss() == fgmm_accs.NumGauss()
71  && gmm.Dim() == fgmm_accs.Dim());
72  Init(gmm.Dim());
73  int32 dim = gmm.Dim(), num_gauss = gmm.NumGauss();
74  for (int32 g = 0; g < num_gauss; g++) {
75  double this_occ = fgmm_accs.occupancy()(g);
76  if (this_occ == 0) continue;
77  SubVector<BaseFloat> this_mean_invvar(gmm.means_invvars(), g);
78  SubVector<BaseFloat> this_invvar(gmm.inv_vars(), g);
79  SubVector<double> this_mean_acc(fgmm_accs.mean_accumulator(), g);
80  Vector<double> this_mean_invvar_dbl(this_mean_invvar);
81  Vector<double> this_extended_mean_acc(dim+1);
82  this_extended_mean_acc.Range(0, dim).CopyFromVec(this_mean_acc);
83  this_extended_mean_acc(dim) = this_occ; // acc of x^+
84  Matrix<double> this_cov_acc(fgmm_accs.covariance_accumulator()[g]); // copy to
85  // regular Matrix.
86  Matrix<double> this_extended_cov_acc(dim+1, dim+1); // make as if accumulated
87  // using x^+, not x.
88  this_extended_cov_acc.Range(0, dim, 0, dim).CopyFromMat(this_cov_acc);
89  this_extended_cov_acc.Row(dim).CopyFromVec(this_extended_mean_acc);
90  this_extended_cov_acc.CopyColFromVec(this_extended_mean_acc, dim); // since
91  // there is no Col() function, use a member-function of the matrix class.
92  SpMatrix<double> this_extended_cov_acc_sp(this_extended_cov_acc);
93  beta_ += this_occ;
94  K_.AddVecVec(1.0, this_mean_invvar_dbl, this_extended_mean_acc);
95  for (int32 d = 0; d < dim; d++)
96  G_[d].AddSp(this_invvar(d), this_extended_cov_acc_sp);
97  }
98 }
Matrix< double > K_
K_ is the summed outer product of [mean times inverse variance] with [extended data], scaled by the occupation counts; dimension is dim by (dim+1)
SingleFrameStats single_frame_stats_
std::vector< SpMatrix< double > > G_
G_ is the outer product of extended-data, scaled by inverse variance, for each dimension.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddVecVec(const Real alpha, const VectorBase< OtherReal > &a, const VectorBase< OtherReal > &b)
*this += alpha * a * b^T
void Init(size_t dim)
double beta_
beta_ is the occupation count.

Member Function Documentation

BaseFloat AccumulateForGmm ( const DiagGmm gmm,
const VectorBase< BaseFloat > &  data,
BaseFloat  weight 
)

Accumulate stats for a single GMM in the model; returns log likelihood.

Definition at line 101 of file fmllr-diag-gmm.cc.

References FmllrDiagGmmAccs::AccumulateFromPosteriors(), DiagGmm::ComponentPosteriors(), DiagGmm::NumGauss(), and VectorBase< Real >::Scale().

Referenced by kaldi::AccumulateForUtterance(), main(), kaldi::UnitTestFmllrDiagGmm(), kaldi::UnitTestFmllrDiagGmmDiagonal(), and kaldi::UnitTestFmllrDiagGmmOffset().

103  {
104  int32 num_comp = pdf.NumGauss();
105  Vector<BaseFloat> posterior(num_comp);
106  BaseFloat loglike;
107 
108  loglike = pdf.ComponentPosteriors(data, &posterior);
109  posterior.Scale(weight);
110  AccumulateFromPosteriors(pdf, data, posterior);
111  return loglike;
112 }
void AccumulateFromPosteriors(const DiagGmm &gmm, const VectorBase< BaseFloat > &data, const VectorBase< BaseFloat > &posteriors)
Accumulate stats for a GMM, given supplied posteriors.
float BaseFloat
Definition: kaldi-types.h:29
BaseFloat AccumulateForGmmPreselect ( const DiagGmm gmm,
const std::vector< int32 > &  gselect,
const VectorBase< BaseFloat > &  data,
BaseFloat  weight 
)

This is like AccumulateForGmm but when you have gselect (Gaussian selection) information.

Definition at line 114 of file fmllr-diag-gmm.cc.

References FmllrDiagGmmAccs::AccumulateFromPosteriorsPreselect(), KALDI_ASSERT, and DiagGmm::LogLikelihoodsPreselect().

118  {
119  KALDI_ASSERT(!gselect.empty() && "Empty gselect information");
120  Vector<BaseFloat> loglikes;
121  pdf.LogLikelihoodsPreselect(data, gselect, &loglikes);
122 
123  BaseFloat loglike = loglikes.ApplySoftMax(); // they are now posteriors.
124  loglikes.Scale(weight);
125  AccumulateFromPosteriorsPreselect(pdf, gselect, data, loglikes);
126  return loglike;
127 }
float BaseFloat
Definition: kaldi-types.h:29
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AccumulateFromPosteriorsPreselect(const DiagGmm &gmm, const std::vector< int32 > &gselect, const VectorBase< BaseFloat > &data, const VectorBase< BaseFloat > &posteriors)
Accumulate stats for a GMM, given supplied posteriors.
void AccumulateFromPosteriors ( const DiagGmm gmm,
const VectorBase< BaseFloat > &  data,
const VectorBase< BaseFloat > &  posteriors 
)

Accumulate stats for a GMM, given supplied posteriors.

Definition at line 30 of file fmllr-diag-gmm.cc.

References FmllrDiagGmmAccs::SingleFrameStats::a, VectorBase< Real >::AddMatVec(), FmllrDiagGmmAccs::SingleFrameStats::b, FmllrDiagGmmAccs::CommitSingleFrameStats(), FmllrDiagGmmAccs::SingleFrameStats::count, FmllrDiagGmmAccs::DataHasChanged(), FmllrDiagGmmAccs::InitSingleFrameStats(), DiagGmm::inv_vars(), kaldi::kTrans, DiagGmm::means_invvars(), FmllrDiagGmmAccs::single_frame_stats_, and VectorBase< Real >::Sum().

Referenced by FmllrDiagGmmAccs::AccumulateForGmm(), kaldi::AccumulateForUtterance(), kaldi::UnitTestFmllrDiagGmm(), kaldi::UnitTestFmllrDiagGmmDiagonal(), and kaldi::UnitTestFmllrDiagGmmOffset().

33  {
34 
35  if (this->DataHasChanged(data)) {
38  }
39  SingleFrameStats &stats = this->single_frame_stats_;
40  stats.count += posterior.Sum();
41  stats.a.AddMatVec(1.0, pdf.means_invvars(), kTrans, posterior, 1.0);
42  stats.b.AddMatVec(1.0, pdf.inv_vars(), kTrans, posterior, 1.0);
43 }
SingleFrameStats single_frame_stats_
void InitSingleFrameStats(const VectorBase< BaseFloat > &data)
bool DataHasChanged(const VectorBase< BaseFloat > &data) const
void AccumulateFromPosteriorsPreselect ( const DiagGmm gmm,
const std::vector< int32 > &  gselect,
const VectorBase< BaseFloat > &  data,
const VectorBase< BaseFloat > &  posteriors 
)

Accumulate stats for a GMM, given supplied posteriors.

The "posteriors" vector should be have the same size as "gselect".

Definition at line 45 of file fmllr-diag-gmm.cc.

References FmllrDiagGmmAccs::SingleFrameStats::a, VectorBase< Real >::AddVec(), FmllrDiagGmmAccs::SingleFrameStats::b, FmllrDiagGmmAccs::CommitSingleFrameStats(), FmllrDiagGmmAccs::SingleFrameStats::count, FmllrDiagGmmAccs::DataHasChanged(), VectorBase< Real >::Dim(), rnnlm::i, FmllrDiagGmmAccs::InitSingleFrameStats(), DiagGmm::inv_vars(), KALDI_ASSERT, DiagGmm::means_invvars(), MatrixBase< Real >::Row(), FmllrDiagGmmAccs::single_frame_stats_, and VectorBase< Real >::Sum().

Referenced by FmllrDiagGmmAccs::AccumulateForGmmPreselect(), and kaldi::AccumulateForUtterance().

49  {
50 
51  if (this->DataHasChanged(data)) {
54  }
55  SingleFrameStats &stats = this->single_frame_stats_;
56  stats.count += posterior.Sum();
57 
58  const Matrix<BaseFloat> &means_invvars = pdf.means_invvars(),
59  &inv_vars = pdf.inv_vars();
60  KALDI_ASSERT(static_cast<int32>(gselect.size()) == posterior.Dim());
61  for (size_t i = 0; i < gselect.size(); i++) {
62  stats.a.AddVec(posterior(i), means_invvars.Row(gselect[i]));
63  stats.b.AddVec(posterior(i), inv_vars.Row(gselect[i]));
64  }
65 }
SingleFrameStats single_frame_stats_
void InitSingleFrameStats(const VectorBase< BaseFloat > &data)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
bool DataHasChanged(const VectorBase< BaseFloat > &data) const
void CommitSingleFrameStats ( )
private

Definition at line 562 of file fmllr-diag-gmm.cc.

References FmllrDiagGmmAccs::SingleFrameStats::a, SpMatrix< Real >::AddVec2(), MatrixBase< Real >::AddVecVec(), FmllrDiagGmmAccs::SingleFrameStats::b, AffineXformStats::beta_, FmllrDiagGmmAccs::SingleFrameStats::count, AffineXformStats::Dim(), AffineXformStats::G_, rnnlm::i, AffineXformStats::K_, KALDI_ASSERT, FmllrDiagGmmAccs::opts_, VectorBase< Real >::Range(), VectorBase< Real >::SetZero(), FmllrDiagGmmAccs::single_frame_stats_, FmllrOptions::update_type, and FmllrDiagGmmAccs::SingleFrameStats::x.

Referenced by FmllrDiagGmmAccs::AccumulateFromPosteriors(), FmllrDiagGmmAccs::AccumulateFromPosteriorsPreselect(), and FmllrDiagGmmAccs::Update().

562  {
563  // Commit the stats for this from (in SingleFrameStats).
564  int32 dim = Dim();
565  SingleFrameStats &stats = single_frame_stats_;
566  if (stats.count == 0.0) return;
567 
568  Vector<double> xplus(dim+1);
569  xplus.Range(0, dim).CopyFromVec(stats.x);
570  xplus(dim) = 1.0;
571 
572  this->beta_ += stats.count;
573  this->K_.AddVecVec(1.0, Vector<double>(stats.a), xplus);
574 
575 
576  if (opts_.update_type == "full") {
577  SpMatrix<double> scatter(dim+1);
578  scatter.AddVec2(1.0, xplus);
579 
580  KALDI_ASSERT(static_cast<size_t>(dim) == this->G_.size());
581  for (int32 i = 0; i < dim; i++)
582  this->G_[i].AddSp(stats.b(i), scatter);
583  } else {
584  // We only need some elements of these stats, so just update those elements.
585  for (int32 i = 0; i < dim; i++) {
586  BaseFloat scale = stats.b(i), x_i = xplus(i);
587  this->G_[i](i, i) += scale * x_i * x_i;
588  this->G_[i](dim, i) += scale * 1.0 * x_i;
589  this->G_[i](dim, dim) += scale * 1.0 * 1.0;
590  }
591  }
592 
593  stats.count = 0.0;
594  stats.a.SetZero();
595  stats.b.SetZero();
596 }
Matrix< double > K_
K_ is the summed outer product of [mean times inverse variance] with [extended data], scaled by the occupation counts; dimension is dim by (dim+1)
SingleFrameStats single_frame_stats_
std::string update_type
"full", "diag", "offset", "none"
float BaseFloat
Definition: kaldi-types.h:29
std::vector< SpMatrix< double > > G_
G_ is the outer product of extended-data, scaled by inverse variance, for each dimension.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddVecVec(const Real alpha, const VectorBase< OtherReal > &a, const VectorBase< OtherReal > &b)
*this += alpha * a * b^T
double beta_
beta_ is the occupation count.
bool DataHasChanged ( const VectorBase< BaseFloat > &  data) const
private

Definition at line 542 of file fmllr-diag-gmm.cc.

References VectorBase< Real >::ApproxEqual(), AffineXformStats::Dim(), VectorBase< Real >::Dim(), KALDI_ASSERT, FmllrDiagGmmAccs::single_frame_stats_, and FmllrDiagGmmAccs::SingleFrameStats::x.

Referenced by FmllrDiagGmmAccs::AccumulateFromPosteriors(), and FmllrDiagGmmAccs::AccumulateFromPosteriorsPreselect().

542  {
543  KALDI_ASSERT(data.Dim() == this->Dim());
544  return !data.ApproxEqual(single_frame_stats_.x, 0.0);
545 }
SingleFrameStats single_frame_stats_
bool ApproxEqual(const VectorBase< Real > &other, float tol=0.01) const
Returns true if ((*this)-other).Norm(2.0) <= tol * (*this).Norm(2.0).
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:59
void Init ( size_t  dim)
inline
void InitSingleFrameStats ( const VectorBase< BaseFloat > &  data)
private

Definition at line 554 of file fmllr-diag-gmm.cc.

References FmllrDiagGmmAccs::SingleFrameStats::a, FmllrDiagGmmAccs::SingleFrameStats::b, VectorBase< Real >::CopyFromVec(), FmllrDiagGmmAccs::SingleFrameStats::count, VectorBase< Real >::SetZero(), FmllrDiagGmmAccs::single_frame_stats_, and FmllrDiagGmmAccs::SingleFrameStats::x.

Referenced by FmllrDiagGmmAccs::AccumulateFromPosteriors(), and FmllrDiagGmmAccs::AccumulateFromPosteriorsPreselect().

554  {
555  SingleFrameStats &stats = single_frame_stats_;
556  stats.x.CopyFromVec(data);
557  stats.count = 0.0;
558  stats.a.SetZero();
559  stats.b.SetZero();
560 }
SingleFrameStats single_frame_stats_
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
void Read ( std::istream &  in,
bool  binary,
bool  add 
)
inline
void Update ( const FmllrOptions opts,
MatrixBase< BaseFloat > *  fmllr_mat,
BaseFloat objf_impr,
BaseFloat count 
)

Update.

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

References AffineXformStats::beta_, FmllrDiagGmmAccs::CommitSingleFrameStats(), kaldi::ComputeFmllrMatrixDiagGmmDiagonal(), kaldi::ComputeFmllrMatrixDiagGmmFull(), kaldi::ComputeFmllrMatrixDiagGmmOffset(), MatrixBase< Real >::CopyFromMat(), MatrixBase< Real >::IsZero(), KALDI_ASSERT, KALDI_ERR, KALDI_WARN, FmllrOptions::min_count, FmllrOptions::num_iters, and FmllrOptions::update_type.

Referenced by main(), kaldi::UnitTestFmllrDiagGmm(), kaldi::UnitTestFmllrDiagGmmDiagonal(), and kaldi::UnitTestFmllrDiagGmmOffset().

134  {
135  KALDI_ASSERT(fmllr_mat != NULL);
137  if (fmllr_mat->IsZero())
138  KALDI_ERR << "You must initialize the fMLLR matrix to a non-singular value "
139  "(so we can report objective function changes); e.g. call SetUnit()";
140  if (opts.update_type == "full" && this->opts_.update_type != "full") {
141  KALDI_ERR << "You are requesting a full-fMLLR update but you accumulated "
142  << "stats for more limited update type.";
143  }
144  if (beta_ > opts.min_count) {
145  Matrix<BaseFloat> tmp_old(*fmllr_mat), tmp_new(*fmllr_mat);
146  BaseFloat objf_change;
147  if (opts.update_type == "full")
148  objf_change = ComputeFmllrMatrixDiagGmmFull(tmp_old, *this, opts.num_iters, &tmp_new);
149  else if (opts.update_type == "diag")
150  objf_change = ComputeFmllrMatrixDiagGmmDiagonal(tmp_old, *this, &tmp_new);
151  else if (opts.update_type == "offset")
152  objf_change = ComputeFmllrMatrixDiagGmmOffset(tmp_old, *this, &tmp_new);
153  else if (opts.update_type == "none")
154  objf_change = 0.0;
155  else
156  KALDI_ERR << "Unknown fMLLR update type " << opts.update_type
157  << ", fmllr-update-type must be one of \"full\"|\"diag\"|\"offset\"|\"none\"";
158  fmllr_mat->CopyFromMat(tmp_new);
159  if (objf_impr) *objf_impr = objf_change;
160  if (count) *count = beta_;
161  } else { // Not changing matrix.
162  KALDI_WARN << "Not updating fMLLR since below min-count: count is " << beta_;
163  if (objf_impr) *objf_impr = 0.0;
164  if (count) *count = beta_;
165  }
166 }
BaseFloat ComputeFmllrMatrixDiagGmmFull(const MatrixBase< BaseFloat > &in_xform, const AffineXformStats &stats, int32 num_iters, MatrixBase< BaseFloat > *out_xform)
Updates the FMLLR matrix using Mark Gales' row-by-row update.
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
BaseFloat ComputeFmllrMatrixDiagGmmOffset(const MatrixBase< BaseFloat > &in_xform, const AffineXformStats &stats, MatrixBase< BaseFloat > *out_xform)
This does offset-only fMLLR, i.e. it only estimates an offset.
const size_t count
float BaseFloat
Definition: kaldi-types.h:29
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_WARN
Definition: kaldi-error.h:130
BaseFloat ComputeFmllrMatrixDiagGmmDiagonal(const MatrixBase< BaseFloat > &in_xform, const AffineXformStats &stats, MatrixBase< BaseFloat > *out_xform)
This does diagonal fMLLR (i.e.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
bool IsZero(Real cutoff=1.0e-05) const
Returns true if matrix is all zeros.
double beta_
beta_ is the occupation count.

Member Data Documentation

FmllrOptions opts_
private

Definition at line 155 of file fmllr-diag-gmm.h.

Referenced by FmllrDiagGmmAccs::CommitSingleFrameStats().


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