FmllrSgmm2Accs Class Reference

Class for computing the accumulators needed for the maximum-likelihood estimate of FMLLR transforms for a subspace GMM acoustic model. More...

#include <fmllr-sgmm2.h>

Collaboration diagram for FmllrSgmm2Accs:

Public Member Functions

 FmllrSgmm2Accs ()
 
 ~FmllrSgmm2Accs ()
 
void Init (int32 dim, int32 num_gaussians)
 
void SetZero ()
 
void Write (std::ostream &out_stream, bool binary) const
 
void Read (std::istream &in_stream, bool binary, bool add)
 
BaseFloat Accumulate (const AmSgmm2 &sgmm, const VectorBase< BaseFloat > &data, const Sgmm2PerFrameDerivedVars &frame_vars, int32 state_index, BaseFloat weight, Sgmm2PerSpkDerivedVars *spk)
 Accumulation routine that computes the Gaussian posteriors and calls the AccumulateFromPosteriors function with the computed posteriors. More...
 
void AccumulateFromPosteriors (const AmSgmm2 &sgmm, const Sgmm2PerSpkDerivedVars &spk, const VectorBase< BaseFloat > &data, const std::vector< int32 > &gauss_select, const Matrix< BaseFloat > &posteriors, int32 state_index)
 
void AccumulateForFmllrSubspace (const AmSgmm2 &sgmm, const Sgmm2FmllrGlobalParams &fmllr_globals, SpMatrix< double > *grad_scatter)
 
BaseFloat FmllrObjGradient (const AmSgmm2 &sgmm, const Matrix< BaseFloat > &xform, Matrix< BaseFloat > *grad_out, Matrix< BaseFloat > *G_out) const
 
bool Update (const AmSgmm2 &model, const Sgmm2FmllrGlobalParams &fmllr_globals, const Sgmm2FmllrConfig &opts, Matrix< BaseFloat > *out_xform, BaseFloat *frame_count, BaseFloat *auxf_improv) const
 Computes the FMLLR transform from the accumulated stats, using the pre-transforms in fmllr_globals. More...
 
int32 Dim () const
 Accessors. More...
 
const AffineXformStatsstats () const
 

Private Member Functions

 KALDI_DISALLOW_COPY_AND_ASSIGN (FmllrSgmm2Accs)
 

Private Attributes

AffineXformStats stats_
 Accumulated stats. More...
 
int32 dim_
 Dimension of feature vectors. More...
 

Detailed Description

Class for computing the accumulators needed for the maximum-likelihood estimate of FMLLR transforms for a subspace GMM acoustic model.

Definition at line 122 of file fmllr-sgmm2.h.

Constructor & Destructor Documentation

◆ FmllrSgmm2Accs()

FmllrSgmm2Accs ( )
inline

Definition at line 124 of file fmllr-sgmm2.h.

124 : dim_(-1) {}
int32 dim_
Dimension of feature vectors.
Definition: fmllr-sgmm2.h:176

◆ ~FmllrSgmm2Accs()

~FmllrSgmm2Accs ( )
inline

Definition at line 125 of file fmllr-sgmm2.h.

125 {}

Member Function Documentation

◆ Accumulate()

BaseFloat Accumulate ( const AmSgmm2 sgmm,
const VectorBase< BaseFloat > &  data,
const Sgmm2PerFrameDerivedVars frame_vars,
int32  state_index,
BaseFloat  weight,
Sgmm2PerSpkDerivedVars spk 
)

Accumulation routine that computes the Gaussian posteriors and calls the AccumulateFromPosteriors function with the computed posteriors.

The 'data' argument is not FMLLR-transformed and is needed in addition to the the 'frame_vars' since the latter only contains a copy of the transformed feature vector.

Definition at line 156 of file fmllr-sgmm2.cc.

References AmSgmm2::ComponentPosteriors(), Sgmm2PerFrameDerivedVars::gselect, and MatrixBase< Real >::Scale().

Referenced by TestSgmm2FmllrAccsIO(), and TestSgmm2FmllrSubspace().

