nnet-shuffle-egs-discriminative.cc File Reference
Include dependency graph for nnet-shuffle-egs-discriminative.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 25 of file nnet-shuffle-egs-discriminative.cc.

References SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), rnnlm::i, KALDI_ASSERT, KALDI_LOG, SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), kaldi::RandInt(), ParseOptions::Read(), ParseOptions::Register(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

25  {
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 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
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(std::ostream &os, bool binary) const
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
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
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95