model-test-common.cc
Go to the documentation of this file.
1 // gmm/model-test-common.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation; Jan Silovsky;
4 // Saarland University
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 <vector>
23 
24 #include "matrix/matrix-lib.h"
25 #include "gmm/model-test-common.h"
26 
27 namespace kaldi {
28 namespace unittest {
29 
31  TpMatrix<BaseFloat> *matrix_sqrt, BaseFloat *logdet) {
32  // generate random (non-singular) matrix
33  Matrix<BaseFloat> tmp(dim, dim);
34  while (1) {
35  tmp.SetRandn();
36  if (tmp.Cond() < 100) break;
37  KALDI_LOG << "Condition number of random matrix large "
38  << static_cast<float>(tmp.Cond())
39  << ", trying again (this is normal)\n";
40  }
41  // tmp * tmp^T will give positive definite matrix
42  matrix->AddMat2(1.0, tmp, kNoTrans, 0.0);
43 
44  if (matrix_sqrt != NULL) matrix_sqrt->Cholesky(*matrix);
45  if (logdet != NULL) *logdet = matrix->LogPosDefDet();
46  if ((matrix_sqrt == NULL) && (logdet == NULL)) {
47  TpMatrix<BaseFloat> sqrt(dim);
48  sqrt.Cholesky(*matrix);
49  }
50 }
51 
52 void RandDiagGaussFeatures(int32 num_samples,
53  const VectorBase<BaseFloat> &mean,
54  const VectorBase<BaseFloat> &sqrt_var,
55  MatrixBase<BaseFloat> *feats) {
56  int32 dim = mean.Dim();
57  KALDI_ASSERT(feats != NULL);
58  KALDI_ASSERT(feats->NumRows() == num_samples &&
59  feats->NumCols() == dim);
60  KALDI_ASSERT(sqrt_var.Dim() == dim);
61 
62  Vector<BaseFloat> rnd_vec(dim);
63  for (int32 counter = 0; counter < num_samples; counter++) {
64  for (int32 d = 0; d < dim; d++) {
65  rnd_vec(d) = RandGauss();
66  }
67  feats->Row(counter).CopyFromVec(mean);
68  feats->Row(counter).AddVecVec(1.0, sqrt_var, rnd_vec, 1.0);
69  }
70 }
71 
72 void RandFullGaussFeatures(int32 num_samples,
73  const VectorBase<BaseFloat> &mean,
74  const TpMatrix<BaseFloat> &sqrt_var,
75  MatrixBase<BaseFloat> *feats) {
76  int32 dim = mean.Dim();
77  KALDI_ASSERT(feats != NULL);
78  KALDI_ASSERT(feats->NumRows() == num_samples && feats->NumCols() == dim);
79  KALDI_ASSERT(sqrt_var.NumRows() == dim);
80 
81  Vector<BaseFloat> rnd_vec(dim);
82  for (int32 counter = 0; counter < num_samples; counter++) {
83  for (int32 d = 0; d < dim; d++) {
84  rnd_vec(d) = RandGauss();
85  }
86  feats->Row(counter).CopyFromVec(mean);
87  feats->Row(counter).AddTpVec(1.0, sqrt_var, kNoTrans, rnd_vec, 1.0);
88  }
89 }
90 
91 void InitRandDiagGmm(int32 dim, int32 num_comp, DiagGmm *gmm) {
92  Vector<BaseFloat> weights(num_comp);
93  Matrix<BaseFloat> means(num_comp, dim), inv_vars(num_comp, dim);
94 
95  for (int32 m = 0; m < num_comp; m++) {
96  weights(m) = Exp(RandGauss());
97  for (int32 d= 0; d < dim; d++) {
98  means(m, d) = RandGauss() / (1 + d);
99  inv_vars(m, d) = Exp(RandGauss() / (1 + d)) + 1e-2;
100  }
101  }
102  weights.Scale(1.0 / weights.Sum());
103 
104  gmm->Resize(num_comp, dim);
105  gmm->SetWeights(weights);
106  gmm->SetInvVarsAndMeans(inv_vars, means);
107  gmm->ComputeGconsts();
108 }
109 
110 void InitRandFullGmm(int32 dim, int32 num_comp, FullGmm *gmm) {
111  Vector<BaseFloat> weights(num_comp);
112  Matrix<BaseFloat> means(num_comp, dim);
113  std::vector< SpMatrix<BaseFloat> > invcovars(num_comp);
114  for (int32 mix = 0; mix < num_comp; mix++) {
115  invcovars[mix].Resize(dim);
116  }
117 
118  BaseFloat tot_weight = 0.0;
119  for (int32 m = 0; m < num_comp; m++) {
120  weights(m) = RandUniform() + 1e-2;
121  for (int32 d= 0; d < dim; d++) {
122  means(m, d) = RandGauss();
123  }
124  RandPosdefSpMatrix(dim, &invcovars[m], NULL, NULL);
125  invcovars[m].InvertDouble();
126  tot_weight += weights(m);
127  }
128  weights.Scale(1/tot_weight);
129 
130  gmm->Resize(num_comp, dim);
131  gmm->SetWeights(weights);
132  gmm->SetInvCovarsAndMeans(invcovars, means);
133  gmm->ComputeGconsts();
134 }
135 
136 } // End namespace unittests
137 } // End namespace kaldi
void AddMat2(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, const Real beta)
rank-N update: if (transM == kNoTrans) (*this) = beta*(*this) + alpha * M * M^T, or (if transM == kTr...
Definition: sp-matrix.cc:1110
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
double Exp(double x)
Definition: kaldi-math.h:83
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
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
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
Definition: kaldi-math.h:151
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
Real Cond() const
Returns condition number by computing Svd.
Base class which provides matrix operations not involving resizing or allocation. ...
Definition: kaldi-matrix.h:49
int32 ComputeGconsts()
Sets the gconsts.
Definition: full-gmm.cc:92
Definition for Gaussian Mixture Model with full covariances.
Definition: full-gmm.h:40
void Resize(int32 nMix, int32 dim)
Resizes arrays to this dim. Does not initialize data.
Definition: diag-gmm.cc:66
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
int32 ComputeGconsts()
Sets the gconsts.
Definition: diag-gmm.cc:114
float RandGauss(struct RandomState *state=NULL)
Definition: kaldi-math.h:155
kaldi::int32 int32
MatrixIndexT NumRows() const
void RandPosdefSpMatrix(int32 dim, SpMatrix< BaseFloat > *matrix, TpMatrix< BaseFloat > *matrix_sqrt, BaseFloat *logdet)
void RandFullGaussFeatures(int32 num_samples, const VectorBase< BaseFloat > &mean, const TpMatrix< BaseFloat > &sqrt_var, MatrixBase< BaseFloat > *feats)
void Cholesky(const SpMatrix< Real > &orig)
Definition: tp-matrix.cc:88
float BaseFloat
Definition: kaldi-types.h:29
void Resize(int32 nMix, int32 dim)
Resizes arrays to this dim. Does not initialize data.
Definition: full-gmm.cc:41
Real LogPosDefDet() const
Computes log determinant but only for +ve-def matrices (it uses Cholesky).
Definition: sp-matrix.cc:36
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
void InitRandDiagGmm(int32 dim, int32 num_comp, DiagGmm *gmm)
void SetRandn()
Sets to random values of a normal distribution.
Packed symetric matrix class.
Definition: matrix-common.h:63
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
void Scale(Real alpha)
Multiplies all elements by this constant.
Real Sum() const
Returns sum of the elements.
void InitRandFullGmm(int32 dim, int32 num_comp, FullGmm *gmm)
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
Definition for Gaussian Mixture Model with diagonal covariances.
Definition: diag-gmm.h:42
void RandDiagGaussFeatures(int32 num_samples, const VectorBase< BaseFloat > &mean, const VectorBase< BaseFloat > &sqrt_var, MatrixBase< BaseFloat > *feats)
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
#define KALDI_LOG
Definition: kaldi-error.h:153