160  {
161  // Calulate Gaussian posteriors and collect statistics
162  Matrix<BaseFloat> posteriors;
163  BaseFloat log_like = model.ComponentPosteriors(frame_vars, pdf_index,
164  spk, &posteriors);
165  posteriors.Scale(weight);
166  AccumulateFromPosteriors(model, *spk, data, frame_vars.gselect, posteriors,
167  pdf_index);
168  return log_like;
169 }
void AccumulateFromPosteriors(const AmSgmm2 &sgmm, const Sgmm2PerSpkDerivedVars &spk, const VectorBase< BaseFloat > &data, const std::vector< int32 > &gauss_select, const Matrix< BaseFloat > &posteriors, int32 state_index)
Definition: fmllr-sgmm2.cc:171
float BaseFloat
Definition: kaldi-types.h:29

◆ AccumulateForFmllrSubspace()

void AccumulateForFmllrSubspace ( const AmSgmm2 sgmm,
const Sgmm2FmllrGlobalParams fmllr_globals,
SpMatrix< double > *  grad_scatter 
)

Definition at line 205 of file fmllr-sgmm2.cc.

References SpMatrix< Real >::AddVec2(), kaldi::ApplyHessianXformToGradient(), kaldi::ApplyPreXformToGradient(), VectorBase< Real >::CopyRowsFromMat(), AmSgmm2::FeatureDim(), KALDI_LOG, KALDI_WARN, kaldi::kSetZero, kaldi::kUndefined, VectorBase< Real >::Scale(), and MatrixBase< Real >::SetUnit().

Referenced by TestSgmm2FmllrSubspace().

206  {
207  if (stats_.beta_ <= 0.0) {
208  KALDI_WARN << "Not committing any stats since no stats accumulated.";
209  return;
210  }
211  int32 dim = sgmm.FeatureDim();
212  Matrix<BaseFloat> xform(dim, dim + 1, kUndefined);
213  xform.SetUnit();
214  Matrix<BaseFloat> grad(dim, dim + 1, kSetZero);
215  this->FmllrObjGradient(sgmm, xform, &grad, NULL);
216  Matrix<BaseFloat> pre_xformed_grad(dim, dim + 1, kSetZero);
217  ApplyPreXformToGradient(globals, grad, &pre_xformed_grad);
218  Matrix<BaseFloat> hess_xformed_grad(dim, dim + 1, kSetZero);
219  ApplyHessianXformToGradient(globals, pre_xformed_grad, &hess_xformed_grad);
220  Vector<double> grad_vec(dim * (dim + 1));
221  grad_vec.CopyRowsFromMat(hess_xformed_grad);
222  grad_vec.Scale(1 / std::sqrt(stats_.beta_));
223  grad_scatter->AddVec2(1.0, grad_vec);
224  KALDI_LOG << "Frame counts for when committing fMLLR subspace stats are "
225  << stats_.beta_;
226 }
BaseFloat FmllrObjGradient(const AmSgmm2 &sgmm, const Matrix< BaseFloat > &xform, Matrix< BaseFloat > *grad_out, Matrix< BaseFloat > *G_out) const
Definition: fmllr-sgmm2.cc:229
static void ApplyPreXformToGradient(const Sgmm2FmllrGlobalParams &globals, const Matrix< BaseFloat > &gradient_in, Matrix< BaseFloat > *gradient_out)
Definition: fmllr-sgmm2.cc:31
kaldi::int32 int32
void AddVec2(const Real alpha, const VectorBase< OtherReal > &v)
rank-one update, this <– this + alpha v v&#39;
Definition: sp-matrix.cc:946
#define KALDI_WARN
Definition: kaldi-error.h:150
static void ApplyHessianXformToGradient(const Sgmm2FmllrGlobalParams &globals, const Matrix< BaseFloat > &gradient_in, Matrix< BaseFloat > *gradient_out)
Definition: fmllr-sgmm2.cc:59
#define KALDI_LOG
Definition: kaldi-error.h:153
double beta_
beta_ is the occupation count.
AffineXformStats stats_
Accumulated stats.
Definition: fmllr-sgmm2.h:175

◆ AccumulateFromPosteriors()

void AccumulateFromPosteriors ( const AmSgmm2 sgmm,
const Sgmm2PerSpkDerivedVars spk,
const VectorBase< BaseFloat > &  data,
const std::vector< int32 > &  gauss_select,
const Matrix< BaseFloat > &  posteriors,
int32  state_index 
)

