Mse Class Reference

#include <nnet-loss.h>

Inheritance diagram for Mse:
Collaboration diagram for Mse:

Public Member Functions

 Mse (LossOptions &opts)
 
 ~Mse ()
 
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,. More...
 
void Eval (const VectorBase< BaseFloat > &frame_weights, const CuMatrixBase< BaseFloat > &net_out, const Posterior &target, CuMatrix< BaseFloat > *diff)
 Evaluate mean square error using target-posteior,. More...
 
std::string Report ()
 Generate string with error report. More...
 
BaseFloat AvgLoss ()
 Get loss value (frame average),. More...
 
- Public Member Functions inherited from LossItf
 LossItf (LossOptions &opts)
 
virtual ~LossItf ()
 

Private Attributes

double frames_
 
double loss_
 
double frames_progress_
 
double loss_progress_
 
std::vector< floatloss_vec_
 
CuVector< BaseFloatframe_weights_
 
CuMatrix< BaseFloattgt_mat_
 
CuMatrix< BaseFloatdiff_pow_2_
 

Additional Inherited Members

- Protected Attributes inherited from LossItf
LossOptions opts_
 
Timer timer_
 

Detailed Description

Definition at line 149 of file nnet-loss.h.

Constructor & Destructor Documentation

◆ Mse()

Mse ( LossOptions opts)
inline

Definition at line 151 of file nnet-loss.h.

151  :
152  LossItf(opts),
153  frames_(0.0),
154  loss_(0.0),
155  frames_progress_(0.0),
156  loss_progress_(0.0)
157  { }
double loss_progress_
Definition: nnet-loss.h:188
LossItf(LossOptions &opts)
Definition: nnet-loss.h:53
double frames_progress_
Definition: nnet-loss.h:187

◆ ~Mse()

~Mse ( )
inline

Definition at line 159 of file nnet-loss.h.

160  { }

Member Function Documentation

◆ AvgLoss()

BaseFloat AvgLoss ( )
inlinevirtual

Get loss value (frame average),.

Implements LossItf.

Definition at line 178 of file nnet-loss.h.

178  {
179  if (frames_ == 0) return 0.0;
180  return loss_ / frames_;
181  }

◆ Eval() [1/2]

void Eval ( const VectorBase< BaseFloat > &  frame_weights,
const CuMatrixBase< BaseFloat > &  net_out,
const CuMatrixBase< BaseFloat > &  target,
CuMatrix< BaseFloat > *  diff 
)
virtual

Evaluate mean square error using target-matrix,.

Implements LossItf.

Definition at line 228 of file nnet-loss.cc.

References CuMatrixBase< Real >::AddMat(), VectorBase< Real >::Dim(), Xent::frame_weights_, Xent::frames_, Xent::frames_progress_, KALDI_ASSERT, KALDI_ISFINITE, KALDI_LOG, LossOptions::loss_report_frames, Xent::loss_vec_, CuMatrixBase< Real >::MulElements(), CuMatrixBase< Real >::MulRowsVec(), CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), LossItf::opts_, VectorBase< Real >::Sum(), and CuMatrixBase< Real >::Sum().

Referenced by main().

231  {
232  // check inputs,
233  KALDI_ASSERT(net_out.NumCols() == target.NumCols());
234  KALDI_ASSERT(net_out.NumRows() == target.NumRows());
235  KALDI_ASSERT(net_out.NumRows() == frame_weights.Dim());
236 
237  KALDI_ASSERT(KALDI_ISFINITE(frame_weights.Sum()));
238  KALDI_ASSERT(KALDI_ISFINITE(net_out.Sum()));
239  KALDI_ASSERT(KALDI_ISFINITE(target.Sum()));
240 
241  int32 num_frames = frame_weights.Sum();
242  KALDI_ASSERT(num_frames >= 0.0);
243 
244  // get frame_weights to GPU,
245  frame_weights_ = frame_weights;
246 
247  // compute derivative w.r.t. neural nerwork outputs
248  *diff = net_out; // y
249  diff->AddMat(-1.0, target); // (y - t)
250  diff->MulRowsVec(frame_weights_); // weighting,
251 
252  // Compute MeanSquareError loss of mini-batch
253  diff_pow_2_ = *diff;
254  diff_pow_2_.MulElements(diff_pow_2_); // (y - t)^2
255  diff_pow_2_.MulRowsVec(frame_weights_); // w*(y - t)^2
256  double mean_square_error = 0.5 * diff_pow_2_.Sum(); // sum the matrix,
257 
258  KALDI_ASSERT(KALDI_ISFINITE(mean_square_error));
259 
260  // accumulate
261  loss_ += mean_square_error;
262  frames_ += num_frames;
263 
264  // progressive loss reporting
265  if (opts_.loss_report_frames > 0) {
266  frames_progress_ += num_frames;
267  loss_progress_ += mean_square_error;
269  KALDI_LOG << "ProgressLoss[last "
270  << static_cast<int>(frames_progress_/100/3600) << "h of "
271  << static_cast<int>(frames_/100/3600) << "h]: "
272  << loss_progress_/frames_progress_ << " (Mse)";
273  // store
275  // reset
276  frames_progress_ = 0;
277  loss_progress_ = 0.0;
278  }
279  }
280 }
CuMatrix< BaseFloat > diff_pow_2_
Definition: nnet-loss.h:193
#define KALDI_ISFINITE(x)
Definition: kaldi-math.h:74
kaldi::int32 int32
int32 loss_report_frames
Report loss value every &#39;report_interval&#39; frames,.
Definition: nnet-loss.h:39
double loss_progress_
Definition: nnet-loss.h:188
CuVector< BaseFloat > frame_weights_
Definition: nnet-loss.h:191
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::vector< float > loss_vec_
Definition: nnet-loss.h:189
#define KALDI_LOG
Definition: kaldi-error.h:153
LossOptions opts_
Definition: nnet-loss.h:77
double frames_progress_
Definition: nnet-loss.h:187

