nnet-shuffle-egs-discriminative.cc
Go to the documentation of this file.
1 // nnet2bin/nnet-shuffle-egs-discriminative.cc
2 
3 // Copyright 2012-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 
20 #include "base/kaldi-common.h"
21 #include "util/common-utils.h"
22 #include "hmm/transition-model.h"
24 
25 int main(int argc, char *argv[]) {
26  try {
27  using namespace kaldi;
28  using namespace kaldi::nnet2;
29  typedef kaldi::int32 int32;
30  typedef kaldi::int64 int64;
31 
32  const char *usage =
33  "Copy examples (typically single frames) for neural network training,\n"
34  "from the input to output, but randomly shuffle the order. This program will keep\n"
35  "all of the examples in memory at once, so don't give it too many.\n"
36  "\n"
37  "Usage: nnet-shuffle-egs-discriminative [options] <egs-rspecifier> <egs-wspecifier>\n"
38  "\n"
39  "nnet-shuffle-egs-discriminative --srand=1 ark:train.degs ark:shuffled.degs\n";
40 
41  int32 srand_seed = 0;
42  int32 buffer_size = 0;
43  ParseOptions po(usage);
44  po.Register("srand", &srand_seed, "Seed for random number generator ");
45  po.Register("buffer-size", &buffer_size, "If >0, size of a buffer we use "
46  "to do limited-memory partial randomization. Otherwise, do "
47  "full randomization.");
48 
49  po.Read(argc, argv);
50 
51  srand(srand_seed);
52 
53  if (po.NumArgs() != 2) {
54  po.PrintUsage();
55  exit(1);
56  }
57 
58  std::string examples_rspecifier = po.GetArg(1),
59  examples_wspecifier = po.GetArg(2);
60 
61  int64 num_done = 0;
62 
63  std::vector<DiscriminativeNnetExample*> egs;
65  examples_rspecifier);
66  DiscriminativeNnetExampleWriter example_writer(
67  examples_wspecifier);
68  if (buffer_size == 0) { // Do full randomization
69  // Putting in an extra level of indirection here to avoid excessive
70  // computation and memory demands when we have to resize the vector.
71 
72  for (; !example_reader.Done(); example_reader.Next())
73  egs.push_back(new DiscriminativeNnetExample(
74  example_reader.Value()));
75 
76  std::random_shuffle(egs.begin(), egs.end());
77  } else {
78  KALDI_ASSERT(buffer_size > 0);
79  egs.resize(buffer_size, NULL);
80  for (; !example_reader.Done(); example_reader.Next()) {
81  int32 index = RandInt(0, buffer_size - 1);
82  if (egs[index] == NULL) {
83  egs[index] = new DiscriminativeNnetExample(example_reader.Value());
84  } else {
85  std::ostringstream ostr;
86  ostr << num_done;
87  example_writer.Write(ostr.str(), *(egs[index]));
88  *(egs[index]) = example_reader.Value();
89  num_done++;
90  }
91  }
92  }
93  for (size_t i = 0; i < egs.size(); i++) {
94  std::ostringstream ostr;
95  ostr << num_done;
96  if (egs[i] != NULL) {
97  example_writer.Write(ostr.str(), *(egs[i]));
98  delete egs[i];
99  }
100  num_done++;
101  }
102 
103  KALDI_LOG << "Shuffled order of " << num_done
104  << " neural-network training examples "
105  << (buffer_size ? "using a buffer (partial randomization)" : "");
106 
107  return (num_done == 0 ? 1 : 0);
108  } catch(const std::exception &e) {
109  std::cerr << e.what() << '\n';
110  return -1;
111  }
112 }
113 
114 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int main(int argc, char *argv[])
int NumArgs() const
Number of positional parameters (c.f. argc-1).
This struct is used to store the information we need for discriminative training (MMI or MPE)...
Definition: nnet-example.h:136
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
#define KALDI_LOG
Definition: kaldi-error.h:153
Note on how to parse this filename: it contains functions relatied to neural-net training examples...
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95