nnet-various.h
Go to the documentation of this file.
1 // nnet/nnet-various.h
2 
3 // Copyright 2012-2016 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 
21 #ifndef KALDI_NNET_NNET_VARIOUS_H_
22 #define KALDI_NNET_NNET_VARIOUS_H_
23 
24 #include <string>
25 #include <vector>
26 #include <algorithm>
27 #include <sstream>
28 
29 #include "nnet/nnet-component.h"
30 #include "nnet/nnet-utils.h"
31 #include "cudamatrix/cu-math.h"
32 #include "util/text-utils.h"
33 
34 namespace kaldi {
35 namespace nnet1 {
36 
42 class Splice: public Component {
43  public:
44  Splice(int32 dim_in, int32 dim_out):
45  Component(dim_in, dim_out)
46  { }
47 
49  { }
50 
51  Component* Copy() const { return new Splice(*this); }
52  ComponentType GetType() const { return kSplice; }
53 
54  void InitData(std::istream &is) {
55  // define options,
56  std::vector<std::vector<int32> > build_vector;
57  // parse config,
58  std::string token;
59  while (is >> std::ws, !is.eof()) {
60  ReadToken(is, false, &token);
61  if (token == "<ReadVector>") {
62  frame_offsets_.Read(is, false);
63  } else if (token == "<BuildVector>") {
64  // Parse the list of 'matlab-like' indices:
65  // <BuildVector> 1:1:1000 1 2 3 1:10 </BuildVector>
66  while (is >> std::ws, !is.eof()) {
67  std::string colon_sep_list_or_end;
68  ReadToken(is, false, &colon_sep_list_or_end);
69  if (colon_sep_list_or_end == "</BuildVector>") break;
70  std::vector<int32> v;
71  SplitStringToIntegers(colon_sep_list_or_end, ":", false, &v);
72  build_vector.push_back(v);
73  }
74  } else {
75  KALDI_ERR << "Unknown token " << token << ", a typo in config?"
76  << " (ReadVector|BuildVector)";
77  }
78  }
79 
80  if (build_vector.size() > 0) {
81  // build the vector, using <BuildVector> ... </BuildVector> inputs,
82  BuildIntegerVector(build_vector, &frame_offsets_);
83  }
84 
85  // check dim
87  }
88 
89  void ReadData(std::istream &is, bool binary) {
90  frame_offsets_.Read(is, binary);
92  }
93 
94  void WriteData(std::ostream &os, bool binary) const {
95  frame_offsets_.Write(os, binary);
96  }
97 
98  std::string Info() const {
99  std::ostringstream ostr;
100  ostr << "\n frame_offsets " << frame_offsets_;
101  std::string str = ostr.str();
102  str.erase(str.end()-1);
103  return str;
104  }
105 
108  cu::Splice(in, frame_offsets_, out);
109  }
110 
112  const CuMatrixBase<BaseFloat> &out,
113  const CuMatrixBase<BaseFloat> &out_diff,
114  CuMatrixBase<BaseFloat> *in_diff) {
115  // WARNING!!! WARNING!!! WARNING!!!
116  // THIS BACKPROPAGATION CAN BE USED ONLY WITH 'PER-UTTERANCE' TRAINING!
117  // IN MINI-BATCH TRAINING, THIS <Splice> COMPONENT HAS TO BE PART OF THE
118  // 'feature_transform' SO WE DON'T BACKPROPAGATE THROUGH IT...
119 
120  // dims,
121  int32 input_dim = in.NumCols(),
122  num_frames = out_diff.NumRows();
123  // Copy offsets to 'host',
124  std::vector<int32> offsets(frame_offsets_.Dim());
125  frame_offsets_.CopyToVec(&offsets);
126  // loop over the offsets,
127  for (int32 i = 0; i < offsets.size(); i++) {
128  int32 o_i = offsets.at(i);
129  int32 n_rows = num_frames - abs(o_i),
130  src_row = std::max(-o_i, 0),
131  tgt_row = std::max(o_i, 0);
132  const CuSubMatrix<BaseFloat> src = out_diff.Range(src_row, n_rows, i*input_dim, input_dim);
133  CuSubMatrix<BaseFloat> tgt = in_diff->RowRange(tgt_row, n_rows);
134  tgt.AddMat(1.0, src, kNoTrans);
135  }
136  }
137 
138  protected:
140 };
141 
142 
146 class CopyComponent: public Component {
147  public:
148  CopyComponent(int32 dim_in, int32 dim_out):
149  Component(dim_in, dim_out)
150  { }
151 
153  { }
154 
155  Component* Copy() const { return new CopyComponent(*this); }
156  ComponentType GetType() const { return kCopy; }
157 
158  void InitData(std::istream &is) {
159  // define options,
160  std::vector<std::vector<int32> > build_vector;
161  // parse config,
162  std::string token;
163  while (is >> std::ws, !is.eof()) {
164  ReadToken(is, false, &token);
165  if (token == "<ReadVector>") {
166  copy_from_indices_.Read(is, false);
167  } else if (token == "<BuildVector>") {
168  // <BuildVector> 1:1:1000 1:1:1000 1 2 3 1:10 </BuildVector>
169  // 'matlab-line' indexing, read the colon-separated-lists:
170  while (is >> std::ws, !is.eof()) {
171  std::string colon_sep_list_or_end;
172  ReadToken(is, false, &colon_sep_list_or_end);
173  if (colon_sep_list_or_end == "</BuildVector>") break;
174  std::vector<int32> v;
175  SplitStringToIntegers(colon_sep_list_or_end, ":", false, &v);
176  build_vector.push_back(v);
177  }
178  } else {
179  KALDI_ERR << "Unknown token " << token << ", a typo in config?"
180  << " (ReadVector|BuildVector)";
181  }
182  }
183 
184  if (build_vector.size() > 0) {
185  // build the vector, using <BuildVector> ... </BuildVector> inputs,
186  BuildIntegerVector(build_vector, &copy_from_indices_);
187  }
188 
189  // decrease by 1,
190  copy_from_indices_.Add(-1);
191 
192  // check range,
193  KALDI_ASSERT(copy_from_indices_.Min() >= 0);
194  KALDI_ASSERT(copy_from_indices_.Max() < InputDim());
195  // check dim,
196  KALDI_ASSERT(copy_from_indices_.Dim() == OutputDim());
197  }
198 
199  void ReadData(std::istream &is, bool binary) {
200  copy_from_indices_.Read(is, binary);
201  KALDI_ASSERT(copy_from_indices_.Dim() == OutputDim());
202  copy_from_indices_.Add(-1); // -1 from each element,
203  }
204 
205  void WriteData(std::ostream &os, bool binary) const {
206  CuArray<int32> tmp(copy_from_indices_);
207  tmp.Add(1); // +1 to each element,
208  tmp.Write(os, binary);
209  }
210 
211  std::string Info() const {
212  return std::string("\n min ") + ToString(copy_from_indices_.Min()) +
213  ", max " + ToString(copy_from_indices_.Max());
214  }
215 
218  cu::Copy(in, copy_from_indices_,out);
219  }
220 
222  const CuMatrixBase<BaseFloat> &out,
223  const CuMatrixBase<BaseFloat> &out_diff,
224  CuMatrixBase<BaseFloat> *in_diff) {
225  static bool warning_displayed = false;
226  if (!warning_displayed) {
228  << __func__ << "() Not implemented!";
229 
230  warning_displayed = true;
231  }
232  in_diff->SetZero();
233  }
234 
235  protected:
237 };
238 
239 
240 
245  public:
246  LengthNormComponent(int32 dim_in, int32 dim_out):
247  Component(dim_in, dim_out)
248  { }
249 
251  { }
252 
253  Component* Copy() const { return new LengthNormComponent(*this); }
255 
258  // resize vector when needed,
259  if (row_scales_.Dim() != in.NumRows()) {
260  row_scales_.Resize(in.NumRows());
261  }
262  // get the normalization scalars,
263  l2_aux_ = in;
264  l2_aux_.MulElements(l2_aux_); // x^2,
265  row_scales_.AddColSumMat(1.0, l2_aux_, 0.0); // sum_of_cols(x^2),
266  row_scales_.ApplyPow(0.5); // L2norm = sqrt(sum_of_cols(x^2)),
267  row_scales_.InvertElements(); // 1/L2norm,
268  // compute the output,
269  out->CopyFromMat(in);
270  out->MulRowsVec(row_scales_); // re-normalize,
271  }
272 
274  const CuMatrixBase<BaseFloat> &out,
275  const CuMatrixBase<BaseFloat> &out_diff,
276  CuMatrixBase<BaseFloat> *in_diff) {
277  in_diff->CopyFromMat(out_diff);
278  in_diff->MulRowsVec(row_scales_); // diff_by_x(s * x) = s,
279  }
280 
281  private:
284 };
285 
286 
291 class AddShift : public UpdatableComponent {
292  public:
293  AddShift(int32 dim_in, int32 dim_out):
294  UpdatableComponent(dim_in, dim_out),
295  shift_data_(dim_in)
296  { }
297 
299  { }
300 
301  Component* Copy() const { return new AddShift(*this); }
302  ComponentType GetType() const { return kAddShift; }
303 
304  void InitData(std::istream &is) {
305  // define options
306  float init_param = 0.0;
307  // parse config
308  std::string token;
309  while (is >> std::ws, !is.eof()) {
310  ReadToken(is, false, &token);
311  if (token == "<InitParam>") ReadBasicType(is, false, &init_param);
312  else if (token == "<LearnRateCoef>") ReadBasicType(is, false, &learn_rate_coef_);
313  else KALDI_ERR << "Unknown token " << token << ", a typo in config?"
314  << " (InitParam)";
315  }
316  // initialize
317  shift_data_.Resize(InputDim(), kSetZero); // set to zero
318  shift_data_.Set(init_param);
319  }
320 
321  void ReadData(std::istream &is, bool binary) {
322  // optional learning-rate coef,
323  if ('<' == Peek(is, binary)) {
324  ExpectToken(is, binary, "<LearnRateCoef>");
325  ReadBasicType(is, binary, &learn_rate_coef_);
326  }
327  // read the shift data
328  shift_data_.Read(is, binary);
329  }
330 
331  void WriteData(std::ostream &os, bool binary) const {
332  WriteToken(os, binary, "<LearnRateCoef>");
333  WriteBasicType(os, binary, learn_rate_coef_);
334  shift_data_.Write(os, binary);
335  }
336 
337  int32 NumParams() const { return shift_data_.Dim(); }
338 
339  void GetGradient(VectorBase<BaseFloat>* gradient) const {
340  KALDI_ASSERT(gradient->Dim() == NumParams());
341  shift_data_grad_.CopyToVec(gradient);
342  }
343 
344  void GetParams(VectorBase<BaseFloat>* params) const {
345  KALDI_ASSERT(params->Dim() == NumParams());
346  shift_data_.CopyToVec(params);
347  }
348 
349  void SetParams(const VectorBase<BaseFloat>& params) {
350  KALDI_ASSERT(params.Dim() == NumParams());
351  shift_data_.CopyFromVec(params);
352  }
353 
354  std::string Info() const {
355  return std::string("\n shift_data") +
356  MomentStatistics(shift_data_) +
357  ", lr-coef " + ToString(learn_rate_coef_);
358  }
359 
360  std::string InfoGradient() const {
361  return std::string("\n shift_data_grad") +
362  MomentStatistics(shift_data_grad_) +
363  ", lr-coef " + ToString(learn_rate_coef_);
364  }
365 
368  // copy, add the shift,
369  out->CopyFromMat(in);
370  out->AddVecToRows(1.0, shift_data_, 1.0);
371  }
372 
374  const CuMatrixBase<BaseFloat> &out,
375  const CuMatrixBase<BaseFloat> &out_diff,
376  CuMatrixBase<BaseFloat> *in_diff) {
377  // the derivative of additive constant is zero...
378  in_diff->CopyFromMat(out_diff);
379  }
380 
381  void Update(const CuMatrixBase<BaseFloat> &input,
382  const CuMatrixBase<BaseFloat> &diff) {
383  // we use following hyperparameters from the option class,
384  const BaseFloat lr = opts_.learn_rate;
385  // gradient,
386  shift_data_grad_.Resize(InputDim(), kSetZero); // reset to zero,
387  shift_data_grad_.AddRowSumMat(1.0, diff, 0.0);
388  // update,
389  shift_data_.AddVec(-lr * learn_rate_coef_, shift_data_grad_);
390  }
391 
392  void SetLearnRateCoef(BaseFloat c) { learn_rate_coef_ = c; }
393 
394  protected:
397 };
398 
399 
404 class Rescale : public UpdatableComponent {
405  public:
406  Rescale(int32 dim_in, int32 dim_out):
407  UpdatableComponent(dim_in, dim_out),
408  scale_data_(dim_in)
409  { }
410 
412  { }
413 
414  Component* Copy() const { return new Rescale(*this); }
415  ComponentType GetType() const { return kRescale; }
416 
417  void InitData(std::istream &is) {
418  // define options
419  float init_param = 0.0;
420  // parse config
421  std::string token;
422  while (is >> std::ws, !is.eof()) {
423  ReadToken(is, false, &token);
424  if (token == "<InitParam>") ReadBasicType(is, false, &init_param);
425  else if (token == "<LearnRateCoef>") ReadBasicType(is, false, &learn_rate_coef_);
426  else KALDI_ERR << "Unknown token " << token << ", a typo in config?"
427  << " (InitParam)";
428  }
429  // initialize
430  scale_data_.Resize(InputDim(), kSetZero);
431  scale_data_.Set(init_param);
432  }
433 
434  void ReadData(std::istream &is, bool binary) {
435  // optional learning-rate coef,
436  if ('<' == Peek(is, binary)) {
437  ExpectToken(is, binary, "<LearnRateCoef>");
438  ReadBasicType(is, binary, &learn_rate_coef_);
439  }
440  // read the shift data
441  scale_data_.Read(is, binary);
442  }
443 
444  void WriteData(std::ostream &os, bool binary) const {
445  WriteToken(os, binary, "<LearnRateCoef>");
446  WriteBasicType(os, binary, learn_rate_coef_);
447  scale_data_.Write(os, binary);
448  }
449 
450  int32 NumParams() const { return scale_data_.Dim(); }
451 
452  void GetGradient(VectorBase<BaseFloat>* gradient) const {
453  KALDI_ASSERT(gradient->Dim() == NumParams());
454  scale_data_grad_.CopyToVec(gradient);
455  }
456 
457  void GetParams(VectorBase<BaseFloat>* params) const {
458  KALDI_ASSERT(params->Dim() == NumParams());
459  scale_data_.CopyToVec(params);
460  }
461 
462  void SetParams(const VectorBase<BaseFloat>& params) {
463  KALDI_ASSERT(params.Dim() == NumParams());
464  scale_data_.CopyFromVec(params);
465  }
466 
467  std::string Info() const {
468  return std::string("\n scale_data") +
469  MomentStatistics(scale_data_) +
470  ", lr-coef " + ToString(learn_rate_coef_);
471  }
472 
473  std::string InfoGradient() const {
474  return std::string("\n scale_data_grad") +
475  MomentStatistics(scale_data_grad_) +
476  ", lr-coef " + ToString(learn_rate_coef_);
477  }
478 
481  // copy, rescale the data,
482  out->CopyFromMat(in);
483  out->MulColsVec(scale_data_);
484  }
485 
487  const CuMatrixBase<BaseFloat> &out,
488  const CuMatrixBase<BaseFloat> &out_diff,
489  CuMatrixBase<BaseFloat> *in_diff) {
490  // derivatives are scaled with the scale_data_,
491  in_diff->CopyFromMat(out_diff);
492  in_diff->MulColsVec(scale_data_);
493  }
494 
495  void Update(const CuMatrixBase<BaseFloat> &input,
496  const CuMatrixBase<BaseFloat> &diff) {
497  // we use following hyperparameters from the option class,
498  const BaseFloat lr = opts_.learn_rate;
499  // gradient,
500  scale_data_grad_.Resize(InputDim(), kSetZero); // reset,
501  CuMatrix<BaseFloat> gradient_aux(diff);
502  gradient_aux.MulElements(input);
503  scale_data_grad_.AddRowSumMat(1.0, gradient_aux, 0.0);
504  // update,
505  scale_data_.AddVec(-lr * learn_rate_coef_, scale_data_grad_);
506  }
507 
508  void SetLearnRateCoef(BaseFloat c) { learn_rate_coef_ = c; }
509 
510  protected:
513 };
514 
515 } // namespace nnet1
516 } // namespace kaldi
517 
518 #endif // KALDI_NNET_NNET_VARIOUS_H_
std::string ToString(const T &t)
Convert basic type to a string (please don&#39;t overuse),.
Definition: nnet-utils.h:52
ComponentType GetType() const
Get Type Identification of the component,.
Definition: nnet-various.h:254
void CopyFromMat(const MatrixBase< OtherReal > &src, MatrixTransposeType trans=kNoTrans)
Definition: cu-matrix.cc:344
std::string Info() const
Print some additional info (after <ComponentName> and the dims),.
Definition: nnet-various.h:467
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
CuVector< BaseFloat > shift_data_
Definition: nnet-various.h:395
CuMatrix< BaseFloat > l2_aux_
auxiliary matrix for L2 norm computation,
Definition: nnet-various.h:282
void ReadData(std::istream &is, bool binary)
Reads the component content.
Definition: nnet-various.h:199
void SetLearnRateCoef(BaseFloat c)
Set the learn-rate coefficient,.
Definition: nnet-various.h:508
ComponentType GetType() const
Get Type Identification of the component,.
Definition: nnet-various.h:52
std::string MomentStatistics(const VectorBase< Real > &vec)
Get a string with statistics of the data in a vector, so we can print them easily.
Definition: nnet-utils.h:63
void CopyToVec(std::vector< T > *dst) const
This function resizes *dst if needed.
Definition: cu-array-inl.h:177
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
void BackpropagateFnc(const CuMatrixBase< BaseFloat > &in, const CuMatrixBase< BaseFloat > &out, const CuMatrixBase< BaseFloat > &out_diff, CuMatrixBase< BaseFloat > *in_diff)
Backward pass transformation (to be implemented by descending class...)
Definition: nnet-various.h:111
void ReadData(std::istream &is, bool binary)
Reads the component content.
Definition: nnet-various.h:434
AddShift(int32 dim_in, int32 dim_out)
Definition: nnet-various.h:293
bool SplitStringToIntegers(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< I > *out)
Split a string (e.g.
Definition: text-utils.h:68
void WriteData(std::ostream &os, bool binary) const
Writes the component content.
Definition: nnet-various.h:205
void ReadData(std::istream &is, bool binary)
Reads the component content.
Definition: nnet-various.h:321
void SetLearnRateCoef(BaseFloat c)
Set the learn-rate coefficient,.
Definition: nnet-various.h:392
Component * Copy() const
Copy component (deep copy),.
Definition: nnet-various.h:51
void BackpropagateFnc(const CuMatrixBase< BaseFloat > &in, const CuMatrixBase< BaseFloat > &out, const CuMatrixBase< BaseFloat > &out_diff, CuMatrixBase< BaseFloat > *in_diff)
Backward pass transformation (to be implemented by descending class...)
Definition: nnet-various.h:221
Class UpdatableComponent is a Component which has trainable parameters, it contains SGD training hype...
CuSubMatrix< Real > Range(const MatrixIndexT row_offset, const MatrixIndexT num_rows, const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Definition: cu-matrix.h:653
Rearrange the matrix columns according to the indices in copy_from_indices_.
Definition: nnet-various.h:146
void GetParams(VectorBase< BaseFloat > *params) const
Get the trainable parameters reshaped as a vector,.
Definition: nnet-various.h:344
kaldi::int32 int32
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
Definition: io-funcs.cc:154
void AddMat(Real alpha, const CuMatrixBase< Real > &A, MatrixTransposeType trans=kNoTrans)
*this += alpha * A
Definition: cu-matrix.cc:954
CuVector< BaseFloat > shift_data_grad_
Definition: nnet-various.h:396
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
void PropagateFnc(const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out)
Abstract interface for propagation/backpropagation.
Definition: nnet-various.h:366
int32 NumParams() const
Number of trainable parameters,.
Definition: nnet-various.h:337
void PropagateFnc(const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out)
Abstract interface for propagation/backpropagation.
Definition: nnet-various.h:216
int Peek(std::istream &is, bool binary)
Peek consumes whitespace (if binary == false) and then returns the peek() value of the stream...
Definition: io-funcs.cc:145
ComponentType
Component type identification mechanism,.
void PropagateFnc(const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out)
Abstract interface for propagation/backpropagation.
Definition: nnet-various.h:479
Rescale the data column-wise by a vector (can be used for global variance normalization) ...
Definition: nnet-various.h:404
void Update(const CuMatrixBase< BaseFloat > &input, const CuMatrixBase< BaseFloat > &diff)
Compute gradient and update parameters,.
Definition: nnet-various.h:495
void BackpropagateFnc(const CuMatrixBase< BaseFloat > &in, const CuMatrixBase< BaseFloat > &out, const CuMatrixBase< BaseFloat > &out_diff, CuMatrixBase< BaseFloat > *in_diff)
Backward pass transformation (to be implemented by descending class...)
Definition: nnet-various.h:373
void Add(const T &value)
Add a constant value.
Definition: cu-array-inl.h:254
Rescale the matrix-rows to have unit length (L2-norm).
Definition: nnet-various.h:244
CopyComponent(int32 dim_in, int32 dim_out)
Definition: nnet-various.h:148
ComponentType GetType() const
Get Type Identification of the component,.
Definition: nnet-various.h:415
CuVector< BaseFloat > row_scales_
normalization scale of each row,
Definition: nnet-various.h:283
static const char * TypeToMarker(ComponentType t)
Converts component type to marker,.
void SetParams(const VectorBase< BaseFloat > &params)
Set the trainable parameters from, reshaped as a vector,.
Definition: nnet-various.h:462
Adds shift to all the lines of the matrix (can be used for global mean normalization) ...
Definition: nnet-various.h:291
ComponentType GetType() const
Get Type Identification of the component,.
Definition: nnet-various.h:156
void WriteData(std::ostream &os, bool binary) const
Writes the component content.
Definition: nnet-various.h:444
void AddVecToRows(Real alpha, const CuVectorBase< Real > &row, Real beta=1.0)
(for each row r of *this), r = alpha * row + beta * r
Definition: cu-matrix.cc:1261
CuVector< BaseFloat > scale_data_grad_
Definition: nnet-various.h:512
void SetZero()
Math operations, some calling kernels.
Definition: cu-matrix.cc:509
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
Definition: io-funcs.cc:191
void InitData(std::istream &is)
Initialize the content of the component by the &#39;line&#39; from the prototype,.
Definition: nnet-various.h:304
void MulElements(const CuMatrixBase< Real > &A)
Multiply two matrices elementwise: C = C .* A.
Definition: cu-matrix.cc:667
CuArray< int32 > frame_offsets_
Definition: nnet-various.h:139
void Read(std::istream &is, bool binary)
I/O.
Definition: cu-array-inl.h:300
Splices the time context of the input features in N, out k*N, FrameOffset o_1,o_2,...,o_k FrameOffset example 11frames: -5 -4 -3 -2 -1 0 1 2 3 4 5.
Definition: nnet-various.h:42
int32 InputDim() const
Get the dimension of the input,.
void BackpropagateFnc(const CuMatrixBase< BaseFloat > &in, const CuMatrixBase< BaseFloat > &out, const CuMatrixBase< BaseFloat > &out_diff, CuMatrixBase< BaseFloat > *in_diff)
Backward pass transformation (to be implemented by descending class...)
Definition: nnet-various.h:486
void WriteData(std::ostream &os, bool binary) const
Writes the component content.
Definition: nnet-various.h:94
void InitData(std::istream &is)
Virtual interface for initialization and I/O,.
Definition: nnet-various.h:158
void BuildIntegerVector(const std::vector< std::vector< int32 > > &in, std::vector< int32 > *out)
Build &#39;integer vector&#39; out of vector of &#39;matlab-like&#39; representation: &#39;b, b:e, b:s:e&#39;.
Definition: nnet-utils.h:239
#define KALDI_ERR
Definition: kaldi-error.h:147
Splice(int32 dim_in, int32 dim_out)
Definition: nnet-various.h:44
#define KALDI_WARN
Definition: kaldi-error.h:150
This class is used for a piece of a CuMatrix.
Definition: matrix-common.h:70
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
Definition: io-funcs.cc:134
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
CuSubMatrix< Real > RowRange(const MatrixIndexT row_offset, const MatrixIndexT num_rows) const
Definition: cu-matrix.h:660
void WriteData(std::ostream &os, bool binary) const
Writes the component content.
Definition: nnet-various.h:331
void InitData(std::istream &is)
Initialize the content of the component by the &#39;line&#39; from the prototype,.
Definition: nnet-various.h:417
void ReadData(std::istream &is, bool binary)
Reads the component content.
Definition: nnet-various.h:89
void Write(std::ostream &is, bool binary) const
Definition: cu-array-inl.h:307
void MulColsVec(const CuVectorBase< Real > &scale)
scale i&#39;th column by scale[i]
Definition: cu-matrix.cc:765
void Splice(const CuMatrixBase< Real > &src, const CuArray< int32 > &frame_offsets, CuMatrixBase< Real > *tgt)
Splice concatenates frames of src as specified in frame_offsets into tgt.
Definition: cu-math.cc:132
void GetParams(VectorBase< BaseFloat > *params) const
Get the trainable parameters reshaped as a vector,.
Definition: nnet-various.h:457
Matrix for CUDA computing.
Definition: matrix-common.h:69
LengthNormComponent(int32 dim_in, int32 dim_out)
Definition: nnet-various.h:246
CuArray< int32 > copy_from_indices_
Definition: nnet-various.h:236
MatrixIndexT NumCols() const
Definition: cu-matrix.h:216
void BackpropagateFnc(const CuMatrixBase< BaseFloat > &in, const CuMatrixBase< BaseFloat > &out, const CuMatrixBase< BaseFloat > &out_diff, CuMatrixBase< BaseFloat > *in_diff)
Backward pass transformation (to be implemented by descending class...)
Definition: nnet-various.h:273
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::string Info() const
Print some additional info (after <ComponentName> and the dims),.
Definition: nnet-various.h:98
void SetParams(const VectorBase< BaseFloat > &params)
Set the trainable parameters from, reshaped as a vector,.
Definition: nnet-various.h:349
void Update(const CuMatrixBase< BaseFloat > &input, const CuMatrixBase< BaseFloat > &diff)
Compute gradient and update parameters,.
Definition: nnet-various.h:381
void PropagateFnc(const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out)
Abstract interface for propagation/backpropagation.
Definition: nnet-various.h:106
Rescale(int32 dim_in, int32 dim_out)
Definition: nnet-various.h:406
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34
std::string InfoGradient() const
Print some additional info about gradient (after <...> and dims),.
Definition: nnet-various.h:360
Abstract class, building block of the network.
ComponentType GetType() const
Get Type Identification of the component,.
Definition: nnet-various.h:302
Component * Copy() const
Copy component (deep copy),.
Definition: nnet-various.h:253
void PropagateFnc(const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out)
Abstract interface for propagation/backpropagation.
Definition: nnet-various.h:256
void GetGradient(VectorBase< BaseFloat > *gradient) const
Get gradient reshaped as a vector,.
Definition: nnet-various.h:452
int32 OutputDim() const
Get the dimension of the output,.
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:215
Provides a vector abstraction class.
Definition: kaldi-vector.h:41
MatrixIndexT Dim() const
Return the vector dimension.
Definition: cu-array.h:49
Component * Copy() const
Copy component (deep copy),.
Definition: nnet-various.h:155
std::string Info() const
Print some additional info (after <ComponentName> and the dims),.
Definition: nnet-various.h:211
Component * Copy() const
Copy component (deep copy),.
Definition: nnet-various.h:414
void MulRowsVec(const CuVectorBase< Real > &scale)
scale i&#39;th row by scale[i]
Definition: cu-matrix.cc:792
std::string InfoGradient() const
Print some additional info about gradient (after <...> and dims),.
Definition: nnet-various.h:473
Component * Copy() const
Copy component (deep copy),.
Definition: nnet-various.h:301
CuVector< BaseFloat > scale_data_
Definition: nnet-various.h:511
void GetGradient(VectorBase< BaseFloat > *gradient) const
Get gradient reshaped as a vector,.
Definition: nnet-various.h:339
std::string Info() const
Print some additional info (after <ComponentName> and the dims),.
Definition: nnet-various.h:354
void InitData(std::istream &is)
Virtual interface for initialization and I/O,.
Definition: nnet-various.h:54
int32 NumParams() const
Number of trainable parameters,.
Definition: nnet-various.h:450
void Copy(const CuMatrixBase< Real > &src, const CuArray< int32 > &copy_from_indices, CuMatrixBase< Real > *tgt)
Copies elements from src into tgt as given by copy_from_indices.
Definition: cu-math.cc:173