]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/kaldi.git/blobdiff - src/bin/copy-matrix.cc
[src,egs,scripts] Cosmetic and other minor fixes, some required for segmentation...
[processor-sdk/kaldi.git] / src / bin / copy-matrix.cc
index d7b8181c64c4fe555b8ffe968f296a0befc2a144..4665634506519beff581601f0dd113eaaac9f804 100644 (file)
 #include "matrix/kaldi-matrix.h"
 #include "transform/transform-common.h"
 
+namespace kaldi {
+
+void ApplySoftMaxPerRow(MatrixBase<BaseFloat> *mat) {
+  for (int32 i = 0; i < mat->NumRows(); i++) {
+    mat->Row(i).ApplySoftMax();
+  }
+}
+
+}  // namespace kaldi
 
 int main(int argc, char *argv[]) {
   try {
@@ -36,16 +45,30 @@ int main(int argc, char *argv[]) {
         " e.g.: copy-matrix --binary=false 1.mat -\n"
         "   copy-matrix ark:2.trans ark,t:-\n"
         "See also: copy-feats\n";
-    
+
     bool binary = true;
+    bool apply_log = false;
+    bool apply_exp = false;
+    bool apply_softmax_per_row = false;
+    BaseFloat apply_power = 1.0;
     BaseFloat scale = 1.0;
+
     ParseOptions po(usage);
 
     po.Register("binary", &binary,
                 "Write in binary mode (only relevant if output is a wxfilename)");
     po.Register("scale", &scale,
                 "This option can be used to scale the matrices being copied.");
-    
+    po.Register("apply-log", &apply_log,
+                "This option can be used to apply log on the matrices. "
+                "Must be avoided if matrix has negative quantities.");
+    po.Register("apply-exp", &apply_exp,
+                "This option can be used to apply exp on the matrices");
+    po.Register("apply-power", &apply_power,
+                "This option can be used to apply a power on the matrices");
+    po.Register("apply-softmax-per-row", &apply_softmax_per_row,
+                "This option can be used to apply softmax per row of the matrices");
+
     po.Read(argc, argv);
 
     if (po.NumArgs() != 2) {
@@ -53,6 +76,10 @@ int main(int argc, char *argv[]) {
       exit(1);
     }
 
+    if ( (apply_log && apply_exp) || (apply_softmax_per_row && apply_exp) ||
+          (apply_softmax_per_row && apply_log) )
+      KALDI_ERR << "Only one of apply-log, apply-exp and "
+                << "apply-softmax-per-row can be given";
 
     std::string matrix_in_fn = po.GetArg(1),
         matrix_out_fn = po.GetArg(2);
@@ -68,11 +95,18 @@ int main(int argc, char *argv[]) {
 
     if (in_is_rspecifier != out_is_wspecifier)
       KALDI_ERR << "Cannot mix archives with regular files (copying matrices)";
-    
+
     if (!in_is_rspecifier) {
       Matrix<BaseFloat> mat;
       ReadKaldiObject(matrix_in_fn, &mat);
       if (scale != 1.0) mat.Scale(scale);
+      if (apply_log) {
+        mat.ApplyFloor(1.0e-20);
+        mat.ApplyLog();
+      }
+      if (apply_exp) mat.ApplyExp();
+      if (apply_softmax_per_row) ApplySoftMaxPerRow(&mat);
+      if (apply_power != 1.0) mat.ApplyPow(apply_power);
       Output ko(matrix_out_fn, binary);
       mat.Write(ko.Stream(), binary);
       KALDI_LOG << "Copied matrix to " << matrix_out_fn;
@@ -82,9 +116,17 @@ int main(int argc, char *argv[]) {
       BaseFloatMatrixWriter writer(matrix_out_fn);
       SequentialBaseFloatMatrixReader reader(matrix_in_fn);
       for (; !reader.Done(); reader.Next(), num_done++) {
-        if (scale != 1.0) {
+        if (scale != 1.0 || apply_log || apply_exp ||
+              apply_power != 1.0 || apply_softmax_per_row) {
           Matrix<BaseFloat> mat(reader.Value());
-          mat.Scale(scale);
+          if (scale != 1.0) mat.Scale(scale);
+          if (apply_log) {
+            mat.ApplyFloor(1.0e-20);
+            mat.ApplyLog();
+          }
+          if (apply_exp) mat.ApplyExp();
+          if (apply_softmax_per_row) ApplySoftMaxPerRow(&mat);
+          if (apply_power != 1.0) mat.ApplyPow(apply_power);
           writer.Write(reader.Key(), mat);
         } else {
           writer.Write(reader.Key(), reader.Value());