nnet3-init.cc
Go to the documentation of this file.
1 // nnet3bin/nnet3-init.cc
2 
3 // Copyright 2012-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"
22 #include "nnet3/nnet-nnet.h"
23 #include "hmm/transition-model.h"
24 #include "tree/context-dep.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 
32  const char *usage =
33  "Initialize nnet3 neural network from a config file; outputs 'raw' nnet\n"
34  "without associated information such as transition model and priors.\n"
35  "Search for examples in scripts in /egs/wsj/s5/steps/nnet3/\n"
36  "Can also be used to add layers to existing model (provide existing model\n"
37  "as 1st arg)\n"
38  "\n"
39  "Usage: nnet3-init [options] [<existing-model-in>] <config-in> <raw-nnet-out>\n"
40  "e.g.:\n"
41  " nnet3-init nnet.config 0.raw\n"
42  "or: nnet3-init 1.raw nnet.config 2.raw\n"
43  "See also: nnet3-copy, nnet3-info\n";
44 
45  bool binary_write = true;
46  int32 srand_seed = 0;
47 
48  ParseOptions po(usage);
49  po.Register("binary", &binary_write, "Write output in binary mode");
50  po.Register("srand", &srand_seed, "Seed for random number generator");
51 
52  po.Read(argc, argv);
53  srand(srand_seed);
54 
55  if (po.NumArgs() < 2 || po.NumArgs() > 3) {
56  po.PrintUsage();
57  exit(1);
58  }
59 
60  std::string raw_nnet_rxfilename = (po.NumArgs() == 3 ?
61  po.GetArg(1) : std::string("")),
62  config_rxfilename = po.GetArg(po.NumArgs() == 3 ? 2 : 1),
63  raw_nnet_wxfilename = po.GetArg(po.NumArgs() == 3 ? 3 : 2);
64 
65  Nnet nnet;
66  if (po.NumArgs() == 3) {
67  ReadKaldiObject(raw_nnet_rxfilename, &nnet);
68  KALDI_LOG << "Read raw neural net from "
69  << raw_nnet_rxfilename;
70  }
71 
72  {
73  bool binary;
74  Input ki(config_rxfilename, &binary);
75  KALDI_ASSERT(!binary && "Expect config file to contain text.");
76  nnet.ReadConfig(ki.Stream());
77  }
78 
79  WriteKaldiObject(nnet, raw_nnet_wxfilename, binary_write);
80  KALDI_LOG << "Initialized raw neural net and wrote it to "
81  << raw_nnet_wxfilename;
82  return 0;
83  } catch(const std::exception &e) {
84  std::cerr << e.what() << '\n';
85  return -1;
86  }
87 }
88 
89 
90 /*
91 Test script:
92 
93 cat <<EOF | nnet3-init --binary=false - foo.raw
94 component name=affine1 type=NaturalGradientAffineComponent input-dim=72 output-dim=59
95 component name=relu1 type=RectifiedLinearComponent dim=59
96 component name=final_affine type=NaturalGradientAffineComponent input-dim=59 output-dim=298
97 component name=logsoftmax type=SoftmaxComponent dim=298
98 input-node name=input dim=18
99 component-node name=affine1_node component=affine1 input=Append(Offset(input, -4), Offset(input, -3), Offset(input, -2), Offset(input, 0))
100 component-node name=nonlin1 component=relu1 input=affine1_node
101 component-node name=final_affine component=final_affine input=nonlin1
102 component-node name=output_nonlin component=logsoftmax input=final_affine
103 output-node name=output input=output_nonlin
104 EOF
105 
106 cat <<EOF | nnet3-init --binary=false foo.raw - bar.raw
107 component name=affine2 type=NaturalGradientAffineComponent input-dim=59 output-dim=59
108 component name=relu2 type=RectifiedLinearComponent dim=59
109 component name=final_affine type=NaturalGradientAffineComponent input-dim=59 output-dim=298
110 component-node name=affine2 component=affine2 input=nonlin1
111 component-node name=relu2 component=relu2 input=affine2
112 component-node name=final_affine component=final_affine input=relu2
113 EOF
114 
115 rm foo.raw bar.raw
116 
117  */
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void ReadConfig(std::istream &config_file)
Definition: nnet-nnet.cc:189
int main(int argc, char *argv[])
Definition: nnet3-init.cc:26
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
kaldi::int32 int32
void Register(const std::string &name, bool *ptr, const std::string &doc)
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
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
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).
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
#define KALDI_LOG
Definition: kaldi-error.h:153