All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
rand-prune-post.cc File Reference
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "gmm/am-diag-gmm.h"
#include "hmm/transition-model.h"
#include "hmm/hmm-utils.h"
#include "hmm/posterior.h"
Include dependency graph for rand-prune-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 30 of file rand-prune-post.cc.

References kaldi::ConvertStringToReal(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), rnnlm::i, rnnlm::j, KALDI_ERR, KALDI_LOG, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), kaldi::RandPrune(), ParseOptions::Read(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

30  {
31  using namespace kaldi;
32  typedef kaldi::int32 int32;
33  try {
34  const char *usage =
35  "Randomized pruning of posteriors less than threshold\n"
36  "Note: for posteriors derived from alignments, threshold must be\n"
37  "greater than one, or this will have no effect (speedup factor will\n"
38  "be roughly the same as the threshold)\n"
39  "Usage: rand-prune-post [options] <rand-prune-value> <posteriors-rspecifier> <posteriors-wspecifier>\n"
40  "e.g.:\n"
41  " rand-prune-post 5.0 ark:- ark:-\n";
42 
43  ParseOptions po(usage);
44 
45  po.Read(argc, argv);
46 
47  if (po.NumArgs() != 3) {
48  po.PrintUsage();
49  exit(1);
50  }
51 
52  std::string rand_prune_str = po.GetArg(1),
53  posteriors_rspecifier = po.GetArg(2),
54  posteriors_wspecifier = po.GetArg(3);
55 
56  BaseFloat rand_prune = 0.0;
57  if (!ConvertStringToReal(rand_prune_str, &rand_prune) || rand_prune < 0.0)
58  KALDI_ERR << "Invalid rand_prune parameter: expected float, got \""
59  << rand_prune_str << '"';
60 
61  int32 num_posteriors = 0;
62  SequentialPosteriorReader posterior_reader(posteriors_rspecifier);
63  PosteriorWriter posterior_writer(posteriors_wspecifier);
64 
65  for (; !posterior_reader.Done(); posterior_reader.Next()) {
66  num_posteriors++;
67  // Posterior is vector<vector<pair<int32, BaseFloat> > >
68  const Posterior &posterior = posterior_reader.Value();
69  // Posterior is vector<vector<pair<int32, BaseFloat> > >
70  if (rand_prune == 0.0) {
71  posterior_writer.Write(posterior_reader.Key(), posterior);
72  } else {
73  Posterior new_post(posterior.size());
74  for (size_t i = 0; i < posterior.size(); i++) {
75  for (size_t j = 0; j < posterior[i].size(); j++) {
76  int32 tid = posterior[i][j].first;
77  BaseFloat weight = RandPrune(posterior[i][j].second, rand_prune);
78  if (weight != 0.0)
79  new_post[i].push_back(std::make_pair(tid, weight));
80  }
81  }
82  posterior_writer.Write(posterior_reader.Key(), new_post);
83  }
84  }
85  KALDI_LOG << "rand-prune-post: processed " << num_posteriors << " posteriors.";
86  } catch(const std::exception &e) {
87  std::cerr << e.what();
88  return -1;
89  }
90 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
Float RandPrune(Float post, BaseFloat prune_thresh, struct RandomState *state=NULL)
Definition: kaldi-math.h:174
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
float BaseFloat
Definition: kaldi-types.h:29
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_ERR
Definition: kaldi-error.h:127
bool ConvertStringToReal(const std::string &str, T *out)
ConvertStringToReal converts a string into either float or double and returns false if there was any ...
Definition: text-utils.cc:238
#define KALDI_LOG
Definition: kaldi-error.h:133