kaldi::unittest Namespace Reference

Functions

void RandPosdefSpMatrix (int32 dim, SpMatrix< BaseFloat > *matrix, TpMatrix< BaseFloat > *matrix_sqrt, BaseFloat *logdet)
 
void RandDiagGaussFeatures (int32 num_samples, const VectorBase< BaseFloat > &mean, const VectorBase< BaseFloat > &sqrt_var, MatrixBase< BaseFloat > *feats)
 
void RandFullGaussFeatures (int32 num_samples, const VectorBase< BaseFloat > &mean, const TpMatrix< BaseFloat > &sqrt_var, MatrixBase< BaseFloat > *feats)
 
void InitRandDiagGmm (int32 dim, int32 num_comp, DiagGmm *gmm)
 
void InitRandFullGmm (int32 dim, int32 num_comp, FullGmm *gmm)
 

Function Documentation

◆ InitRandDiagGmm()

void InitRandDiagGmm ( int32  dim,
int32  num_comp,
DiagGmm gmm 
)

Definition at line 91 of file model-test-common.cc.

References DiagGmm::ComputeGconsts(), rnnlm::d, kaldi::Exp(), kaldi::RandGauss(), DiagGmm::Resize(), VectorBase< Real >::Scale(), DiagGmm::SetInvVarsAndMeans(), DiagGmm::SetWeights(), and VectorBase< Real >::Sum().

Referenced by kaldi::TestFmpe(), TestMllrAccsIO(), UnitTestAmDiagGmm(), UnitTestMleAmDiagGmm(), and UnitTestRegtreeMllrDiagGmm().

91  {
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 }
double Exp(double x)
Definition: kaldi-math.h:83
float RandGauss(struct RandomState *state=NULL)
Definition: kaldi-math.h:155
kaldi::int32 int32

◆ InitRandFullGmm()

void InitRandFullGmm ( int32  dim,
int32  num_comp,
FullGmm gmm 
)

Definition at line 110 of file model-test-common.cc.

References FullGmm::ComputeGconsts(), rnnlm::d, kaldi::RandGauss(), RandPosdefSpMatrix(), kaldi::RandUniform(), FullGmm::Resize(), VectorBase< Real >::Scale(), FullGmm::SetInvCovarsAndMeans(), and FullGmm::SetWeights().

Referenced by TestSgmm2Fmllr(), UnitTestEstimateSgmm2(), UnitTestFullGmmEst(), kaldi::UnitTestIvectorExtractor(), and UnitTestSgmm2().

110  {
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 }
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
Definition: kaldi-math.h:151
float RandGauss(struct RandomState *state=NULL)
Definition: kaldi-math.h:155
kaldi::int32 int32
float BaseFloat
Definition: kaldi-types.h:29
void RandPosdefSpMatrix(size_t dim, SpMatrix< BaseFloat > *matrix, TpMatrix< BaseFloat > *matrix_sqrt=NULL, BaseFloat *logdet=NULL)

◆ RandDiagGaussFeatures()

void RandDiagGaussFeatures ( int32  num_samples,
const VectorBase< BaseFloat > &  mean,
const VectorBase< BaseFloat > &  sqrt_var,
MatrixBase< BaseFloat > *  feats 
)

Definition at line 52 of file model-test-common.cc.

References rnnlm::d, VectorBase< Real >::Dim(), KALDI_ASSERT, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), kaldi::RandGauss(), and MatrixBase< Real >::Row().

Referenced by TestSgmm2Fmllr(), UnitTestEstimateSgmm2(), and UnitTestMleAmDiagGmm().

55  {
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 }
float RandGauss(struct RandomState *state=NULL)
Definition: kaldi-math.h:155
kaldi::int32 int32
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ RandFullGaussFeatures()

void RandFullGaussFeatures ( int32  num_samples,
const VectorBase< BaseFloat > &  mean,
const TpMatrix< BaseFloat > &  sqrt_var,
MatrixBase< BaseFloat > *  feats 
)

Definition at line 72 of file model-test-common.cc.

References rnnlm::d, VectorBase< Real >::Dim(), KALDI_ASSERT, kaldi::kNoTrans, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), PackedMatrix< Real >::NumRows(), kaldi::RandGauss(), and MatrixBase< Real >::Row().

75  {
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 }
float RandGauss(struct RandomState *state=NULL)
Definition: kaldi-math.h:155
kaldi::int32 int32
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ RandPosdefSpMatrix()

void RandPosdefSpMatrix ( int32  dim,
SpMatrix< BaseFloat > *  matrix,
TpMatrix< BaseFloat > *  matrix_sqrt,
BaseFloat logdet 
)

Definition at line 30 of file model-test-common.cc.

References SpMatrix< Real >::AddMat2(), TpMatrix< Real >::Cholesky(), MatrixBase< Real >::Cond(), KALDI_LOG, kaldi::kNoTrans, SpMatrix< Real >::LogPosDefDet(), and MatrixBase< Real >::SetRandn().

Referenced by InitRandFullGmm().

31  {
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 }
#define KALDI_LOG
Definition: kaldi-error.h:153