fmllr-raw-test.cc
Go to the documentation of this file.
1 // transform/fmllr-raw-test.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 // 2013 Johns Hopkins University (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 "util/common-utils.h"
22 #include "gmm/diag-gmm.h"
24 #include "transform/fmllr-raw.h"
25 
26 namespace kaldi {
27 
28 
29 void InitRandomGmm (DiagGmm *gmm_in) {
30  int32 num_gauss = 5 + rand () % 4;
31  int32 dim = 6 + Rand() % 5;
32  DiagGmm &gmm(*gmm_in);
33  gmm.Resize(num_gauss, dim);
34  Matrix<BaseFloat> inv_vars(num_gauss, dim),
35  means(num_gauss, dim);
36  Vector<BaseFloat> weights(num_gauss);
37  for (int32 i = 0; i < num_gauss; i++) {
38  for (int32 j = 0; j < dim; j++) {
39  inv_vars(i, j) = Exp(RandGauss() * (1.0 / (1 + j)));
40  means(i, j) = RandGauss() * (1.0 / (1 + j));
41  }
42  weights(i) = Exp(RandGauss());
43  }
44  weights.Scale(1.0 / weights.Sum());
45  gmm.SetWeights(weights);
46  gmm.SetInvVarsAndMeans(inv_vars, means);
47  gmm.ComputeGconsts();
48 }
49 
50 void UnitTestFmllrRaw(bool use_offset) {
51  using namespace kaldi;
52  DiagGmm gmm;
53  InitRandomGmm(&gmm);
54  int32 model_dim = gmm.Dim();
55 
56  int32 raw_dim = 5 + Rand() % 3;
57  int32 num_splice = 1 + Rand() % 5;
58  while (num_splice * raw_dim < model_dim) {
59  num_splice++;
60  }
61 
62  int32 full_dim = num_splice * raw_dim;
63  int32 npoints = raw_dim*(raw_dim+1)*10;
64 
65  Matrix<BaseFloat> rand_points(npoints, full_dim);
66  rand_points.SetRandn();
67 
68  Matrix<BaseFloat> lda_mllt(full_dim, full_dim + (use_offset ? 1 : 0)); // This is the full LDA+MLLT
69  // matrix. TODO: test with offset.
70  lda_mllt.SetRandn();
71 
72  FmllrRawAccs accs(raw_dim, model_dim, lda_mllt);
73 
74  BaseFloat prev_objf_impr;
75  for (int32 iter = 0; iter < 4; iter++) {
76 
77  for (int32 i = 0; i < npoints; i++) {
78  SubVector<BaseFloat> sample(rand_points, i);
79  accs.AccumulateForGmm(gmm, sample, 1.0);
80  }
81 
82  Matrix<BaseFloat> fmllr_mat(raw_dim, raw_dim + 1);
83  fmllr_mat.SetUnit(); // sets diagonal elements to one.
84 
85  FmllrRawOptions opts;
86  BaseFloat objf_impr, count;
87  accs.Update(opts, &fmllr_mat, &objf_impr, &count);
88 
89  KALDI_ASSERT(objf_impr > 0.0);
90 
91  if (iter != 0) {
92  // This is not something provable, but is always true
93  // in practice.
94  KALDI_ASSERT(objf_impr < prev_objf_impr);
95  }
96  prev_objf_impr = objf_impr;
97 
98 
99  // Now transform the raw features.
100  for (int32 splice = 0; splice < num_splice; splice++) {
101  SubMatrix<BaseFloat> raw_feats(rand_points,
102  0, npoints,
103  splice * raw_dim, raw_dim);
104  for (int32 t = 0; t < npoints; t++) {
105  SubVector<BaseFloat> this_feat(raw_feats, t);
106  ApplyAffineTransform(fmllr_mat, &this_feat);
107  }
108  }
109  accs.SetZero();
110  }
111 }
112 
113 
114 } // namespace kaldi ends here
115 
116 int main() {
118 
119  for (int i = 0; i < 2; i++) { // did more iterations when first testing...
120  kaldi::UnitTestFmllrRaw(i % 2 == 0);
121  }
122  std::cout << "Test OK.\n";
123 }
void InitRandomGmm(DiagGmm *gmm_in)
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
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: diag-gmm.h:74
void Update(const FmllrRawOptions &opts, MatrixBase< BaseFloat > *raw_fmllr_mat, BaseFloat *objf_impr, BaseFloat *count)
Update "raw_fmllr_mat"; it should have the correct dimension and reasonable values at entry (see the ...
Definition: fmllr-raw.cc:299
float RandGauss(struct RandomState *state=NULL)
Definition: kaldi-math.h:155
kaldi::int32 int32
void SetUnit()
Sets to zero, except ones along diagonal [for non-square matrices too].
const size_t count
void UnitTestFmllrRaw(bool use_offset)
void SetRandn()
Sets to random values of a normal distribution.
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32 g_kaldi_verbose_level
This is set by util/parse-options.
Definition: kaldi-error.cc:46
Definition for Gaussian Mixture Model with diagonal covariances.
Definition: diag-gmm.h:42
BaseFloat AccumulateForGmm(const DiagGmm &gmm, const VectorBase< BaseFloat > &data, BaseFloat weight)
Accumulate stats for a single GMM in the model; returns log likelihood.
Definition: fmllr-raw.cc:107
void ApplyAffineTransform(const MatrixBase< BaseFloat > &xform, VectorBase< BaseFloat > *vec)
Applies the affine transform &#39;xform&#39; to the vector &#39;vec&#39; and overwrites the contents of &#39;vec&#39;...
Sub-matrix representation.
Definition: kaldi-matrix.h:988
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501
int main()