mle-am-diag-gmm-test.cc File Reference
Include dependency graph for mle-am-diag-gmm-test.cc:

Go to the source code of this file.

Functions

void TestAmDiagGmmAccsIO (const AmDiagGmm &am_gmm, const Matrix< BaseFloat > &feats)
 
void UnitTestMleAmDiagGmm ()
 
int main ()
 

Function Documentation

◆ main()

int main ( )

Definition at line 132 of file mle-am-diag-gmm-test.cc.

References rnnlm::i, and UnitTestMleAmDiagGmm().

132  {
133 // std::srand(time(NULL));
134  for (int i = 0; i < 10; i++)
136  std::cout << "Test OK.\n";
137  return 0;
138 }
void UnitTestMleAmDiagGmm()

◆ TestAmDiagGmmAccsIO()

void TestAmDiagGmmAccsIO ( const AmDiagGmm am_gmm,
const Matrix< BaseFloat > &  feats 
)

Definition at line 34 of file mle-am-diag-gmm-test.cc.

References AccumAmDiagGmm::AccumulateForGmm(), kaldi::AssertEqual(), AmDiagGmm::CopyFromAmDiagGmm(), rnnlm::i, AccumAmDiagGmm::Init(), KALDI_LOG, kaldi::kGmmAll, AmDiagGmm::LogLikelihood(), kaldi::MleAmDiagGmmUpdate(), AmDiagGmm::NumPdfs(), MatrixBase< Real >::NumRows(), kaldi::RandInt(), AccumAmDiagGmm::Read(), MatrixBase< Real >::Row(), Input::Stream(), AccumAmDiagGmm::TotCount(), AccumAmDiagGmm::TotLogLike(), and AccumAmDiagGmm::Write().

Referenced by UnitTestMleAmDiagGmm().

35  {
37  AccumAmDiagGmm accs;
38  accs.Init(am_gmm, flags);
39  BaseFloat loglike = 0.0;
40  for (int32 i = 0; i < feats.NumRows(); i++) {
41  int32 state = RandInt(0, am_gmm.NumPdfs()-1);
42  loglike += accs.AccumulateForGmm(am_gmm, feats.Row(i), state, 1.0);
43  }
44  KALDI_LOG << "Data log-likelihood = " << loglike << " over "
45  << feats.NumRows() << " frames.";
46  KALDI_LOG << "Accumulated values: log-like = " << accs.TotLogLike()
47  << ", # frames = " << accs.TotCount();
48  AssertEqual(accs.TotLogLike(), loglike, 1e-5);
49  AssertEqual(accs.TotCount(), static_cast<BaseFloat>(feats.NumRows()), 1e-5);
50 
51  MleDiagGmmOptions config;
52  AmDiagGmm *am_gmm1 = new AmDiagGmm();
53  am_gmm1->CopyFromAmDiagGmm(am_gmm);
54  MleAmDiagGmmUpdate(config, accs, flags, am_gmm1, NULL, NULL);
55 
56  int32 check_pdf = RandInt(0, am_gmm.NumPdfs()-1),
57  check_frame = RandInt(0, feats.NumRows()-1);
58  BaseFloat loglike1 = am_gmm1->LogLikelihood(check_pdf, feats.Row(check_frame));
59  delete am_gmm1;
60 
61  // First, non-binary write
62  accs.Write(kaldi::Output("tmpf", false).Stream(), false);
63  bool binary_in;
64  AccumAmDiagGmm *accs1 = new AccumAmDiagGmm();
65  // Non-binary read
66  kaldi::Input ki1("tmpf", &binary_in);
67  accs1->Read(ki1.Stream(), binary_in, false);
68  AmDiagGmm *am_gmm2 = new AmDiagGmm();
69  am_gmm2->CopyFromAmDiagGmm(am_gmm);
70  MleAmDiagGmmUpdate(config, accs, flags, am_gmm2, NULL, NULL);
71  BaseFloat loglike2 = am_gmm2->LogLikelihood(check_pdf, feats.Row(check_frame));
72  kaldi::AssertEqual(loglike1, loglike2, 1e-6);
73  delete am_gmm2;
74  delete accs1;
75 
76  // Next, binary write
77  accs.Write(kaldi::Output("tmpfb", true).Stream(), true);
78  AccumAmDiagGmm *accs2 = new AccumAmDiagGmm();
79  // Binary read
80  kaldi::Input ki2("tmpfb", &binary_in);
81  accs2->Read(ki2.Stream(), binary_in, false);
82  AmDiagGmm *am_gmm3 = new AmDiagGmm();
83  am_gmm3->CopyFromAmDiagGmm(am_gmm);
84  MleAmDiagGmmUpdate(config, accs, flags, am_gmm3, NULL, NULL);
85  BaseFloat loglike3 = am_gmm3->LogLikelihood(check_pdf, feats.Row(check_frame));
86  kaldi::AssertEqual(loglike1, loglike3, 1e-6);
87  delete am_gmm3;
88  delete accs2;
89 
90  unlink("tmpf");
91  unlink("tmpfb");
92 }
void MleAmDiagGmmUpdate(const MleDiagGmmOptions &config, const AccumAmDiagGmm &am_diag_gmm_acc, GmmFlagsType flags, AmDiagGmm *am_gmm, BaseFloat *obj_change_out, BaseFloat *count_out)
for computing the maximum-likelihood estimates of the parameters of an acoustic model that uses diago...
void CopyFromAmDiagGmm(const AmDiagGmm &other)
Copies the parameters from another model. Allocates necessary memory.
Definition: am-diag-gmm.cc:79
BaseFloat AccumulateForGmm(const AmDiagGmm &model, const VectorBase< BaseFloat > &data, int32 gmm_index, BaseFloat weight)
Accumulate stats for a single GMM in the model; returns log likelihood.
kaldi::int32 int32
BaseFloat TotCount() const
uint16 GmmFlagsType
Bitwise OR of the above flags.
Definition: model-common.h:35
float BaseFloat
Definition: kaldi-types.h:29
BaseFloat TotLogLike() const
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
BaseFloat LogLikelihood(const int32 pdf_index, const VectorBase< BaseFloat > &data) const
Definition: am-diag-gmm.h:108
Configuration variables like variance floor, minimum occupancy, etc.
Definition: mle-diag-gmm.h:38
void Read(std::istream &in_stream, bool binary, bool add=false)
int32 NumPdfs() const
Definition: am-diag-gmm.h:82
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void Write(std::ostream &out_stream, bool binary) const
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
Definition: kaldi-math.h:276
#define KALDI_LOG
Definition: kaldi-error.h:153
void Init(const AmDiagGmm &model, GmmFlagsType flags)
Initializes accumulators for each GMM based on the number of components and dimension.
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95