Definition at line 171 of file fmllr-sgmm2.cc.

References SpMatrix< Real >::AddVec2(), AmSgmm2::GetVarScaledSubstateSpeakerMean(), rnnlm::i, kaldi::kSetZero, AmSgmm2::NumSubstatesForGroup(), AmSgmm2::Pdf2Group(), and VectorBase< Real >::Range().

Referenced by kaldi::AccumulateForUtterance().

177  {
178  Vector<double> var_scaled_mean(dim_), extended_data(dim_+1);
179  extended_data.Range(0, dim_).CopyFromVec(data);
180  extended_data(dim_) = 1.0;
181  SpMatrix<double> scatter(dim_+1, kSetZero);
182  scatter.AddVec2(1.0, extended_data);
183  int32 j1 = model.Pdf2Group(j2);
184  for (int32 ki = 0, ki_max = gselect.size(); ki < ki_max; ki++) {
185  int32 i = gselect[ki];
186 
187  for (int32 m = 0; m < model.NumSubstatesForGroup(j1); m++) {
188  // posterior gamma_{jkmi}(t) eq.(39)
189  BaseFloat gammat_jmi = posteriors(ki, m);
190 
191  // Accumulate statistics for non-zero gaussian posterior
192  if (gammat_jmi > 0.0) {
193  stats_.beta_ += gammat_jmi;
194  model.GetVarScaledSubstateSpeakerMean(j1, m, i, spk,
195  &var_scaled_mean);
196  // Eq. (52): K += \gamma_{jmi} \Sigma_{i}^{-1} \mu_{jmi}^{(s)} x^{+T}
197  stats_.K_.AddVecVec(gammat_jmi, var_scaled_mean, extended_data);
198  // Eq. (53): G_{i} += \gamma_{jmi} x^{+} x^{+T}
199  stats_.G_[i].AddSp(gammat_jmi, scatter);
200  } // non-zero posteriors
201  } // loop over substates
202  } // loop over selected Gaussians
203 }
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)
kaldi::int32 int32
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.
void AddVecVec(const Real alpha, const VectorBase< OtherReal > &a, const VectorBase< OtherReal > &b)
*this += alpha * a * b^T
int32 dim_
Dimension of feature vectors.
Definition: fmllr-sgmm2.h:176
double beta_
beta_ is the occupation count.
AffineXformStats stats_
Accumulated stats.
Definition: fmllr-sgmm2.h:175

◆ Dim()

int32 Dim ( ) const
inline

Accessors.

Definition at line 171 of file fmllr-sgmm2.h.

171 { return dim_; }
int32 dim_
Dimension of feature vectors.
Definition: fmllr-sgmm2.h:176

◆ FmllrObjGradient()

BaseFloat FmllrObjGradient ( const AmSgmm2 sgmm,
const Matrix< BaseFloat > &  xform,
Matrix< BaseFloat > *  grad_out,
Matrix< BaseFloat > *  G_out 
) const

Definition at line 229 of file fmllr-sgmm2.cc.

References MatrixBase< Real >::AddMat(), MatrixBase< Real >::AddSpMat(), MatrixBase< Real >::CopyFromMat(), AmSgmm2::FeatureDim(), AmSgmm2::GetInvCovars(), rnnlm::i, KALDI_ASSERT, kaldi::kNoTrans, kaldi::kSetZero, kaldi::kTrans, MatrixBase< Real >::LogDet(), AmSgmm2::NumGauss(), MatrixBase< Real >::Range(), MatrixBase< Real >::Scale(), and kaldi::TraceMatMat().

