Blame view

src/nnet3bin/nnet3-discriminative-compute-from-egs.cc 4.86 KB
8dcb6dfcb   Yannick Estève   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
  // nnet3bin/nnet3-discriminative-compute-from-egs.cc
  
  // Copyright 2015  Johns Hopkins University (author: Daniel Povey)
  
  // See ../../COPYING for clarification regarding multiple authors
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //  http://www.apache.org/licenses/LICENSE-2.0
  //
  // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  // MERCHANTABLITY OR NON-INFRINGEMENT.
  // See the Apache 2 License for the specific language governing permissions and
  // limitations under the License.
  
  #include "base/kaldi-common.h"
  #include "util/common-utils.h"
  #include "hmm/transition-model.h"
  #include "nnet3/nnet-nnet.h"
  #include "nnet3/nnet-example-utils.h"
  #include "nnet3/nnet-discriminative-example.h"
  #include "nnet3/nnet-optimize.h"
  
  
  namespace kaldi {
  namespace nnet3 {
  
  class NnetComputerFromEg {
   public:
    NnetComputerFromEg(const Nnet &nnet):
        nnet_(nnet), compiler_(nnet) { }
  
    // Compute the output (which will have the same number of rows as the number
    // of Indexes in the output of the eg), and put it in "output".
    void Compute(const NnetExample &eg, Matrix<BaseFloat> *output) {
      ComputationRequest request;
      bool need_backprop = false, store_stats = false;
      GetComputationRequest(nnet_, eg, need_backprop, store_stats, &request);
      const NnetComputation &computation = *(compiler_.Compile(request));
      NnetComputeOptions options;
      if (GetVerboseLevel() >= 3)
        options.debug = true;
      NnetComputer computer(options, computation, nnet_, NULL);
      computer.AcceptInputs(nnet_, eg.io);
      computer.Run();
      const CuMatrixBase<BaseFloat> &nnet_output = computer.GetOutput("output");
      output->Resize(nnet_output.NumRows(), nnet_output.NumCols());
      nnet_output.CopyToMat(output);
    }
   private:
    const Nnet &nnet_;
    CachingOptimizingCompiler compiler_;
  
  };
  
  }
  }
  
  int main(int argc, char *argv[]) {
    try {
      using namespace kaldi;
      using namespace kaldi::nnet3;
      typedef kaldi::int32 int32;
      typedef kaldi::int64 int64;
  
      const char *usage =
          "Read input nnet discriminative training examples, and compute the "
          "output for each one. This program is similar to "
          "nnet3-compute-from-egs, but works with discriminative egs. 
  "
          "If --apply-exp=true, apply the Exp() function to the output before writing
  "
          "it out.
  "
          "Note: This program uses only the input; it does not do forward-backward
  "
          "over the lattice. See nnet3-discriminative-compute-objf for that.
  "
          "
  "
          "Usage:  nnet3-discriminative-compute-from-egs [options] <raw-nnet-in> <training-examples-in> <matrices-out>
  "
          "e.g.:
  "
          "nnet3-discriminative-compute-from-egs --apply-exp=true 0.raw ark:1.degs ark:- | matrix-sum-rows ark:- ... 
  "
          "See also: nnet3-compute nnet3-compute-from-egs
  ";
  
      bool binary_write = true,
          apply_exp = false;
      std::string use_gpu = "yes";
  
      ParseOptions po(usage);
      po.Register("binary", &binary_write, "Write output in binary mode");
      po.Register("apply-exp", &apply_exp, "If true, apply exp function to "
                  "output");
      po.Register("use-gpu", &use_gpu,
                  "yes|no|optional|wait, only has effect if compiled with CUDA");
  
      po.Read(argc, argv);
  
      if (po.NumArgs() != 3) {
        po.PrintUsage();
        exit(1);
      }
  
  #if HAVE_CUDA==1
      CuDevice::Instantiate().SelectGpuId(use_gpu);
  #endif
  
      std::string nnet_rxfilename = po.GetArg(1),
          examples_rspecifier = po.GetArg(2),
          matrix_wspecifier = po.GetArg(3);
  
      Nnet nnet;
      ReadKaldiObject(nnet_rxfilename, &nnet);
  
      NnetComputerFromEg computer(nnet);
  
      int64 num_egs = 0;
  
      SequentialNnetDiscriminativeExampleReader example_reader(examples_rspecifier);
      BaseFloatMatrixWriter matrix_writer(matrix_wspecifier);
  
      for (; !example_reader.Done(); example_reader.Next(), num_egs++) {
        Matrix<BaseFloat> output;
        NnetExample eg;
        NnetDiscriminativeExample disc_eg = example_reader.Value();
        eg.io.swap(disc_eg.inputs);
  
        for (int32 i = 0; i < disc_eg.outputs.size(); i++) {
          NnetIo io;
          io.name = disc_eg.outputs[i].name;
          io.indexes = disc_eg.outputs[i].indexes;
          eg.io.push_back(io);
        }
  
        computer.Compute(eg, &output);
        KALDI_ASSERT(output.NumRows() != 0);
        if (apply_exp)
          output.ApplyExp();
        matrix_writer.Write(example_reader.Key(), output);
      }
  #if HAVE_CUDA==1
      CuDevice::Instantiate().PrintProfile();
  #endif
      KALDI_LOG << "Processed " << num_egs << " examples.";
      return 0;
    } catch(const std::exception &e) {
      std::cerr << e.what() << '
  ';
      return -1;
    }
  }