nnet3-shuffle-egs.cc File Reference
Include dependency graph for nnet3-shuffle-egs.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 26 of file nnet3-shuffle-egs.cc.

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

26  {
27  try {
28  using namespace kaldi;
29  using namespace kaldi::nnet3;
30  typedef kaldi::int32 int32;
31  typedef kaldi::int64 int64;
32 
33  const char *usage =
34  "Copy examples (typically single frames or small groups of frames) for\n"
35  "neural network training, from the input to output, but randomly shuffle the order.\n"
36  "This program will keep all of the examples in memory at once, unless you\n"
37  "use the --buffer-size option\n"
38  "\n"
39  "Usage: nnet3-shuffle-egs [options] <egs-rspecifier> <egs-wspecifier>\n"
40  "\n"
41  "nnet3-shuffle-egs --srand=1 ark:train.egs ark:shuffled.egs\n";
42 
43  int32 srand_seed = 0;
44  int32 buffer_size = 0;
45  ParseOptions po(usage);
46  po.Register("srand", &srand_seed, "Seed for random number generator ");
47  po.Register("buffer-size", &buffer_size, "If >0, size of a buffer we use "
48  "to do limited-memory partial randomization. Otherwise, do "
49  "full randomization.");
50 
51  po.Read(argc, argv);
52 
53  srand(srand_seed);
54 
55  if (po.NumArgs() != 2) {
56  po.PrintUsage();
57  exit(1);
58  }
59 
60  std::string examples_rspecifier = po.GetArg(1),
61  examples_wspecifier = po.GetArg(2);
62 
63  int64 num_done = 0;
64 
65  std::vector<std::pair<std::string, NnetExample*> > egs;
66 
67  SequentialNnetExampleReader example_reader(examples_rspecifier);
68  NnetExampleWriter example_writer(examples_wspecifier);
69  if (buffer_size == 0) { // Do full randomization
70  // Putting in an extra level of indirection here to avoid excessive
71  // computation and memory demands when we have to resize the vector.
72 
73  for (; !example_reader.Done(); example_reader.Next())
74  egs.push_back(std::make_pair(example_reader.Key(),
75  new NnetExample(example_reader.Value())));
76 
77  std::random_shuffle(egs.begin(), egs.end());
78  } else {
79  KALDI_ASSERT(buffer_size > 0);
80  egs.resize(buffer_size,
81  std::pair<std::string, NnetExample*>("", NULL));
82  for (; !example_reader.Done(); example_reader.Next()) {
83  int32 index = RandInt(0, buffer_size - 1);
84  if (egs[index].second == NULL) {
85  egs[index] = std::make_pair(example_reader.Key(),
86  new NnetExample(example_reader.Value()));
87  } else {
88  example_writer.Write(egs[index].first, *(egs[index].second));
89  egs[index].first = example_reader.Key();
90  *(egs[index].second) = example_reader.Value();
91  num_done++;
92  }
93  }
94  }
95  for (size_t i = 0; i < egs.size(); i++) {
96  if (egs[i].second != NULL) {
97  example_writer.Write(egs[i].first, *(egs[i].second));
98  delete egs[i].second;
99  num_done++;
100  }
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 }
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
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
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
#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