All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
copy-matrix.cc
Go to the documentation of this file.
1 // bin/copy-matrix.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
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"
24 
25 namespace kaldi {
26 
28  for (int32 i = 0; i < mat->NumRows(); i++) {
29  mat->Row(i).ApplySoftMax();
30  }
31 }
32 
33 } // namespace kaldi
34 
35 int main(int argc, char *argv[]) {
36  try {
37  using namespace kaldi;
38 
39  const char *usage =
40  "Copy matrices, or archives of matrices (e.g. features or transforms)\n"
41  "Also see copy-feats which has other format options\n"
42  "\n"
43  "Usage: copy-matrix [options] <matrix-in-rspecifier> <matrix-out-wspecifier>\n"
44  " or: copy-matrix [options] <matrix-in-rxfilename> <matrix-out-wxfilename>\n"
45  " e.g.: copy-matrix --binary=false 1.mat -\n"
46  " copy-matrix ark:2.trans ark,t:-\n"
47  "See also: copy-feats\n";
48 
49  bool binary = true;
50  bool apply_log = false;
51  bool apply_exp = false;
52  bool apply_softmax_per_row = false;
53  BaseFloat apply_power = 1.0;
54  BaseFloat scale = 1.0;
55 
56  ParseOptions po(usage);
57 
58  po.Register("binary", &binary,
59  "Write in binary mode (only relevant if output is a wxfilename)");
60  po.Register("scale", &scale,
61  "This option can be used to scale the matrices being copied.");
62  po.Register("apply-log", &apply_log,
63  "This option can be used to apply log on the matrices. "
64  "Must be avoided if matrix has negative quantities.");
65  po.Register("apply-exp", &apply_exp,
66  "This option can be used to apply exp on the matrices");
67  po.Register("apply-power", &apply_power,
68  "This option can be used to apply a power on the matrices");
69  po.Register("apply-softmax-per-row", &apply_softmax_per_row,
70  "This option can be used to apply softmax per row of the matrices");
71 
72  po.Read(argc, argv);
73 
74  if (po.NumArgs() != 2) {
75  po.PrintUsage();
76  exit(1);
77  }
78 
79  if ( (apply_log && apply_exp) || (apply_softmax_per_row && apply_exp) ||
80  (apply_softmax_per_row && apply_log) )
81  KALDI_ERR << "Only one of apply-log, apply-exp and "
82  << "apply-softmax-per-row can be given";
83 
84  std::string matrix_in_fn = po.GetArg(1),
85  matrix_out_fn = po.GetArg(2);
86 
87  // all these "fn"'s are either rspecifiers or filenames.
88 
89  bool in_is_rspecifier =
90  (ClassifyRspecifier(matrix_in_fn, NULL, NULL)
91  != kNoRspecifier),
92  out_is_wspecifier =
93  (ClassifyWspecifier(matrix_out_fn, NULL, NULL, NULL)
94  != kNoWspecifier);
95 
96  if (in_is_rspecifier != out_is_wspecifier)
97  KALDI_ERR << "Cannot mix archives with regular files (copying matrices)";
98 
99  if (!in_is_rspecifier) {
100  Matrix<BaseFloat> mat;
101  ReadKaldiObject(matrix_in_fn, &mat);
102  if (scale != 1.0) mat.Scale(scale);
103  if (apply_log) {
104  mat.ApplyFloor(1.0e-20);
105  mat.ApplyLog();
106  }
107  if (apply_exp) mat.ApplyExp();
108  if (apply_softmax_per_row) ApplySoftMaxPerRow(&mat);
109  if (apply_power != 1.0) mat.ApplyPow(apply_power);
110  Output ko(matrix_out_fn, binary);
111  mat.Write(ko.Stream(), binary);
112  KALDI_LOG << "Copied matrix to " << matrix_out_fn;
113  return 0;
114  } else {
115  int num_done = 0;
116  BaseFloatMatrixWriter writer(matrix_out_fn);
117  SequentialBaseFloatMatrixReader reader(matrix_in_fn);
118  for (; !reader.Done(); reader.Next(), num_done++) {
119  if (scale != 1.0 || apply_log || apply_exp ||
120  apply_power != 1.0 || apply_softmax_per_row) {
121  Matrix<BaseFloat> mat(reader.Value());
122  if (scale != 1.0) mat.Scale(scale);
123  if (apply_log) {
124  mat.ApplyFloor(1.0e-20);
125  mat.ApplyLog();
126  }
127  if (apply_exp) mat.ApplyExp();
128  if (apply_softmax_per_row) ApplySoftMaxPerRow(&mat);
129  if (apply_power != 1.0) mat.ApplyPow(apply_power);
130  writer.Write(reader.Key(), mat);
131  } else {
132  writer.Write(reader.Key(), reader.Value());
133  }
134  }
135  KALDI_LOG << "Copied " << num_done << " matrices.";
136  return (num_done != 0 ? 0 : 1);
137  }
138  } catch(const std::exception &e) {
139  std::cerr << e.what();
140  return -1;
141  }
142 }
143 
144 
void ApplyLog()
Calculates log of all the matrix elemnts.
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
void Write(const std::string &key, const T &value) const
int main(int argc, char *argv[])
Definition: copy-matrix.cc:35
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
void Register(const std::string &name, bool *ptr, const std::string &doc)
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:829
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:185
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
std::ostream & Stream()
Definition: kaldi-io.cc:698
void Scale(Real alpha)
Multiply each element with a scalar value.
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
void ApplySoftMaxPerRow(MatrixBase< BaseFloat > *mat)
Definition: copy-matrix.cc:27
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#define KALDI_ERR
Definition: kaldi-error.h:127
void Write(std::ostream &out, bool binary) const
write to stream.
void ApplyExp()
Exponentiate each of the elements.
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *wxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
int NumArgs() const
Number of positional parameters (c.f. argc-1).
WspecifierType ClassifyWspecifier(const std::string &wspecifier, std::string *archive_wxfilename, std::string *script_wxfilename, WspecifierOptions *opts)
Definition: kaldi-table.cc:135
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
void ApplyFloor(Real floor_val)
Applies floor to all matrix elements.
void ApplyPow(Real power)
Applies power to all matrix elements.
#define KALDI_LOG
Definition: kaldi-error.h:133
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.