Xent Class Reference

#include <nnet-loss.h>

Inheritance diagram for Xent:
Collaboration diagram for Xent:

Public Member Functions

 Xent (LossOptions &opts)
 
 ~Xent ()
 
void Eval (const VectorBase< BaseFloat > &frame_weights, const CuMatrixBase< BaseFloat > &net_out, const CuMatrixBase< BaseFloat > &target, CuMatrix< BaseFloat > *diff)
 Evaluate cross entropy using target-matrix (supports soft labels),. More...
 
void Eval (const VectorBase< BaseFloat > &frame_weights, const CuMatrixBase< BaseFloat > &net_out, const Posterior &target, CuMatrix< BaseFloat > *diff)
 Evaluate cross entropy using target-posteriors (supports soft labels),. More...
 
std::string Report ()
 Generate string with error report,. More...
 
std::string ReportPerClass ()
 Generate string with per-class error report,. More...
 
BaseFloat AvgLoss ()
 Get loss value (frame average),. More...
 
- Public Member Functions inherited from LossItf
 LossItf (LossOptions &opts)
 
virtual ~LossItf ()
 

Private Attributes

CuVector< double > frames_
 
Vector< double > correct_
 
CuVector< double > xentropy_
 
CuVector< double > entropy_
 
double frames_progress_
 
double xentropy_progress_
 
double entropy_progress_
 
std::vector< floatloss_vec_
 
double elapsed_seconds_
 
CuVector< BaseFloatframe_weights_
 
CuVector< BaseFloattarget_sum_
 
CuMatrix< BaseFloattgt_mat_
 
CuMatrix< BaseFloatframes_aux_
 
CuMatrix< BaseFloatxentropy_aux_
 
CuMatrix< BaseFloatentropy_aux_
 
CuArray< int32max_id_out_
 
CuArray< int32max_id_tgt_
 

Additional Inherited Members

- Protected Attributes inherited from LossItf
LossOptions opts_
 
Timer timer_
 

Detailed Description

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

Constructor & Destructor Documentation

◆ Xent()

Xent ( LossOptions opts)
inline

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

Referenced by MultiTaskLoss::InitFromString().

84  :
85  LossItf(opts),
86  frames_progress_(0.0),
87  xentropy_progress_(0.0),
88  entropy_progress_(0.0),
89  elapsed_seconds_(0.0)
90  { }
double xentropy_progress_
Definition: nnet-loss.h:128
double elapsed_seconds_
Definition: nnet-loss.h:131
double entropy_progress_
Definition: nnet-loss.h:129
LossItf(LossOptions &opts)
Definition: nnet-loss.h:53
double frames_progress_
Definition: nnet-loss.h:127

◆ ~Xent()

~Xent ( )
inline

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

93  { }

Member Function Documentation

◆ AvgLoss()

BaseFloat AvgLoss ( )
inlinevirtual

Get loss value (frame average),.

Implements LossItf.

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

Referenced by MultiTaskLoss::Report().

114  {
115  if (frames_.Sum() == 0) return 0.0;
116  return (xentropy_.Sum() - entropy_.Sum()) / frames_.Sum();
117  }
CuVector< double > frames_
Definition: nnet-loss.h:121
Real Sum() const
Definition: cu-vector.cc:297
CuVector< double > xentropy_
Definition: nnet-loss.h:123
CuVector< double > entropy_
Definition: nnet-loss.h:124

◆ Eval() [1/2]

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

Evaluate cross entropy using target-matrix (supports soft labels),.

Implements LossItf.

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

References CuMatrixBase< Real >::AddMat(), CuVectorBase< Real >::AddRowSumMat(), Xent::correct_, kaldi::nnet1::CountCorrectFramesWeighted(), VectorBase< Real >::Dim(), CuVectorBase< Real >::Dim(), Timer::Elapsed(), Xent::elapsed_seconds_, Xent::entropy_, Xent::entropy_aux_, Xent::entropy_progress_, CuMatrixBase< Real >::FindRowMaxId(), Xent::frame_weights_, Xent::frames_, Xent::frames_aux_, Xent::frames_progress_, KALDI_ASSERT, KALDI_ISFINITE, KALDI_LOG, LossOptions::loss_report_frames, Xent::loss_vec_, Xent::max_id_out_, Xent::max_id_tgt_, CuMatrixBase< Real >::MulRowsVec(), CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), LossItf::opts_, CuVector< Real >::Resize(), Vector< Real >::Resize(), CuVectorBase< Real >::Sum(), VectorBase< Real >::Sum(), CuMatrixBase< Real >::Sum(), Xent::target_sum_, LossItf::timer_, Xent::xentropy_, Xent::xentropy_aux_, and Xent::xentropy_progress_.

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

