nnet1-to-raw-nnet.cc
Go to the documentation of this file.
1 // nnet2bin/nnet1-to-raw-nnet.cc
2 
3 // Copyright 2013 Johns Hopkins University (author: Daniel Povey, Hainan Xu)
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 "nnet/nnet-nnet.h"
25 #include "nnet/nnet-activation.h"
26 #include "nnet/nnet-various.h"
27 #include "nnet2/nnet-nnet.h"
28 #include "nnet2/nnet-component.h"
29 
30 namespace kaldi {
31 
33  const nnet1::Component &nnet1_component,
34  const bool use_preconditioned_affine_component) {
35  const nnet1::AffineTransform *affine =
36  dynamic_cast<const nnet1::AffineTransform*>(&nnet1_component);
37  KALDI_ASSERT(affine != NULL);
38  // default learning rate is 1.0e-05, you can use the --learning-rate or
39  // --learning-rates option to nnet-am-copy to change it if you need.
40  BaseFloat learning_rate = 1.0e-05;
41  if (use_preconditioned_affine_component) {
42  int32 rank_in = 20,
43  rank_out = 80,
44  update_period = 4;
45  BaseFloat num_samples_history = 2000.,
46  alpha = 4.;
49  affine->GetBias(),
50  learning_rate),
51  rank_in,
52  rank_out,
53  update_period,
54  num_samples_history,
55  alpha);
56  } else {
57  return new nnet2::AffineComponent(affine->GetLinearity(),
58  affine->GetBias(),
59  learning_rate);
60  }
61 }
62 
64  const nnet1::Component &nnet1_component) {
65  const nnet1::Softmax *softmax =
66  dynamic_cast<const nnet1::Softmax*>(&nnet1_component);
67  KALDI_ASSERT(softmax != NULL);
68  return new nnet2::SoftmaxComponent(softmax->InputDim());
69 }
70 
72  const nnet1::Component &nnet1_component) {
73  const nnet1::Sigmoid *sigmoid =
74  dynamic_cast<const nnet1::Sigmoid*>(&nnet1_component);
75  KALDI_ASSERT(sigmoid != NULL);
76  return new nnet2::SigmoidComponent(sigmoid->InputDim());
77 }
78 
80  const nnet1::Component &nnet1_component) {
81  const nnet1::Splice *splice =
82  dynamic_cast<const nnet1::Splice*>(&nnet1_component);
83  KALDI_ASSERT(splice != NULL);
84 // int32 low, high;
85  std::vector<int32> frame_offsets;
86 
87  std::ostringstream ostr;
88  splice->WriteData(ostr, false);
89 
90  std::istringstream istr(ostr.str());
91  ReadIntegerVector(istr, false, &frame_offsets);
92 
94  res->Init(splice->InputDim(), frame_offsets);
95  return res;
96 }
97 
98 
100  const nnet1::Component &nnet1_component) {
101  const nnet1::AddShift *add_shift =
102  dynamic_cast<const nnet1::AddShift*>(&nnet1_component);
103  KALDI_ASSERT(add_shift != NULL);
104  Vector<BaseFloat> bias(add_shift->NumParams());
105 
106  add_shift->GetParams(&bias);
107  CuVector<BaseFloat> cu_bias(bias);
108 
110  res->Init(cu_bias);
111  return res;
112 }
113 
115  const nnet1::Component &nnet1_component) {
116  const nnet1::Rescale *rescale =
117  dynamic_cast<const nnet1::Rescale*>(&nnet1_component);
118  KALDI_ASSERT(rescale != NULL);
119 
120  Vector<BaseFloat> scale(rescale->NumParams());
121  rescale->GetParams(&scale);
122 
123  CuVector<BaseFloat> cu_scale(scale);
124 
126  res->Init(cu_scale);
127  return res;
128 }
129 
131  const bool use_preconditioned_affine_component) {
132  nnet1::Component::ComponentType type_in = nnet1_component.GetType();
133  switch (type_in) {
135  return ConvertAffineTransformComponent(nnet1_component,
136  use_preconditioned_affine_component);
138  return ConvertSoftmaxComponent(nnet1_component);
140  return ConvertSigmoidComponent(nnet1_component);
142  return ConvertSpliceComponent(nnet1_component); // note, this will for now only handle the
143  // special case nnet1::Component::where all splice indexes in nnet1_component are contiguous, e.g.
144  // -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 .
146  return ConvertAddShiftComponent(nnet1_component); // convert to FixedBiasComponent
148  return ConvertRescaleComponent(nnet1_component); // convert to FixedScaleComponent
149  default: KALDI_ERR << "Un-handled nnet1 component type "
150  << nnet1::Component::TypeToMarker(type_in);
151  return NULL;
152  }
153 }
154 
155 
157  const bool use_preconditioned_affine_component) {
158  // get a vector of nnet2::Component pointers and initialize the nnet2::Nnet with it.
159  size_t size = nnet1.NumComponents();
160  std::vector<nnet2::Component*> *components = new std::vector<nnet2::Component*>();
161  components->resize(size);
162  for (size_t i = 0; i < size; i++) {
163  (*components)[i] = ConvertComponent(nnet1.GetComponent(i),
164  use_preconditioned_affine_component);
165  }
166 
167  nnet2::Nnet *res = new nnet2::Nnet();
168  res->Init(components);
169  delete components;
170  return res;
171 }
172 
173 } // namespace kaldi
174 
175 
176 int main(int argc, char *argv[]) {
177  try {
178  using namespace kaldi;
179  typedef kaldi::int32 int32;
180 
181  const char *usage =
182  "Convert nnet1 neural net to nnet2 'raw' neural net\n"
183  "\n"
184  "Usage: nnet1-to-raw-nnet [options] <nnet1-in> <nnet2-out>\n"
185  "e.g.:\n"
186  " nnet1-to-raw-nnet srcdir/final.nnet - | nnet-am-init dest/tree dest/topo - dest/0.mdl\n";
187 
188  bool binary_write = true, use_preconditioned_affine_component = false;
189  int32 srand_seed = 0;
190 
191  ParseOptions po(usage);
192  po.Register("binary", &binary_write, "Write output in binary mode");
193 
194  po.Register("use_preconditioned_affine_component",
195  &use_preconditioned_affine_component,
196  "Using AffineComponentPreconditionOnline instead AffineComponent");
197 
198  po.Read(argc, argv);
199  srand(srand_seed);
200 
201  if (po.NumArgs() != 2) {
202  po.PrintUsage();
203  exit(1);
204  }
205 
206  std::string nnet1_rxfilename = po.GetArg(1),
207  raw_nnet2_wxfilename = po.GetArg(2);
208 
209  nnet1::Nnet nnet1;
210  ReadKaldiObject(nnet1_rxfilename, &nnet1);
211  nnet2::Nnet *nnet2 = ConvertNnet1ToNnet2(nnet1,
212  use_preconditioned_affine_component);
213  WriteKaldiObject(*nnet2, raw_nnet2_wxfilename, binary_write);
214  KALDI_LOG << "Converted nnet1 neural net to raw nnet2 and wrote it to "
215  << PrintableWxfilename(raw_nnet2_wxfilename);
216  delete nnet2;
217  return 0;
218  } catch(const std::exception &e) {
219  std::cerr << e.what() << '\n';
220  return -1;
221  }
222 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
nnet2::Component * ConvertSoftmaxComponent(const nnet1::Component &nnet1_component)
void Init(const CuVectorBase< BaseFloat > &scales)
int32 NumComponents() const
Returns the number of &#39;Components&#39; which form the NN.
Definition: nnet-nnet.h:66
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
Abstract class, basic element of the network, it is a box with defined inputs, outputs, and tranformation functions interface.
void Init(int32 input_dim, std::vector< int32 > context, int32 const_component_dim=0)
const CuMatrixBase< BaseFloat > & GetLinearity() const
void GetParams(VectorBase< BaseFloat > *params) const
Get the trainable parameters reshaped as a vector,.
Definition: nnet-various.h:344
kaldi::int32 int32
int32 NumParams() const
Number of trainable parameters,.
Definition: nnet-various.h:337
ComponentType
Component type identification mechanism,.
Rescale the data column-wise by a vector (can be used for global variance normalization) ...
Definition: nnet-various.h:404
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
nnet2::Component * ConvertSpliceComponent(const nnet1::Component &nnet1_component)
FixedScaleComponent applies a fixed per-element scale; it&#39;s similar to the Rescale component in the n...
static const char * TypeToMarker(ComponentType t)
Converts component type to marker,.
nnet2::Component * ConvertRescaleComponent(const nnet1::Component &nnet1_component)
Adds shift to all the lines of the matrix (can be used for global mean normalization) ...
Definition: nnet-various.h:291
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
void ReadIntegerVector(std::istream &is, bool binary, std::vector< T > *v)
Function for reading STL vector of integer types.
Definition: io-funcs-inl.h:232
Splices the time context of the input features in N, out k*N, FrameOffset o_1,o_2,...,o_k FrameOffset example 11frames: -5 -4 -3 -2 -1 0 1 2 3 4 5.
Definition: nnet-various.h:42
int32 InputDim() const
Get the dimension of the input,.
void WriteData(std::ostream &os, bool binary) const
Writes the component content.
Definition: nnet-various.h:94
nnet2::Nnet * ConvertNnet1ToNnet2(const nnet1::Nnet &nnet1, const bool use_preconditioned_affine_component)
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#define KALDI_ERR
Definition: kaldi-error.h:147
nnet2::Component * ConvertAddShiftComponent(const nnet1::Component &nnet1_component)
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
const CuVectorBase< BaseFloat > & GetBias() const
Accessors to the component parameters,.
FixedBiasComponent applies a fixed per-element bias; it&#39;s similar to the AddShift component in the nn...
int NumArgs() const
Number of positional parameters (c.f. argc-1).
nnet2::Component * ConvertAffineTransformComponent(const nnet1::Component &nnet1_component, const bool use_preconditioned_affine_component)
void GetParams(VectorBase< BaseFloat > *params) const
Get the trainable parameters reshaped as a vector,.
Definition: nnet-various.h:457
A class representing a vector.
Definition: kaldi-vector.h:406
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Keywords: natural gradient descent, NG-SGD, naturalgradient.
const Component & GetComponent(int32 c) const
Component accessor,.
Definition: nnet-nnet.cc:153
virtual ComponentType GetType() const =0
Get Type Identification of the component,.
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
std::string PrintableWxfilename(const std::string &wxfilename)
PrintableWxfilename turns the wxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:73
Abstract class, building block of the network.
void Init(const CuVectorBase< BaseFloat > &scales)
int main(int argc, char *argv[])
Splices a context window of frames together [over time].
nnet2::Component * ConvertSigmoidComponent(const nnet1::Component &nnet1_component)
#define KALDI_LOG
Definition: kaldi-error.h:153
int32 NumParams() const
Number of trainable parameters,.
Definition: nnet-various.h:450
nnet2::Component * ConvertComponent(const nnet1::Component &nnet1_component, const bool use_preconditioned_affine_component)