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 ()
 
 ~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 ()
 
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_
 

Detailed Description

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

Constructor & Destructor Documentation

Mse ( )
inline

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

127  :
128  frames_(0.0),
129  loss_(0.0),
130  frames_progress_(0.0),
131  loss_progress_(0.0)
132  { }
double loss_progress_
Definition: nnet-loss.h:162
double frames_progress_
Definition: nnet-loss.h:161
~Mse ( )
inline

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

135  { }

Member Function Documentation

BaseFloat AvgLoss ( )
inlinevirtual

Get loss value (frame average),.

Implements LossItf.

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

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

153  {
154  return loss_ / frames_;
155  }
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 217 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_VLOG, Mse::loss_, Mse::loss_progress_, Mse::loss_vec_, CuMatrixBase< Real >::MulElements(), CuMatrixBase< Real >::MulRowsVec(), CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), VectorBase< Real >::Sum(), and CuMatrixBase< Real >::Sum().

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

220  {
221  // check inputs,
222  KALDI_ASSERT(net_out.NumCols() == target.NumCols());
223  KALDI_ASSERT(net_out.NumRows() == target.NumRows());
224  KALDI_ASSERT(net_out.NumRows() == frame_weights.Dim());
225 
226  KALDI_ASSERT(KALDI_ISFINITE(frame_weights.Sum()));
227  KALDI_ASSERT(KALDI_ISFINITE(net_out.Sum()));
228  KALDI_ASSERT(KALDI_ISFINITE(target.Sum()));
229 
230  int32 num_frames = frame_weights.Sum();
231  KALDI_ASSERT(num_frames >= 0.0);
232 
233  // get frame_weights to GPU,
234  frame_weights_ = frame_weights;
235 
236  // compute derivative w.r.t. neural nerwork outputs
237  *diff = net_out; // y
238  diff->AddMat(-1.0, target); // (y - t)
239  diff->MulRowsVec(frame_weights_); // weighting,
240 
241  // Compute MeanSquareError loss of mini-batch
242  diff_pow_2_ = *diff;
243  diff_pow_2_.MulElements(diff_pow_2_); // (y - t)^2
244  diff_pow_2_.MulRowsVec(frame_weights_); // w*(y - t)^2
245  double mean_square_error = 0.5 * diff_pow_2_.Sum(); // sum the matrix,
246 
247  KALDI_ASSERT(KALDI_ISFINITE(mean_square_error));
248 
249  // accumulate
250  loss_ += mean_square_error;
251  frames_ += num_frames;
252 
253  // progressive loss reporting
254  {
255  static const int32 progress_step = 3600*100; // 1h
256  frames_progress_ += num_frames;
257  loss_progress_ += mean_square_error;
258  if (frames_progress_ > progress_step) {
259  KALDI_VLOG(1) << "ProgressLoss[last "
260  << static_cast<int>(frames_progress_/100/3600) << "h of "
261  << static_cast<int>(frames_/100/3600) << "h]: "
262  << loss_progress_/frames_progress_ << " (Mse)";
263  // store
265  // reset
266  frames_progress_ = 0;
267  loss_progress_ = 0.0;
268  }
269  }
270 }
Real Sum() const
Definition: cu-matrix.cc:2621
CuMatrix< BaseFloat > diff_pow_2_
Definition: nnet-loss.h:167
#define KALDI_ISFINITE(x)
Definition: kaldi-math.h:74
Real Sum() const
Returns sum of the elements.
MatrixIndexT NumCols() const
Definition: cu-matrix.h:201
void MulRowsVec(const CuVectorBase< Real > &scale)
scale i'th row by scale[i]
Definition: cu-matrix.cc:752
double loss_progress_
Definition: nnet-loss.h:162
CuVector< BaseFloat > frame_weights_
Definition: nnet-loss.h:165
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:200
void AddMat(Real alpha, const CuMatrixBase< Real > &A, MatrixTransposeType trans=kNoTrans)
*this += alpha * A
Definition: cu-matrix.cc:913
#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:652
#define KALDI_VLOG(v)
Definition: kaldi-error.h:136
std::vector< float > loss_vec_
Definition: nnet-loss.h:163
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:59
double frames_progress_
Definition: nnet-loss.h:161
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 273 of file nnet-loss.cc.

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

276  {
277  int32 num_frames = net_out.NumRows(),
278  num_nn_outputs = net_out.NumCols();
279  KALDI_ASSERT(num_frames == post.size());
280 
281  // convert posterior to matrix,
282  PosteriorToMatrix(post, num_nn_outputs, &tgt_mat_);
283 
284  // call the other eval function,
285  Eval(frame_weights, net_out, tgt_mat_, diff);
286 }
CuMatrix< BaseFloat > tgt_mat_
Definition: nnet-loss.h:166
MatrixIndexT NumCols() const
Definition: cu-matrix.h:201
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:200
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:217
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::string Report ( )
virtual

Generate string with error report.

Implements LossItf.

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

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

Referenced by main().

289  {
290  // compute root mean square,
291  int32 num_tgt = diff_pow_2_.NumCols();
292  BaseFloat root_mean_square = sqrt(loss_/frames_/num_tgt);
293  // build the message,
294  std::ostringstream oss;
295  oss << "AvgLoss: " << loss_/frames_ << " (Mse), "
296  << "[RMS " << root_mean_square << ", frames "
297  << frames_ << "]" << std::endl;
298  oss << "progress: [";
299  std::copy(loss_vec_.begin(), loss_vec_.end(),
300  std::ostream_iterator<float>(oss, " "));
301  oss << "]" << std::endl;
302  return oss.str();
303 }
CuMatrix< BaseFloat > diff_pow_2_
Definition: nnet-loss.h:167
MatrixIndexT NumCols() const
Definition: cu-matrix.h:201
float BaseFloat
Definition: kaldi-types.h:29
std::vector< float > loss_vec_
Definition: nnet-loss.h:163

Member Data Documentation

CuMatrix<BaseFloat> diff_pow_2_
private

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

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

CuVector<BaseFloat> frame_weights_
private

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

Referenced by Mse::Eval().

double frames_
private

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

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

double frames_progress_
private

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

Referenced by Mse::Eval().

double loss_
private

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

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

double loss_progress_
private

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

Referenced by Mse::Eval().

std::vector<float> loss_vec_
private

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

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

CuMatrix<BaseFloat> tgt_mat_
private

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

Referenced by Mse::Eval().


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