matrix-sum-rows.cc File Reference
Include dependency graph for matrix-sum-rows.cc:

Go to the source code of this file.

Functions

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

Function Documentation

◆ main()

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

Definition at line 25 of file matrix-sum-rows.cc.

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

25  {
26  try {
27  using namespace kaldi;
28 
29  const char *usage =
30  "Sum the rows of an input table of matrices and output the corresponding\n"
31  "table of vectors\n"
32  "\n"
33  "Usage: matrix-sum-rows [options] <matrix-rspecifier> <vector-wspecifier>\n"
34  "e.g.: matrix-sum-rows ark:- ark:- | vector-sum ark:- sum.vec\n"
35  "See also: matrix-sum, vector-sum\n";
36 
37 
38  ParseOptions po(usage);
39 
40  po.Read(argc, argv);
41 
42  if (po.NumArgs() != 2) {
43  po.PrintUsage();
44  exit(1);
45  }
46  std::string rspecifier = po.GetArg(1);
47  std::string wspecifier = po.GetArg(2);
48 
49  SequentialBaseFloatMatrixReader mat_reader(rspecifier);
50  BaseFloatVectorWriter vec_writer(wspecifier);
51 
52  int32 num_done = 0;
53  int64 num_rows_done = 0;
54 
55  for (; !mat_reader.Done(); mat_reader.Next()) {
56  std::string key = mat_reader.Key();
57  Matrix<double> mat(mat_reader.Value());
58  Vector<double> vec(mat.NumCols());
59  vec.AddRowSumMat(1.0, mat, 0.0);
60  // Do the summation in double, to minimize roundoff.
61  Vector<BaseFloat> float_vec(vec);
62  vec_writer.Write(key, float_vec);
63  num_done++;
64  num_rows_done += mat.NumRows();
65  }
66 
67  KALDI_LOG << "Summed rows " << num_done << " matrices, "
68  << num_rows_done << " rows in total.";
69 
70  return (num_done != 0 ? 0 : 1);
71  } catch(const std::exception &e) {
72  std::cerr << e.what();
73  return -1;
74  }
75 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void AddRowSumMat(Real alpha, const MatrixBase< Real > &M, Real beta=1.0)
Does *this = alpha * (sum of rows of M) + beta * *this.
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
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_LOG
Definition: kaldi-error.h:153