train-transitions.cc
Go to the documentation of this file.
1 // nnetbin/train-transitions.cc
2 
3 // Copyright 2015 Brno University of Technology (author: Karel Vesely)
4 // 2012 Johns Hopkins University (author: Daniel Povey)
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "tree/context-dep.h"
24 #include "hmm/transition-model.h"
25 
26 int main(int argc, char *argv[]) {
27  try {
28  using namespace kaldi;
29  typedef kaldi::int32 int32;
30 
31  const char *usage =
32  "Train the transition probabilities in transition-model "
33  "(used in nnet1 recipe).\n"
34  "\n"
35  "Usage: train-transitions [options] "
36  "<trans-model-in> <alignments-rspecifier> <trans-model-out>\n"
37  "e.g.: train-transitions 1.mdl \"ark:gunzip -c ali.*.gz|\" 2.mdl\n";
38 
39  bool binary_write = true;
40  MleTransitionUpdateConfig transition_update_config;
41 
42  ParseOptions po(usage);
43  po.Register("binary", &binary_write, "Write output in binary mode");
44  transition_update_config.Register(&po);
45 
46  po.Read(argc, argv);
47 
48  if (po.NumArgs() != 3) {
49  po.PrintUsage();
50  exit(1);
51  }
52 
53  std::string trans_model_rxfilename = po.GetArg(1),
54  ali_rspecifier = po.GetArg(2),
55  trans_model_wxfilename = po.GetArg(3);
56 
57  TransitionModel trans_model;
58  {
59  bool binary_read;
60  Input ki(trans_model_rxfilename, &binary_read);
61  trans_model.Read(ki.Stream(), binary_read);
62  }
63 
64  Vector<double> transition_accs;
65  trans_model.InitStats(&transition_accs);
66 
67  int32 num_done = 0;
68  SequentialInt32VectorReader ali_reader(ali_rspecifier);
69  for (; !ali_reader.Done(); ali_reader.Next()) {
70  const std::vector<int32> alignment(ali_reader.Value());
71  for (size_t i = 0; i < alignment.size(); i++) {
72  int32 tid = alignment[i];
73  BaseFloat weight = 1.0;
74  trans_model.Accumulate(weight, tid, &transition_accs);
75  }
76  num_done++;
77  }
78  KALDI_LOG << "Accumulated transition stats from " << num_done
79  << " utterances.";
80 
81  {
82  BaseFloat objf_impr, count;
83  trans_model.MleUpdate(transition_accs, transition_update_config,
84  &objf_impr, &count);
85  KALDI_LOG << "Transition model update: average " << (objf_impr/count)
86  << " log-like improvement per frame over " << count
87  << " frames.";
88  }
89 
90  {
91  Output ko(trans_model_wxfilename, binary_write);
92  trans_model.Write(ko.Stream(), binary_write);
93  }
94  KALDI_LOG << "Trained transition model and wrote it to "
95  << trans_model_wxfilename;
96  return 0;
97  } catch(const std::exception &e) {
98  std::cerr << e.what();
99  return -1;
100  }
101 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void MleUpdate(const Vector< double > &stats, const MleTransitionUpdateConfig &cfg, BaseFloat *objf_impr_out, BaseFloat *count_out)
Does Maximum Likelihood estimation.
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
kaldi::int32 int32
int main(int argc, char *argv[])
void Register(const std::string &name, bool *ptr, const std::string &doc)
const size_t count
std::istream & Stream()
Definition: kaldi-io.cc:826
float BaseFloat
Definition: kaldi-types.h:29
void InitStats(Vector< double > *stats) const
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 Accumulate(BaseFloat prob, int32 trans_id, Vector< double > *stats) const
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).
void Write(std::ostream &os, bool binary) const
void Register(OptionsItf *opts)
#define KALDI_LOG
Definition: kaldi-error.h:153