65  {
66  // check inputs,
67  KALDI_ASSERT(net_out.NumCols() == targets.NumCols());
68  KALDI_ASSERT(net_out.NumRows() == targets.NumRows());
69  KALDI_ASSERT(net_out.NumRows() == frame_weights.Dim());
70 
71  KALDI_ASSERT(KALDI_ISFINITE(frame_weights.Sum()));
72  KALDI_ASSERT(KALDI_ISFINITE(net_out.Sum()));
73  KALDI_ASSERT(KALDI_ISFINITE(targets.Sum()));
74 
75  // buffer initialization,
76  int32 num_classes = targets.NumCols();
77  if (frames_.Dim() == 0) {
78  frames_.Resize(num_classes);
79  xentropy_.Resize(num_classes);
80  entropy_.Resize(num_classes);
81  correct_.Resize(num_classes);
82  }
83 
84  // get frame_weights to GPU,
85  frame_weights_ = frame_weights;
86 
87  // There may be frames for which the sum of targets is zero.
88  // This happens in multi-lingual training when the frame
89  // has target class in the softmax of another language.
90  // We 'switch-off' such frames by masking the 'frame_weights_',
91  target_sum_.Resize(targets.NumRows());
92  target_sum_.AddColSumMat(1.0, targets, 0.0);
93  frame_weights_.MulElements(target_sum_);
94 
95  // compute derivative wrt. activations of last layer of neurons,
96  *diff = net_out;
97  diff->AddMat(-1.0, targets);
98  diff->MulRowsVec(frame_weights_); // weighting,
99 
100  // count frames per class,
101  frames_aux_ = targets;
102  frames_aux_.MulRowsVec(frame_weights_);
103  frames_.AddRowSumMat(1.0, CuMatrix<double>(frames_aux_));
104 
105  // evaluate the frame-level classification,
106  net_out.FindRowMaxId(&max_id_out_); // find max in nn-output
107  targets.FindRowMaxId(&max_id_tgt_); // find max in targets
110 
111  // calculate cross_entropy (in GPU),
112  xentropy_aux_ = net_out; // y
113  xentropy_aux_.Add(1e-20); // avoid log(0)
114  xentropy_aux_.ApplyLog(); // log(y)
115  xentropy_aux_.MulElements(targets); // t*log(y)
116  xentropy_aux_.MulRowsVec(frame_weights_); // w*t*log(y)
117  xentropy_.AddRowSumMat(-1.0, CuMatrix<double>(xentropy_aux_));
118 
119  // caluculate entropy (in GPU),
120  entropy_aux_ = targets; // t
121  entropy_aux_.Add(1e-20); // avoid log(0)
122  entropy_aux_.ApplyLog(); // log(t)
123  entropy_aux_.MulElements(targets); // t*log(t)
124  entropy_aux_.MulRowsVec(frame_weights_); // w*t*log(t)
125  entropy_.AddRowSumMat(-1.0, CuMatrix<double>(entropy_aux_));
126 
127  // progressive loss reporting
128  if (opts_.loss_report_frames > 0) {
132 
135 
137  // loss value,
138  double progress_value =
140 
141  // time-related info (fps is weighted),
142  double time_now = timer_.Elapsed();
143  double fps = frames_progress_ / (time_now - elapsed_seconds_);
144  double elapsed_hours = time_now / 3600;
145  elapsed_seconds_ = time_now; // store,
146 
147  // print,
148  KALDI_LOG << "ProgressLoss[last "
149  << static_cast<int>(frames_progress_/100/3600) << "h of "
150  << static_cast<int>(frames_.Sum()/100/3600) << "h]: "
151  << progress_value << " (Xent)"
152  << ", fps=" << fps
153  << std::setprecision(3)
154  << ", elapsed " << elapsed_hours << "h";
155  // store,
156  loss_vec_.push_back(progress_value);
157  // reset,
158  frames_progress_ = 0;
159  xentropy_progress_ = 0.0;
160  entropy_progress_ = 0.0;
161  }
162  }
163 }
double xentropy_progress_
Definition: nnet-loss.h:128
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
Real Sum() const
Definition: cu-vector.cc:297
#define KALDI_ISFINITE(x)
Definition: kaldi-math.h:74
std::vector< float > loss_vec_
Definition: nnet-loss.h:130
CuVector< double > xentropy_
Definition: nnet-loss.h:123
kaldi::int32 int32
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
CuMatrix< BaseFloat > entropy_aux_
Definition: nnet-loss.h:141
void CountCorrectFramesWeighted(const CuArray< T > &hyp, const CuArray< T > &ref, const CuVectorBase< BaseFloat > &weights, Vector< double > *correct)
Helper function of Xent::Eval, calculates number of matching elemente in &#39;hyp&#39;, &#39;ref&#39; weighted by &#39;we...
Definition: nnet-loss.cc:41
CuArray< int32 > max_id_out_
Definition: nnet-loss.h:144
Vector< double > correct_
Definition: nnet-loss.h:122
double entropy_progress_
Definition: nnet-loss.h:129
int32 loss_report_frames
Report loss value every &#39;report_interval&#39; frames,.
Definition: nnet-loss.h:39
CuVector< double > entropy_
Definition: nnet-loss.h:124
CuVector< BaseFloat > frame_weights_
Definition: nnet-loss.h:134
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:993
CuMatrix< BaseFloat > xentropy_aux_
Definition: nnet-loss.h:140
CuArray< int32 > max_id_tgt_
Definition: nnet-loss.h:145
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
double frames_progress_
Definition: nnet-loss.h:127
#define KALDI_LOG
Definition: kaldi-error.h:153
double Elapsed() const
Returns time in seconds.
Definition: timer.h:74
LossOptions opts_
Definition: nnet-loss.h:77
void AddRowSumMat(Real alpha, const CuMatrixBase< Real > &mat, Real beta=1.0)
Sum the rows of the matrix, add to vector.
Definition: cu-vector.cc:1277
MatrixIndexT Dim() const
Dimensions.
Definition: cu-vector.h:69

