nnet-am-switch-preconditioning.cc File Reference
Include dependency graph for nnet-am-switch-preconditioning.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 nnet-am-switch-preconditioning.cc.

References ParseOptions::GetArg(), AmNnet::GetNnet(), KALDI_LOG, ParseOptions::NumArgs(), ParseOptions::PrintUsage(), AmNnet::Read(), ParseOptions::Read(), TransitionModel::Read(), ParseOptions::Register(), Output::Stream(), Input::Stream(), Nnet::SwitchToOnlinePreconditioning(), AmNnet::Write(), and TransitionModel::Write().

26  {
27  try {
28  using namespace kaldi;
29  using namespace kaldi::nnet2;
30  typedef kaldi::int32 int32;
31 
32  const char *usage =
33  "Copy a (cpu-based) neural net and its associated transition model,\n"
34  "and switch it to online preconditioning, i.e. change any components\n"
35  "derived from AffineComponent to components of type\n"
36  "AffineComponentPreconditionedOnline.\n"
37  "\n"
38  "Usage: nnet-am-switch-preconditioning [options] <nnet-in> <nnet-out>\n"
39  "e.g.:\n"
40  " nnet-am-switch-preconditioning --binary=false 1.mdl text.mdl\n";
41 
42  int32 rank_in = 20, rank_out = 80, update_period = 4;
43  BaseFloat num_samples_history = 2000.0;
44  BaseFloat alpha = 4.0;
45  bool binary_write = true;
46 
47  ParseOptions po(usage);
48  po.Register("binary", &binary_write, "Write output in binary mode");
49  po.Register("rank-in", &rank_in,
50  "Rank used in online-preconditioning on input side of each layer");
51  po.Register("rank-out", &rank_out,
52  "Rank used in online-preconditioning on output side of each layer");
53  po.Register("update-period", &update_period,
54  "Affects how frequently we update the Fisher-matrix estimate (every "
55  "this-many minibatches).");
56  po.Register("num-samples-history", &num_samples_history,
57  "Number of samples of history to use in online preconditioning "
58  "(affects speed vs accuracy of update of Fisher matrix)");
59  po.Register("alpha", &alpha,
60  "Parameter that affects amount of smoothing with unit matrix "
61  "in online preconditioning (larger -> more smoothing)");
62 
63  po.Read(argc, argv);
64 
65  if (po.NumArgs() != 2) {
66  po.PrintUsage();
67  exit(1);
68  }
69 
70  std::string nnet_rxfilename = po.GetArg(1),
71  nnet_wxfilename = po.GetArg(2);
72 
73  TransitionModel trans_model;
74  AmNnet am_nnet;
75  {
76  bool binary;
77  Input ki(nnet_rxfilename, &binary);
78  trans_model.Read(ki.Stream(), binary);
79  am_nnet.Read(ki.Stream(), binary);
80  }
81 
82  am_nnet.GetNnet().SwitchToOnlinePreconditioning(rank_in, rank_out, update_period,
83  num_samples_history, alpha);
84 
85  {
86  Output ko(nnet_wxfilename, binary_write);
87  trans_model.Write(ko.Stream(), binary_write);
88  am_nnet.Write(ko.Stream(), binary_write);
89  }
90  KALDI_LOG << "Copied neural net from " << nnet_rxfilename
91  << " to " << nnet_wxfilename;
92  return 0;
93  } catch(const std::exception &e) {
94  std::cerr << e.what() << '\n';
95  return -1;
96  }
97 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void Read(std::istream &is, bool binary)
Definition: am-nnet.cc:39
kaldi::int32 int32
void SwitchToOnlinePreconditioning(int32 rank_in, int32 rank_out, int32 update_period, BaseFloat num_samples_history, BaseFloat alpha)
Replaces any components of type AffineComponent or derived classes, with components of type AffineCom...
Definition: nnet-nnet.cc:551
float BaseFloat
Definition: kaldi-types.h:29
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
void Read(std::istream &is, bool binary)
void Write(std::ostream &os, bool binary) const
Definition: am-nnet.cc:31
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