train-transitions.cc File Reference
Include dependency graph for train-transitions.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 train-transitions.cc.

References TransitionModel::Accumulate(), count, SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), rnnlm::i, TransitionModel::InitStats(), KALDI_LOG, TransitionModel::MleUpdate(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), TransitionModel::Read(), ParseOptions::Register(), MleTransitionUpdateConfig::Register(), Output::Stream(), Input::Stream(), SequentialTableReader< Holder >::Value(), and TransitionModel::Write().

26  {
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.
kaldi::int32 int32
const size_t count
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
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
void Write(std::ostream &os, bool binary) const
void Register(OptionsItf *opts)
#define KALDI_LOG
Definition: kaldi-error.h:153