232  {
233  int32 dim = sgmm.FeatureDim(),
234  num_gauss = sgmm.NumGauss();
235  KALDI_ASSERT(stats_.G_.size() == static_cast<size_t>(num_gauss));
236  Matrix<double> xform_d(xform);
237  SubMatrix<double> A(xform_d, 0, dim, 0, dim);
238  Matrix<double> xform_g(dim, dim + 1), total_g(dim, dim + 1);
239  SpMatrix<double> inv_covar(dim);
240  double obj = stats_.beta_ * A.LogDet() +
241  TraceMatMat(xform_d, stats_.K_, kTrans);
242  for (int32 i = 0; i < num_gauss; i++) {
243  sgmm.GetInvCovars(i, &inv_covar);
244  xform_g.AddMatSp(1.0, xform_d, kNoTrans, stats_.G_[i], 0.0);
245  total_g.AddSpMat(1.0, inv_covar, xform_g, kNoTrans, 1.0);
246  }
247  obj -= 0.5 * TraceMatMat(xform_d, total_g, kTrans);
248  if (G_out != NULL) G_out->CopyFromMat(total_g);
249 
250  // Compute the gradient: P = \beta [(A^{-1})^{T} , 0] + K - S
251  if (grad_out != NULL) {
252  Matrix<double> grad_d(dim, dim + 1, kSetZero);
253  grad_d.Range(0, dim, 0, dim).CopyFromMat(A);
254  grad_d.Range(0, dim, 0, dim).InvertDouble();
255  grad_d.Range(0, dim, 0, dim).Transpose();
256  grad_d.Scale(stats_.beta_);
257  grad_d.AddMat(-1.0, total_g, kNoTrans);
258  grad_d.AddMat(1.0, stats_.K_, kNoTrans);
259  grad_out->CopyFromMat(grad_d);
260  }
261 
262  return obj;
263 }
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)
kaldi::int32 int32
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
Real TraceMatMat(const MatrixBase< Real > &A, const MatrixBase< Real > &B, MatrixTransposeType trans)
We need to declare this here as it will be a friend function.
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:185
double beta_
beta_ is the occupation count.
AffineXformStats stats_
Accumulated stats.
Definition: fmllr-sgmm2.h:175

◆ Init()

void Init ( int32  dim,
int32  num_gaussians 
)

Definition at line 146 of file fmllr-sgmm2.cc.

Referenced by main(), TestSgmm2FmllrAccsIO(), and TestSgmm2FmllrSubspace().

146  {
147  if (dim == 0) { // empty stats
148  dim_ = 0; // non-zero dimension is meaningless in empty stats
149  stats_.Init(0, 0); // clear the stats
150  } else {
151  dim_ = dim;
152  stats_.Init(dim, num_gaussians);
153  }
154 }
void Init(int32 dim, int32 num_gs)
int32 dim_
Dimension of feature vectors.
Definition: fmllr-sgmm2.h:176
AffineXformStats stats_
Accumulated stats.
Definition: fmllr-sgmm2.h:175

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( FmllrSgmm2Accs  )
private

◆ Read()

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

Definition at line 275 of file fmllr-sgmm2.cc.

References kaldi::ExpectToken(), KALDI_ASSERT, and kaldi::ReadBasicType().

Referenced by TestSgmm2FmllrAccsIO().

275  {
276  ExpectToken(in, binary, "<FMLLRACCS>");
277  ExpectToken(in, binary, "<DIMENSION>");
278  ReadBasicType(in, binary, &dim_);
279  KALDI_ASSERT(dim_ > 0);
280  ExpectToken(in, binary, "<STATS>");
281  stats_.Read(in, binary, add);
282  ExpectToken(in, binary, "</FMLLRACCS>");
283 }
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
Definition: io-funcs.cc:191
void Read(std::istream &in, bool binary, bool add)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32 dim_
Dimension of feature vectors.
Definition: fmllr-sgmm2.h:176
AffineXformStats stats_
Accumulated stats.
Definition: fmllr-sgmm2.h:175

◆ SetZero()

void SetZero ( )
inline

Definition at line 128 of file fmllr-sgmm2.h.

Referenced by main().

128 { stats_.SetZero(); }
AffineXformStats stats_
Accumulated stats.
Definition: fmllr-sgmm2.h:175

◆ stats()

const AffineXformStats& stats ( ) const
inline

Definition at line 172 of file fmllr-sgmm2.h.

172 { return stats_; }
AffineXformStats stats_
Accumulated stats.
Definition: fmllr-sgmm2.h:175

◆ Update()

bool Update ( const AmSgmm2 model,
const Sgmm2FmllrGlobalParams fmllr_globals,
const Sgmm2FmllrConfig opts,
Matrix< BaseFloat > *  out_xform,
BaseFloat frame_count,
BaseFloat auxf_improv 
) const