◆ Eval() [2/2]

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

Evaluate cross entropy using target-posteriors (supports soft labels),.

Implements LossItf.

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

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

169  {
170  int32 num_frames = net_out.NumRows(),
171  num_pdf = net_out.NumCols();
172  KALDI_ASSERT(num_frames == post.size());
173 
174  // convert posterior to matrix,
175  PosteriorToMatrix(post, num_pdf, &tgt_mat_);
176 
177  // call the other eval function,
178  Eval(frame_weights, net_out, tgt_mat_, diff);
179 }
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
CuMatrix< BaseFloat > tgt_mat_
Definition: nnet-loss.h:138
void Eval(const VectorBase< BaseFloat > &frame_weights, const CuMatrixBase< BaseFloat > &net_out, const CuMatrixBase< BaseFloat > &target, CuMatrix< BaseFloat > *diff)
Evaluate cross entropy using target-matrix (supports soft labels),.
Definition: nnet-loss.cc:62
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Report()

std::string Report ( )
virtual

Generate string with error report,.

Implements LossItf.

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

References Xent::correct_, Xent::entropy_, Xent::frames_, Xent::loss_vec_, CuVectorBase< Real >::Sum(), VectorBase< Real >::Sum(), and Xent::xentropy_.

Referenced by main().

182  {
183  double loss_value =
184  (xentropy_.Sum() - entropy_.Sum()) / frames_.Sum();
185  std::ostringstream oss;
186  oss << "AvgLoss: " << loss_value << " (Xent), "
187  << "[AvgXent: " << xentropy_.Sum() / frames_.Sum()
188  << ", AvgTargetEnt: " << entropy_.Sum() / frames_.Sum()
189  << "]" << std::endl;
190 
191  oss << "progress: [";
192  std::copy(loss_vec_.begin(), loss_vec_.end(),
193  std::ostream_iterator<float>(oss, " "));
194  oss << "]" << std::endl;
195 
196  double frame_accuracy = 100.0 * correct_.Sum() / frames_.Sum();
197  oss << "FRAME_ACCURACY >> " << frame_accuracy << "% <<" << std::endl;
198 
199  return oss.str();
200 }
CuVector< double > frames_
Definition: nnet-loss.h:121
Real Sum() const
Definition: cu-vector.cc:297
std::vector< float > loss_vec_
Definition: nnet-loss.h:130
CuVector< double > xentropy_
Definition: nnet-loss.h:123
Vector< double > correct_
Definition: nnet-loss.h:122
CuVector< double > entropy_
Definition: nnet-loss.h:124
Real Sum() const
Returns sum of the elements.

