decodable-am-diag-gmm.cc
Go to the documentation of this file.
1 // gmm/decodable-am-diag-gmm.cc
2 
3 // Copyright 2009-2011 Saarland University; Lukas Burget
4 // 2013 Johns Hopkins Universith (author: Daniel Povey)
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include <vector>
22 using std::vector;
23 
25 
26 namespace kaldi {
27 
29  int32 frame, int32 state) {
30  KALDI_ASSERT(static_cast<size_t>(frame) <
31  static_cast<size_t>(NumFramesReady()));
32  KALDI_ASSERT(static_cast<size_t>(state) < static_cast<size_t>(NumIndices()) &&
33  "Likely graph/model mismatch, e.g. using wrong HCLG.fst");
34 
35  if (log_like_cache_[state].hit_time == frame) {
36  return log_like_cache_[state].log_like; // return cached value, if found
37  }
38 
39  if (frame != previous_frame_) { // cache the squared stats.
40  data_squared_.CopyFromVec(feature_matrix_.Row(frame));
41  data_squared_.ApplyPow(2.0);
42  previous_frame_ = frame;
43  }
44 
45  const DiagGmm &pdf = acoustic_model_.GetPdf(state);
46  const VectorBase<BaseFloat> &data = feature_matrix_.Row(frame);
47 
48  // check if everything is in order
49  if (pdf.Dim() != data.Dim()) {
50  KALDI_ERR << "Dim mismatch: data dim = " << data.Dim()
51  << " vs. model dim = " << pdf.Dim();
52  }
53  if (!pdf.valid_gconsts()) {
54  KALDI_ERR << "State " << (state) << ": Must call ComputeGconsts() "
55  "before computing likelihood.";
56  }
57 
58  Vector<BaseFloat> loglikes(pdf.gconsts()); // need to recreate for each pdf
59  // loglikes += means * inv(vars) * data.
60  loglikes.AddMatVec(1.0, pdf.means_invvars(), kNoTrans, data, 1.0);
61  // loglikes += -0.5 * inv(vars) * data_sq.
62  loglikes.AddMatVec(-0.5, pdf.inv_vars(), kNoTrans, data_squared_, 1.0);
63 
64  BaseFloat log_sum = loglikes.LogSumExp(log_sum_exp_prune_);
65  if (KALDI_ISNAN(log_sum) || KALDI_ISINF(log_sum))
66  KALDI_ERR << "Invalid answer (overflow or invalid variances/features?)";
67 
68  log_like_cache_[state].log_like = log_sum;
69  log_like_cache_[state].hit_time = frame;
70 
71  return log_sum;
72 }
73 
75  if (static_cast<int32>(log_like_cache_.size()) != acoustic_model_.NumPdfs()) {
77  }
78  vector<LikelihoodCacheRecord>::iterator it = log_like_cache_.begin(),
79  end = log_like_cache_.end();
80  for (; it != end; ++it) { it->hit_time = -1; }
81 }
82 
83 
84 } // namespace kaldi
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: diag-gmm.h:74
virtual int32 NumIndices() const
Returns the number of states in the acoustic model (they will be indexed one-based, i.e.
const Matrix< BaseFloat > & means_invvars() const
Definition: diag-gmm.h:179
#define KALDI_ISINF
Definition: kaldi-math.h:73
const Vector< BaseFloat > & gconsts() const
Const accessors.
Definition: diag-gmm.h:174
bool valid_gconsts() const
Definition: diag-gmm.h:181
kaldi::int32 int32
Vector< BaseFloat > data_squared_
Cache for fast likelihood calculation.
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
#define KALDI_ERR
Definition: kaldi-error.h:147
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
const Matrix< BaseFloat > & feature_matrix_
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:92
std::vector< LikelihoodCacheRecord > log_like_cache_
int32 NumPdfs() const
Definition: am-diag-gmm.h:82
DiagGmm & GetPdf(int32 pdf_index)
Accessors.
Definition: am-diag-gmm.h:119
A class representing a vector.
Definition: kaldi-vector.h:406
#define KALDI_ISNAN
Definition: kaldi-math.h:72
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Definition for Gaussian Mixture Model with diagonal covariances.
Definition: diag-gmm.h:42
virtual int32 NumFramesReady() const
The call NumFramesReady() will return the number of frames currently available for this decodable obj...
Provides a vector abstraction class.
Definition: kaldi-vector.h:41
virtual BaseFloat LogLikelihoodZeroBased(int32 frame, int32 state_index)
const Matrix< BaseFloat > & inv_vars() const
Definition: diag-gmm.h:180