nnet-train-discriminative-simple.cc
Go to the documentation of this file.
1 // nnet2bin/nnet-train-discriminative-simple.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  "\n"
39  "Usage: nnet-train-discriminative-simple [options] <model-in> <training-examples-in> <model-out>\n"
40  "e.g.:\n"
41  "nnet-train-discriminative-simple 1.nnet ark:1.degs 2.nnet\n";
42 
43  bool binary_write = true;
44  std::string use_gpu = "yes";
46 
47  ParseOptions po(usage);
48  po.Register("binary", &binary_write, "Write output in binary mode");
49  po.Register("use-gpu", &use_gpu,
50  "yes|no|optional|wait, only has effect if compiled with CUDA");
51  update_opts.Register(&po);
52 
53  po.Read(argc, argv);
54 
55  if (po.NumArgs() != 3) {
56  po.PrintUsage();
57  exit(1);
58  }
59 
60 #if HAVE_CUDA==1
61  CuDevice::Instantiate().SelectGpuId(use_gpu);
62 #endif
63 
64  std::string nnet_rxfilename = po.GetArg(1),
65  examples_rspecifier = po.GetArg(2),
66  nnet_wxfilename = po.GetArg(3);
67 
68  int64 num_examples = 0;
69 
70  {
71  TransitionModel trans_model;
72  AmNnet am_nnet;
73  {
74  bool binary_read;
75  Input ki(nnet_rxfilename, &binary_read);
76  trans_model.Read(ki.Stream(), binary_read);
77  am_nnet.Read(ki.Stream(), binary_read);
78  }
79 
80 
82  SequentialDiscriminativeNnetExampleReader example_reader(examples_rspecifier);
83 
84  for (; !example_reader.Done(); example_reader.Next(), num_examples++) {
85  NnetDiscriminativeUpdate(am_nnet, trans_model, update_opts,
86  example_reader.Value(),
87  &(am_nnet.GetNnet()), &stats);
88  if (num_examples % 10 == 0 && num_examples != 0) { // each example might be 500 frames.
89  if (GetVerboseLevel() >= 2) {
90  stats.Print(update_opts.criterion);
91  }
92  }
93  }
94 
95  stats.Print(update_opts.criterion);
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  KALDI_LOG << "Finished training, processed " << num_examples
107  << " training examples. Wrote model to "
108  << nnet_wxfilename;
109  return (num_examples == 0 ? 1 : 0);
110  } catch(const std::exception &e) {
111  std::cerr << e.what() << '\n';
112  return -1;
113  }
114 }
115 
116 
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].
int32 GetVerboseLevel()
Get verbosity level, usually set via command line &#39;–verbose=&#39; switch.
Definition: kaldi-error.h:60
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).
int main(int argc, char *argv[])
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
void NnetDiscriminativeUpdate(const AmNnet &am_nnet, const TransitionModel &tmodel, const NnetDiscriminativeUpdateOptions &opts, const DiscriminativeNnetExample &eg, Nnet *nnet_to_update, NnetDiscriminativeStats *stats)
Does the neural net computation, lattice forward-backward, and backprop, for either the MMI...