All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
weight-post.cc File Reference
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "hmm/posterior.h"
Include dependency graph for weight-post.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 26 of file weight-post.cc.

References VectorBase< Real >::Dim(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), rnnlm::i, rnnlm::j, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), ParseOptions::Register(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

26  {
27  try {
28  using namespace kaldi;
29  typedef kaldi::int32 int32;
30 
31  int32 length_tolerance = 2;
32 
33  const char *usage =
34  "Takes archives (typically per-utterance) of posteriors and per-frame weights,\n"
35  "and weights the posteriors by the per-frame weights\n"
36  "\n"
37  "Usage: weight-post <post-rspecifier> <weights-rspecifier> <post-wspecifier>\n";
38 
39  ParseOptions po(usage);
40 
41  po.Register("length-tolerance", &length_tolerance,
42  "Tolerate this many frames of length mismatch between "
43  "posteriors and weights");
44 
45  po.Read(argc, argv);
46 
47  if (po.NumArgs() != 3) {
48  po.PrintUsage();
49  exit(1);
50  }
51 
52  std::string post_rspecifier = po.GetArg(1),
53  weights_rspecifier = po.GetArg(2),
54  post_wspecifier = po.GetArg(3);
55 
56  SequentialPosteriorReader posterior_reader(post_rspecifier);
57  RandomAccessBaseFloatVectorReader weights_reader(weights_rspecifier);
58  PosteriorWriter post_writer(post_wspecifier);
59 
60  int32 num_done = 0, num_err = 0;
61 
62  for (; !posterior_reader.Done(); posterior_reader.Next()) {
63  std::string key = posterior_reader.Key();
64  Posterior post = posterior_reader.Value();
65  if (!weights_reader.HasKey(key)) {
66  KALDI_WARN << "No weights for utterance " << key;
67  num_err++;
68  continue;
69  }
70  const Vector<BaseFloat> &weights = weights_reader.Value(key);
71  if (std::abs(weights.Dim() - static_cast<int32>(post.size())) >
72  length_tolerance) {
73  KALDI_WARN << "Weights for utterance " << key
74  << " have wrong size, " << weights.Dim()
75  << " vs. " << post.size();
76  num_err++;
77  continue;
78  }
79  for (size_t i = 0; i < post.size(); i++) {
80  if (weights(i) == 0.0) post[i].clear();
81  for (size_t j = 0; j < post[i].size(); j++)
82  post[i][j].second *= i < weights.Dim() ? weights(i) : 0.0;
83  }
84  post_writer.Write(key, post);
85  num_done++;
86  }
87  KALDI_LOG << "Scaled " << num_done << " posteriors; errors on " << num_err;
88  return (num_done != 0 ? 0 : 1);
89  } catch(const std::exception &e) {
90  std::cerr << e.what();
91  return -1;
92  }
93 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
std::vector< std::vector< std::pair< int32, BaseFloat > > > Posterior
Posterior is a typedef for storing acoustic-state (actually, transition-id) posteriors over an uttera...
Definition: posterior.h:43
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
#define KALDI_WARN
Definition: kaldi-error.h:130
#define KALDI_LOG
Definition: kaldi-error.h:133
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:63