◆ UnitTestMleAmDiagGmm()

void UnitTestMleAmDiagGmm ( )

Definition at line 94 of file mle-am-diag-gmm-test.cc.

References AmDiagGmm::AddPdf(), rnnlm::d, kaldi::Exp(), rnnlm::i, kaldi::unittest::InitRandDiagGmm(), kaldi::unittest::RandDiagGaussFeatures(), kaldi::RandGauss(), kaldi::RandInt(), Matrix< Real >::Resize(), MatrixBase< Real >::Row(), and TestAmDiagGmmAccsIO().

Referenced by main().

94  {
95  int32 dim = 1 + kaldi::RandInt(0, 9), // random dimension of the gmm
96  num_pdfs = 5 + kaldi::RandInt(0, 9); // random number of states
97 
98  AmDiagGmm am_gmm;
99  int32 total_num_comp = 0;
100  for (int32 i = 0; i < num_pdfs; i++) {
101  int32 num_comp = 1 + kaldi::RandInt(0, 9); // random mixture size
102  kaldi::DiagGmm gmm;
103  ut::InitRandDiagGmm(dim, num_comp, &gmm);
104  am_gmm.AddPdf(gmm);
105  total_num_comp += num_comp;
106  }
107 
109 
110  { // First, generate random means and variances
111  int32 num_feat_comp = total_num_comp + kaldi::RandInt(-total_num_comp/2,
112  total_num_comp/2);
113  kaldi::Matrix<BaseFloat> means(num_feat_comp, dim),
114  vars(num_feat_comp, dim);
115  for (int32 m = 0; m < num_feat_comp; m++) {
116  for (int32 d= 0; d < dim; d++) {
117  means(m, d) = kaldi::RandGauss();
118  vars(m, d) = Exp(kaldi::RandGauss()) + 1e-2;
119  }
120  }
121  // Now generate random features with those means and variances.
122  feats.Resize(num_feat_comp * 200, dim);
123  for (int32 m = 0; m < num_feat_comp; m++) {
124  kaldi::SubMatrix<BaseFloat> tmp(feats, m*200, 200, 0, dim);
125  ut::RandDiagGaussFeatures(200, means.Row(m), vars.Row(m), &tmp);
126  }
127  }
128  TestAmDiagGmmAccsIO(am_gmm, feats);
129 }
double Exp(double x)
Definition: kaldi-math.h:83
void AddPdf(const DiagGmm &gmm)
Adds a GMM to the model, and increments the total number of PDFs.
Definition: am-diag-gmm.cc:57
float RandGauss(struct RandomState *state=NULL)
Definition: kaldi-math.h:155
kaldi::int32 int32
void InitRandDiagGmm(int32 dim, int32 num_comp, DiagGmm *gmm)
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 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).
void TestAmDiagGmmAccsIO(const AmDiagGmm &am_gmm, const Matrix< BaseFloat > &feats)
Sub-matrix representation.
Definition: kaldi-matrix.h:988
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95