plda-test.cc
Go to the documentation of this file.
1 // ivector/plda-test.cc
2 
3 // Copyright 2013 Daniel Povey
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 #include "ivector/plda.h"
21 
22 
23 namespace kaldi {
24 
26  int32 num_classes = 1000 + Rand() % 10;
27  Matrix<double> between_proj(dim, dim);
28  while (between_proj.Cond() > 100)
29  between_proj.SetRandn();
30  Matrix<double> within_proj(dim, dim);
31  while (within_proj.Cond() > 100)
32  within_proj.SetRandn();
33 
34 
35  Vector<double> global_mean(dim);
36  global_mean.SetRandn();
37  global_mean.Scale(10.0);
38 
39  PldaStats stats;
40 
41  for (int32 n = 0; n < num_classes; n++) {
42  int32 num_egs = 1 + Rand() % 30;
43  Vector<double> rand_vec(dim);
44  rand_vec.SetRandn();
45  Vector<double> class_mean(global_mean);
46  class_mean.AddMatVec(1.0, between_proj, kNoTrans, rand_vec, 1.0);
47 
48  Matrix<double> rand_mat(num_egs, dim);
49  rand_mat.SetRandn();
50  Matrix<double> offset_mat(num_egs, dim);
51  offset_mat.AddMatMat(1.0, rand_mat, kNoTrans, within_proj,
52  kTrans, 0.0);
53  offset_mat.AddVecToRows(1.0, class_mean);
54 
55  double weight = 1.0 + (0.1 * (Rand() % 30));
56  stats.AddSamples(weight,
57  offset_mat);
58  }
59 
60 
61 
62  SpMatrix<double> between_var(dim), within_var(dim);
63  between_var.AddMat2(1.0, between_proj, kNoTrans, 0.0);
64  within_var.AddMat2(1.0, within_proj, kNoTrans, 0.0);
65 
66  stats.Sort();
67  PldaEstimator estimator(stats);
68  Plda plda;
69  PldaEstimationConfig config;
70  estimator.Estimate(config, &plda);
71 
72  KALDI_LOG << "Trace of true within-var is " << within_var.Trace();
73  KALDI_LOG << "Trace of true between-var is " << between_var.Trace();
74 
75  {
76  TpMatrix<double> C(dim);
77  C.Cholesky(within_var);
78  C.Invert();
79  SpMatrix<double> between_var_proj(dim);
80  between_var_proj.AddTp2Sp(1.0, C, kNoTrans, between_var, 0.0);
81  Vector<double> s(dim);
82  between_var_proj.Eig(&s);
83  s.Scale(-1.0);
84  std::sort(s.Data(), s.Data() + s.Dim());
85  s.Scale(-1.0);
86  KALDI_LOG << "Diagonal of between-class variance in normalized space "
87  << "should be: " << s;
88  }
89 
90 }
91 
92 }
93 
94 
95 /*
96  This test is really just making sure that the PLDA estimation does not
97  crash. As for testing that it's working: I did this by eyeballing the
98  output where it says "Trace of true within-var is XX" or "Trace of true
99  between-var is XX" and comparing with the output from the estimation
100  that says, "Trace of within-class variance is XX" and "Trace of betweeen-class
101  variance is XX" (on the last iteration). I make sure they are similar.
102  I also checked that the objective function (where it says
103  "Objective function is XX" is non-decreasing, and seems to be converging.
104 */
105 int main() {
106  using namespace kaldi;
107  SetVerboseLevel(3);
108  for (int i = 0; i < 5; i++)
110 
111  // UnitTestPldaEstimation(400);
113  std::cout << "Test OK.\n";
114  return 0;
115 }
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
void Estimate(const PldaEstimationConfig &config, Plda *output)
Definition: plda.cc:525
Real Cond() const
Returns condition number by computing Svd.
Real Trace() const
Definition: sp-matrix.cc:171
kaldi::int32 int32
void Eig(VectorBase< Real > *s, MatrixBase< Real > *P=NULL) const
Solves the symmetric eigenvalue problem: at end we should have (*this) = P * diag(s) * P^T...
Definition: qr.cc:433
void AddTp2Sp(const Real alpha, const TpMatrix< Real > &T, MatrixTransposeType transM, const SpMatrix< Real > &A, const Real beta=0.0)
The following function does: this <– beta*this + alpha * T * A * T^T.
Definition: sp-matrix.cc:1139
void SetVerboseLevel(int32 i)
This should be rarely used, except by programs using Kaldi as library; command-line programs set the ...
Definition: kaldi-error.h:64
void Cholesky(const SpMatrix< Real > &orig)
Definition: tp-matrix.cc:88
void UnitTestPldaEstimation(int32 dim)
Definition: plda-test.cc:25
void AddVecToRows(const Real alpha, const VectorBase< OtherReal > &v)
[each row of *this] += alpha * v
struct rnnlm::@11::@12 n
void SetRandn()
Sets to random values of a normal distribution.
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
Real * Data()
Returns a pointer to the start of the vector&#39;s data.
Definition: kaldi-vector.h:70
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
void Scale(Real alpha)
Multiplies all elements by this constant.
void AddMatVec(const Real alpha, const MatrixBase< Real > &M, const MatrixTransposeType trans, const VectorBase< Real > &v, const Real beta)
Add matrix times vector : this <– beta*this + alpha*M*v.
Definition: kaldi-vector.cc:92
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
void SetRandn()
Set vector to random normally-distributed noise.
int main()
Definition: plda-test.cc:105
void AddSamples(double weight, const Matrix< double > &group)
The dimension is set up the first time you add samples.
Definition: plda.cc:286
#define KALDI_LOG
Definition: kaldi-error.h:153
void Sort()
Definition: plda.h:188