nnet-compute-test.cc
Go to the documentation of this file.
1 // nnet2/nnet-compute-test.cc
2 
3 // Copyright 2014 Johns Hopkins University (author: Daniel Povey)
4 // Copyright 2015 David Snyder
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 "nnet2/nnet-nnet.h"
22 #include "nnet2/nnet-compute.h"
24 
25 namespace kaldi {
26 namespace nnet2 {
27 
28 
30  int32 input_dim = 10 + rand() % 40, output_dim = 100 + rand() % 500;
31  bool pad_input = (rand() % 2 == 0);
32 
33  Nnet *nnet = GenRandomNnet(input_dim, output_dim);
34  KALDI_LOG << "Left context = " << nnet->LeftContext() << ", right context = "
35  << nnet->RightContext() << ", pad-input = " << pad_input;
36  KALDI_LOG << "NNet info is " << nnet->Info();
37  int32 num_feats = 5 + rand() % 1000;
38  CuMatrix<BaseFloat> input(num_feats, input_dim);
39  input.SetRandn();
40 
41  int32 num_output_rows = num_feats -
42  (pad_input ? 0 : nnet->LeftContext() + nnet->RightContext());
43  if (num_output_rows <= 0)
44  return;
45  CuMatrix<BaseFloat> output1(num_output_rows, output_dim);
46  NnetComputation(*nnet, input, pad_input, &output1);
47  CuMatrix<BaseFloat> output2(output1.NumRows(), output1.NumCols());
48  int32 cur_input_pos = 0, cur_output_pos = 0;
49 
50  NnetOnlineComputer computer(*nnet, pad_input);
51  while (cur_input_pos <= num_feats) {
52  int32 feats_left = num_feats - cur_input_pos;
53  CuMatrix<BaseFloat> output_part;
54  if (feats_left > 0) {
55  int32 chunk_size = std::min<int32>(1 + rand() % 10, feats_left);
56  CuSubMatrix<BaseFloat> input_part(input, cur_input_pos, chunk_size,
57  0, input_dim);
58  computer.Compute(input_part, &output_part);
59  cur_input_pos += chunk_size;
60  } else {
61  computer.Flush(&output_part);
62  cur_input_pos++; // will terminate the loop.
63  }
64  if (output_part.NumRows() != 0) {
65  output2.Range(cur_output_pos, output_part.NumRows(),
66  0, output_dim).CopyFromMat(output_part);
67  cur_output_pos += output_part.NumRows();
68  }
69  }
70  AssertEqual(output1, output2);
71  for (int32 i = 0; i < output1.NumRows(); i++) {
72  // just double-check that the frames near the end are right, in case
73  // the test above somehow passed despite that.
74  if (i < 10 || output1.NumRows() - i < 10) {
75  CuSubVector<BaseFloat> vec1(output1, i), vec2(output2, i);
76  AssertEqual(vec1, vec2);
77  }
78  }
79  KALDI_LOG << "OK";
80  delete nnet;
81 }
82 
84  int32 input_dim = 10 + rand() % 40, output_dim = 100 + rand() % 500;
85  bool pad_input = true;
86 
87  Nnet *nnet = GenRandomNnet(input_dim, output_dim);
88  int32 num_feats = 100 + rand() % 500;
89  int32 chunk_size = num_feats / (2 + rand() % 10);
90  CuMatrix<BaseFloat> input(num_feats, input_dim);
91  input.SetRandn();
92 
93  KALDI_LOG << "Left context = " << nnet->LeftContext()
94  << ", right context = " << nnet->RightContext()
95  << ", chunk size = " << chunk_size;
96  KALDI_LOG << "NNet info is " << nnet->Info();
97 
98  int32 num_output_rows = num_feats;
99  CuMatrix<BaseFloat> cu_output1(num_output_rows, output_dim);
100  CuMatrix<BaseFloat> cu_output2(num_output_rows, output_dim);
101  NnetComputation(*nnet, input, pad_input, &cu_output1);
102  NnetComputationChunked(*nnet, CuMatrix<BaseFloat>(input), chunk_size,
103  &cu_output2);
104  Matrix<BaseFloat> output1(cu_output1);
105  Matrix<BaseFloat> output2(cu_output2);
106  AssertEqual(output1, output2);
107  for (int32 i = 0; i < output1.NumRows(); i++) {
108  // just double-check that the frames near the end are right, in case
109  // the test above somehow passed despite that.
110  if (i < 10 || output1.NumRows() - i < 10) {
111  SubVector<BaseFloat> vec1(output1, i), vec2(output2, i);
112  AssertEqual(vec1, vec2);
113  }
114  }
115  KALDI_LOG << "OK";
116  delete nnet;
117 }
118 
119 } // namespace nnet2
120 } // namespace kaldi
121 
122 #include "matrix/matrix-functions.h"
123 
124 
125 int main() {
126  using namespace kaldi;
127  using namespace kaldi::nnet2;
128 
129  for (int32 i = 0; i < 10; i++)
132  return 0;
133 }
134 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int32 LeftContext() const
Returns the left-context summed over all the Components...
Definition: nnet-nnet.cc:42
int main()
Nnet * GenRandomNnet(int32 input_dim, int32 output_dim)
This function generates a random neural net, for testing purposes.
Definition: nnet-nnet.cc:772
kaldi::int32 int32
void NnetComputationChunked(const Nnet &nnet, const CuMatrixBase< BaseFloat > &input, int32 chunk_size, CuMatrixBase< BaseFloat > *output)
Does the basic neural net computation, on a sequence of data (e.g.
This class represents a matrix that&#39;s stored on the GPU if we have one, and in memory if not...
Definition: matrix-common.h:71
void NnetComputation(const Nnet &nnet, const CuMatrixBase< BaseFloat > &input, bool pad_input, CuMatrixBase< BaseFloat > *output)
Does the basic neural net computation, on a sequence of data (e.g.
void Flush(CuMatrix< BaseFloat > *output)
void UnitTestNnetComputeChunked()
int32 RightContext() const
Returns the right-context summed over all the Components...
Definition: nnet-nnet.cc:56
std::string Info() const
Definition: nnet-nnet.cc:257
void UnitTestNnetCompute()
This class is used for a piece of a CuMatrix.
Definition: matrix-common.h:70
MatrixIndexT NumCols() const
Definition: cu-matrix.h:216
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void Compute(const CuMatrixBase< BaseFloat > &input, CuMatrix< BaseFloat > *output)
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
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:215
#define KALDI_LOG
Definition: kaldi-error.h:153
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501