nnet-train-simple.cc File Reference
Include dependency graph for nnet-train-simple.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 27 of file nnet-train-simple.cc.

References ParseOptions::GetArg(), AmNnet::GetNnet(), KALDI_LOG, ParseOptions::NumArgs(), ParseOptions::PrintUsage(), AmNnet::Read(), ParseOptions::Read(), TransitionModel::Read(), NnetSimpleTrainerConfig::Register(), ParseOptions::Register(), Output::Stream(), Input::Stream(), kaldi::nnet2::TrainNnetSimple(), AmNnet::Write(), TransitionModel::Write(), and Nnet::ZeroStats().

27  {
28  try {
29  using namespace kaldi;
30  using namespace kaldi::nnet2;
31  typedef kaldi::int32 int32;
32  typedef kaldi::int64 int64;
33 
34  const char *usage =
35  "Train the neural network parameters with backprop and stochastic\n"
36  "gradient descent using minibatches. Training examples would be\n"
37  "produced by nnet-get-egs.\n"
38  "\n"
39  "Usage: nnet-train-simple [options] <model-in> <training-examples-in> <model-out>\n"
40  "\n"
41  "e.g.:\n"
42  "nnet-train-simple 1.nnet ark:1.egs 2.nnet\n";
43 
44  bool binary_write = true;
45  bool zero_stats = true;
46  int32 srand_seed = 0;
47  std::string use_gpu = "yes";
48  NnetSimpleTrainerConfig train_config;
49 
50  ParseOptions po(usage);
51  po.Register("binary", &binary_write, "Write output in binary mode");
52  po.Register("zero-stats", &zero_stats, "If true, zero occupation "
53  "counts stored with the neural net (only affects mixing up).");
54  po.Register("srand", &srand_seed, "Seed for random number generator "
55  "(relevant if you have layers of type AffineComponentPreconditioned "
56  "with l2-penalty != 0.0");
57  po.Register("use-gpu", &use_gpu,
58  "yes|no|optional|wait, only has effect if compiled with CUDA");
59 
60  train_config.Register(&po);
61 
62  po.Read(argc, argv);
63 
64  if (po.NumArgs() != 3) {
65  po.PrintUsage();
66  exit(1);
67  }
68  srand(srand_seed);
69 
70 #if HAVE_CUDA==1
71  CuDevice::Instantiate().SelectGpuId(use_gpu);
72 #endif
73 
74  std::string nnet_rxfilename = po.GetArg(1),
75  examples_rspecifier = po.GetArg(2),
76  nnet_wxfilename = po.GetArg(3);
77 
78  int64 num_examples;
79 
80  {
81  TransitionModel trans_model;
82  AmNnet am_nnet;
83  {
84  bool binary_read;
85  Input ki(nnet_rxfilename, &binary_read);
86  trans_model.Read(ki.Stream(), binary_read);
87  am_nnet.Read(ki.Stream(), binary_read);
88  }
89 
90  if (zero_stats) am_nnet.GetNnet().ZeroStats();
91 
92  SequentialNnetExampleReader example_reader(examples_rspecifier);
93 
94  num_examples = TrainNnetSimple(train_config, &(am_nnet.GetNnet()),
95  &example_reader);
96 
97  {
98  Output ko(nnet_wxfilename, binary_write);
99  trans_model.Write(ko.Stream(), binary_write);
100  am_nnet.Write(ko.Stream(), binary_write);
101  }
102  }
103 #if HAVE_CUDA==1
104  CuDevice::Instantiate().PrintProfile();
105 #endif
106 
107  KALDI_LOG << "Finished training, processed " << num_examples
108  << " training examples. Wrote model to "
109  << nnet_wxfilename;
110  return (num_examples == 0 ? 1 : 0);
111  } catch(const std::exception &e) {
112  std::cerr << e.what() << '\n';
113  return -1;
114  }
115 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void Register(OptionsItf *opts)
Definition: train-nnet.h:38
void Read(std::istream &is, bool binary)
Definition: am-nnet.cc:39
kaldi::int32 int32
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
int64 TrainNnetSimple(const NnetSimpleTrainerConfig &config, Nnet *nnet, SequentialNnetExampleReader *reader, double *tot_weight_ptr, double *tot_logprob_ptr)
Train on all the examples it can read from the reader.
Definition: train-nnet.cc:147
void Read(std::istream &is, bool binary)
void Write(std::ostream &os, bool binary) const
Definition: am-nnet.cc:31
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
void Write(std::ostream &os, bool binary) const
#define KALDI_LOG
Definition: kaldi-error.h:153
const Nnet & GetNnet() const
Definition: am-nnet.h:61