All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
reverse-weights.cc File Reference
Include dependency graph for reverse-weights.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 25 of file reverse-weights.cc.

References VectorBase< Real >::Add(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), KALDI_LOG, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), ParseOptions::Register(), VectorBase< Real >::Scale(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

25  {
26  try {
27  using namespace kaldi;
28  typedef kaldi::int32 int32;
29 
30  const char *usage =
31  "Modify per-frame weights by outputting 1.0-weight (if --reverse=true);\n"
32  "if --reverse=false, do nothing to them.\n"
33  "Usage: reverse-weights weights-rspecifier weights-wspecifier\n";
34 
35  bool reverse = true;
36  ParseOptions po(usage);
37  po.Register("reverse", &reverse,
38  "If true, reverse weights by setting to 1.0-weight; else do nothing.");
39  po.Read(argc, argv);
40 
41  if (po.NumArgs() != 2) {
42  po.PrintUsage();
43  exit(1);
44  }
45 
46  std::string weights_rspecifier = po.GetArg(1),
47  weights_wspecifier = po.GetArg(2);
48 
49  kaldi::SequentialBaseFloatVectorReader weights_reader(weights_rspecifier);
50  kaldi::BaseFloatVectorWriter weights_writer(weights_wspecifier);
51 
52  int32 num_done = 0;
53 
54  for (; !weights_reader.Done(); weights_reader.Next()) {
55  std::string key = weights_reader.Key();
56  Vector<BaseFloat> weights = weights_reader.Value();
57  if (reverse) { // set each weight to 1.0-weight.
58  weights.Scale(-1.0);
59  weights.Add(1.0);
60  }
61  weights_writer.Write(key, weights);
62  num_done++;
63  }
64  if (reverse) KALDI_LOG << "Done reversing " << num_done << " weights.";
65  else KALDI_LOG << "Done copying " << num_done << " weights.";
66  return (num_done != 0 ? 0 : 1);
67  } catch(const std::exception &e) {
68  std::cerr << e.what();
69  return -1;
70  }
71 }
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:366
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
void Scale(Real alpha)
Multiplies all elements by this constant.
void Add(Real c)
Add a constant to each element of a vector.
#define KALDI_LOG
Definition: kaldi-error.h:133