full-gmm-inl.h
Go to the documentation of this file.
1 // gmm/full-gmm-inl.h
2 
3 // Copyright 2009-2011 Jan Silovsky; Saarland University;
4 // Microsoft Corporation
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 #ifndef KALDI_GMM_FULL_GMM_INL_H_
22 #define KALDI_GMM_FULL_GMM_INL_H_
23 
24 #include <vector>
25 
26 #include "util/stl-utils.h"
27 
28 namespace kaldi {
29 
30 template<class Real>
32  KALDI_ASSERT(weights_.Dim() == w.Dim());
33  weights_.CopyFromVec(w);
34  valid_gconsts_ = false;
35 }
36 
37 template<class Real>
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 }
48 
49 template<class Real>
51  const std::vector<SpMatrix<Real> > &invcovars, const Matrix<Real> &means) {
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 }
65 
66 template<class Real>
68  const std::vector<SpMatrix<Real> > &invcovars,
70  KALDI_ASSERT(means_invcovars_.NumRows() == means_invcovars.NumRows()
71  && means_invcovars_.NumCols() == means_invcovars.NumCols()
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  }
78  means_invcovars_.CopyFromMat(means_invcovars);
79  valid_gconsts_ = false;
80 }
81 
82 template<class Real>
83 void FullGmm::SetInvCovars(const std::vector<SpMatrix<Real> > &v) {
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 }
104 
105 template<class Real>
106 void FullGmm::GetCovars(std::vector<SpMatrix<Real> > *v) const {
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 }
116 
117 template<class Real>
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 }
130 
131 template<class Real>
132 void FullGmm::GetCovarsAndMeans(std::vector< SpMatrix<Real> > *covars,
133  Matrix<Real> *means) const {
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 }
148 
149 
150 template<class Real>
152  VectorBase<Real> *out) const {
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 }
163 
164 
165 } // End namespace kaldi
166 
167 #endif // KALDI_GMM_FULL_GMM_INL_H_
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void SetWeights(const Vector< Real > &w)
Mutators for both float or double.
Definition: full-gmm-inl.h:31
Packed symetric matrix class.
Definition: matrix-common.h:62
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: full-gmm.h:60
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
void InvertDouble(Real *logdet=NULL, Real *det_sign=NULL, bool inverse_needed=true)
Definition: sp-matrix.cc:312
void SetInvCovarsAndMeans(const std::vector< SpMatrix< Real > > &invcovars, const Matrix< Real > &means)
Use SetInvCovarsAndMeans if updating both means and (inverse) covariances.
Definition: full-gmm-inl.h:50
Vector< BaseFloat > weights_
weights (not log).
Definition: full-gmm.h:201
kaldi::int32 int32
A class for storing matrices.
Definition: kaldi-matrix.h:823
void AddSpVec(const Real alpha, const SpMatrix< Real > &M, const VectorBase< Real > &v, const Real beta)
Add symmetric positive definite matrix times vector: this <– beta*this + alpha*M*v.
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
void CopyFromSp(const SpMatrix< Real > &other)
Definition: sp-matrix.h:85
void GetCovars(std::vector< SpMatrix< Real > > *v) const
Accessor for covariances.
Definition: full-gmm-inl.h:106
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
bool valid_gconsts_
Recompute gconsts_ if false.
Definition: full-gmm.h:200
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
void SetInvCovarsAndMeansInvCovars(const std::vector< SpMatrix< Real > > &invcovars, const Matrix< Real > &means_invcovars)
Use this if setting both, in the class&#39;s native format.
Definition: full-gmm-inl.h:67
void GetMeans(Matrix< Real > *m) const
Accessor for means.
Definition: full-gmm-inl.h:118
void SetInvCovars(const std::vector< SpMatrix< Real > > &v)
Set the (inverse) covariances and recompute means_invcovars_.
Definition: full-gmm-inl.h:83
A class representing a vector.
Definition: kaldi-vector.h:406
#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
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
const Matrix< BaseFloat > & means_invcovars() const
Definition: full-gmm.h:145
Provides a vector abstraction class.
Definition: kaldi-vector.h:41
Matrix< BaseFloat > means_invcovars_
Means times inverse covariances.
Definition: full-gmm.h:203
void SetZero()
Set vector to all zeros.
void SetMeans(const Matrix< Real > &m)
Use SetMeans to update only the Gaussian means (and not variances)
Definition: full-gmm-inl.h:38
void GetCovarsAndMeans(std::vector< SpMatrix< Real > > *covars, Matrix< Real > *means) const
Accessor for covariances and means.
Definition: full-gmm-inl.h:132
void GetComponentMean(int32 gauss, VectorBase< Real > *out) const
Accessor for component mean.
Definition: full-gmm-inl.h:151