diag-gmm-normal.cc
Go to the documentation of this file.
1 // gmm/diag-gmm-normal.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation; Saarland University;
4 // Yanmin Qian
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 <algorithm>
22 #include <limits>
23 #include <string>
24 #include <vector>
25 
26 #include "gmm/diag-gmm-normal.h"
27 #include "gmm/diag-gmm.h"
28 
29 namespace kaldi {
30 
32  KALDI_ASSERT(nmix > 0 && dim > 0);
33 
34  if (weights_.Dim() != nmix)
35  weights_.Resize(nmix);
36 
37  if (vars_.NumRows() != nmix ||
38  vars_.NumCols() != dim)
39  vars_.Resize(nmix, dim);
40 
41  if (means_.NumRows() != nmix ||
42  means_.NumCols() != dim)
43  means_.Resize(nmix, dim);
44 }
45 
47  int32 num_comp = diaggmm.weights_.Dim(), dim = diaggmm.Dim();
48  Resize(num_comp, dim);
49 
50  weights_.CopyFromVec(diaggmm.weights_);
51  vars_.CopyFromMat(diaggmm.inv_vars_);
55 }
56 
57 void DiagGmmNormal::CopyToDiagGmm(DiagGmm *diaggmm, GmmFlagsType flags) const {
58  KALDI_ASSERT((static_cast<int32>(diaggmm->Dim()) == means_.NumCols())
59  && (static_cast<int32>(diaggmm->weights_.Dim()) == weights_.Dim()));
60 
61  DiagGmmNormal oldg(*diaggmm);
62 
63  if (flags & kGmmWeights)
64  diaggmm->weights_.CopyFromVec(weights_);
65 
66  if (flags & kGmmVariances) {
67  diaggmm->inv_vars_.CopyFromMat(vars_);
68  diaggmm->inv_vars_.InvertElements();
69 
70  // update the mean related natural part with the old mean, if necessary
71  if (!(flags & kGmmMeans)) {
72  diaggmm->means_invvars_.CopyFromMat(oldg.means_);
73  diaggmm->means_invvars_.MulElements(diaggmm->inv_vars_);
74  }
75  }
76 
77  if (flags & kGmmMeans) {
79  diaggmm->means_invvars_.MulElements(diaggmm->inv_vars_);
80  }
81 
82  diaggmm->valid_gconsts_ = false;
83 }
84 
85 } // End 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
Definition for Gaussian Mixture Model with diagonal covariances in normal mode: where the parameters ...
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
void Resize(int32 nMix, int32 dim)
Resizes arrays to this dim. Does not initialize data.
kaldi::int32 int32
void CopyFromDiagGmm(const DiagGmm &diaggmm)
Copies from given DiagGmm.
uint16 GmmFlagsType
Bitwise OR of the above flags.
Definition: model-common.h:35
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
bool valid_gconsts_
Recompute gconsts_ if false.
Definition: diag-gmm.h:233
Matrix< BaseFloat > inv_vars_
Inverted (diagonal) variances.
Definition: diag-gmm.h:235
Matrix< double > vars_
diagonal variance
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.
Matrix< double > means_
Means.
void InvertElements()
Inverts all the elements of the matrix.
Vector< BaseFloat > weights_
weights (not log).
Definition: diag-gmm.h:234
#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
Definition for Gaussian Mixture Model with diagonal covariances.
Definition: diag-gmm.h:42
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).
Vector< double > weights_
weights (not log).
Matrix< BaseFloat > means_invvars_
Means times inverted variance.
Definition: diag-gmm.h:236
void CopyToDiagGmm(DiagGmm *diaggmm, GmmFlagsType flags=kGmmAll) const
Copies to DiagGmm the requested parameters.