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

References kaldi::ClassifyRspecifier(), kaldi::ClassifyWspecifier(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), KALDI_ERR, SequentialTableReader< Holder >::Key(), kaldi::kNoRspecifier, kaldi::kNoWspecifier, SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), Vector< Real >::Read(), ParseOptions::Register(), VectorBase< Real >::Scale(), Output::Stream(), Input::Stream(), SequentialTableReader< Holder >::Value(), VectorBase< Real >::Write(), and TableWriter< Holder >::Write().

26  {
27  try {
28  using namespace kaldi;
29 
30  const char *usage =
31  "Scale vectors, or archives of vectors (useful for speaker vectors and "
32  "per-frame weights)\n"
33  "Usage: vector-scale [options] <vector-in-rspecifier> <vector-out-wspecifier>\n"
34  " or: vector-scale [options] <vector-in-rxfilename> <vector-out-wxfilename>\n"
35  " e.g.: vector-scale --scale=-1.0 1.vec -\n"
36  " vector-scale --scale=-2.0 ark:vec.ark ark,t:-\n"
37  "See also: copy-vector, vector-sum\n";
38 
39  ParseOptions po(usage);
40  BaseFloat scale = 1.0;
41  bool binary = false;
42 
43  po.Register("binary", &binary, "If true, write output as binary "
44  "not relevant for archives");
45  po.Register("scale", &scale, "Scaling factor for vectors");
46  po.Read(argc, argv);
47 
48  if (po.NumArgs() != 2) {
49  po.PrintUsage();
50  exit(1);
51  }
52 
53  std::string vector_in_fn = po.GetArg(1);
54  std::string vector_out_fn = po.GetArg(2);
55 
56  if (ClassifyWspecifier(vector_in_fn, NULL, NULL, NULL) != kNoWspecifier) {
57  if (ClassifyRspecifier(vector_in_fn, NULL, NULL) == kNoRspecifier) {
58  KALDI_ERR << "Cannot mix archives and regular files";
59  }
60  BaseFloatVectorWriter vec_writer(vector_out_fn);
61  SequentialBaseFloatVectorReader vec_reader(vector_in_fn);
62  for (; !vec_reader.Done(); vec_reader.Next()) {
63  Vector<BaseFloat> vec(vec_reader.Value());
64  vec.Scale(scale);
65  vec_writer.Write(vec_reader.Key(), vec);
66  }
67  } else {
68  if (ClassifyRspecifier(vector_in_fn, NULL, NULL) != kNoRspecifier) {
69  KALDI_ERR << "Cannot mix archives and regular files";
70  }
71  bool binary_in;
72  Input ki(vector_in_fn, &binary_in);
74  vec.Read(ki.Stream(), binary_in);
75  vec.Scale(scale);
76  Output ko(vector_out_fn, binary);
77  vec.Write(ko.Stream(), binary);
78  }
79 
80  return 0;
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:368
float BaseFloat
Definition: kaldi-types.h:29
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
void Scale(Real alpha)
Multiplies all elements by this constant.
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *wxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
WspecifierType ClassifyWspecifier(const std::string &wspecifier, std::string *archive_wxfilename, std::string *script_wxfilename, WspecifierOptions *opts)
Definition: kaldi-table.cc:135
void Read(std::istream &in, bool binary, bool add=false)
Read function using C++ streams.