Computes the FMLLR transform from the accumulated stats, using the pre-transforms in fmllr_globals.

Expects the transform matrix out_xform to be initialized to the correct size. Returns true if the transform was updated (i.e. had enough counts).

Definition at line 356 of file fmllr-sgmm2.cc.

References MatrixBase< Real >::AddMat(), kaldi::ApplyHessianXformToGradient(), kaldi::ApplyInvHessianXformToChange(), kaldi::ApplyInvPreXformToChange(), kaldi::ApplyPreXformToGradient(), kaldi::AssertEqual(), Sgmm2FmllrConfig::bases_occ_scale, kaldi::CalcFmllrStepSize(), MatrixBase< Real >::CopyFromMat(), Sgmm2FmllrGlobalParams::fmllr_bases_, Sgmm2FmllrConfig::fmllr_iters, Sgmm2FmllrConfig::fmllr_min_count, Sgmm2FmllrConfig::fmllr_min_count_basis, Sgmm2FmllrConfig::fmllr_min_count_full, Sgmm2FmllrGlobalParams::HasBasis(), Sgmm2FmllrGlobalParams::IsEmpty(), KALDI_ASSERT, KALDI_ERR, KALDI_LOG, KALDI_VLOG, KALDI_WARN, kaldi::kNoTrans, kaldi::kSetZero, kaldi::kTrans, kaldi::kUndefined, MatrixBase< Real >::LogDet(), Sgmm2FmllrConfig::num_fmllr_bases, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), MatrixBase< Real >::Scale(), MatrixBase< Real >::SetZero(), and kaldi::TraceMatMat().

Referenced by main(), TestSgmm2FmllrAccsIO(), and TestSgmm2FmllrSubspace().

