posterior-test.cc
2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// hmm/posterior-test.cc
// Copyright 2014 Johns Hopkins University (author: Daniel Povey)
// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
#include "hmm/posterior.h"
#include "base/kaldi-math.h"
namespace kaldi {
void TestVectorToPosteriorEntry() {
int32 n = 10 + rand () % 50, gselect = 1 + rand() % 5;
BaseFloat min_post = 0.1 + 0.8 * RandUniform();
Vector<BaseFloat> loglikes(n);
loglikes.SetRandn();
loglikes.Scale(10.0);
std::vector<std::pair<int32, BaseFloat> > post_entry;
VectorToPosteriorEntry(loglikes, gselect, min_post, &post_entry);
KALDI_ASSERT(post_entry.size() <= gselect);
int32 max_elem;
loglikes.Max(&max_elem);
KALDI_ASSERT(post_entry[0].first == max_elem);
KALDI_ASSERT(post_entry.back().second >= min_post);
KALDI_ASSERT(post_entry.back().second <= post_entry.front().second);
BaseFloat sum = 0.0;
for (size_t i = 0; i < post_entry.size(); i++)
sum += post_entry[i].second;
KALDI_ASSERT(fabs(sum - 1.0) < 0.01);
}
void TestPosteriorIo() {
int32 post_size = RandInt(0, 5);
post_size = post_size * post_size;
Posterior post(post_size);
for (int32 i = 0; i < post.size(); i++) {
int32 s = RandInt(0, 3);
for (int32 j = 0; j < s; j++)
post[i].push_back(std::pair<int32,BaseFloat>(
RandInt(-10, 100), RandUniform()));
}
bool binary = (RandInt(0, 1) == 0);
std::ostringstream os;
WritePosterior(os, binary, post);
Posterior post2;
if (RandInt(0, 1) == 0)
post2 = post;
std::istringstream is(os.str());
ReadPosterior(is, binary, &post2);
if (binary) {
KALDI_ASSERT(post == post2);
} else {
KALDI_ASSERT(post.size() == post2.size());
for (int32 i = 0; i < post.size(); i++) {
KALDI_ASSERT(post[i].size() == post2[i].size());
for (int32 j = 0; j < post[i].size(); j++) {
KALDI_ASSERT(post[i][j].first == post2[i][j].first &&
fabs(post[i][j].second - post2[i][j].second) < 0.01);
}
}
}
}
}
int main() {
// repeat the test ten times
for (int i = 0; i < 10; i++) {
kaldi::TestVectorToPosteriorEntry();
kaldi::TestPosteriorIo();
}
std::cout << "Test OK.\n";
}