nnet-train-discriminative-parallel.cc
Go to the documentation of this file.
1 // nnet2bin/nnet-train-discriminative-parallel.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 
20 #include "base/kaldi-common.h"
21 #include "util/common-utils.h"
22 #include "hmm/transition-model.h"
23 #include "nnet2/am-nnet.h"
25 
26 
27 int main(int argc, char *argv[]) {
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 a discriminative objective\n"
36  "function (MMI, SMBR or MPFE). This uses training examples prepared with\n"
37  "nnet-get-egs-discriminative\n"
38  "This version uses multiple threads (but no GPU)"
39  "\n"
40  "Usage: nnet-train-discriminative-parallel [options] <model-in> <training-examples-in> <model-out>\n"
41  "e.g.:\n"
42  "nnet-train-discriminative-parallel --num-threads=8 1.nnet ark:1.degs 2.nnet\n";
43 
44  bool binary_write = true;
45  std::string use_gpu = "yes";
46  int32 num_threads = 1;
48 
49  ParseOptions po(usage);
50  po.Register("binary", &binary_write, "Write output in binary mode");
51  po.Register("num-threads", &num_threads, "Number of threads to use");
52  update_opts.Register(&po);
53 
54  po.Read(argc, argv);
55 
56  if (po.NumArgs() != 3) {
57  po.PrintUsage();
58  exit(1);
59  }
60 
61  std::string nnet_rxfilename = po.GetArg(1),
62  examples_rspecifier = po.GetArg(2),
63  nnet_wxfilename = po.GetArg(3);
64 
65  TransitionModel trans_model;
66  AmNnet am_nnet;
67  {
68  bool binary_read;
69  Input ki(nnet_rxfilename, &binary_read);
70  trans_model.Read(ki.Stream(), binary_read);
71  am_nnet.Read(ki.Stream(), binary_read);
72  }
73 
74 
77  examples_rspecifier);
78 
79  NnetDiscriminativeUpdateParallel(am_nnet, trans_model,
80  update_opts, num_threads, &example_reader,
81  &(am_nnet.GetNnet()), &stats);
82  {
83  Output ko(nnet_wxfilename, binary_write);
84  trans_model.Write(ko.Stream(), binary_write);
85  am_nnet.Write(ko.Stream(), binary_write);
86  }
87 
88  return (stats.tot_t == 0 ? 1 : 0);
89  } catch(const std::exception &e) {
90  std::cerr << e.what() << '\n';
91  return -1;
92  }
93 }
94 
95 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int main(int argc, char *argv[])
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
void Read(std::istream &is, bool binary)
Definition: am-nnet.cc:39
kaldi::int32 int32
void Register(const std::string &name, bool *ptr, const std::string &doc)
std::istream & Stream()
Definition: kaldi-io.cc:826
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
std::ostream & Stream()
Definition: kaldi-io.cc:701
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
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 NumArgs() const
Number of positional parameters (c.f. argc-1).
void Write(std::ostream &os, bool binary) const
void NnetDiscriminativeUpdateParallel(const AmNnet &am_nnet, const TransitionModel &tmodel, const NnetDiscriminativeUpdateOptions &opts, int32 num_threads, SequentialDiscriminativeNnetExampleReader *example_reader, Nnet *nnet_to_update, NnetDiscriminativeStats *stats)
const Nnet & GetNnet() const
Definition: am-nnet.h:61