nnet-component.h
Go to the documentation of this file.
1 // nnet2/nnet-component.h
2 
3 // Copyright 2011-2013 Karel Vesely
4 // 2012-2014 Johns Hopkins University (author: Daniel Povey)
5 // 2013 Xiaohui Zhang
6 // 2014 Vijayaditya Peddinti
7 // 2014-2015 Guoguo Chen
8 
9 // See ../../COPYING for clarification regarding multiple authors
10 //
11 // Licensed under the Apache License, Version 2.0 (the "License");
12 // you may not use this file except in compliance with the License.
13 // You may obtain a copy of the License at
14 //
15 // http://www.apache.org/licenses/LICENSE-2.0
16 //
17 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
19 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
20 // MERCHANTABLITY OR NON-INFRINGEMENT.
21 // See the Apache 2 License for the specific language governing permissions and
22 // limitations under the License.
23 
24 #ifndef KALDI_NNET2_NNET_COMPONENT_H_
25 #define KALDI_NNET2_NNET_COMPONENT_H_
26 
27 #include <mutex>
28 #include "base/kaldi-common.h"
29 #include "itf/options-itf.h"
30 #include "matrix/matrix-lib.h"
33 
34 #include <iostream>
35 
36 namespace kaldi {
37 namespace nnet2 {
38 
39 
72 class ChunkInfo {
73  public:
74  ChunkInfo() // default constructor we assume this object will not be used
75  : feat_dim_(0), num_chunks_(0),
77  offsets_() { }
78 
79  ChunkInfo(int32 feat_dim, int32 num_chunks,
80  int32 first_offset, int32 last_offset )
81  : feat_dim_(feat_dim), num_chunks_(num_chunks),
82  first_offset_(first_offset), last_offset_(last_offset),
83  offsets_() { Check(); }
84 
85  ChunkInfo(int32 feat_dim, int32 num_chunks,
86  const std::vector<int32> offsets)
87  : feat_dim_(feat_dim), num_chunks_(num_chunks),
88  first_offset_(offsets.front()), last_offset_(offsets.back()),
89  offsets_(offsets) { if (last_offset_ - first_offset_ + 1 == offsets_.size())
90  offsets_.clear();
91  Check(); }
92 
93  // index : actual row index in the current chunk
94  // offset : the time offset of feature frame at current row in the chunk
95  // As described above offsets can take a variety of values, we see the indices
96  // corresponding to the offsets in each case
97  // 1) if first_offset = 0 & last_offset = 691, then chunk has data
98  // corresponding to time offsets 0:691, so index = offset
99  // 2) if first_offset = 7 & last_offset = 684,
100  // then index = offset - first offset
101  // 3) if offsets = {2, 10, 12} then indices for these offsets are 0, 1 and 2
102 
103  // Returns the chunk row index corresponding to given time offset
104  int32 GetIndex (int32 offset) const;
105 
106  // Returns time offset at the current row index in the chunk
107  int32 GetOffset (int32 index) const;
108 
109  // Makes the offsets vector empty, to ensure that the chunk is processed as a
110  // contiguous chunk with the given first_offset and last_offset
111  void MakeOffsetsContiguous () { offsets_.clear(); Check(); }
112 
113  // Returns chunk size, meaning the number of distinct frame-offsets we
114  // have for each chunk (they don't have to be contiguous).
115  inline int32 ChunkSize() const { return NumRows() / num_chunks_; }
116 
117  // Returns number of chunks we expect the feature matrix to have
118  inline int32 NumChunks() const { return num_chunks_; }
119 
121  int32 NumRows() const {
122  return num_chunks_ * (!offsets_.empty() ? offsets_.size() :
123  last_offset_ - first_offset_ + 1); }
124 
126  int32 NumCols() const { return feat_dim_; }
127 
129  void CheckSize(const CuMatrixBase<BaseFloat> &mat) const;
130 
132  void Check() const;
133 
134  private:
135  int32 feat_dim_; // Feature dimension.
136  int32 num_chunks_; // Number of separate equal-sized chunks of features
137  int32 first_offset_; // Start time offset within each chunk, numbered so that at
138  // the input to the network, the first_offset of the first
139  // feature would always be zero.
140  int32 last_offset_; // End time offset within each chunk.
141  std::vector<int32> offsets_; // offsets is only nonempty if the chunk contains
142  // a non-contiguous sequence. If nonempty, it must
143  // be sorted, and offsets.front() == first_offset,
144  // offsets.back() == last_offset.
145 
146 };
147 
157 class Component {
158  public:
159  Component(): index_(-1) { }
160 
161  virtual std::string Type() const = 0; // each type should return a string such as
162  // "SigmoidComponent".
163 
166  virtual int32 Index() const { return index_; }
167 
168  virtual void SetIndex(int32 index) { index_ = index; }
169 
173  virtual void InitFromString(std::string args) = 0;
174 
176  virtual int32 InputDim() const = 0;
177 
179  virtual int32 OutputDim() const = 0;
180 
188  virtual std::vector<int32> Context() const { return std::vector<int32>(1, 0); }
189 
197  virtual void Propagate(const ChunkInfo &in_info,
198  const ChunkInfo &out_info,
199  const CuMatrixBase<BaseFloat> &in,
200  CuMatrixBase<BaseFloat> *out) const = 0;
201 
203  void Propagate(const ChunkInfo &in_info,
204  const ChunkInfo &out_info,
205  const CuMatrixBase<BaseFloat> &in,
206  CuMatrix<BaseFloat> *out) const {
207  if (out->NumRows() != out_info.NumRows() ||
208  out->NumCols() != out_info.NumCols()) {
209  out->Resize(out_info.NumRows(), out_info.NumCols());
210  }
211 
212  // Cast to CuMatrixBase to use the virtual version of propagate function.
213  Propagate(in_info, out_info, in,
214  static_cast<CuMatrixBase<BaseFloat>*>(out));
215  }
216 
227  virtual void Backprop(const ChunkInfo &in_info,
228  const ChunkInfo &out_info,
229  const CuMatrixBase<BaseFloat> &in_value,
230  const CuMatrixBase<BaseFloat> &out_value,
231  const CuMatrixBase<BaseFloat> &out_deriv,
232  Component *to_update, // may be identical to "this".
233  CuMatrix<BaseFloat> *in_deriv) const = 0;
234 
235  virtual bool BackpropNeedsInput() const { return true; } // if this returns false,
236  // the "in_value" to Backprop may be a dummy variable.
237  virtual bool BackpropNeedsOutput() const { return true; } // if this returns false,
238  // the "out_value" to Backprop may be a dummy variable.
239 
241  static Component* ReadNew(std::istream &is, bool binary);
242 
244  virtual Component* Copy() const = 0;
245 
250  static Component *NewFromString(const std::string &initializer_line);
251 
254  static Component *NewComponentOfType(const std::string &type);
255 
256  virtual void Read(std::istream &is, bool binary) = 0; // This Read function
257  // requires that the Component has the correct type.
258 
260  virtual void Write(std::ostream &os, bool binary) const = 0;
261 
262  virtual std::string Info() const;
263 
264  virtual ~Component() { }
265 
266  private:
269 };
270 
271 
280  public:
282  learning_rate_(other.learning_rate_){ }
283 
284  void Init(BaseFloat learning_rate) {
285  learning_rate_ = learning_rate;
286  }
287  UpdatableComponent(BaseFloat learning_rate) {
288  Init(learning_rate);
289  }
290 
295  virtual void SetZero(bool treat_as_gradient) = 0;
296 
297  UpdatableComponent(): learning_rate_(0.001) { }
298 
299  virtual ~UpdatableComponent() { }
300 
305  virtual BaseFloat DotProduct(const UpdatableComponent &other) const = 0;
306 
309  virtual void PerturbParams(BaseFloat stddev) = 0;
310 
313  virtual void Scale(BaseFloat scale) = 0;
314 
318  virtual void Add(BaseFloat alpha, const UpdatableComponent &other) = 0;
319 
321  void SetLearningRate(BaseFloat lrate) { learning_rate_ = lrate; }
323  BaseFloat LearningRate() const { return learning_rate_; }
324 
325  virtual std::string Info() const;
326 
327  // The next few functions are not implemented everywhere; they are
328  // intended for use by L-BFGS code, and we won't implement them
329  // for all child classes.
330 
333  virtual int32 GetParameterDim() const { KALDI_ASSERT(0); return 0; }
334 
338  virtual void Vectorize(VectorBase<BaseFloat> *params) const { KALDI_ASSERT(0); }
340  virtual void UnVectorize(const VectorBase<BaseFloat> &params) {
341  KALDI_ASSERT(0);
342  }
343 
344  protected:
346  private:
347  const UpdatableComponent &operator = (const UpdatableComponent &other); // Disallow.
348 };
349 
353  public:
354  void Init(int32 dim) { dim_ = dim; count_ = 0.0; }
355  explicit NonlinearComponent(int32 dim) { Init(dim); }
356  NonlinearComponent(): dim_(0) { } // e.g. prior to Read().
357  explicit NonlinearComponent(const NonlinearComponent &other);
358 
359  virtual int32 InputDim() const { return dim_; }
360  virtual int32 OutputDim() const { return dim_; }
361 
363  virtual void InitFromString(std::string args);
364 
366  virtual void Read(std::istream &is, bool binary);
367 
369  virtual void Write(std::ostream &os, bool binary) const;
370 
371  void Scale(BaseFloat scale); // relates to scaling stats, not parameters.
372  void Add(BaseFloat alpha, const NonlinearComponent &other); // relates to
373  // adding stats
374 
375  // The following functions are unique to NonlinearComponent.
376  // They mostly relate to diagnostics.
377  const CuVector<double> &ValueSum() const { return value_sum_; }
378  const CuVector<double> &DerivSum() const { return deriv_sum_; }
379  double Count() const { return count_; }
380 
381  // The following function is used when "widening" neural networks.
382  void SetDim(int32 dim);
383 
384  protected:
385  friend class NormalizationComponent;
386  friend class SigmoidComponent;
387  friend class TanhComponent;
388  friend class SoftmaxComponent;
389  friend class LogSoftmaxComponent;
391  friend class SoftHingeComponent;
392 
393 
394  // This function updates the stats "value_sum_", "deriv_sum_", and
395  // count_. (If deriv == NULL, it won't update "deriv_sum_").
396  // It will be called from the Backprop function of child classes.
397  void UpdateStats(const CuMatrixBase<BaseFloat> &out_value,
398  const CuMatrixBase<BaseFloat> *deriv = NULL);
399 
400 
401  const NonlinearComponent &operator = (const NonlinearComponent &other); // Disallow.
403  CuVector<double> value_sum_; // stats at the output.
404  CuVector<double> deriv_sum_; // stats of the derivative of the nonlinearity (only
405  // applicable to element-by-element nonlinearities, not Softmax.
406  double count_;
407  // The mutex is used in UpdateStats, only for resizing vectors.
408  std::mutex mutex_;
409 };
410 
411 class MaxoutComponent: public Component {
412  public:
413  void Init(int32 input_dim, int32 output_dim);
414  explicit MaxoutComponent(int32 input_dim, int32 output_dim) {
415  Init(input_dim, output_dim);
416  }
417  MaxoutComponent(): input_dim_(0), output_dim_(0) { }
418  virtual std::string Type() const { return "MaxoutComponent"; }
419  virtual void InitFromString(std::string args);
420  virtual int32 InputDim() const { return input_dim_; }
421  virtual int32 OutputDim() const { return output_dim_; }
422  using Component::Propagate; // to avoid name hiding
423  virtual void Propagate(const ChunkInfo &in_info,
424  const ChunkInfo &out_info,
425  const CuMatrixBase<BaseFloat> &in,
426  CuMatrixBase<BaseFloat> *out) const;
427  virtual void Backprop(const ChunkInfo &in_info,
428  const ChunkInfo &out_info,
429  const CuMatrixBase<BaseFloat> &in_value,
430  const CuMatrixBase<BaseFloat> &, //out_value,
431  const CuMatrixBase<BaseFloat> &out_deriv,
432  Component *to_update, // may be identical to "this".
433  CuMatrix<BaseFloat> *in_deriv) const;
434  virtual bool BackpropNeedsInput() const { return true; }
435  virtual bool BackpropNeedsOutput() const { return true; }
436  virtual Component* Copy() const { return new MaxoutComponent(input_dim_,
437  output_dim_); }
438 
439  virtual void Read(std::istream &is, bool binary); // This Read function
440  // requires that the Component has the correct type.
441 
443  virtual void Write(std::ostream &os, bool binary) const;
444 
445  virtual std::string Info() const;
446  protected:
449 };
450 
469  public:
470  void Init(int32 input_dim, int32 output_dim,
471  int32 pool_size, int32 pool_stride);
472  explicit MaxpoolingComponent(int32 input_dim, int32 output_dim,
473  int32 pool_size, int32 pool_stride) {
474  Init(input_dim, output_dim, pool_size, pool_stride);
475  }
476  MaxpoolingComponent(): input_dim_(0), output_dim_(0),
477  pool_size_(0), pool_stride_(0) { }
478  virtual std::string Type() const { return "MaxpoolingComponent"; }
479  virtual void InitFromString(std::string args);
480  virtual int32 InputDim() const { return input_dim_; }
481  virtual int32 OutputDim() const { return output_dim_; }
482  using Component::Propagate; // to avoid name hiding
483  virtual void Propagate(const ChunkInfo &in_info,
484  const ChunkInfo &out_info,
485  const CuMatrixBase<BaseFloat> &in,
486  CuMatrixBase<BaseFloat> *out) const;
487  virtual void Backprop(const ChunkInfo &in_info,
488  const ChunkInfo &out_info,
489  const CuMatrixBase<BaseFloat> &in_value,
490  const CuMatrixBase<BaseFloat> &, //out_value,
491  const CuMatrixBase<BaseFloat> &out_deriv,
492  Component *to_update, // may be identical to "this".
493  CuMatrix<BaseFloat> *in_deriv) const;
494  virtual bool BackpropNeedsInput() const { return true; }
495  virtual bool BackpropNeedsOutput() const { return true; }
496  virtual Component* Copy() const {
497  return new MaxpoolingComponent(input_dim_, output_dim_,
498  pool_size_, pool_stride_); }
499 
500  virtual void Read(std::istream &is, bool binary); // This Read function
501  // requires that the Component has the correct type.
502 
504  virtual void Write(std::ostream &os, bool binary) const;
505 
506  virtual std::string Info() const;
507  protected:
512 };
513 
514 class PnormComponent: public Component {
515  public:
516  void Init(int32 input_dim, int32 output_dim, BaseFloat p);
517  explicit PnormComponent(int32 input_dim, int32 output_dim, BaseFloat p) {
518  Init(input_dim, output_dim, p);
519  }
520  PnormComponent(): input_dim_(0), output_dim_(0), p_(0) { }
521  virtual std::string Type() const { return "PnormComponent"; }
522  virtual void InitFromString(std::string args);
523  virtual int32 InputDim() const { return input_dim_; }
524  virtual int32 OutputDim() const { return output_dim_; }
525  using Component::Propagate; // to avoid name hiding
526  virtual void Propagate(const ChunkInfo &in_info,
527  const ChunkInfo &out_info,
528  const CuMatrixBase<BaseFloat> &in,
529  CuMatrixBase<BaseFloat> *out) const;
530  virtual void Backprop(const ChunkInfo &in_info,
531  const ChunkInfo &out_info,
532  const CuMatrixBase<BaseFloat> &in_value,
533  const CuMatrixBase<BaseFloat> &, //out_value,
534  const CuMatrixBase<BaseFloat> &out_deriv,
535  Component *to_update, // may be identical to "this".
536  CuMatrix<BaseFloat> *in_deriv) const;
537  virtual bool BackpropNeedsInput() const { return true; }
538  virtual bool BackpropNeedsOutput() const { return true; }
539  virtual Component* Copy() const { return new PnormComponent(input_dim_,
540  output_dim_, p_); }
541 
542  virtual void Read(std::istream &is, bool binary); // This Read function
543  // requires that the Component has the correct type.
544 
546  virtual void Write(std::ostream &os, bool binary) const;
547 
548  virtual std::string Info() const;
549  protected:
553 };
554 
556  public:
558  explicit NormalizeComponent(const NormalizeComponent &other): NonlinearComponent(other) { }
560  virtual std::string Type() const { return "NormalizeComponent"; }
561  virtual Component* Copy() const { return new NormalizeComponent(*this); }
562  virtual bool BackpropNeedsInput() const { return true; }
563  virtual bool BackpropNeedsOutput() const { return false; }
564  using Component::Propagate; // to avoid name hiding
565  virtual void Propagate(const ChunkInfo &in_info,
566  const ChunkInfo &out_info,
567  const CuMatrixBase<BaseFloat> &in,
568  CuMatrixBase<BaseFloat> *out) const;
569  virtual void Backprop(const ChunkInfo &in_info,
570  const ChunkInfo &out_info,
571  const CuMatrixBase<BaseFloat> &in_value,
572  const CuMatrixBase<BaseFloat> &out_value,
573  const CuMatrixBase<BaseFloat> &out_deriv,
574  Component *to_update, // may be identical to "this".
575  CuMatrix<BaseFloat> *in_deriv) const;
576  private:
577  NormalizeComponent &operator = (const NormalizeComponent &other); // Disallow.
578  static const BaseFloat kNormFloor;
579  // about 0.7e-20. We need a value that's exactly representable in
580  // float and whose inverse square root is also exactly representable
581  // in float (hence, an even power of two).
582 };
583 
584 
586  public:
587  explicit SigmoidComponent(int32 dim): NonlinearComponent(dim) { }
588  explicit SigmoidComponent(const SigmoidComponent &other): NonlinearComponent(other) { }
590  virtual std::string Type() const { return "SigmoidComponent"; }
591  virtual bool BackpropNeedsInput() const { return false; }
592  virtual bool BackpropNeedsOutput() const { return true; }
593  virtual Component* Copy() const { return new SigmoidComponent(*this); }
594  using Component::Propagate; // to avoid name hiding
595  virtual void Propagate(const ChunkInfo &in_info,
596  const ChunkInfo &out_info,
597  const CuMatrixBase<BaseFloat> &in,
598  CuMatrixBase<BaseFloat> *out) const;
599  virtual void Backprop(const ChunkInfo &in_info,
600  const ChunkInfo &out_info,
601  const CuMatrixBase<BaseFloat> &in_value,
602  const CuMatrixBase<BaseFloat> &out_value,
603  const CuMatrixBase<BaseFloat> &out_deriv,
604  Component *to_update, // may be identical to "this".
605  CuMatrix<BaseFloat> *in_deriv) const;
606  private:
607  SigmoidComponent &operator = (const SigmoidComponent &other); // Disallow.
608 };
609 
611  public:
612  explicit TanhComponent(int32 dim): NonlinearComponent(dim) { }
613  explicit TanhComponent(const TanhComponent &other): NonlinearComponent(other) { }
615  virtual std::string Type() const { return "TanhComponent"; }
616  virtual Component* Copy() const { return new TanhComponent(*this); }
617  virtual bool BackpropNeedsInput() const { return false; }
618  virtual bool BackpropNeedsOutput() const { return true; }
619  using Component::Propagate; // to avoid name hiding
620  virtual void Propagate(const ChunkInfo &in_info,
621  const ChunkInfo &out_info,
622  const CuMatrixBase<BaseFloat> &in,
623  CuMatrixBase<BaseFloat> *out) const;
624  virtual void Backprop(const ChunkInfo &in_info,
625  const ChunkInfo &out_info,
626  const CuMatrixBase<BaseFloat> &in_value,
627  const CuMatrixBase<BaseFloat> &out_value,
628  const CuMatrixBase<BaseFloat> &out_deriv,
629  Component *to_update, // may be identical to "this".
630  CuMatrix<BaseFloat> *in_deriv) const;
631  private:
632  TanhComponent &operator = (const TanhComponent &other); // Disallow.
633 };
634 
638  public:
639  void Init(int32 dim, BaseFloat power = 2);
640  explicit PowerComponent(int32 dim, BaseFloat power = 2) {
641  Init(dim, power);
642  }
643  PowerComponent(): dim_(0), power_(2) { }
644  virtual std::string Type() const { return "PowerComponent"; }
645  virtual void InitFromString(std::string args);
646  virtual int32 InputDim() const { return dim_; }
647  virtual int32 OutputDim() const { return dim_; }
648  using Component::Propagate; // to avoid name hiding
649  virtual void Propagate(const ChunkInfo &in_info,
650  const ChunkInfo &out_info,
651  const CuMatrixBase<BaseFloat> &in,
652  CuMatrixBase<BaseFloat> *out) const;
653  virtual void Backprop(const ChunkInfo &in_info,
654  const ChunkInfo &out_info,
655  const CuMatrixBase<BaseFloat> &in_value,
656  const CuMatrixBase<BaseFloat> &out_value,
657  const CuMatrixBase<BaseFloat> &out_deriv,
658  Component *to_update, // may be identical to "this".
659  CuMatrix<BaseFloat> *in_deriv) const;
660  virtual bool BackpropNeedsInput() const { return true; }
661  virtual bool BackpropNeedsOutput() const { return true; }
662  virtual Component* Copy() const { return new PowerComponent(dim_, power_); }
663  virtual void Read(std::istream &is, bool binary); // This Read function
664  // requires that the Component has the correct type.
665 
667  virtual void Write(std::ostream &os, bool binary) const;
668 
669  virtual std::string Info() const;
670 
671  private:
674 };
675 
677  public:
681  virtual std::string Type() const { return "RectifiedLinearComponent"; }
682  virtual Component* Copy() const { return new RectifiedLinearComponent(*this); }
683  virtual bool BackpropNeedsInput() const { return false; }
684  virtual bool BackpropNeedsOutput() const { return true; }
685  using Component::Propagate; // to avoid name hiding
686  virtual void Propagate(const ChunkInfo &in_info,
687  const ChunkInfo &out_info,
688  const CuMatrixBase<BaseFloat> &in,
689  CuMatrixBase<BaseFloat> *out) const;
690  virtual void Backprop(const ChunkInfo &in_info,
691  const ChunkInfo &out_info,
692  const CuMatrixBase<BaseFloat> &in_value,
693  const CuMatrixBase<BaseFloat> &out_value,
694  const CuMatrixBase<BaseFloat> &out_deriv,
695  Component *to_update, // may be identical to "this".
696  CuMatrix<BaseFloat> *in_deriv) const;
697  private:
698  RectifiedLinearComponent &operator = (const RectifiedLinearComponent &other); // Disallow.
699 };
700 
702  public:
704  explicit SoftHingeComponent(const SoftHingeComponent &other): NonlinearComponent(other) { }
706  virtual std::string Type() const { return "SoftHingeComponent"; }
707  virtual Component* Copy() const { return new SoftHingeComponent(*this); }
708  virtual bool BackpropNeedsInput() const { return true; }
709  virtual bool BackpropNeedsOutput() const { return true; }
710  using Component::Propagate; // to avoid name hiding
711  virtual void Propagate(const ChunkInfo &in_info,
712  const ChunkInfo &out_info,
713  const CuMatrixBase<BaseFloat> &in,
714  CuMatrixBase<BaseFloat> *out) const;
715  virtual void Backprop(const ChunkInfo &in_info,
716  const ChunkInfo &out_info,
717  const CuMatrixBase<BaseFloat> &in_value,
718  const CuMatrixBase<BaseFloat> &out_value,
719  const CuMatrixBase<BaseFloat> &out_deriv,
720  Component *to_update, // may be identical to "this".
721  CuMatrix<BaseFloat> *in_deriv) const;
722  private:
723  SoftHingeComponent &operator = (const SoftHingeComponent &other); // Disallow.
724 };
725 
726 
727 // This class scales the input by a specified constant. This is, of course,
728 // useless, but we use it when we want to change how fast the next layer learns.
729 // (e.g. a smaller scale will make the next layer learn slower.)
730 class ScaleComponent: public Component {
731  public:
732  explicit ScaleComponent(int32 dim, BaseFloat scale): dim_(dim), scale_(scale) { }
733  explicit ScaleComponent(const ScaleComponent &other):
734  dim_(other.dim_), scale_(other.scale_) { }
735  ScaleComponent(): dim_(0), scale_(0.0) { }
736  virtual std::string Type() const { return "ScaleComponent"; }
737  virtual Component* Copy() const { return new ScaleComponent(*this); }
738  virtual bool BackpropNeedsInput() const { return false; }
739  virtual bool BackpropNeedsOutput() const { return false; }
740  using Component::Propagate; // to avoid name hiding
741  virtual void Propagate(const ChunkInfo &in_info,
742  const ChunkInfo &out_info,
743  const CuMatrixBase<BaseFloat> &in,
744  CuMatrixBase<BaseFloat> *out) const;
745  virtual void Backprop(const ChunkInfo &in_info,
746  const ChunkInfo &out_info,
747  const CuMatrixBase<BaseFloat> &in_value,
748  const CuMatrixBase<BaseFloat> &out_value,
749  const CuMatrixBase<BaseFloat> &out_deriv,
750  Component *to_update, // may be identical to "this".
751  CuMatrix<BaseFloat> *in_deriv) const;
752 
753  virtual int32 InputDim() const { return dim_; }
754  virtual int32 OutputDim() const { return dim_; }
755  virtual void Read(std::istream &is, bool binary);
756 
757  virtual void Write(std::ostream &os, bool binary) const;
758 
759  void Init(int32 dim, BaseFloat scale);
760 
761  virtual void InitFromString(std::string args);
762 
763  virtual std::string Info() const;
764 
765  private:
768  ScaleComponent &operator = (const ScaleComponent &other); // Disallow.
769 };
770 
771 
772 
773 class SumGroupComponent; // Forward declaration.
774 class AffineComponent; // Forward declaration.
775 class FixedScaleComponent; // Forward declaration.
776 
778  public:
779  explicit SoftmaxComponent(int32 dim): NonlinearComponent(dim) { }
780  explicit SoftmaxComponent(const SoftmaxComponent &other): NonlinearComponent(other) { }
782  virtual std::string Type() const { return "SoftmaxComponent"; }
783  virtual bool BackpropNeedsInput() const { return false; }
784  virtual bool BackpropNeedsOutput() const { return true; }
785  using Component::Propagate; // to avoid name hiding
786  virtual void Propagate(const ChunkInfo &in_info,
787  const ChunkInfo &out_info,
788  const CuMatrixBase<BaseFloat> &in,
789  CuMatrixBase<BaseFloat> *out) const;
790  virtual void Backprop(const ChunkInfo &in_info,
791  const ChunkInfo &out_info,
792  const CuMatrixBase<BaseFloat> &in_value,
793  const CuMatrixBase<BaseFloat> &out_value,
794  const CuMatrixBase<BaseFloat> &out_deriv,
795  Component *to_update, // may be identical to "this".
796  CuMatrix<BaseFloat> *in_deriv) const;
797 
798  void MixUp(int32 num_mixtures,
799  BaseFloat power,
800  BaseFloat min_count,
801  BaseFloat perturb_stddev,
802  AffineComponent *ac,
803  SumGroupComponent *sc);
804 
805  virtual Component* Copy() const { return new SoftmaxComponent(*this); }
806  private:
807  SoftmaxComponent &operator = (const SoftmaxComponent &other); // Disallow.
808 };
809 
811  public:
813  explicit LogSoftmaxComponent(const LogSoftmaxComponent &other): NonlinearComponent(other) { }
815  virtual std::string Type() const { return "LogSoftmaxComponent"; }
816  virtual bool BackpropNeedsInput() const { return false; }
817  virtual bool BackpropNeedsOutput() const { return true; }
818  using Component::Propagate; // to avoid name hiding
819  virtual void Propagate(const ChunkInfo &in_info,
820  const ChunkInfo &out_info,
821  const CuMatrixBase<BaseFloat> &in,
822  CuMatrixBase<BaseFloat> *out) const;
823  virtual void Backprop(const ChunkInfo &in_info,
824  const ChunkInfo &out_info,
825  const CuMatrixBase<BaseFloat> &in_value,
826  const CuMatrixBase<BaseFloat> &out_value,
827  const CuMatrixBase<BaseFloat> &out_deriv,
828  Component *to_update, // may be identical to "this".
829  CuMatrix<BaseFloat> *in_deriv) const;
830 
831  virtual Component* Copy() const { return new LogSoftmaxComponent(*this); }
832  private:
833  LogSoftmaxComponent &operator = (const LogSoftmaxComponent &other); // Disallow.
834 };
835 
836 
838 
839 // Affine means a linear function plus an offset.
840 // Note: although this class can be instantiated, it also
841 // functions as a base-class for more specialized versions of
842 // AffineComponent.
844  friend class SoftmaxComponent; // Friend declaration relates to mixing up.
845  public:
846  AffineComponent(const AffineComponent &other);
847  // The next constructor is used in converting from nnet1.
848  AffineComponent(const CuMatrixBase<BaseFloat> &linear_params,
849  const CuVectorBase<BaseFloat> &bias_params,
850  BaseFloat learning_rate);
851 
852  virtual int32 InputDim() const { return linear_params_.NumCols(); }
853  virtual int32 OutputDim() const { return linear_params_.NumRows(); }
854  void Init(BaseFloat learning_rate,
855  int32 input_dim, int32 output_dim,
856  BaseFloat param_stddev, BaseFloat bias_stddev);
857  void Init(BaseFloat learning_rate,
858  std::string matrix_filename);
859 
860  // This function resizes the dimensions of the component, setting the
861  // parameters to zero, while leaving any other configuration values the same.
862  virtual void Resize(int32 input_dim, int32 output_dim);
863 
864  // The following functions are used for collapsing multiple layers
865  // together. They return a pointer to a new Component equivalent to
866  // the sequence of two components. We haven't implemented this for
867  // FixedLinearComponent yet.
868  Component *CollapseWithNext(const AffineComponent &next) const ;
869  Component *CollapseWithNext(const FixedAffineComponent &next) const;
870  Component *CollapseWithNext(const FixedScaleComponent &next) const;
871  Component *CollapseWithPrevious(const FixedAffineComponent &prev) const;
872 
873  virtual std::string Info() const;
874  virtual void InitFromString(std::string args);
875 
876  AffineComponent(): is_gradient_(false) { } // use Init to really initialize.
877  virtual std::string Type() const { return "AffineComponent"; }
878  virtual bool BackpropNeedsInput() const { return true; }
879  virtual bool BackpropNeedsOutput() const { return false; }
880  using Component::Propagate; // to avoid name hiding
881  virtual void Propagate(const ChunkInfo &in_info,
882  const ChunkInfo &out_info,
883  const CuMatrixBase<BaseFloat> &in,
884  CuMatrixBase<BaseFloat> *out) const;
885  virtual void Scale(BaseFloat scale);
886  virtual void Add(BaseFloat alpha, const UpdatableComponent &other);
887  virtual void Backprop(const ChunkInfo &in_info,
888  const ChunkInfo &out_info,
889  const CuMatrixBase<BaseFloat> &in_value,
890  const CuMatrixBase<BaseFloat> &out_value,
891  const CuMatrixBase<BaseFloat> &out_deriv,
892  Component *to_update, // may be identical to "this".
893  CuMatrix<BaseFloat> *in_deriv) const;
894  virtual void SetZero(bool treat_as_gradient);
895  virtual void Read(std::istream &is, bool binary);
896  virtual void Write(std::ostream &os, bool binary) const;
897  virtual BaseFloat DotProduct(const UpdatableComponent &other) const;
898  virtual Component* Copy() const;
899  virtual void PerturbParams(BaseFloat stddev);
900  // This new function is used when mixing up:
901  virtual void SetParams(const VectorBase<BaseFloat> &bias,
902  const MatrixBase<BaseFloat> &linear);
903  const CuVector<BaseFloat> &BiasParams() { return bias_params_; }
904  const CuMatrix<BaseFloat> &LinearParams() { return linear_params_; }
905 
906  virtual int32 GetParameterDim() const;
907  virtual void Vectorize(VectorBase<BaseFloat> *params) const;
908  virtual void UnVectorize(const VectorBase<BaseFloat> &params);
909 
912  virtual void LimitRank(int32 dimension,
913  AffineComponent **a, AffineComponent **b) const;
914 
916  void Widen(int32 new_dimension,
917  BaseFloat param_stddev,
918  BaseFloat bias_stddev,
919  std::vector<NonlinearComponent*> c2, // will usually have just one
920  // element.
921  AffineComponent *c3);
922  protected:
924  // This function Update() is for extensibility; child classes may override this.
925  virtual void Update(
926  const CuMatrixBase<BaseFloat> &in_value,
927  const CuMatrixBase<BaseFloat> &out_deriv) {
928  UpdateSimple(in_value, out_deriv);
929  }
930  // UpdateSimple is used when *this is a gradient. Child classes may
931  // or may not override this.
932  virtual void UpdateSimple(
933  const CuMatrixBase<BaseFloat> &in_value,
934  const CuMatrixBase<BaseFloat> &out_deriv);
935 
936  const AffineComponent &operator = (const AffineComponent &other); // Disallow.
939 
940  bool is_gradient_; // If true, treat this as just a gradient.
941 };
942 
943 
944 // This is an idea Dan is trying out, a little bit like
945 // preconditioning the update with the Fisher matrix, but the
946 // Fisher matrix has a special structure.
947 // [note: it is currently used in the standard recipe].
949  public:
950  virtual std::string Type() const { return "AffineComponentPreconditioned"; }
951 
952  virtual void Read(std::istream &is, bool binary);
953  virtual void Write(std::ostream &os, bool binary) const;
954  void Init(BaseFloat learning_rate,
955  int32 input_dim, int32 output_dim,
956  BaseFloat param_stddev, BaseFloat bias_stddev,
957  BaseFloat alpha, BaseFloat max_change);
958  void Init(BaseFloat learning_rate, BaseFloat alpha,
959  BaseFloat max_change, std::string matrix_filename);
960 
961  virtual void InitFromString(std::string args);
962  virtual std::string Info() const;
963  virtual Component* Copy() const;
964  AffineComponentPreconditioned(): alpha_(1.0), max_change_(0.0) { }
965  void SetMaxChange(BaseFloat max_change) { max_change_ = max_change; }
966  protected:
969  BaseFloat max_change_; // If > 0, this is the maximum amount of parameter change (in L2 norm)
970  // that we allow per minibatch. This was introduced in order to
971  // control instability. Instead of the exact L2 parameter change,
972  // for efficiency purposes we limit a bound on the exact change.
973  // The limit is applied via a constant <= 1.0 for each minibatch,
974  // A suitable value might be, for example, 10 or so; larger if there are
975  // more parameters.
976 
982  BaseFloat GetScalingFactor(const CuMatrix<BaseFloat> &in_value_precon,
983  const CuMatrix<BaseFloat> &out_deriv_precon);
984 
985  virtual void Update(
986  const CuMatrixBase<BaseFloat> &in_value,
987  const CuMatrixBase<BaseFloat> &out_deriv);
988 };
989 
990 
998  public:
999  virtual std::string Type() const {
1000  return "AffineComponentPreconditionedOnline";
1001  }
1002 
1003  virtual void Read(std::istream &is, bool binary);
1004  virtual void Write(std::ostream &os, bool binary) const;
1005  void Init(BaseFloat learning_rate,
1006  int32 input_dim, int32 output_dim,
1007  BaseFloat param_stddev, BaseFloat bias_stddev,
1008  int32 rank_in, int32 rank_out, int32 update_period,
1009  BaseFloat num_samples_history, BaseFloat alpha,
1010  BaseFloat max_change_per_sample);
1011  void Init(BaseFloat learning_rate, int32 rank_in,
1012  int32 rank_out, int32 update_period,
1013  BaseFloat num_samples_history,
1014  BaseFloat alpha, BaseFloat max_change_per_sample,
1015  std::string matrix_filename);
1016 
1017  virtual void Resize(int32 input_dim, int32 output_dim);
1018 
1019  // This constructor is used when converting neural networks partway through
1020  // training, from AffineComponent or AffineComponentPreconditioned to
1021  // AffineComponentPreconditionedOnline.
1023  int32 rank_in, int32 rank_out,
1024  int32 update_period,
1025  BaseFloat eta, BaseFloat alpha);
1026 
1027  virtual void InitFromString(std::string args);
1028  virtual std::string Info() const;
1029  virtual Component* Copy() const;
1030  AffineComponentPreconditionedOnline(): max_change_per_sample_(0.0) { }
1031 
1032  private:
1034 
1035 
1036  // Configs for preconditioner. The input side tends to be better conditioned ->
1037  // smaller rank needed, so make them separately configurable.
1043 
1045 
1047 
1049  // If > 0, max_change_per_sample_ this is the maximum amount of parameter
1050  // change (in L2 norm) that we allow per sample, averaged over the minibatch.
1051  // This was introduced in order to control instability.
1052  // Instead of the exact L2 parameter change, for
1053  // efficiency purposes we limit a bound on the exact
1054  // change. The limit is applied via a constant <= 1.0
1055  // for each minibatch, A suitable value might be, for
1056  // example, 10 or so; larger if there are more
1057  // parameters.
1058 
1067  BaseFloat GetScalingFactor(const CuVectorBase<BaseFloat> &in_products,
1068  BaseFloat gamma_prod,
1069  CuVectorBase<BaseFloat> *out_products);
1070 
1071  // Sets the configs rank, alpha and eta in the preconditioner objects,
1072  // from the class variables.
1073  void SetPreconditionerConfigs();
1074 
1075  virtual void Update(
1076  const CuMatrixBase<BaseFloat> &in_value,
1077  const CuMatrixBase<BaseFloat> &out_deriv);
1078 };
1079 
1081  public:
1082  // This function is required in testing code and in other places we need
1083  // consistency in the random number generation (e.g. when optimizing
1084  // validation-set performance), but check where else we call sRand(). You'll
1085  // need to call srand as well as making this call.
1086  void ResetGenerator() { random_generator_.SeedGpu(); }
1087  protected:
1089 };
1090 
1093  public:
1094  SpliceComponent() { } // called only prior to Read() or Init().
1095  // Note: it is required that the elements of "context" be in
1096  // strictly increasing order, that the lowest element of component
1097  // be nonpositive, and the highest element be nonnegative.
1098  void Init(int32 input_dim,
1099  std::vector<int32> context,
1100  int32 const_component_dim=0);
1101  virtual std::string Type() const { return "SpliceComponent"; }
1102  virtual std::string Info() const;
1103  virtual void InitFromString(std::string args);
1104  virtual int32 InputDim() const { return input_dim_; }
1105  virtual int32 OutputDim() const;
1106  virtual std::vector<int32> Context() const { return context_; }
1107  using Component::Propagate; // to avoid name hiding
1108  virtual void Propagate(const ChunkInfo &in_info,
1109  const ChunkInfo &out_info,
1110  const CuMatrixBase<BaseFloat> &in,
1111  CuMatrixBase<BaseFloat> *out) const;
1112  virtual void Backprop(const ChunkInfo &in_info,
1113  const ChunkInfo &out_info,
1114  const CuMatrixBase<BaseFloat> &in_value,
1115  const CuMatrixBase<BaseFloat> &out_value,
1116  const CuMatrixBase<BaseFloat> &out_deriv,
1117  Component *to_update, // may be identical to "this".
1118  CuMatrix<BaseFloat> *in_deriv) const;
1119  virtual bool BackpropNeedsInput() const { return false; }
1120  virtual bool BackpropNeedsOutput() const { return false; }
1121  virtual Component* Copy() const;
1122  virtual void Read(std::istream &is, bool binary);
1123  virtual void Write(std::ostream &os, bool binary) const;
1124  private:
1127  std::vector<int32> context_;
1129 };
1130 
1134  public:
1135  SpliceMaxComponent() { } // called only prior to Read() or Init().
1136  void Init(int32 dim,
1137  std::vector<int32> context);
1138  virtual std::string Type() const { return "SpliceMaxComponent"; }
1139  virtual std::string Info() const;
1140  virtual void InitFromString(std::string args);
1141  virtual int32 InputDim() const { return dim_; }
1142  virtual int32 OutputDim() const { return dim_; }
1143  virtual std::vector<int32> Context() const { return context_; }
1144  using Component::Propagate; // to avoid name hiding
1145  virtual void Propagate(const ChunkInfo &in_info,
1146  const ChunkInfo &out_info,
1147  const CuMatrixBase<BaseFloat> &in,
1148  CuMatrixBase<BaseFloat> *out) const;
1149  virtual void Backprop(const ChunkInfo &in_info,
1150  const ChunkInfo &out_info,
1151  const CuMatrixBase<BaseFloat> &in_value,
1152  const CuMatrixBase<BaseFloat> &out_value,
1153  const CuMatrixBase<BaseFloat> &out_deriv,
1154  Component *to_update, // may be identical to "this".
1155  CuMatrix<BaseFloat> *in_deriv) const;
1156  virtual bool BackpropNeedsInput() const { return true; }
1157  virtual bool BackpropNeedsOutput() const { return false; }
1158  virtual Component* Copy() const;
1159  virtual void Read(std::istream &is, bool binary);
1160  virtual void Write(std::ostream &os, bool binary) const;
1161  private:
1164  std::vector<int32> context_;
1165 };
1166 
1167 
1168 // Affine means a linear function plus an offset. "Block" means
1169 // here that we support a number of equal-sized blocks of parameters,
1170 // in the linear part, so e.g. 2 x 500 would mean 2 blocks of 500 each.
1172  public:
1173  virtual int32 InputDim() const { return linear_params_.NumCols() * num_blocks_; }
1174  virtual int32 OutputDim() const { return linear_params_.NumRows(); }
1175  virtual int32 GetParameterDim() const;
1176  virtual void Vectorize(VectorBase<BaseFloat> *params) const;
1177  virtual void UnVectorize(const VectorBase<BaseFloat> &params);
1178 
1179  // Note: num_blocks must divide input_dim.
1180  void Init(BaseFloat learning_rate,
1181  int32 input_dim, int32 output_dim,
1182  BaseFloat param_stddev, BaseFloat bias_stddev,
1183  int32 num_blocks);
1184  virtual void InitFromString(std::string args);
1185 
1186  BlockAffineComponent() { } // use Init to really initialize.
1187  virtual std::string Type() const { return "BlockAffineComponent"; }
1188  virtual bool BackpropNeedsInput() const { return true; }
1189  virtual bool BackpropNeedsOutput() const { return false; }
1190  using Component::Propagate; // to avoid name hiding
1191  virtual void Propagate(const ChunkInfo &in_info,
1192  const ChunkInfo &out_info,
1193  const CuMatrixBase<BaseFloat> &in,
1194  CuMatrixBase<BaseFloat> *out) const;
1195  virtual void Backprop(const ChunkInfo &in_info,
1196  const ChunkInfo &out_info,
1197  const CuMatrixBase<BaseFloat> &in_value,
1198  const CuMatrixBase<BaseFloat> &out_value,
1199  const CuMatrixBase<BaseFloat> &out_deriv,
1200  Component *to_update, // may be identical to "this".
1201  CuMatrix<BaseFloat> *in_deriv) const;
1202  virtual void SetZero(bool treat_as_gradient);
1203  virtual void Read(std::istream &is, bool binary);
1204  virtual void Write(std::ostream &os, bool binary) const;
1205  virtual BaseFloat DotProduct(const UpdatableComponent &other) const;
1206  virtual Component* Copy() const;
1207  virtual void PerturbParams(BaseFloat stddev);
1208  virtual void Scale(BaseFloat scale);
1209  virtual void Add(BaseFloat alpha, const UpdatableComponent &other);
1210  protected:
1211  virtual void Update(
1212  const CuMatrixBase<BaseFloat> &in_value,
1213  const CuMatrixBase<BaseFloat> &out_deriv) {
1214  UpdateSimple(in_value, out_deriv);
1215  }
1216  // UpdateSimple is used when *this is a gradient. Child classes may
1217  // override this.
1218  virtual void UpdateSimple(
1219  const CuMatrixBase<BaseFloat> &in_value,
1220  const CuMatrixBase<BaseFloat> &out_deriv);
1221 
1222  // The matrix linear_params_ has a block structure, with num_blocks_ blocks of
1223  // equal size. The blocks are stored in linear_params_ as
1224  // [ M
1225  // N
1226  // O ] but we actually treat it as the matrix:
1227  // [ M 0 0
1228  // 0 N 0
1229  // 0 0 O ]
1233  private:
1235 
1236 };
1237 
1238 
1239 // Affine means a linear function plus an offset. "Block" means
1240 // here that we support a number of equal-sized blocks of parameters,
1241 // in the linear part, so e.g. 2 x 500 would mean 2 blocks of 500 each.
1243  public:
1244  // Note: num_blocks must divide input_dim.
1245  void Init(BaseFloat learning_rate,
1246  int32 input_dim, int32 output_dim,
1247  BaseFloat param_stddev, BaseFloat bias_stddev,
1248  int32 num_blocks, BaseFloat alpha);
1249 
1250  virtual void InitFromString(std::string args);
1251 
1252  BlockAffineComponentPreconditioned() { } // use Init to really initialize.
1253  virtual std::string Type() const { return "BlockAffineComponentPreconditioned"; }
1254  virtual void SetZero(bool treat_as_gradient);
1255  virtual void Read(std::istream &is, bool binary);
1256  virtual void Write(std::ostream &os, bool binary) const;
1257  virtual Component* Copy() const;
1258  private:
1260  virtual void Update(
1261  const CuMatrixBase<BaseFloat> &in_value,
1262  const CuMatrixBase<BaseFloat> &out_deriv);
1263 
1266 };
1267 
1268 // SumGroupComponent is used to sum up groups of posteriors.
1269 // It's used to introduce a kind of Gaussian-mixture-model-like
1270 // idea into neural nets. This is basically a degenerate case of
1271 // MixtureProbComponent; we had to implement it separately to
1272 // be efficient for CUDA (we can use this one regardless whether
1273 // we have CUDA or not; it's the normal case we want anyway).
1275 public:
1276  virtual int32 InputDim() const { return input_dim_; }
1277  virtual int32 OutputDim() const { return output_dim_; }
1278  void Init(const std::vector<int32> &sizes); // the vector is of the input dim
1279  // (>= 1) for each output dim.
1280  void GetSizes(std::vector<int32> *sizes) const; // Get a vector saying, for
1281  // each output-dim, how many
1282  // inputs were summed over.
1283  virtual void InitFromString(std::string args);
1285  virtual std::string Type() const { return "SumGroupComponent"; }
1286  virtual bool BackpropNeedsInput() const { return false; }
1287  virtual bool BackpropNeedsOutput() const { return false; }
1288  using Component::Propagate; // to avoid name hiding
1289  virtual void Propagate(const ChunkInfo &in_info,
1290  const ChunkInfo &out_info,
1291  const CuMatrixBase<BaseFloat> &in,
1292  CuMatrixBase<BaseFloat> *out) const;
1293  // Note: in_value and out_value are both dummy variables.
1294  virtual void Backprop(const ChunkInfo &in_info,
1295  const ChunkInfo &out_info,
1296  const CuMatrixBase<BaseFloat> &in_value,
1297  const CuMatrixBase<BaseFloat> &out_value,
1298  const CuMatrixBase<BaseFloat> &out_deriv,
1299  Component *to_update, // may be identical to "this".
1300  CuMatrix<BaseFloat> *in_deriv) const;
1301  virtual Component* Copy() const;
1302  virtual void Read(std::istream &is, bool binary);
1303  virtual void Write(std::ostream &os, bool binary) const;
1304 
1305 private:
1307  // Note: Int32Pair is just struct{ int32 first; int32 second }; it's defined
1308  // in cu-matrixdim.h as extern "C" which is needed for the CUDA interface.
1309  CuArray<Int32Pair> indexes_; // for each output index, the (start, end) input
1310  // index.
1311  CuArray<int32> reverse_indexes_; // for each input index, the output index.
1314 };
1315 
1316 
1321  public:
1322  void Init(int32 dim);
1323  void Init(const std::vector<int32> &reorder);
1324  PermuteComponent(int32 dim) { Init(dim); }
1325  PermuteComponent(const std::vector<int32> &reorder) { Init(reorder); }
1326 
1327  PermuteComponent() { } // e.g. prior to Read() or Init()
1328 
1329  virtual int32 InputDim() const { return reorder_.size(); }
1330  virtual int32 OutputDim() const { return reorder_.size(); }
1331  virtual Component *Copy() const;
1332 
1333  virtual void InitFromString(std::string args);
1334  virtual void Read(std::istream &is, bool binary);
1335  virtual void Write(std::ostream &os, bool binary) const;
1336  virtual std::string Type() const { return "PermuteComponent"; }
1337  virtual bool BackpropNeedsInput() const { return false; }
1338  virtual bool BackpropNeedsOutput() const { return false; }
1339  using Component::Propagate; // to avoid name hiding
1340  virtual void Propagate(const ChunkInfo &in_info,
1341  const ChunkInfo &out_info,
1342  const CuMatrixBase<BaseFloat> &in,
1343  CuMatrixBase<BaseFloat> *out) const;
1344  virtual void Backprop(const ChunkInfo &in_info,
1345  const ChunkInfo &out_info,
1346  const CuMatrixBase<BaseFloat> &in_value,
1347  const CuMatrixBase<BaseFloat> &out_value,
1348  const CuMatrixBase<BaseFloat> &out_deriv,
1349  Component *to_update, // may be identical to "this".
1350  CuMatrix<BaseFloat> *in_deriv) const;
1351 
1352  private:
1354  std::vector<int32> reorder_; // This class sends input dimension i to
1355  // output dimension reorder_[i].
1356 };
1357 
1358 
1361 class DctComponent: public Component {
1362  public:
1363  DctComponent() { dim_ = 0; }
1364  virtual std::string Type() const { return "DctComponent"; }
1365  virtual std::string Info() const;
1366  //dim = dimension of vector being processed
1367  //dct_dim = effective lenght of DCT, i.e. how many compoments will be kept
1368  void Init(int32 dim, int32 dct_dim, bool reorder, int32 keep_dct_dim=0);
1369  // InitFromString takes numeric options
1370  // dim, dct-dim, and (optionally) reorder={true,false}, keep-dct-dim
1371  // Note: reorder defaults to false. keep-dct-dim defaults to dct-dim
1372  virtual void InitFromString(std::string args);
1373  virtual int32 InputDim() const { return dim_; }
1374  virtual int32 OutputDim() const { return dct_mat_.NumRows() * (dim_ / dct_mat_.NumCols()); }
1375  using Component::Propagate; // to avoid name hiding
1376  virtual void Propagate(const ChunkInfo &in_info,
1377  const ChunkInfo &out_info,
1378  const CuMatrixBase<BaseFloat> &in,
1379  CuMatrixBase<BaseFloat> *out) const;
1380  virtual void Backprop(const ChunkInfo &in_info,
1381  const ChunkInfo &out_info,
1382  const CuMatrixBase<BaseFloat> &in_value,
1383  const CuMatrixBase<BaseFloat> &out_value,
1384  const CuMatrixBase<BaseFloat> &out_deriv,
1385  Component *to_update, // may be identical to "this".
1386  CuMatrix<BaseFloat> *in_deriv) const;
1387  virtual bool BackpropNeedsInput() const { return false; }
1388  virtual bool BackpropNeedsOutput() const { return false; }
1389  virtual Component* Copy() const;
1390  virtual void Read(std::istream &is, bool binary);
1391  virtual void Write(std::ostream &os, bool binary) const;
1392  private:
1393  void Reorder(CuMatrixBase<BaseFloat> *mat, bool reverse) const;
1394  int32 dim_; // The input dimension of the (sub)vector.
1395 
1396  bool reorder_; // If true, transformation matrix we use is not
1397  // block diagonal but is block diagonal after reordering-- so
1398  // effectively we transform with the Kronecker product D x I,
1399  // rather than a matrix with D's on the diagonal (i.e. I x D,
1400  // where x is the Kronecker product). We'll set reorder_ to
1401  // true if we want to use this to transform in the time domain,
1402  // because the SpliceComponent splices blocks of e.g. MFCCs
1403  // together so each time is a dimension of the block.
1404 
1406 
1408 };
1409 
1410 
1414  public:
1416  virtual std::string Type() const { return "FixedLinearComponent"; }
1417  virtual std::string Info() const;
1418 
1419  void Init(const CuMatrixBase<BaseFloat> &matrix) { mat_ = matrix; }
1420 
1421  // InitFromString takes only the option matrix=<string>,
1422  // where the string is the filename of a Kaldi-format matrix to read.
1423  virtual void InitFromString(std::string args);
1424 
1425  virtual int32 InputDim() const { return mat_.NumCols(); }
1426  virtual int32 OutputDim() const { return mat_.NumRows(); }
1427  using Component::Propagate; // to avoid name hiding
1428  virtual void Propagate(const ChunkInfo &in_info,
1429  const ChunkInfo &out_info,
1430  const CuMatrixBase<BaseFloat> &in,
1431  CuMatrixBase<BaseFloat> *out) const;
1432  virtual void Backprop(const ChunkInfo &in_info,
1433  const ChunkInfo &out_info,
1434  const CuMatrixBase<BaseFloat> &in_value,
1435  const CuMatrixBase<BaseFloat> &out_value,
1436  const CuMatrixBase<BaseFloat> &out_deriv,
1437  Component *to_update, // may be identical to "this".
1438  CuMatrix<BaseFloat> *in_deriv) const;
1439  virtual bool BackpropNeedsInput() const { return false; }
1440  virtual bool BackpropNeedsOutput() const { return false; }
1441  virtual Component* Copy() const;
1442  virtual void Read(std::istream &is, bool binary);
1443  virtual void Write(std::ostream &os, bool binary) const;
1444  protected:
1445  friend class AffineComponent;
1447 
1449 };
1450 
1451 
1455  public:
1457  virtual std::string Type() const { return "FixedAffineComponent"; }
1458  virtual std::string Info() const;
1459 
1461  void Init(const CuMatrixBase<BaseFloat> &matrix);
1462 
1463  // InitFromString takes only the option matrix=<string>,
1464  // where the string is the filename of a Kaldi-format matrix to read.
1465  virtual void InitFromString(std::string args);
1466 
1467  virtual int32 InputDim() const { return linear_params_.NumCols(); }
1468  virtual int32 OutputDim() const { return linear_params_.NumRows(); }
1469  using Component::Propagate; // to avoid name hiding
1470  virtual void Propagate(const ChunkInfo &in_info,
1471  const ChunkInfo &out_info,
1472  const CuMatrixBase<BaseFloat> &in,
1473  CuMatrixBase<BaseFloat> *out) const;
1474  virtual void Backprop(const ChunkInfo &in_info,
1475  const ChunkInfo &out_info,
1476  const CuMatrixBase<BaseFloat> &in_value,
1477  const CuMatrixBase<BaseFloat> &out_value,
1478  const CuMatrixBase<BaseFloat> &out_deriv,
1479  Component *to_update, // may be identical to "this".
1480  CuMatrix<BaseFloat> *in_deriv) const;
1481  virtual bool BackpropNeedsInput() const { return false; }
1482  virtual bool BackpropNeedsOutput() const { return false; }
1483  virtual Component* Copy() const;
1484  virtual void Read(std::istream &is, bool binary);
1485  virtual void Write(std::ostream &os, bool binary) const;
1486 
1487  // Function to provide access to linear_params_.
1488  const CuMatrix<BaseFloat> &LinearParams() const { return linear_params_; }
1489  protected:
1490  friend class AffineComponent;
1493 
1495 };
1496 
1497 
1502  public:
1504  virtual std::string Type() const { return "FixedScaleComponent"; }
1505  virtual std::string Info() const;
1506 
1507  void Init(const CuVectorBase<BaseFloat> &scales);
1508 
1509  // InitFromString takes only the option scales=<string>,
1510  // where the string is the filename of a Kaldi-format matrix to read.
1511  virtual void InitFromString(std::string args);
1512 
1513  virtual int32 InputDim() const { return scales_.Dim(); }
1514  virtual int32 OutputDim() const { return scales_.Dim(); }
1515  using Component::Propagate; // to avoid name hiding
1516  virtual void Propagate(const ChunkInfo &in_info,
1517  const ChunkInfo &out_info,
1518  const CuMatrixBase<BaseFloat> &in,
1519  CuMatrixBase<BaseFloat> *out) const;
1520  virtual void Backprop(const ChunkInfo &in_info,
1521  const ChunkInfo &out_info,
1522  const CuMatrixBase<BaseFloat> &in_value,
1523  const CuMatrixBase<BaseFloat> &out_value,
1524  const CuMatrixBase<BaseFloat> &out_deriv,
1525  Component *to_update, // may be identical to "this".
1526  CuMatrix<BaseFloat> *in_deriv) const;
1527  virtual bool BackpropNeedsInput() const { return false; }
1528  virtual bool BackpropNeedsOutput() const { return false; }
1529  virtual Component* Copy() const;
1530  virtual void Read(std::istream &is, bool binary);
1531  virtual void Write(std::ostream &os, bool binary) const;
1532 
1533  protected:
1534  friend class AffineComponent; // necessary for collapse
1537 };
1538 
1543  public:
1545  virtual std::string Type() const { return "FixedBiasComponent"; }
1546  virtual std::string Info() const;
1547 
1548  void Init(const CuVectorBase<BaseFloat> &scales);
1549 
1550  // InitFromString takes only the option bias=<string>,
1551  // where the string is the filename of a Kaldi-format matrix to read.
1552  virtual void InitFromString(std::string args);
1553 
1554  virtual int32 InputDim() const { return bias_.Dim(); }
1555  virtual int32 OutputDim() const { return bias_.Dim(); }
1556  using Component::Propagate; // to avoid name hiding
1557  virtual void Propagate(const ChunkInfo &in_info,
1558  const ChunkInfo &out_info,
1559  const CuMatrixBase<BaseFloat> &in,
1560  CuMatrixBase<BaseFloat> *out) const;
1561  virtual void Backprop(const ChunkInfo &in_info,
1562  const ChunkInfo &out_info,
1563  const CuMatrixBase<BaseFloat> &in_value,
1564  const CuMatrixBase<BaseFloat> &out_value,
1565  const CuMatrixBase<BaseFloat> &out_deriv,
1566  Component *to_update, // may be identical to "this".
1567  CuMatrix<BaseFloat> *in_deriv) const ;
1568  virtual bool BackpropNeedsInput() const { return false; }
1569  virtual bool BackpropNeedsOutput() const { return false; }
1570  virtual Component* Copy() const;
1571  virtual void Read(std::istream &is, bool binary);
1572  virtual void Write(std::ostream &os, bool binary) const;
1573 
1574  protected:
1577 };
1578 
1579 
1585  public:
1592  void Init(int32 dim,
1593  BaseFloat dropout_proportion = 0.5,
1594  BaseFloat dropout_scale = 0.0);
1595  DropoutComponent(int32 dim, BaseFloat dp = 0.5, BaseFloat sc = 0.0) {
1596  Init(dim, dp, sc);
1597  }
1598  DropoutComponent(): dim_(0), dropout_proportion_(0.5) { }
1599  virtual int32 InputDim() const { return dim_; }
1600  virtual int32 OutputDim() const { return dim_; }
1601  virtual void InitFromString(std::string args);
1602 
1603  virtual void Read(std::istream &is, bool binary);
1604 
1605  virtual void Write(std::ostream &os, bool binary) const;
1606 
1607  virtual std::string Type() const { return "DropoutComponent"; }
1608 
1609  void SetDropoutScale(BaseFloat scale) { dropout_scale_ = scale; }
1610  virtual bool BackpropNeedsInput() const { return true; }
1611  virtual bool BackpropNeedsOutput() const { return true; }
1612  virtual Component* Copy() const;
1613  using Component::Propagate; // to avoid name hiding
1614  virtual void Propagate(const ChunkInfo &in_info,
1615  const ChunkInfo &out_info,
1616  const CuMatrixBase<BaseFloat> &in,
1617  CuMatrixBase<BaseFloat> *out) const;
1618  virtual void Backprop(const ChunkInfo &in_info,
1619  const ChunkInfo &out_info,
1620  const CuMatrixBase<BaseFloat> &in_value,
1621  const CuMatrixBase<BaseFloat> &out_value,
1622  const CuMatrixBase<BaseFloat> &out_deriv,
1623  Component *to_update, // may be identical to "this".
1624  CuMatrix<BaseFloat> *in_deriv) const;
1625  virtual std::string Info() const;
1626  private:
1629  BaseFloat dropout_scale_; // Set the scale that we scale "dropout_proportion_"
1630  // of the neurons by (default 0.0, but can be set arbitrarily close to 1.0).
1631 };
1632 
1636  public:
1637  void Init(int32 dim, BaseFloat noise_stddev);
1638  AdditiveNoiseComponent(int32 dim, BaseFloat stddev) { Init(dim, stddev); }
1639  AdditiveNoiseComponent(): dim_(0), stddev_(1.0) { }
1640  virtual int32 InputDim() const { return dim_; }
1641  virtual int32 OutputDim() const { return dim_; }
1642  virtual void InitFromString(std::string args);
1643 
1644  virtual void Read(std::istream &is, bool binary);
1645 
1646  virtual void Write(std::ostream &os, bool binary) const;
1647 
1648  virtual std::string Type() const { return "AdditiveNoiseComponent"; }
1649 
1650  virtual bool BackpropNeedsInput() const { return false; }
1651  virtual bool BackpropNeedsOutput() const { return false; }
1652  virtual Component* Copy() const {
1653  return new AdditiveNoiseComponent(dim_, stddev_);
1654  }
1655  using Component::Propagate; // to avoid name hiding
1656  virtual void Propagate(const ChunkInfo &in_info,
1657  const ChunkInfo &out_info,
1658  const CuMatrixBase<BaseFloat> &in,
1659  CuMatrixBase<BaseFloat> *out) const;
1660  virtual void Backprop(const ChunkInfo &in_info,
1661  const ChunkInfo &out_info,
1662  const CuMatrixBase<BaseFloat> &in_value,
1663  const CuMatrixBase<BaseFloat> &out_value,
1664  const CuMatrixBase<BaseFloat> &out_deriv,
1665  Component *to_update, // may be identical to "this".
1666  CuMatrix<BaseFloat> *in_deriv) const { *in_deriv = out_deriv; }
1667  private:
1670 };
1671 
1719  public:
1721  // constructor using another component
1723  // constructor using parameters
1725  const CuVectorBase<BaseFloat> &bias_params,
1726  BaseFloat learning_rate);
1727 
1728  int32 InputDim() const;
1729  int32 OutputDim() const;
1730  void Init(BaseFloat learning_rate, int32 input_dim, int32 output_dim,
1731  int32 patch_dim, int32 patch_step, int32 patch_stride,
1732  BaseFloat param_stddev, BaseFloat bias_stddev, bool appended_conv);
1733  void Init(BaseFloat learning_rate,
1734  int32 patch_dim, int32 patch_step, int32 patch_stride,
1735  std::string matrix_filename, bool appended_conv);
1736 
1737  // resize the component, setting the parameters to zero, while
1738  // leaving any other configuration values the same
1739  void Resize(int32 input_dim, int32 output_dim);
1740  std::string Info() const;
1741  void InitFromString(std::string args);
1742  std::string Type() const { return "Convolutional1dComponent"; }
1743  bool BackpropNeedsInput() const { return true; }
1744  bool BackpropNeedsOutput() const { return false; }
1745  using Component::Propagate; // to avoid name hiding
1746  void Propagate(const ChunkInfo &in_info,
1747  const ChunkInfo &out_info,
1748  const CuMatrixBase<BaseFloat> &in,
1749  CuMatrixBase<BaseFloat> *out) const;
1750  void Scale(BaseFloat scale);
1751  virtual void Add(BaseFloat alpha, const UpdatableComponent &other);
1752  virtual void Backprop(const ChunkInfo &in_info,
1753  const ChunkInfo &out_info,
1754  const CuMatrixBase<BaseFloat> &in_value,
1755  const CuMatrixBase<BaseFloat> &out_value,
1756  const CuMatrixBase<BaseFloat> &out_deriv,
1757  Component *to_update_in,
1758  CuMatrix<BaseFloat> *in_deriv) const;
1759  void SetZero(bool treat_as_gradient);
1760  void Read(std::istream &is, bool binary);
1761  void Write(std::ostream &os, bool binary) const;
1762  virtual BaseFloat DotProduct(const UpdatableComponent &other) const;
1763  Component* Copy() const;
1764  void PerturbParams(BaseFloat stddev);
1765  void SetParams(const VectorBase<BaseFloat> &bias,
1766  const MatrixBase<BaseFloat> &filter);
1767  const CuVector<BaseFloat> &BiasParams() { return bias_params_; }
1768  const CuMatrix<BaseFloat> &LinearParams() { return filter_params_; }
1769  int32 GetParameterDim() const;
1770  void Update(const CuMatrixBase<BaseFloat> &in_value,
1771  const CuMatrixBase<BaseFloat> &out_deriv);
1772 
1773  private:
1777 
1778  static void ReverseIndexes(const std::vector<int32> &forward_indexes,
1779  int32 input_dim,
1780  std::vector<std::vector<int32> > *backward_indexes);
1781  static void RearrangeIndexes(const std::vector<std::vector<int32> > &in,
1782  std::vector<std::vector<int32> > *out);
1783 
1784  const Convolutional1dComponent &operator = (const Convolutional1dComponent &other); // Disallow.
1787  // When appending convolutional1dcomponents, appended_conv_ should be
1788  // set ture for the appended convolutional1dcomponents.
1791 };
1792 
1793 
1797 bool ParseFromString(const std::string &name, std::string *string,
1798  int32 *param);
1800 bool ParseFromString(const std::string &name, std::string *string,
1801  BaseFloat *param);
1804 bool ParseFromString(const std::string &name, std::string *string,
1805  std::vector<int32> *param);
1808 bool ParseFromString(const std::string &name, std::string *string,
1809  bool *param);
1810 
1811 
1812 } // namespace nnet2
1813 } // namespace kaldi
1814 
1815 
1816 #endif
virtual bool BackpropNeedsInput() const
virtual bool BackpropNeedsOutput() const
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
This kind of Component is a base-class for things like sigmoid and softmax.
virtual Component * Copy() const
Copy component (deep copy).
virtual bool BackpropNeedsInput() const
virtual std::string Type() const
virtual int32 InputDim() const
Get size of input vectors.
virtual int32 InputDim() const
Get size of input vectors.
std::vector< int32 > offsets_
virtual Component * Copy() const
Copy component (deep copy).
CuVector< BaseFloat > bias_params_
FixedAffineComponent is an affine transform that is supplied at network initialization time and is no...
SigmoidComponent(const SigmoidComponent &other)
void Check() const
Checks that the data in the ChunkInfo is valid, and die if not.
virtual bool BackpropNeedsInput() const
virtual int32 OutputDim() const
Get size of output vectors.
virtual int32 OutputDim() const
Get size of output vectors.
virtual int32 OutputDim() const
Get size of output vectors.
virtual bool BackpropNeedsInput() const
virtual int32 InputDim() const
Get size of input vectors.
virtual Component * Copy() const
Copy component (deep copy).
virtual int32 OutputDim() const
Get size of output vectors.
void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrix< BaseFloat > *out) const
A non-virtual propagate function that first resizes output if necessary.
virtual int32 OutputDim() const
Get size of output vectors.
virtual int32 InputDim() const
Get size of input vectors.
const CuMatrix< BaseFloat > & LinearParams() const
Base class which provides matrix operations not involving resizing or allocation. ...
Definition: kaldi-matrix.h:49
PermuteComponent(const std::vector< int32 > &reorder)
virtual std::string Type() const
void Init(const CuMatrixBase< BaseFloat > &matrix)
std::vector< int32 > context_
virtual int32 OutputDim() const
Get size of output vectors.
virtual std::vector< int32 > Context() const
Return a vector describing the temporal context this component requires for each frame of output...
virtual Component * Copy() const
Copy component (deep copy).
virtual bool BackpropNeedsInput() const
Abstract class, basic element of the network, it is a box with defined inputs, outputs, and tranformation functions interface.
virtual bool BackpropNeedsOutput() const
MaxpoolingComponent(int32 input_dim, int32 output_dim, int32 pool_size, int32 pool_stride)
virtual std::string Type() const
virtual bool BackpropNeedsInput() const
virtual int32 InputDim() const
Get size of input vectors.
virtual std::string Type() const
const CuVector< BaseFloat > & BiasParams()
virtual int32 InputDim() const
Get size of input vectors.
virtual bool BackpropNeedsInput() const
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
ScaleComponent(const ScaleComponent &other)
virtual int32 OutputDim() const
Get size of output vectors.
virtual bool BackpropNeedsOutput() const
UpdatableComponent(const UpdatableComponent &other)
kaldi::int32 int32
virtual int32 InputDim() const
Get size of input vectors.
virtual int32 OutputDim() const
Get size of output vectors.
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
PermuteComponent does a permutation of the dimensions (by default, a fixed random permutation...
virtual std::string Type() const
This is a bit similar to dropout but adding (not multiplying) Gaussian noise with a given standard de...
#define KALDI_DISALLOW_COPY_AND_ASSIGN(type)
Definition: kaldi-utils.h:121
virtual int32 OutputDim() const
Get size of output vectors.
virtual bool BackpropNeedsOutput() const
virtual bool BackpropNeedsInput() const
virtual int32 InputDim() const
Get size of input vectors.
virtual void Vectorize(VectorBase< BaseFloat > *params) const
Turns the parameters into vector form.
virtual std::vector< int32 > Context() const
Return a vector describing the temporal context this component requires for each frame of output...
RectifiedLinearComponent(const RectifiedLinearComponent &other)
virtual std::vector< int32 > Context() const
Return a vector describing the temporal context this component requires for each frame of output...
virtual Component * Copy() const
Copy component (deep copy).
virtual std::string Type() const
void SetDropoutScale(BaseFloat scale)
ChunkInfo(int32 feat_dim, int32 num_chunks, const std::vector< int32 > offsets)
virtual bool BackpropNeedsInput() const
virtual int32 InputDim() const
Get size of input vectors.
virtual int32 InputDim() const
Get size of input vectors.
virtual void Update(const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_deriv)
static const BaseFloat kNormFloor
virtual bool BackpropNeedsInput() const
virtual bool BackpropNeedsOutput() const
Discrete cosine transform.
TanhComponent(const TanhComponent &other)
virtual int32 InputDim() const
Get size of input vectors.
FixedScaleComponent applies a fixed per-element scale; it&#39;s similar to the Rescale component in the n...
virtual bool BackpropNeedsOutput() const
virtual int32 OutputDim() const
Get size of output vectors.
bool ParseFromString(const std::string &name, std::string *string, int32 *param)
Functions used in Init routines.
int32 GetOffset(int32 index) const
virtual bool BackpropNeedsOutput() const
virtual bool BackpropNeedsOutput() const
virtual bool BackpropNeedsInput() const
virtual std::string Type() const
virtual std::string Type() const
void Init(BaseFloat learning_rate)
virtual Component * Copy() const
Copy component (deep copy).
DropoutComponent(int32 dim, BaseFloat dp=0.5, BaseFloat sc=0.0)
virtual bool BackpropNeedsOutput() const
CuMatrix< BaseFloat > dct_mat_
virtual int32 InputDim() const
Get size of input vectors.
virtual bool BackpropNeedsOutput() const
Take the absoute values of an input vector to a power.
virtual int32 InputDim() const
Get size of input vectors.
LogSoftmaxComponent(const LogSoftmaxComponent &other)
virtual bool BackpropNeedsOutput() const
virtual std::string Type() const
virtual int32 OutputDim() const
Get size of output vectors.
virtual std::string Type() const
BaseFloat learning_rate_
learning rate (0.0..0.01)
This Component, if present, randomly zeroes half of the inputs and multiplies the other half by two...
ChunkInfo is a class whose purpose is to describe the structure of matrices holding features...
virtual std::string Type() const
virtual bool BackpropNeedsInput() const
virtual int32 InputDim() const
Get size of input vectors.
This is as SpliceComponent but outputs the max of any of the inputs (taking the max across time)...
virtual bool BackpropNeedsInput() const
virtual void Update(const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_deriv)
virtual int32 OutputDim() const
Get size of output vectors.
const CuVector< double > & ValueSum() const
virtual int32 OutputDim() const
Get size of output vectors.
virtual std::string Type() const
SoftmaxComponent(const SoftmaxComponent &other)
virtual bool BackpropNeedsInput() const
BaseFloat DotProduct(const Nnet &nnet1, const Nnet &nnet2)
Returns dot product between two networks of the same structure (calls the DotProduct functions of the...
Definition: nnet-utils.cc:250
virtual std::string Type() const
virtual bool BackpropNeedsOutput() const
virtual std::string Type() const
virtual std::string Type() const
NormalizeComponent(const NormalizeComponent &other)
SoftHingeComponent(const SoftHingeComponent &other)
FixedBiasComponent applies a fixed per-element bias; it&#39;s similar to the AddShift component in the nn...
virtual void SetIndex(int32 index)
virtual int32 Index() const
Returns the index in the sequence of layers in the neural net; intended only to be used in debugging ...
int32 NumRows() const
Returns the number of rows that we expect the feature matrix to have.
virtual bool BackpropNeedsInput() const
int32 GetIndex(int32 offset) const
virtual bool BackpropNeedsInput() const
AdditiveNoiseComponent(int32 dim, BaseFloat stddev)
virtual bool BackpropNeedsOutput() const
virtual bool BackpropNeedsOutput() const
CuMatrix< BaseFloat > linear_params_
virtual Component * Copy() const
Copy component (deep copy).
void CheckSize(const CuMatrixBase< BaseFloat > &mat) const
Checks that the matrix has the size we expect, and die if not.
MaxPoolingComponent : Maxpooling component was firstly used in ConvNet for selecting an representativ...
virtual int32 InputDim() const
Get size of input vectors.
virtual std::string Type() const
virtual bool BackpropNeedsOutput() const
virtual Component * Copy() const
Copy component (deep copy).
virtual int32 InputDim() const
Get size of input vectors.
FixedLinearComponent is a linear transform that is supplied at network initialization time and is not...
virtual bool BackpropNeedsInput() const
virtual int32 OutputDim() const
Get size of output vectors.
Matrix for CUDA computing.
Definition: matrix-common.h:69
virtual Component * Copy() const
Copy component (deep copy).
MatrixIndexT NumCols() const
Definition: cu-matrix.h:216
virtual int32 GetParameterDim() const
The following new virtual function returns the total dimension of the parameters in this class...
PnormComponent(int32 input_dim, int32 output_dim, BaseFloat p)
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const =0
Perform forward pass propagation Input->Output.
virtual bool BackpropNeedsInput() const
virtual std::string Type() const
virtual std::string Type() const
virtual std::string Type() const
virtual void UnVectorize(const VectorBase< BaseFloat > &params)
Converts the parameters from vector form.
virtual bool BackpropNeedsOutput() const
virtual int32 OutputDim() const
Get size of output vectors.
virtual bool BackpropNeedsOutput() const
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
const CuMatrix< BaseFloat > & LinearParams()
std::vector< int32 > reorder_
virtual bool BackpropNeedsOutput() const
virtual bool BackpropNeedsOutput() const
virtual bool BackpropNeedsInput() const
Keywords: natural gradient descent, NG-SGD, naturalgradient.
virtual bool BackpropNeedsOutput() const
virtual int32 OutputDim() const
Get size of output vectors.
virtual int32 OutputDim() const
Get size of output vectors.
CuVector< BaseFloat > bias_params_
virtual int32 InputDim() const
Get size of input vectors.
void PerturbParams(BaseFloat stddev, Nnet *nnet)
Calls PerturbParams (with the given stddev) on all updatable components of the nnet.
Definition: nnet-utils.cc:199
PowerComponent(int32 dim, BaseFloat power=2)
virtual Component * Copy() const
Copy component (deep copy).
virtual std::string Type() const
const CuVector< double > & DerivSum() const
virtual bool BackpropNeedsInput() const
const CuMatrix< BaseFloat > & LinearParams()
virtual bool BackpropNeedsOutput() const
virtual Component * Copy() const
Copy component (deep copy).
virtual std::string Type() const
CuMatrix< BaseFloat > linear_params_
virtual int32 OutputDim() const
Get size of output vectors.
Keywords for search: natural gradient, naturalgradient, NG-SGD.
void SetLearningRate(BaseFloat lrate)
Sets the learning rate of gradient descent.
virtual std::string Type() const
Convolutional1dComponent implements convolution over frequency axis.
virtual std::string Type() const
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:215
Provides a vector abstraction class.
Definition: kaldi-vector.h:41
MaxoutComponent(int32 input_dim, int32 output_dim)
Splices a context window of frames together [over time].
virtual Component * Copy() const
Copy component (deep copy).
CuVector< BaseFloat > bias_params_
UpdatableComponent(BaseFloat learning_rate)
CuMatrix< BaseFloat > linear_params_
const CuVector< BaseFloat > & BiasParams()
BaseFloat LearningRate() const
Gets the learning rate of gradient descent.
virtual bool BackpropNeedsOutput() const
int32 NumCols() const
Returns the number of columns that we expect the feature matrix to have.
ChunkInfo(int32 feat_dim, int32 num_chunks, int32 first_offset, int32 last_offset)
void Resize(MatrixIndexT rows, MatrixIndexT cols, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Allocate the memory.
Definition: cu-matrix.cc:50
virtual bool BackpropNeedsInput() const
virtual bool BackpropNeedsInput() const
virtual std::string Type() const
virtual bool BackpropNeedsOutput() const
ScaleComponent(int32 dim, BaseFloat scale)
void RearrangeIndexes(const std::vector< std::vector< int32 > > &in, std::vector< std::vector< int32 > > *out)
virtual bool BackpropNeedsInput() const
Vector for CUDA computing.
Definition: matrix-common.h:72
CuRand< BaseFloat > random_generator_
virtual bool BackpropNeedsOutput() const
Class UpdatableComponent is a Component which has trainable parameters and contains some global param...
virtual std::string Type() const
virtual bool BackpropNeedsOutput() const
virtual int32 InputDim() const
Get size of input vectors.
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
virtual bool BackpropNeedsInput() const
virtual Component * Copy() const
Copy component (deep copy).
virtual int32 InputDim() const
Get size of input vectors.