nnet-example-functions-test.cc
Go to the documentation of this file.
1 // nnet2/nnet-example-functions-test.cc
2 
3 // Copyright 2013 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 
21 #include "util/common-utils.h"
22 
23 namespace kaldi {
24 namespace nnet2 {
25 
26 // Note: most of these functions we're testing from the command line,
27 // this is just to test the function to solve the packing problem.
28 
30  size_t size = Rand() % 20;
31  std::vector<BaseFloat> item_costs;
32  for (size_t i = 0; i < size; i++) {
33  item_costs.push_back(0.5 * (Rand() % 15));
34  }
35  BaseFloat max_cost = 0.66 + Rand() % 5;
36 
37  std::vector<std::vector<size_t> > groups;
38  SolvePackingProblem(max_cost, item_costs, &groups);
39 
40  std::vector<size_t> all_indices;
41  for (size_t i = 0; i < groups.size(); i++) {
42  BaseFloat this_group_cost = 0.0;
43  for (size_t j = 0; j < groups[i].size(); j++) {
44  size_t index = groups[i][j];
45  all_indices.push_back(index);
46  this_group_cost += item_costs[index];
47  }
48  KALDI_ASSERT(!groups[i].empty());
49  KALDI_ASSERT(groups[i].size() == 1 || this_group_cost <= max_cost);
50  }
51  SortAndUniq(&all_indices);
52  KALDI_ASSERT(all_indices.size() == size);
53  if (!all_indices.empty())
54  KALDI_ASSERT(all_indices.back() + 1 == size);
55 }
56 
57 
58 } // namespace nnet2
59 } // namespace kaldi
60 
61 
62 int main() {
63  using namespace kaldi;
64  using namespace kaldi::nnet2;
65  using kaldi::int32;
66  for (int32 i = 0; i < 10; i++)
68 }
69 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
kaldi::int32 int32
void SortAndUniq(std::vector< T > *vec)
Sorts and uniq&#39;s (removes duplicates) from a vector.
Definition: stl-utils.h:39
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Note on how to parse this filename: it contains functions relatied to neural-net training examples...
void SolvePackingProblem(BaseFloat max_cost, const std::vector< BaseFloat > &costs, std::vector< std::vector< size_t > > *groups)
This function solves the "packing problem" using the "first fit" algorithm.