matrix-sum-rows.cc
Go to the documentation of this file.
1 // bin/matrix-sum-rows.cc
2 
3 // Copyright 2012 Johns Hopkins University (author: Daniel Povey)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #include "base/kaldi-common.h"
21 #include "util/common-utils.h"
22 #include "matrix/kaldi-matrix.h"
23 
24 
25 int main(int argc, char *argv[]) {
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 }
76 
77 
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.
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
void Write(const std::string &key, const T &value) const
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
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
int main(int argc, char *argv[])
#define KALDI_LOG
Definition: kaldi-error.h:153