nnet-example-test.cc
Go to the documentation of this file.
1 // nnet3/nnet-example-test.cc
2 
3 // Copyright 2015 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 "nnet3/nnet-nnet.h"
21 #include "nnet3/nnet-compile.h"
22 #include "nnet3/nnet-analyze.h"
23 #include "nnet3/nnet-test-utils.h"
24 #include "nnet3/nnet-optimize.h"
25 #include "nnet3/nnet-compute.h"
26 #include "nnet3/nnet-example.h"
28 #include "base/kaldi-math.h"
29 
30 namespace kaldi {
31 namespace nnet3 {
32 
33 
34 
36  for (int32 n = 0; n < 50; n++) {
37 
38  NnetExample eg;
39  int32 num_supervised_frames = RandInt(1, 10),
40  left_context = RandInt(0, 5),
41  right_context = RandInt(0, 5),
42  input_dim = RandInt(1, 10),
43  output_dim = RandInt(5, 10),
44  ivector_dim = RandInt(-1, 2);
45  GenerateSimpleNnetTrainingExample(num_supervised_frames, left_context,
46  right_context, input_dim, output_dim,
47  ivector_dim, &eg);
48  bool binary = (RandInt(0, 1) == 0);
49  std::ostringstream os;
50  eg.Write(os, binary);
51  NnetExample eg_copy;
52  if (RandInt(0, 1) == 0)
53  eg_copy = eg;
54  std::istringstream is(os.str());
55  eg_copy.Read(is, binary);
56  std::ostringstream os2;
57  eg_copy.Write(os2, binary);
58  if (binary) {
59  KALDI_ASSERT(os.str() == os2.str());
60  KALDI_ASSERT(eg_copy == eg);
61  }
62  KALDI_ASSERT(ExampleApproxEqual(eg, eg_copy, 0.1));
63  }
64 }
65 
66 
68  for (int32 n = 0; n < 50; n++) {
69  int32 num_supervised_frames = RandInt(1, 10),
70  left_context = RandInt(0, 5),
71  right_context = RandInt(0, 5),
72  input_dim = RandInt(1, 10),
73  output_dim = RandInt(5, 10),
74  ivector_dim = RandInt(-1, 2);
75 
76  int32 num_egs = RandInt(1, 4);
77  std::vector<NnetExample> egs_to_be_merged(num_egs);
78  for (int32 i = 0; i < num_egs; i++) {
79  NnetExample eg;
80  // sometimes omit the ivector. just tests things a bit more
81  // thoroughly.
82  GenerateSimpleNnetTrainingExample(num_supervised_frames, left_context,
83  right_context, input_dim, output_dim,
84  RandInt(0, 1) == 0 ? 0 : ivector_dim,
85  &eg);
86  KALDI_LOG << i << "'th example to be merged is: ";
87  eg.Write(std::cerr, false);
88  egs_to_be_merged[i].Swap(&eg);
89  }
90  NnetExample eg_merged;
91  bool compress = (RandInt(0, 1) == 0);
92  MergeExamples(egs_to_be_merged, compress, &eg_merged);
93  KALDI_LOG << "Merged example is: ";
94  eg_merged.Write(std::cerr, false);
95  }
96 }
97 
98 
99 
100 } // namespace nnet3
101 } // namespace kaldi
102 
103 int main() {
104  using namespace kaldi;
105  using namespace kaldi::nnet3;
106 
109 
110  KALDI_LOG << "Nnet-example tests succeeded.";
111 
112  return 0;
113 }
114 
NnetExample is the input data and corresponding label (or labels) for one or more frames of input...
Definition: nnet-example.h:111
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void GenerateSimpleNnetTrainingExample(int32 num_supervised_frames, int32 left_context, int32 right_context, int32 output_dim, int32 input_dim, int32 ivector_dim, NnetExample *example)
Low-level function that generates an nnet training example.
void UnitTestNnetMergeExamples()
int main()
This file contains various routines that are useful in test code.
This file contains utilities for analyzing and checking computations, which are used in the optimizat...
kaldi::int32 int32
void UnitTestNnetExample()
void Write(std::ostream &os, bool binary) const
struct rnnlm::@11::@12 n
bool ExampleApproxEqual(const NnetExample &eg1, const NnetExample &eg2, BaseFloat delta)
Returns true if the examples are approximately equal (only intended to be used in testing)...
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void Read(std::istream &is, bool binary)
#define KALDI_LOG
Definition: kaldi-error.h:153
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95
void MergeExamples(const std::vector< NnetExample > &src, bool compress, NnetExample *merged_eg)
Merge a set of input examples into a single example (typically the size of "src" will be the minibatc...