◆ ReportPerClass()

std::string ReportPerClass ( )

Generate string with per-class error report,.

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

References CuVectorBase< Real >::Add(), CuVectorBase< Real >::AddVec(), CuVectorBase< Real >::ApplyPow(), Xent::correct_, Xent::entropy_, Xent::frames_, CuVectorBase< Real >::MulElements(), CuVectorBase< Real >::Scale(), and Xent::xentropy_.

Referenced by main().

203  {
204  std::ostringstream oss;
205  oss << "PER-CLASS PERFORMANCE:" << std::endl;
206  oss << "@@@ Frames per-class:" << frames_;
207  // get inverted counts,
208  CuVector<double> inv_frames(frames_);
209  inv_frames.Add(0.5); // avoid 0-frames,
210  inv_frames.ApplyPow(-1.0);
211  // loss, kl = xentropy-entropy,
212  CuVector<double> loss(xentropy_);
213  loss.AddVec(-1.0, entropy_);
214  loss.MulElements(inv_frames);
215  oss << "@@@ Loss per-class:" << loss;
216  // frame accuracy (assuming targets are binary),
217  CuVector<double> frm_accu(correct_);
218  frm_accu.MulElements(inv_frames);
219  frm_accu.Scale(100.0);
220  oss << "@@@ Frame-accuracy per-class:" << frm_accu;
221  //
222  return oss.str();
223 }
CuVector< double > frames_
Definition: nnet-loss.h:121
CuVector< double > xentropy_
Definition: nnet-loss.h:123
Vector< double > correct_
Definition: nnet-loss.h:122
CuVector< double > entropy_
Definition: nnet-loss.h:124

Member Data Documentation

◆ correct_

Vector<double> correct_
private

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

Referenced by Xent::Eval(), Xent::Report(), and Xent::ReportPerClass().

◆ elapsed_seconds_

double elapsed_seconds_
private

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

Referenced by Xent::Eval().

◆ entropy_

CuVector<double> entropy_
private

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

Referenced by Xent::Eval(), Xent::Report(), and Xent::ReportPerClass().

◆ entropy_aux_

CuMatrix<BaseFloat> entropy_aux_
private

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

Referenced by Xent::Eval().

◆ entropy_progress_

double entropy_progress_
private

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

Referenced by Xent::Eval().

◆ frame_weights_

CuVector<BaseFloat> frame_weights_
private

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

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

◆ frames_

CuVector<double> frames_
private

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

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

◆ frames_aux_

CuMatrix<BaseFloat> frames_aux_
private

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

Referenced by Xent::Eval().

◆ frames_progress_

double frames_progress_
private

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

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

◆ loss_vec_

◆ max_id_out_

CuArray<int32> max_id_out_
private

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

Referenced by Xent::Eval().

◆ max_id_tgt_

CuArray<int32> max_id_tgt_
private

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

Referenced by Xent::Eval().

◆ target_sum_

CuVector<BaseFloat> target_sum_
private

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

Referenced by Xent::Eval().

◆ tgt_mat_

CuMatrix<BaseFloat> tgt_mat_
private

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

Referenced by Xent::Eval(), Mse::Eval(), and MultiTaskLoss::Eval().

◆ xentropy_

CuVector<double> xentropy_
private

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

Referenced by Xent::Eval(), Xent::Report(), and Xent::ReportPerClass().

◆ xentropy_aux_

CuMatrix<BaseFloat> xentropy_aux_
private

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

Referenced by Xent::Eval().

◆ xentropy_progress_

double xentropy_progress_
private

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

Referenced by Xent::Eval().


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