nnet-common-test.cc
Go to the documentation of this file.
1 // nnet3/nnet-common-test.cc
2 
3 // Copyright 2015 Johns Hopkins University (author: Daniel Povey)
4 // 2016 Xiaohui Zhang
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include "nnet3/nnet-common.h"
22 
23 namespace kaldi {
24 namespace nnet3 {
25 
26 
27 
29  std::vector<Index> indexes(RandInt(0, 10));
30 
31  for (int32 i = 0; i < indexes.size(); i++) {
32  if (i == 0 || RandInt(0, 1) == 0) {
33  indexes[i].n = RandInt(-1, 2);
34  indexes[i].t = RandInt(-150, 150);
35  indexes[i].x = RandInt(-1, 1);
36  } else {
37  // this case gets optimized while writing. (if abs(diff-in-t) < 125).
38  indexes[i].n = indexes[i-1].n;
39  indexes[i].t = indexes[i-1].t + RandInt(-127, 127);
40  indexes[i].x = indexes[i-1].x;
41  }
42  }
43 
44  std::ostringstream os;
45  bool binary = (RandInt(0, 1) == 0);
46  WriteIndexVector(os, binary, indexes);
47 
48  std::vector<Index> indexes2;
49  if (RandInt(0, 1) == 0)
50  indexes2 = indexes;
51  std::istringstream is(os.str());
52  ReadIndexVector(is, binary, &indexes2);
53  if (indexes != indexes2) {
54  WriteIndexVector(std::cerr, false, indexes);
55  std::cerr << " vs. \n";
56  WriteIndexVector(std::cerr, false, indexes2);
57  std::cerr << "\n";
58  KALDI_ERR << "Indexes differ.";
59  }
60 }
61 
63  std::vector<Cindex> cindexes(RandInt(0, 15));
64 
65  for (int32 i = 0; i < cindexes.size(); i++) {
66  if (i == 0 || RandInt(0, 4) == 0) {
67  cindexes[i].first = RandInt(-256, 256);
68  } else {
69  cindexes[i].first = cindexes[i-1].first;
70  }
71  Index &index = cindexes[i].second;
72  if (i == 0) {
73  if (RandInt(0, 3) == 0) {
74  index.n = 0;
75  index.x = 0;
76  if (RandInt(0, 1) == 0)
77  index.t = RandInt(-5, 5);
78  else if (RandInt(0, 1) == 0) {
79  index.t = 124;
80  } else if (RandInt(0, 1) == 0) {
81  index.t = -124;
82  } else if (RandInt(0, 1) == 0) {
83  index.t = std::numeric_limits<int32>::min();
84  } else {
85  index.t = 0;
86  }
87  } else if (RandInt(0, 1) == 0) {
88  index.t = 0;
89  index.x = 0;
90  index.n = RandInt(0, 1);
91  } else {
92  index.t = RandInt(-3, 3);
93  if (RandInt(0, 1) == 0)
94  index.t = std::numeric_limits<int32>::min();
95  index.x = RandInt(-1,1);
96  index.n = RandInt(-1,1);
97  }
98  } else {
99  if (RandInt(0, 3) == 0) {
100  cindexes[i].second.n = cindexes[i-1].second.n;
101  cindexes[i].second.x = cindexes[i-1].second.x;
102  if (RandInt(0, 1) == 0) {
103  cindexes[i].second.t = cindexes[i-1].second.t + RandInt(-127, 127);
104  } else if (RandInt(0, 1) == 0) {
105  cindexes[i].second.t = cindexes[i-1].second.t + 124;
106  } else if (RandInt(0, 1) == 0) {
107  cindexes[i].second.t = cindexes[i-1].second.t + -124;
108  } else if (RandInt(0, 1) == 0) {
109  cindexes[i].second.t = std::numeric_limits<int32>::min();
110  } else {
111  cindexes[i].second.t = RandInt(-2, 2);
112  }
113  } else if (RandInt(0, 1) == 0) {
114  cindexes[i].second.t = cindexes[i-1].second.t;
115  cindexes[i].second.x = cindexes[i-1].second.x;
116  cindexes[i].second.n = cindexes[i-1].second.n + RandInt(-2,2);
117  } else if (RandInt(0, 1) == 0) {
118  cindexes[i].second.t = cindexes[i-1].second.t + RandInt(-2, 2);
119  cindexes[i].second.x = cindexes[i-1].second.x + RandInt(-2, 2);
120  cindexes[i].second.n = cindexes[i-1].second.n + RandInt(-2,2);
121  if (RandInt(0, 3) == 0)
122  cindexes[i].second.t = std::numeric_limits<int32>::min();
123 
124  } else {
125  cindexes[i].second.t = RandInt(-128, 128);
126  cindexes[i].second.x = RandInt(-128, 128);
127  cindexes[i].second.n = RandInt(-128, 128);
128  if (RandInt(0, 3) == 0)
129  cindexes[i].second.t = std::numeric_limits<int32>::min();
130  }
131  }
132  }
133 
134  if (RandInt(0, 10) == 0) {
135  // trying to reproduce a failure
136  Cindex temp(0, Index(0, 0, 0));
137  cindexes.clear();
138  cindexes.resize(4, temp);
139  cindexes[RandInt(0, 3)].second.t = std::numeric_limits<int32>::min();
140  }
141 
142  std::ostringstream os;
143  bool binary = (RandInt(0, 1) == 0);
144  WriteCindexVector(os, binary, cindexes);
145  std::vector<Cindex> cindexes2;
146  if (RandInt(0, 1) == 0)
147  cindexes2 = cindexes;
148  std::istringstream is(os.str());
149  ReadCindexVector(is, binary, &cindexes2);
150 
151  std::ostringstream os2;
152  WriteCindexVector(os2, binary, cindexes2);
153 
154  if (cindexes != cindexes2 || os.str() != os2.str()) {
155  WriteCindexVector(std::cerr, false, cindexes);
156  std::cerr << " vs. \n";
157  WriteCindexVector(std::cerr, false, cindexes2);
158  std::cerr << "\n";
159  KALDI_ERR << "Indexes differ.";
160  }
161 }
162 
163 } // namespace nnet3
164 } // namespace kaldi
165 
166 int main() {
167  using namespace kaldi;
168  using namespace kaldi::nnet3;
169 
170  for (int32 i = 0; i < 100; i++) {
171  UnitTestIndexIo();
173  }
174 
175  KALDI_LOG << "Nnet-common tests succeeded.";
176 
177  return 0;
178 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void WriteIndexVector(std::ostream &os, bool binary, const std::vector< Index > &vec)
Definition: nnet-common.cc:126
void ReadCindexVector(std::istream &is, bool binary, std::vector< Cindex > *vec)
Definition: nnet-common.cc:309
void UnitTestIndexIo()
void UnitTestCindexIo()
kaldi::int32 int32
struct Index is intended to represent the various indexes by which we number the rows of the matrices...
Definition: nnet-common.h:44
std::pair< int32, Index > Cindex
Definition: nnet-common.h:115
void WriteCindexVector(std::ostream &os, bool binary, const std::vector< Cindex > &vec)
Definition: nnet-common.cc:282
#define KALDI_ERR
Definition: kaldi-error.h:147
int main()
void ReadIndexVector(std::istream &is, bool binary, std::vector< Index > *vec)
Definition: nnet-common.cc:143
#define KALDI_LOG
Definition: kaldi-error.h:153
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95