nnet-loss.h
Go to the documentation of this file.
1 // nnet/nnet-loss.h
2 
3 // Copyright 2011-2015 Brno University of Technology (author: Karel Vesely)
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 #ifndef KALDI_NNET_NNET_LOSS_H_
21 #define KALDI_NNET_NNET_LOSS_H_
22 
23 #include <string>
24 #include <vector>
25 
26 #include "base/kaldi-common.h"
27 #include "base/timer.h"
28 #include "util/kaldi-holder.h"
29 #include "itf/options-itf.h"
30 #include "cudamatrix/cu-matrix.h"
31 #include "cudamatrix/cu-vector.h"
32 #include "cudamatrix/cu-array.h"
33 #include "hmm/posterior.h"
34 
35 namespace kaldi {
36 namespace nnet1 {
37 
38 struct LossOptions {
40 
42  loss_report_frames(5*3600*100) // 5h,
43  { }
44 
45  void Register(OptionsItf *opts) {
46  opts->Register("loss-report-frames", &loss_report_frames,
47  "Report loss per blocks of N frames (0 = no reports)");
48  }
49 };
50 
51 class LossItf {
52  public:
54  opts_ = opts;
55  }
56  virtual ~LossItf() { }
57 
59  virtual void Eval(const VectorBase<BaseFloat> &frame_weights,
60  const CuMatrixBase<BaseFloat> &net_out,
61  const CuMatrixBase<BaseFloat> &target,
62  CuMatrix<BaseFloat> *diff) = 0;
63 
65  virtual void Eval(const VectorBase<BaseFloat> &frame_weights,
66  const CuMatrixBase<BaseFloat> &net_out,
67  const Posterior &target,
68  CuMatrix<BaseFloat> *diff) = 0;
69 
71  virtual std::string Report() = 0;
72 
74  virtual BaseFloat AvgLoss() = 0;
75 
76  protected:
79 };
80 
81 
82 class Xent : public LossItf {
83  public:
84  Xent(LossOptions &opts):
85  LossItf(opts),
86  frames_progress_(0.0),
87  xentropy_progress_(0.0),
88  entropy_progress_(0.0),
89  elapsed_seconds_(0.0)
90  { }
91 
93  { }
94 
96  void Eval(const VectorBase<BaseFloat> &frame_weights,
97  const CuMatrixBase<BaseFloat> &net_out,
98  const CuMatrixBase<BaseFloat> &target,
99  CuMatrix<BaseFloat> *diff);
100 
102  void Eval(const VectorBase<BaseFloat> &frame_weights,
103  const CuMatrixBase<BaseFloat> &net_out,
104  const Posterior &target,
105  CuMatrix<BaseFloat> *diff);
106 
108  std::string Report();
109 
111  std::string ReportPerClass();
112 
115  if (frames_.Sum() == 0) return 0.0;
116  return (xentropy_.Sum() - entropy_.Sum()) / frames_.Sum();
117  }
118 
119  private:
120  // main stats collected per target-class,
125 
126  // partial results during training,
130  std::vector<float> loss_vec_;
132 
133  // weigting buffer,
136 
137  // loss computation buffers,
142 
143  // frame classification buffers,
146 };
147 
148 
149 class Mse : public LossItf {
150  public:
151  Mse(LossOptions &opts):
152  LossItf(opts),
153  frames_(0.0),
154  loss_(0.0),
155  frames_progress_(0.0),
156  loss_progress_(0.0)
157  { }
158 
160  { }
161 
163  void Eval(const VectorBase<BaseFloat> &frame_weights,
164  const CuMatrixBase<BaseFloat>& net_out,
165  const CuMatrixBase<BaseFloat>& target,
166  CuMatrix<BaseFloat>* diff);
167 
169  void Eval(const VectorBase<BaseFloat> &frame_weights,
170  const CuMatrixBase<BaseFloat>& net_out,
171  const Posterior& target,
172  CuMatrix<BaseFloat>* diff);
173 
175  std::string Report();
176 
179  if (frames_ == 0) return 0.0;
180  return loss_ / frames_;
181  }
182 
183  private:
184  double frames_;
185  double loss_;
186 
189  std::vector<float> loss_vec_;
190 
194 };
195 
196 
197 class MultiTaskLoss : public LossItf {
198  public:
200  LossItf(opts)
201  { }
202 
204  while (loss_vec_.size() > 0) {
205  delete loss_vec_.back();
206  loss_vec_.pop_back();
207  }
208  }
209 
215  void InitFromString(const std::string& s);
216 
218  void Eval(const VectorBase<BaseFloat> &frame_weights,
219  const CuMatrixBase<BaseFloat>& net_out,
220  const CuMatrixBase<BaseFloat>& target,
221  CuMatrix<BaseFloat>* diff) {
222  KALDI_ERR << "This is not supposed to be called!";
223  }
224 
226  void Eval(const VectorBase<BaseFloat> &frame_weights,
227  const CuMatrixBase<BaseFloat>& net_out,
228  const Posterior& target,
229  CuMatrix<BaseFloat>* diff);
230 
232  std::string Report();
233 
235  BaseFloat AvgLoss();
236 
237  private:
238  std::vector<LossItf*> loss_vec_;
239  std::vector<int32> loss_dim_;
240  std::vector<BaseFloat> loss_weights_;
241 
242  std::vector<int32> loss_dim_offset_;
243 
245 };
246 
247 } // namespace nnet1
248 } // namespace kaldi
249 
250 #endif // KALDI_NNET_NNET_LOSS_H_
251 
double xentropy_progress_
Definition: nnet-loss.h:128
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
BaseFloat AvgLoss()
Get loss value (frame average),.
Definition: nnet-loss.h:114
CuVector< double > frames_
Definition: nnet-loss.h:121
double elapsed_seconds_
Definition: nnet-loss.h:131
CuMatrix< BaseFloat > frames_aux_
Definition: nnet-loss.h:139
CuVector< BaseFloat > target_sum_
Definition: nnet-loss.h:135
CuMatrix< BaseFloat > diff_pow_2_
Definition: nnet-loss.h:193
CuMatrix< BaseFloat > tgt_mat_
Definition: nnet-loss.h:192
std::vector< float > loss_vec_
Definition: nnet-loss.h:130
CuVector< double > xentropy_
Definition: nnet-loss.h:123
kaldi::int32 int32
This class represents a matrix that&#39;s stored on the GPU if we have one, and in memory if not...
Definition: matrix-common.h:71
CuMatrix< BaseFloat > tgt_mat_
Definition: nnet-loss.h:138
MultiTaskLoss(LossOptions &opts)
Definition: nnet-loss.h:199
CuMatrix< BaseFloat > entropy_aux_
Definition: nnet-loss.h:141
CuArray< int32 > max_id_out_
Definition: nnet-loss.h:144
Vector< double > correct_
Definition: nnet-loss.h:122
virtual void Register(const std::string &name, bool *ptr, const std::string &doc)=0
std::vector< BaseFloat > loss_weights_
Definition: nnet-loss.h:240
double entropy_progress_
Definition: nnet-loss.h:129
std::vector< std::vector< std::pair< int32, BaseFloat > > > Posterior
Posterior is a typedef for storing acoustic-state (actually, transition-id) posteriors over an uttera...
Definition: posterior.h:42
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-matrix,.
Definition: nnet-loss.h:218
int32 loss_report_frames
Report loss value every &#39;report_interval&#39; frames,.
Definition: nnet-loss.h:39
virtual ~LossItf()
Definition: nnet-loss.h:56
BaseFloat AvgLoss()
Get loss value (frame average),.
Definition: nnet-loss.h:178
double loss_progress_
Definition: nnet-loss.h:188
CuMatrix< BaseFloat > tgt_mat_
Definition: nnet-loss.h:244
std::vector< int32 > loss_dim_offset_
Definition: nnet-loss.h:242
LossItf(LossOptions &opts)
Definition: nnet-loss.h:53
CuVector< BaseFloat > frame_weights_
Definition: nnet-loss.h:191
CuVector< double > entropy_
Definition: nnet-loss.h:124
CuVector< BaseFloat > frame_weights_
Definition: nnet-loss.h:134
Mse(LossOptions &opts)
Definition: nnet-loss.h:151
#define KALDI_ERR
Definition: kaldi-error.h:147
CuMatrix< BaseFloat > xentropy_aux_
Definition: nnet-loss.h:140
Matrix for CUDA computing.
Definition: matrix-common.h:69
CuArray< int32 > max_id_tgt_
Definition: nnet-loss.h:145
std::vector< int32 > loss_dim_
Definition: nnet-loss.h:239
double frames_progress_
Definition: nnet-loss.h:127
std::vector< float > loss_vec_
Definition: nnet-loss.h:189
Xent(LossOptions &opts)
Definition: nnet-loss.h:84
std::vector< LossItf * > loss_vec_
Definition: nnet-loss.h:238
void Register(OptionsItf *opts)
Definition: nnet-loss.h:45
Provides a vector abstraction class.
Definition: kaldi-vector.h:41
LossOptions opts_
Definition: nnet-loss.h:77
double frames_progress_
Definition: nnet-loss.h:187