360  {
361  BaseFloat auxf_improv = 0.0, logdet = 0.0;
362  KALDI_ASSERT(out_xform->NumRows() == dim_ && out_xform->NumCols() == dim_+1);
363  BaseFloat mincount = (globals.HasBasis() ?
364  std::min(opts.fmllr_min_count_basis, opts.fmllr_min_count_full) :
365  opts.fmllr_min_count);
366  bool using_subspace = (globals.HasBasis() ?
367  (stats_.beta_ < opts.fmllr_min_count_full) : false);
368 
369  if (globals.IsEmpty())
370  KALDI_ERR << "Must set up pre-transforms before estimating FMLLR.";
371 
372  KALDI_VLOG(1) << "Mincount = " << mincount << "; Basis: "
373  << std::string(globals.HasBasis()? "yes; " : "no; ")
374  << "Using subspace: " << std::string(using_subspace? "yes; "
375  : "no; ");
376 
377  int32 num_bases = 0;
378  if (using_subspace) {
379  KALDI_ASSERT(globals.fmllr_bases_.size() != 0);
380  int32 max_bases = std::min(static_cast<int32>(globals.fmllr_bases_.size()),
381  opts.num_fmllr_bases);
382  num_bases = (opts.bases_occ_scale <= 0.0)? max_bases :
383  std::min(max_bases, static_cast<int32>(std::floor(opts.bases_occ_scale
384  * stats_.beta_)));
385  KALDI_VLOG(1) << "Have " << stats_.beta_ << " frames for speaker: Using "
386  << num_bases << " fMLLR bases.";
387  }
388 
389  // initialization just to get rid of compile errors.
390  BaseFloat auxf_old = 0, auxf_new = 0;
391  if (frame_count != NULL) *frame_count = stats_.beta_;
392 
393  // If occupancy is greater than the min count, update the transform
394  if (stats_.beta_ >= mincount) {
395  for (int32 iter = 0; iter < opts.fmllr_iters; iter++) {
396  Matrix<BaseFloat> grad(dim_, dim_ + 1, kSetZero);
397  Matrix<BaseFloat> G(dim_, dim_ + 1, kSetZero);
398  auxf_new = this->FmllrObjGradient(sgmm, *out_xform, &grad, &G);
399 
400  // For diagnostic purposes
401  KALDI_VLOG(3) << "Iter " << iter << ": Auxiliary function = "
402  << (auxf_new / stats_.beta_) << " per frame over " << stats_.beta_
403  << " frames";
404 
405  if (iter > 0) {
406  // For diagnostic purposes
407  KALDI_VLOG(2) << "Iter " << iter << ": Auxiliary function improvement: "
408  << ((auxf_new - auxf_old) / stats_.beta_) << " per frame over "
409  << (stats_.beta_) << " frames";
410  auxf_improv += auxf_new - auxf_old;
411  }
412 
413  Matrix<BaseFloat> pre_xformed_grad(dim_, dim_ + 1, kSetZero);
414  ApplyPreXformToGradient(globals, grad, &pre_xformed_grad);
415 // std::cout << "Pre-X Grad = " << pre_xformed_grad << std::endl;
416 
417  // Transform P_sk with the Hessian
418  Matrix<BaseFloat> hess_xformed_grad(dim_, dim_ + 1, kSetZero);
419  ApplyHessianXformToGradient(globals, pre_xformed_grad,
420  &hess_xformed_grad);
421 // std::cout << "Hess-X Grad = " << hess_xformed_grad << std::endl;
422 
423  // Update the actual FMLLR transform matrices
424  Matrix<BaseFloat> hess_xformed_delta(dim_, dim_ + 1, kUndefined);
425  if (using_subspace) {
426  // Note that in this case we can simply store the speaker-specific
427  // coefficients for each of the basis matrices. The current
428  // implementation stores the computed transform to simplify the code!
429  hess_xformed_delta.SetZero();
430  for (int32 b = 0; b < num_bases; b++) { // Eq (B.20)
431  hess_xformed_delta.AddMat(TraceMatMat(globals.fmllr_bases_[b],
432  hess_xformed_grad, kTrans),
433  globals.fmllr_bases_[b], kNoTrans);
434  }
435  hess_xformed_delta.Scale(1 / stats_.beta_);
436  } else {
437  hess_xformed_delta.CopyFromMat(hess_xformed_grad);
438  hess_xformed_delta.Scale(1 / stats_.beta_); // Eq. (B.19)
439  }
440 
441 // std::cout << "Hess-X Delta = " << hess_xformed_delta << std::endl;
442 
443  // Transform Delta with the Hessian
444  Matrix<BaseFloat> pre_xformed_delta(dim_, dim_ + 1, kSetZero);
445  ApplyInvHessianXformToChange(globals, hess_xformed_delta,
446  &pre_xformed_delta);
447 
448  // Apply inverse pre-transform to Delta
449  Matrix<BaseFloat> delta(dim_, dim_ + 1, kSetZero);
450  ApplyInvPreXformToChange(globals, pre_xformed_delta, &delta);
451 
452 #ifdef KALDI_PARANOID
453  // Check whether co-ordinate transformation is correct.
454  {
455  BaseFloat tr1 = TraceMatMat(delta, grad, kTrans);
456  BaseFloat tr2 = TraceMatMat(pre_xformed_delta, pre_xformed_grad,
457  kTrans);
458  BaseFloat tr3 = TraceMatMat(hess_xformed_delta, hess_xformed_grad,
459  kTrans);
460  AssertEqual(tr1, tr2, 1e-5);
461  AssertEqual(tr2, tr3, 1e-5);
462  }
463 #endif
464 
465  // Calculate the optimal step size
466  SubMatrix<BaseFloat> A(*out_xform, 0, dim_, 0, dim_);
467  BaseFloat step_size = CalcFmllrStepSize(stats_, sgmm, delta, A, G,
468  opts.fmllr_iters);
469 
470  // Update: W <-- W + k \Delta Eq. (B.34)
471  out_xform->AddMat(step_size, delta, kNoTrans);
472  auxf_old = auxf_new;
473 
474  // Check the objective function change for the last iteration
475  if (iter == opts.fmllr_iters - 1) {
476  auxf_new = this->FmllrObjGradient(sgmm, *out_xform, NULL, NULL);
477  logdet = A.LogDet();
478  // SubMatrix A points to the memory location of out_xform, and so will
479  // contain the updated value
480 
481  KALDI_VLOG(2) << "Iter " << iter << ": Auxiliary function improvement: "
482  << ((auxf_new - auxf_old) / stats_.beta_) << " per frame over "
483  << (stats_.beta_) << " frames";
484  auxf_improv += auxf_new - auxf_old;
485  }
486  }
487  if (auxf_out != NULL) *auxf_out = auxf_improv;
488  auxf_improv /= (stats_.beta_ + 1.0e-10);
489 
490  KALDI_LOG << "Auxiliary function improvement for FMLLR = " << auxf_improv
491  << " per frame over " << stats_.beta_ << " frames. Log-determinant = "
492  << logdet;
493  return true;
494  } else {
495  KALDI_ASSERT(stats_.beta_ < mincount);
496 // std::cerr.precision(10);
497 // std::cerr.setf(std::ios::fixed,std::ios::floatfield);
498  KALDI_WARN << "Not updating FMLLR because count is " << stats_.beta_
499  << " < " << (mincount);
500  if (auxf_out != NULL) *auxf_out = 0.0;
501  return false;
502  } // Do not use the transform if it does not have enough counts
503  KALDI_ASSERT(false); // Should never be reached.
504 }
BaseFloat FmllrObjGradient(const AmSgmm2 &sgmm, const Matrix< BaseFloat > &xform, Matrix< BaseFloat > *grad_out, Matrix< BaseFloat > *G_out) const
Definition: fmllr-sgmm2.cc:229
static void ApplyPreXformToGradient(const Sgmm2FmllrGlobalParams &globals, const Matrix< BaseFloat > &gradient_in, Matrix< BaseFloat > *gradient_out)
Definition: fmllr-sgmm2.cc:31
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
void AddMat(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transA=kNoTrans)
*this += alpha * M [or M^T]
kaldi::int32 int32
static void ApplyInvPreXformToChange(const Sgmm2FmllrGlobalParams &globals, const Matrix< BaseFloat > &delta_in, Matrix< BaseFloat > *delta_out)
Definition: fmllr-sgmm2.cc:45
float BaseFloat
Definition: kaldi-types.h:29
static BaseFloat CalcFmllrStepSize(const AffineXformStats &stats, const AmSgmm2 &sgmm, const MatrixBase< BaseFloat > &Delta, const MatrixBase< BaseFloat > &A, const Matrix< BaseFloat > &G, int32 max_iters)
Definition: fmllr-sgmm2.cc:286
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
static void ApplyInvHessianXformToChange(const Sgmm2FmllrGlobalParams &globals, const Matrix< BaseFloat > &delta_in, Matrix< BaseFloat > *delta_out)
Definition: fmllr-sgmm2.cc:81
Real TraceMatMat(const MatrixBase< Real > &A, const MatrixBase< Real > &B, MatrixTransposeType trans)
We need to declare this here as it will be a friend function.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
static void ApplyHessianXformToGradient(const Sgmm2FmllrGlobalParams &globals, const Matrix< BaseFloat > &gradient_in, Matrix< BaseFloat > *gradient_out)
Definition: fmllr-sgmm2.cc:59
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
Definition: kaldi-math.h:276
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
int32 dim_
Dimension of feature vectors.
Definition: fmllr-sgmm2.h:176
#define KALDI_LOG
Definition: kaldi-error.h:153
double beta_
beta_ is the occupation count.
AffineXformStats stats_
Accumulated stats.
Definition: fmllr-sgmm2.h:175

◆ Write()

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

Definition at line 266 of file fmllr-sgmm2.cc.

References kaldi::WriteBasicType(), and kaldi::WriteToken().

Referenced by TestSgmm2FmllrAccsIO().

266  {
267  WriteToken(out, binary, "<FMLLRACCS>");
268  WriteToken(out, binary, "<DIMENSION>");
269  WriteBasicType(out, binary, dim_);
270  WriteToken(out, binary, "<STATS>");
271  stats_.Write(out, binary);
272  WriteToken(out, binary, "</FMLLRACCS>");
273 }
void Write(std::ostream &out, bool binary) const
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
int32 dim_
Dimension of feature vectors.
Definition: fmllr-sgmm2.h:176
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34
AffineXformStats stats_
Accumulated stats.
Definition: fmllr-sgmm2.h:175

Member Data Documentation

◆ dim_

int32 dim_
private

Dimension of feature vectors.

Definition at line 176 of file fmllr-sgmm2.h.

◆ stats_

AffineXformStats stats_
private

Accumulated stats.

Definition at line 175 of file fmllr-sgmm2.h.


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