forked from kaldi-asr/kaldi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnnet-logprob2-parallel.cc
More file actions
164 lines (136 loc) · 5.65 KB
/
nnet-logprob2-parallel.cc
File metadata and controls
164 lines (136 loc) · 5.65 KB
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
160
161
162
// nnet2bin/nnet-logprob2-parallel.cc
// Copyright 2012 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 "nnet2/nnet-update-parallel.h"
#include "nnet2/am-nnet.h"
#include "nnet2/nnet-compute.h"
#include "thread/kaldi-task-sequence.h"
namespace kaldi {
namespace nnet2 {
struct NnetLogprobTask {
NnetLogprobTask(const AmNnet &am_nnet,
const CuVector<BaseFloat> &inv_priors,
const std::string &key,
const Matrix<BaseFloat> &feats,
BaseFloatMatrixWriter *prob_writer_nodiv,
BaseFloatMatrixWriter *logprob_writer_divided):
am_nnet_(am_nnet), inv_priors_(inv_priors), key_(key), feats_(feats),
prob_writer_nodiv_(prob_writer_nodiv),
logprob_writer_divided_(logprob_writer_divided) { }
void operator () () {
log_probs_.Resize(feats_.NumRows(), am_nnet_.NumPdfs());
bool pad_input = true;
NnetComputation(am_nnet_.GetNnet(), feats_, pad_input,
&log_probs_);
}
~NnetLogprobTask() { // Produces output. Run sequentially.
// at this point they are probabilities, not log-probs, without prior division.
prob_writer_nodiv_->Write(key_, Matrix<BaseFloat>(log_probs_));
log_probs_.MulColsVec(inv_priors_); // scales each column by the corresponding element
// of inv_priors.
for (int32 i = 0; i < log_probs_.NumRows(); i++) {
CuSubVector<BaseFloat> frame(log_probs_, i);
BaseFloat p = frame.Sum();
if (!(p > 0.0)) {
KALDI_WARN << "Bad sum of probabilities " << p;
} else {
frame.Scale(1.0 / p); // re-normalize to sum to one.
}
}
log_probs_.ApplyFloor(1.0e-20); // To avoid log of zero which leads to NaN.
log_probs_.ApplyLog();
logprob_writer_divided_->Write(key_, Matrix<BaseFloat>(log_probs_));
}
private:
const AmNnet &am_nnet_;
const CuVector<BaseFloat> &inv_priors_;
std::string key_;
CuMatrix<BaseFloat> feats_;
CuMatrix<BaseFloat> log_probs_;
BaseFloatMatrixWriter *prob_writer_nodiv_;
BaseFloatMatrixWriter *logprob_writer_divided_;
};
} // namespace nnet2
} // namespace kaldi
int main(int argc, char *argv[]) {
try {
using namespace kaldi;
using namespace kaldi::nnet2;
typedef kaldi::int32 int32;
typedef kaldi::int64 int64;
const char *usage =
"Do the forward computation for a neural net acoustic model, and output\n"
"matrix of logprobs. This version of the program outputs to two tables,\n"
"one table of probabilities without prior division and one table of\n"
"log-probs with prior division. It is intended for use in discriminative\n"
"training. This version supports multi-threaded operation (--num-threads\n"
"option)\n"
"\n"
"Usage: nnet-logprob2-parallel [options] <model-in> <features-rspecifier> "
"<probs-wspecifier-not-divided> <logprobs-wspecifier-divided>\n"
"\n"
"e.g.: nnet-logprob2-parallel 1.nnet \"$feats\" ark:- \"ark:|logprob-to-post ark:- 1.post\" ark:- \\"
" | latgen-faster-mapped [args]\n";
TaskSequencerConfig thread_config;
ParseOptions po(usage);
thread_config.Register(&po);
po.Read(argc, argv);
if (po.NumArgs() != 4) {
po.PrintUsage();
exit(1);
}
std::string nnet_rxfilename = po.GetArg(1),
feats_rspecifier = po.GetArg(2),
prob_wspecifier_nodiv = po.GetArg(3),
logprob_wspecifier_divided = po.GetArg(4);
TransitionModel trans_model;
AmNnet am_nnet;
{
bool binary_read;
Input ki(nnet_rxfilename, &binary_read);
trans_model.Read(ki.Stream(), binary_read);
am_nnet.Read(ki.Stream(), binary_read);
}
int64 num_done = 0, num_err = 0;
CuVector<BaseFloat> inv_priors(am_nnet.Priors());
KALDI_ASSERT(inv_priors.Dim() == am_nnet.NumPdfs() &&
"Priors in neural network not set up.");
inv_priors.ApplyPow(-1.0);
SequentialBaseFloatMatrixReader feature_reader(feats_rspecifier);
BaseFloatMatrixWriter prob_writer_nodiv(prob_wspecifier_nodiv);
BaseFloatMatrixWriter logprob_writer_divided(logprob_wspecifier_divided);
{
TaskSequencer<NnetLogprobTask> sequencer(thread_config);
for (; !feature_reader.Done(); feature_reader.Next()) {
std::string key = feature_reader.Key();
const Matrix<BaseFloat> &feats = feature_reader.Value();
sequencer.Run(new NnetLogprobTask(am_nnet, inv_priors, key, feats,
&prob_writer_nodiv,
&logprob_writer_divided));
num_done++;
}
}
KALDI_LOG << "Finished computing neural net log-probs, processed "
<< num_done << " utterances, " << num_err << " with errors.";
return (num_done == 0 ? 1 : 0);
} catch(const std::exception &e) {
std::cerr << e.what() << '\n';
return -1;
}
}