diag-gmm-inl.h
Go to the documentation of this file.
1 // gmm/diag-gmm-inl.h
2 
3 // Copyright 2009-2011 Jan Silovsky
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #ifndef KALDI_GMM_DIAG_GMM_INL_H_
21 #define KALDI_GMM_DIAG_GMM_INL_H_
22 
23 #include "util/stl-utils.h"
24 
25 namespace kaldi {
26 
27 template<class Real>
29  KALDI_ASSERT(weights_.Dim() == w.Dim());
30  weights_.CopyFromVec(w);
31  valid_gconsts_ = false;
32 }
33 
35  KALDI_ASSERT(w > 0.0);
36  KALDI_ASSERT(g < NumGauss());
37  weights_(g) = w;
38  valid_gconsts_ = false;
39 }
40 
41 
42 template<class Real>
45  && means_invvars_.NumCols() == m.NumCols());
48  valid_gconsts_ = false;
49 }
50 
51 template<class Real>
53  KALDI_ASSERT(g < NumGauss() && Dim() == in.Dim());
54  Vector<Real> tmp(Dim());
55  tmp.CopyRowFromMat(inv_vars_, g);
56  tmp.MulElements(in);
58  valid_gconsts_ = false;
59 }
60 
61 
62 template<class Real>
64  const MatrixBase<Real> &means) {
66  && means_invvars_.NumCols() == means.NumCols()
67  && inv_vars_.NumRows() == invvars.NumRows()
68  && inv_vars_.NumCols() == invvars.NumCols());
69 
70  inv_vars_.CopyFromMat(invvars);
71  Matrix<Real> new_means_invvars(means);
72  new_means_invvars.MulElements(invvars);
73  means_invvars_.CopyFromMat(new_means_invvars);
74  valid_gconsts_ = false;
75 }
76 
77 template<class Real>
80  && inv_vars_.NumCols() == v.NumCols());
81 
82  int32 num_comp = NumGauss(), dim = Dim();
83  Matrix<Real> means(num_comp, dim);
84  Matrix<Real> vars(num_comp, dim);
85 
86  vars.CopyFromMat(inv_vars_);
87  vars.InvertElements(); // This inversion happens in double if Real == double
89  means.MulElements(vars); // These are real means now
90  means.MulElements(v); // v is inverted (in double if Real == double)
91  means_invvars_.CopyFromMat(means); // Means times new inverse variance
93  valid_gconsts_ = false;
94 }
95 
96 template<class Real>
98  KALDI_ASSERT(g < NumGauss() && v.Dim() == Dim());
99 
100  int32 dim = Dim();
101  Vector<Real> mean(dim), var(dim);
102 
103  var.CopyFromVec(inv_vars_.Row(g));
104  var.InvertElements(); // This inversion happens in double if Real == double
105  mean.CopyFromVec(means_invvars_.Row(g));
106  mean.MulElements(var); // This is a real mean now.
107  mean.MulElements(v); // currently, v is inverted (in double if Real == double)
108  means_invvars_.Row(g).CopyFromVec(mean); // Mean times new inverse variance
109  inv_vars_.Row(g).CopyFromVec(v);
110  valid_gconsts_ = false;
111 }
112 
113 
114 template<class Real>
116  KALDI_ASSERT(v != NULL);
117  v->Resize(NumGauss(), Dim());
119  v->InvertElements();
120 }
121 
122 template<class Real>
124  KALDI_ASSERT(m != NULL);
125  m->Resize(NumGauss(), Dim());
126  Matrix<Real> vars(NumGauss(), Dim());
127  vars.CopyFromMat(inv_vars_);
128  vars.InvertElements();
130  m->MulElements(vars);
131 }
132 
133 
134 template<class Real>
136  KALDI_ASSERT(gauss < NumGauss());
137  KALDI_ASSERT(static_cast<int32>(out->Dim()) == Dim());
138  Vector<Real> tmp(Dim());
139  tmp.CopyRowFromMat(inv_vars_, gauss);
140  out->CopyRowFromMat(means_invvars_, gauss);
141  out->DivElements(tmp);
142 }
143 
144 template<class Real>
146  KALDI_ASSERT(gauss < NumGauss());
147  KALDI_ASSERT(static_cast<int32>(out->Dim()) == Dim());
148  out->CopyRowFromMat(inv_vars_, gauss);
149  out->InvertElements();
150 }
151 
152 
153 } // End namespace kaldi
154 
155 #endif // KALDI_GMM_DIAG_GMM_INL_H_
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
void SetInvVarsAndMeans(const MatrixBase< Real > &invvars, const MatrixBase< Real > &means)
Use SetInvVarsAndMeans if updating both means and (inverse) variances.
Definition: diag-gmm-inl.h:63
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
Base class which provides matrix operations not involving resizing or allocation. ...
Definition: kaldi-matrix.h:49
void GetComponentMean(int32 gauss, VectorBase< Real > *out) const
Accessor for single component mean.
Definition: diag-gmm-inl.h:135
kaldi::int32 int32
A class for storing matrices.
Definition: kaldi-matrix.h:823
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
void SetMeans(const MatrixBase< Real > &m)
Use SetMeans to update only the Gaussian means (and not variances)
Definition: diag-gmm-inl.h:43
void CopyRowFromMat(const MatrixBase< Real > &M, MatrixIndexT row)
Extracts a row of the matrix M.
void SetComponentMean(int32 gauss, const VectorBase< Real > &in)
Mutators for single component, supports float or double Set mean for a single component - internally ...
Definition: diag-gmm-inl.h:52
void GetVars(Matrix< Real > *v) const
Accessor for covariances.
Definition: diag-gmm-inl.h:115
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
void GetMeans(Matrix< Real > *m) const
Accessor for means.
Definition: diag-gmm-inl.h:123
bool valid_gconsts_
Recompute gconsts_ if false.
Definition: diag-gmm.h:233
Matrix< BaseFloat > inv_vars_
Inverted (diagonal) variances.
Definition: diag-gmm.h:235
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: diag-gmm.h:72
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
void MulElements(const MatrixBase< Real > &A)
Element by element multiplication with a given matrix.
void SetInvVars(const MatrixBase< Real > &v)
Set the (inverse) variances and recompute means_invvars_.
Definition: diag-gmm-inl.h:78
void GetComponentVariance(int32 gauss, VectorBase< Real > *out) const
Accessor for single component variance.
Definition: diag-gmm-inl.h:145
void InvertElements()
Inverts all the elements of the matrix.
Vector< BaseFloat > weights_
weights (not log).
Definition: diag-gmm.h:234
A class representing a vector.
Definition: kaldi-vector.h:406
void InvertElements()
Invert all elements.
#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 CopyRowFromVec(const VectorBase< Real > &v, const MatrixIndexT row)
Copy vector into specific row of matrix.
void DivElements(const VectorBase< Real > &v)
Divide element-by-element by a vector.
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 SetComponentInvVar(int32 gauss, const VectorBase< Real > &in)
Set inv-var for single component (recommend to do this before setting the mean, if doing both...
Definition: diag-gmm-inl.h:97
void SetWeights(const VectorBase< Real > &w)
Mutators for both float or double.
Definition: diag-gmm-inl.h:28
Provides a vector abstraction class.
Definition: kaldi-vector.h:41
Matrix< BaseFloat > means_invvars_
Means times inverted variance.
Definition: diag-gmm.h:236
void SetComponentWeight(int32 gauss, BaseFloat weight)
Set weight for single component.
Definition: diag-gmm-inl.h:34