nnet-loss.h
6.61 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// nnet/nnet-loss.h
// Copyright 2011-2015 Brno University of Technology (author: Karel Vesely)
// 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.
#ifndef KALDI_NNET_NNET_LOSS_H_
#define KALDI_NNET_NNET_LOSS_H_
#include <string>
#include <vector>
#include "base/kaldi-common.h"
#include "base/timer.h"
#include "util/kaldi-holder.h"
#include "itf/options-itf.h"
#include "cudamatrix/cu-matrix.h"
#include "cudamatrix/cu-vector.h"
#include "cudamatrix/cu-array.h"
#include "hmm/posterior.h"
namespace kaldi {
namespace nnet1 {
struct LossOptions {
int32 loss_report_frames; ///< Report loss value every 'report_interval' frames,
LossOptions():
loss_report_frames(5*3600*100) // 5h,
{ }
void Register(OptionsItf *opts) {
opts->Register("loss-report-frames", &loss_report_frames,
"Report loss per blocks of N frames (0 = no reports)");
}
};
class LossItf {
public:
LossItf(LossOptions& opts) {
opts_ = opts;
}
virtual ~LossItf() { }
/// Evaluate cross entropy using target-matrix (supports soft labels),
virtual void Eval(const VectorBase<BaseFloat> &frame_weights,
const CuMatrixBase<BaseFloat> &net_out,
const CuMatrixBase<BaseFloat> &target,
CuMatrix<BaseFloat> *diff) = 0;
/// Evaluate cross entropy using target-posteriors (supports soft labels),
virtual void Eval(const VectorBase<BaseFloat> &frame_weights,
const CuMatrixBase<BaseFloat> &net_out,
const Posterior &target,
CuMatrix<BaseFloat> *diff) = 0;
/// Generate string with error report,
virtual std::string Report() = 0;
/// Get loss value (frame average),
virtual BaseFloat AvgLoss() = 0;
protected:
LossOptions opts_;
Timer timer_;
};
class Xent : public LossItf {
public:
Xent(LossOptions &opts):
LossItf(opts),
frames_progress_(0.0),
xentropy_progress_(0.0),
entropy_progress_(0.0),
elapsed_seconds_(0.0)
{ }
~Xent()
{ }
/// Evaluate cross entropy using target-matrix (supports soft labels),
void Eval(const VectorBase<BaseFloat> &frame_weights,
const CuMatrixBase<BaseFloat> &net_out,
const CuMatrixBase<BaseFloat> &target,
CuMatrix<BaseFloat> *diff);
/// Evaluate cross entropy using target-posteriors (supports soft labels),
void Eval(const VectorBase<BaseFloat> &frame_weights,
const CuMatrixBase<BaseFloat> &net_out,
const Posterior &target,
CuMatrix<BaseFloat> *diff);
/// Generate string with error report,
std::string Report();
/// Generate string with per-class error report,
std::string ReportPerClass();
/// Get loss value (frame average),
BaseFloat AvgLoss() {
if (frames_.Sum() == 0) return 0.0;
return (xentropy_.Sum() - entropy_.Sum()) / frames_.Sum();
}
private:
// main stats collected per target-class,
CuVector<double> frames_;
Vector<double> correct_;
CuVector<double> xentropy_;
CuVector<double> entropy_;
// partial results during training,
double frames_progress_;
double xentropy_progress_;
double entropy_progress_;
std::vector<float> loss_vec_;
double elapsed_seconds_;
// weigting buffer,
CuVector<BaseFloat> frame_weights_;
CuVector<BaseFloat> target_sum_;
// loss computation buffers,
CuMatrix<BaseFloat> tgt_mat_;
CuMatrix<BaseFloat> frames_aux_;
CuMatrix<BaseFloat> xentropy_aux_;
CuMatrix<BaseFloat> entropy_aux_;
// frame classification buffers,
CuArray<int32> max_id_out_;
CuArray<int32> max_id_tgt_;
};
class Mse : public LossItf {
public:
Mse(LossOptions &opts):
LossItf(opts),
frames_(0.0),
loss_(0.0),
frames_progress_(0.0),
loss_progress_(0.0)
{ }
~Mse()
{ }
/// Evaluate mean square error using target-matrix,
void Eval(const VectorBase<BaseFloat> &frame_weights,
const CuMatrixBase<BaseFloat>& net_out,
const CuMatrixBase<BaseFloat>& target,
CuMatrix<BaseFloat>* diff);
/// Evaluate mean square error using target-posteior,
void Eval(const VectorBase<BaseFloat> &frame_weights,
const CuMatrixBase<BaseFloat>& net_out,
const Posterior& target,
CuMatrix<BaseFloat>* diff);
/// Generate string with error report
std::string Report();
/// Get loss value (frame average),
BaseFloat AvgLoss() {
if (frames_ == 0) return 0.0;
return loss_ / frames_;
}
private:
double frames_;
double loss_;
double frames_progress_;
double loss_progress_;
std::vector<float> loss_vec_;
CuVector<BaseFloat> frame_weights_;
CuMatrix<BaseFloat> tgt_mat_;
CuMatrix<BaseFloat> diff_pow_2_;
};
class MultiTaskLoss : public LossItf {
public:
MultiTaskLoss(LossOptions &opts):
LossItf(opts)
{ }
~MultiTaskLoss() {
while (loss_vec_.size() > 0) {
delete loss_vec_.back();
loss_vec_.pop_back();
}
}
/// Initialize from string, the format for string 's' is :
/// 'multitask,<type1>,<dim1>,<weight1>,...,<typeN>,<dimN>,<weightN>'
///
/// Practically it can look like this :
/// 'multitask,xent,2456,1.0,mse,440,0.001'
void InitFromString(const std::string& s);
/// Evaluate mean square error using target-matrix,
void Eval(const VectorBase<BaseFloat> &frame_weights,
const CuMatrixBase<BaseFloat>& net_out,
const CuMatrixBase<BaseFloat>& target,
CuMatrix<BaseFloat>* diff) {
KALDI_ERR << "This is not supposed to be called!";
}
/// Evaluate mean square error using target-posteior,
void Eval(const VectorBase<BaseFloat> &frame_weights,
const CuMatrixBase<BaseFloat>& net_out,
const Posterior& target,
CuMatrix<BaseFloat>* diff);
/// Generate string with error report
std::string Report();
/// Get loss value (frame average),
BaseFloat AvgLoss();
private:
std::vector<LossItf*> loss_vec_;
std::vector<int32> loss_dim_;
std::vector<BaseFloat> loss_weights_;
std::vector<int32> loss_dim_offset_;
CuMatrix<BaseFloat> tgt_mat_;
};
} // namespace nnet1
} // namespace kaldi
#endif // KALDI_NNET_NNET_LOSS_H_