All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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< float > loss_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 ( 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 ( )
inline

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

160  { }

Member Function Documentation

BaseFloat AvgLoss ( )
inlinevirtual

Get loss value (frame average),.

Implements LossItf.

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

References Mse::frames_, and Mse::loss_.

178  {
179  if (frames_ == 0) return 0.0;
180  return loss_ / frames_;
181  }
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(), Mse::diff_pow_2_, VectorBase< Real >::Dim(), Mse::frame_weights_, Mse::frames_, Mse::frames_progress_, KALDI_ASSERT, KALDI_ISFINITE, KALDI_LOG, Mse::loss_, Mse::loss_progress_, LossOptions::loss_report_frames, Mse::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 Mse::Eval(), and 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 }
Real Sum() const
Definition: cu-matrix.cc:2913
CuMatrix< BaseFloat > diff_pow_2_
Definition: nnet-loss.h:193
#define KALDI_ISFINITE(x)
Definition: kaldi-math.h:74
Real Sum() const
Returns sum of the elements.
MatrixIndexT NumCols() const
Definition: cu-matrix.h:206
void MulRowsVec(const CuVectorBase< Real > &scale)
scale i'th row by scale[i]
Definition: cu-matrix.cc:779
int32 loss_report_frames
Report loss value every 'report_interval' frames,.
Definition: nnet-loss.h:39
double loss_progress_
Definition: nnet-loss.h:188
CuVector< BaseFloat > frame_weights_
Definition: nnet-loss.h:191
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:205
void AddMat(Real alpha, const CuMatrixBase< Real > &A, MatrixTransposeType trans=kNoTrans)
*this += alpha * A
Definition: cu-matrix.cc:941
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void MulElements(const CuMatrixBase< Real > &A)
Multiply two matrices elementwise: C = C .* A.
Definition: cu-matrix.cc:654
std::vector< float > loss_vec_
Definition: nnet-loss.h:189
#define KALDI_LOG
Definition: kaldi-error.h:133
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:62
LossOptions opts_
Definition: nnet-loss.h:77
double frames_progress_
Definition: nnet-loss.h:187
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 Mse::Eval(), KALDI_ASSERT, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), kaldi::nnet1::PosteriorToMatrix(), and Mse::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
MatrixIndexT NumCols() const
Definition: cu-matrix.h:206
void PosteriorToMatrix(const Posterior &post, const int32 post_dim, CuMatrix< Real > *mat)
Wrapper of PosteriorToMatrix with CuMatrix argument.
Definition: nnet-utils.h:292
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:205
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:169
std::string Report ( )
virtual

Generate string with error report.

Implements LossItf.

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

References Mse::diff_pow_2_, Mse::frames_, Mse::loss_, Mse::loss_vec_, and CuMatrixBase< Real >::NumCols().

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
MatrixIndexT NumCols() const
Definition: cu-matrix.h:206
float BaseFloat
Definition: kaldi-types.h:29
std::vector< float > loss_vec_
Definition: nnet-loss.h:189

Member Data Documentation

CuMatrix<BaseFloat> diff_pow_2_
private

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

Referenced by Mse::Eval(), and Mse::Report().

CuVector<BaseFloat> frame_weights_
private

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

Referenced by Mse::Eval().

double frames_
private

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

Referenced by Mse::AvgLoss(), Mse::Eval(), and Mse::Report().

double frames_progress_
private

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

Referenced by Mse::Eval().

double loss_
private

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

Referenced by Mse::AvgLoss(), Mse::Eval(), and Mse::Report().

double loss_progress_
private

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

Referenced by Mse::Eval().

std::vector<float> loss_vec_
private

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

Referenced by Mse::Eval(), and Mse::Report().

CuMatrix<BaseFloat> tgt_mat_
private

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

Referenced by Mse::Eval().


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