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(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

26  {
27  try {
28  using namespace kaldi;
29  typedef kaldi::int32 int32;
30 
31  const char *usage =
32  "Takes archives (typically per-utterance) of posteriors and per-frame weights,\n"
33  "and weights the posteriors by the per-frame weights\n"
34  "\n"
35  "Usage: weight-post <post-rspecifier> <weights-rspecifier> <post-wspecifier>\n";
36 
37  ParseOptions po(usage);
38  po.Read(argc, argv);
39 
40  if (po.NumArgs() != 3) {
41  po.PrintUsage();
42  exit(1);
43  }
44 
45  std::string post_rspecifier = po.GetArg(1),
46  weights_rspecifier = po.GetArg(2),
47  post_wspecifier = po.GetArg(3);
48 
49  SequentialPosteriorReader posterior_reader(post_rspecifier);
50  RandomAccessBaseFloatVectorReader weights_reader(weights_rspecifier);
51  PosteriorWriter post_writer(post_wspecifier);
52 
53  int32 num_done = 0, num_err = 0;
54 
55  for (; !posterior_reader.Done(); posterior_reader.Next()) {
56  std::string key = posterior_reader.Key();
57  Posterior post = posterior_reader.Value();
58  if (!weights_reader.HasKey(key)) {
59  KALDI_WARN << "No weights for utterance " << key;
60  num_err++;
61  continue;
62  }
63  const Vector<BaseFloat> &weights = weights_reader.Value(key);
64  if (weights.Dim() != static_cast<int32>(post.size())) {
65  KALDI_WARN << "Weights for utterance " << key
66  << " have wrong size, " << weights.Dim()
67  << " vs. " << post.size();
68  num_err++;
69  continue;
70  }
71  for (size_t i = 0; i < post.size(); i++) {
72  if (weights(i) == 0.0) post[i].clear();
73  for (size_t j = 0; j < post[i].size(); j++)
74  post[i][j].second *= weights(i);
75  }
76  post_writer.Write(key, post);
77  num_done++;
78  }
79  KALDI_LOG << "Scaled " << num_done << " posteriors; errors on " << num_err;
80  return (num_done != 0 ? 0 : 1);
81  } catch(const std::exception &e) {
82  std::cerr << e.what();
83  return -1;
84  }
85 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
void Write(std::ostream &Out, bool binary) const
Writes to C++ stream (option to write in binary).
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
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:62