◆ Eval() [2/2]

void Eval ( const VectorBase< BaseFloat > &  frame_weights,
const CuMatrixBase< BaseFloat > &  net_out,
const Posterior target,
CuMatrix< BaseFloat > *  diff 
)
virtual

Evaluate mean square error using target-posteior,.

Implements LossItf.

Definition at line 283 of file nnet-loss.cc.

References Xent::Eval(), KALDI_ASSERT, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), kaldi::nnet1::PosteriorToMatrix(), and Xent::tgt_mat_.

286  {
287  int32 num_frames = net_out.NumRows(),
288  num_nn_outputs = net_out.NumCols();
289  KALDI_ASSERT(num_frames == post.size());
290 
291  // convert posterior to matrix,
292  PosteriorToMatrix(post, num_nn_outputs, &tgt_mat_);
293 
294  // call the other eval function,
295  Eval(frame_weights, net_out, tgt_mat_, diff);
296 }
CuMatrix< BaseFloat > tgt_mat_
Definition: nnet-loss.h:192
kaldi::int32 int32
void PosteriorToMatrix(const Posterior &post, const int32 post_dim, CuMatrix< Real > *mat)
Wrapper of PosteriorToMatrix with CuMatrix argument.
Definition: nnet-utils.h:292
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.cc:228
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Report()

std::string Report ( )
virtual

Generate string with error report.

Implements LossItf.

Definition at line 299 of file nnet-loss.cc.

References Xent::frames_, and Xent::loss_vec_.

Referenced by main().

299  {
300  // compute root mean square,
301  int32 num_tgt = diff_pow_2_.NumCols();
302  BaseFloat root_mean_square = sqrt(loss_/frames_/num_tgt);
303  // build the message,
304  std::ostringstream oss;
305  oss << "AvgLoss: " << loss_/frames_ << " (Mse), "
306  << "[RMS " << root_mean_square << ", frames "
307  << frames_ << "]" << std::endl;
308  oss << "progress: [";
309  std::copy(loss_vec_.begin(), loss_vec_.end(),
310  std::ostream_iterator<float>(oss, " "));
311  oss << "]" << std::endl;
312  return oss.str();
313 }
CuMatrix< BaseFloat > diff_pow_2_
Definition: nnet-loss.h:193
kaldi::int32 int32
float BaseFloat
Definition: kaldi-types.h:29
std::vector< float > loss_vec_
Definition: nnet-loss.h:189

Member Data Documentation

◆ diff_pow_2_

CuMatrix<BaseFloat> diff_pow_2_
private

Definition at line 193 of file nnet-loss.h.

◆ frame_weights_

CuVector<BaseFloat> frame_weights_
private

Definition at line 191 of file nnet-loss.h.

◆ frames_

double frames_
private

Definition at line 184 of file nnet-loss.h.

◆ frames_progress_

double frames_progress_
private

Definition at line 187 of file nnet-loss.h.

◆ loss_

double loss_
private

Definition at line 185 of file nnet-loss.h.

◆ loss_progress_

double loss_progress_
private

Definition at line 188 of file nnet-loss.h.

◆ loss_vec_

std::vector<float> loss_vec_
private

Definition at line 189 of file nnet-loss.h.

◆ tgt_mat_

CuMatrix<BaseFloat> tgt_mat_
private

Definition at line 192 of file nnet-loss.h.


The documentation for this class was generated from the following files: