nnet3-discriminative-train.cc
Go to the documentation of this file.
1 // nnet3bin/nnet3-discriminative-train.cc
2 
3 // Copyright 2015 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"
23 #include "nnet3/am-nnet-simple.h"
24 #include "nnet3/nnet-utils.h"
25 
26 int main(int argc, char *argv[]) {
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  "Train nnet3 neural network parameters with discriminative sequence objective \n"
35  "gradient descent. Minibatches are to be created by nnet3-discriminative-merge-egs in\n"
36  "the input pipeline. This training program is single-threaded (best to\n"
37  "use it with a GPU).\n"
38  "\n"
39  "Usage: nnet3-discriminative-train [options] <nnet-in> <discriminative-training-examples-in> <raw-nnet-out>\n"
40  "\n"
41  "nnet3-discriminative-train 1.mdl 'ark:nnet3-merge-egs 1.degs ark:-|' 2.raw\n";
42 
43  bool binary_write = true;
44  std::string use_gpu = "yes";
45  bool dropout_test_mode = true;
46 
48 
49  ParseOptions po(usage);
50  po.Register("binary", &binary_write, "Write output in binary mode");
51  po.Register("use-gpu", &use_gpu,
52  "yes|no|optional|wait, only has effect if compiled with CUDA");
53  po.Register("dropout-test-mode", &dropout_test_mode,
54  "If true, set test-mode to true on any DropoutComponents and "
55  "DropoutMaskComponents.");
56 
57  opts.Register(&po);
58 
59  po.Read(argc, argv);
60 
61  if (po.NumArgs() != 3) {
62  po.PrintUsage();
63  exit(1);
64  }
65 
66 #if HAVE_CUDA==1
67  CuDevice::Instantiate().SelectGpuId(use_gpu);
68 #endif
69 
70  std::string model_rxfilename = po.GetArg(1),
71  examples_rspecifier = po.GetArg(2),
72  model_wxfilename = po.GetArg(3);
73 
74  TransitionModel tmodel;
75  AmNnetSimple am_nnet;
76 
77  bool binary;
78  Input ki(model_rxfilename, &binary);
79 
80  tmodel.Read(ki.Stream(), binary);
81  am_nnet.Read(ki.Stream(), binary);
82 
83  Nnet nnet = am_nnet.GetNnet();
84 
85  if (dropout_test_mode)
86  SetDropoutTestMode(true, &nnet);
87 
88  const VectorBase<BaseFloat> &priors = am_nnet.Priors();
89 
90  NnetDiscriminativeTrainer trainer(opts, tmodel, priors, &nnet);
91 
92  SequentialNnetDiscriminativeExampleReader example_reader(examples_rspecifier);
93 
94  for (; !example_reader.Done(); example_reader.Next())
95  trainer.Train(example_reader.Value());
96 
97  bool ok = trainer.PrintTotalStats();
98 
99 #if HAVE_CUDA==1
100  CuDevice::Instantiate().PrintProfile();
101 #endif
102  Output ko(model_wxfilename, binary_write);
103  nnet.Write(ko.Stream(), binary_write);
104 
105  KALDI_LOG << "Wrote raw nnet model to " << model_wxfilename;
106  return (ok ? 0 : 1);
107  } catch(const std::exception &e) {
108  std::cerr << e.what() << '\n';
109  return -1;
110  }
111 }
112 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void Write(std::ostream &ostream, bool binary) const
Definition: nnet-nnet.cc:630
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
kaldi::int32 int32
const Nnet & GetNnet() const
void Read(std::istream &is, bool binary)
void Register(const std::string &name, bool *ptr, const std::string &doc)
This file contains some miscellaneous functions dealing with class Nnet.
void SetDropoutTestMode(bool test_mode, Nnet *nnet)
This function affects components of child-classes of RandomComponent.
Definition: nnet-utils.cc:573
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 Train(const NnetDiscriminativeExample &eg)
void Read(std::istream &is, bool binary)
int main(int argc, char *argv[])
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
This class is for single-threaded discriminative training of neural nets.
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.
const VectorBase< BaseFloat > & Priors() const
int NumArgs() const
Number of positional parameters (c.f. argc-1).
Provides a vector abstraction class.
Definition: kaldi-vector.h:41
#define KALDI_LOG
Definition: kaldi-error.h:153