posterior-test.cc
Go to the documentation of this file.
1 // hmm/posterior-test.cc
2 
3 // Copyright 2014 Johns Hopkins University (author: 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 "hmm/posterior.h"
21 #include "base/kaldi-math.h"
22 
23 namespace kaldi {
24 
25 
27  int32 n = 10 + rand () % 50, gselect = 1 + rand() % 5;
28  BaseFloat min_post = 0.1 + 0.8 * RandUniform();
29 
30  Vector<BaseFloat> loglikes(n);
31  loglikes.SetRandn();
32  loglikes.Scale(10.0);
33 
34  std::vector<std::pair<int32, BaseFloat> > post_entry;
35 
36  VectorToPosteriorEntry(loglikes, gselect, min_post, &post_entry);
37 
38  KALDI_ASSERT(post_entry.size() <= gselect);
39 
40  int32 max_elem;
41  loglikes.Max(&max_elem);
42  KALDI_ASSERT(post_entry[0].first == max_elem);
43 
44  KALDI_ASSERT(post_entry.back().second >= min_post);
45  KALDI_ASSERT(post_entry.back().second <= post_entry.front().second);
46 
47  BaseFloat sum = 0.0;
48  for (size_t i = 0; i < post_entry.size(); i++)
49  sum += post_entry[i].second;
50  KALDI_ASSERT(fabs(sum - 1.0) < 0.01);
51 }
52 
54  int32 post_size = RandInt(0, 5);
55  post_size = post_size * post_size;
56  Posterior post(post_size);
57  for (int32 i = 0; i < post.size(); i++) {
58  int32 s = RandInt(0, 3);
59  for (int32 j = 0; j < s; j++)
60  post[i].push_back(std::pair<int32,BaseFloat>(
61  RandInt(-10, 100), RandUniform()));
62  }
63  bool binary = (RandInt(0, 1) == 0);
64  std::ostringstream os;
65  WritePosterior(os, binary, post);
66  Posterior post2;
67  if (RandInt(0, 1) == 0)
68  post2 = post;
69  std::istringstream is(os.str());
70  ReadPosterior(is, binary, &post2);
71  if (binary) {
72  KALDI_ASSERT(post == post2);
73  } else {
74  KALDI_ASSERT(post.size() == post2.size());
75  for (int32 i = 0; i < post.size(); i++) {
76  KALDI_ASSERT(post[i].size() == post2[i].size());
77  for (int32 j = 0; j < post[i].size(); j++) {
78  KALDI_ASSERT(post[i][j].first == post2[i][j].first &&
79  fabs(post[i][j].second - post2[i][j].second) < 0.01);
80  }
81  }
82  }
83 }
84 }
85 
86 int main() {
87  // repeat the test ten times
88  for (int i = 0; i < 10; i++) {
91  }
92  std::cout << "Test OK.\n";
93 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
Definition: kaldi-math.h:151
int main()
BaseFloat VectorToPosteriorEntry(const VectorBase< BaseFloat > &log_likes, int32 num_gselect, BaseFloat min_post, std::vector< std::pair< int32, BaseFloat > > *post_entry)
Given a vector of log-likelihoods (typically of Gaussians in a GMM but could be of pdf-ids)...
Definition: posterior.cc:440
kaldi::int32 int32
std::vector< std::vector< std::pair< int32, BaseFloat > > > Posterior
Posterior is a typedef for storing acoustic-state (actually, transition-id) posteriors over an uttera...
Definition: posterior.h:42
struct rnnlm::@11::@12 n
Real Max() const
Returns the maximum value of any element, or -infinity for the empty vector.
void Scale(Real alpha)
Multiplies all elements by this constant.
void SetRandn()
Set vector to random normally-distributed noise.
A class representing a vector.
Definition: kaldi-vector.h:406
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void TestPosteriorIo()
void TestVectorToPosteriorEntry()
void WritePosterior(std::ostream &os, bool binary, const Posterior &post)
stand-alone function for writing a Posterior.
Definition: posterior.cc:32
void ReadPosterior(std::istream &is, bool binary, Posterior *post)
stand-alone function for reading a Posterior.
Definition: posterior.cc:64
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95