All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MaxPoolingComponent Class Reference

MaxPoolingComponent : The input/output matrices are split to submatrices with width 'pool_stride_'. More...

#include <nnet-max-pooling-component.h>

Inheritance diagram for MaxPoolingComponent:
Collaboration diagram for MaxPoolingComponent:

Public Member Functions

 MaxPoolingComponent (int32 dim_in, int32 dim_out)
 
 ~MaxPoolingComponent ()
 
ComponentCopy () const
 Copy component (deep copy),. More...
 
ComponentType GetType () const
 Get Type Identification of the component,. More...
 
void InitData (std::istream &is)
 Virtual interface for initialization and I/O,. More...
 
void ReadData (std::istream &is, bool binary)
 Reads the component content. More...
 
void WriteData (std::ostream &os, bool binary) const
 Writes the component content. More...
 
void PropagateFnc (const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out)
 Abstract interface for propagation/backpropagation. More...
 
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...) More...
 
- Public Member Functions inherited from Component
 Component (int32 input_dim, int32 output_dim)
 Generic interface of a component,. More...
 
virtual ~Component ()
 
virtual bool IsUpdatable () const
 Check if componeny has 'Updatable' interface (trainable components),. More...
 
virtual bool IsMultistream () const
 Check if component has 'Recurrent' interface (trainable and recurrent),. More...
 
int32 InputDim () const
 Get the dimension of the input,. More...
 
int32 OutputDim () const
 Get the dimension of the output,. More...
 
void Propagate (const CuMatrixBase< BaseFloat > &in, CuMatrix< BaseFloat > *out)
 Perform forward-pass propagation 'in' -> 'out',. More...
 
void Backpropagate (const CuMatrixBase< BaseFloat > &in, const CuMatrixBase< BaseFloat > &out, const CuMatrixBase< BaseFloat > &out_diff, CuMatrix< BaseFloat > *in_diff)
 Perform backward-pass propagation 'out_diff' -> 'in_diff'. More...
 
void Write (std::ostream &os, bool binary) const
 Write the component to a stream,. More...
 
virtual std::string Info () const
 Print some additional info (after <ComponentName> and the dims),. More...
 
virtual std::string InfoGradient () const
 Print some additional info about gradient (after <...> and dims),. More...
 

Private Attributes

int32 pool_size_
 
int32 pool_step_
 
int32 pool_stride_
 

Additional Inherited Members

- Public Types inherited from Component
enum  ComponentType {
  kUnknown = 0x0, kUpdatableComponent = 0x0100, kAffineTransform, kLinearTransform,
  kConvolutionalComponent, kConvolutional2DComponent, kLstmProjected, kBlstmProjected,
  kRecurrentComponent, kActivationFunction = 0x0200, kSoftmax, kHiddenSoftmax,
  kBlockSoftmax, kSigmoid, kTanh, kParametricRelu,
  kDropout, kLengthNormComponent, kTranform = 0x0400, kRbm,
  kSplice, kCopy, kTranspose, kBlockLinearity,
  kAddShift, kRescale, kKlHmm = 0x0800, kSentenceAveragingComponent,
  kSimpleSentenceAveragingComponent, kAveragePoolingComponent, kAveragePooling2DComponent, kMaxPoolingComponent,
  kMaxPooling2DComponent, kFramePoolingComponent, kParallelComponent, kMultiBasisComponent
}
 Component type identification mechanism,. More...
 
- Static Public Member Functions inherited from Component
static const char * TypeToMarker (ComponentType t)
 Converts component type to marker,. More...
 
static ComponentType MarkerToType (const std::string &s)
 Converts marker to component type (case insensitive),. More...
 
static ComponentInit (const std::string &conf_line)
 Initialize component from a line in config file,. More...
 
static ComponentRead (std::istream &is, bool binary)
 Read the component from a stream (static method),. More...
 
- Static Public Attributes inherited from Component
static const struct key_value kMarkerMap []
 The table with pairs of Component types and markers (defined in nnet-component.cc),. More...
 
- Protected Attributes inherited from Component
int32 input_dim_
 Data members,. More...
 
int32 output_dim_
 Dimension of the output of the Component,. More...
 

Detailed Description

MaxPoolingComponent : The input/output matrices are split to submatrices with width 'pool_stride_'.

The pooling is done over 3rd axis, of the set of 2d matrices. Our pooling supports overlaps, overlaps occur when (pool_step_ < pool_size_).

Definition at line 40 of file nnet-max-pooling-component.h.

Constructor & Destructor Documentation

MaxPoolingComponent ( int32  dim_in,
int32  dim_out 
)
inline

Definition at line 42 of file nnet-max-pooling-component.h.

Referenced by MaxPoolingComponent::Copy().

42  :
43  Component(dim_in, dim_out),
44  pool_size_(0),
45  pool_step_(0),
46  pool_stride_(0)
47  { }
Component(int32 input_dim, int32 output_dim)
Generic interface of a component,.
~MaxPoolingComponent ( )
inline

Definition at line 49 of file nnet-max-pooling-component.h.

50  { }

Member Function Documentation

void BackpropagateFnc ( const CuMatrixBase< BaseFloat > &  in,
const CuMatrixBase< BaseFloat > &  out,
const CuMatrixBase< BaseFloat > &  out_diff,
CuMatrixBase< BaseFloat > *  in_diff 
)
inlinevirtual

Backward pass transformation (to be implemented by descending class...)

Implements Component.

Definition at line 121 of file nnet-max-pooling-component.h.

References CuMatrixBase< Real >::ColRange(), CuMatrixBase< Real >::EqualElementMask(), Component::input_dim_, KALDI_ASSERT, MaxPoolingComponent::pool_size_, MaxPoolingComponent::pool_step_, MaxPoolingComponent::pool_stride_, and CuMatrixBase< Real >::SetZero().

124  {
125  // useful dims
126  int32 num_patches = input_dim_ / pool_stride_;
127  int32 num_pools = 1 + (num_patches - pool_size_) / pool_step_;
128 
129  //
130  // here we note how many diff matrices are summed for each input patch,
131  std::vector<int32> patch_summands(num_patches, 0);
132  // this metainfo will be used to divide diff of patches
133  // used in more than one pool.
134  //
135 
136  in_diff->SetZero(); // reset
137 
138  for (int32 q = 0; q<num_pools; q++) { // sum
139  for (int32 r = 0; r<pool_size_; r++) {
140  int32 p = r + q * pool_step_; // patch number
141  //
142  CuSubMatrix<BaseFloat> in_p(in.ColRange(p*pool_stride_, pool_stride_));
143  CuSubMatrix<BaseFloat> out_q(out.ColRange(q*pool_stride_, pool_stride_));
144  //
145  CuSubMatrix<BaseFloat> tgt(in_diff->ColRange(p*pool_stride_, pool_stride_));
146  CuMatrix<BaseFloat> src(out_diff.ColRange(q*pool_stride_, pool_stride_));
147 
148  // Only the pool-inputs with 'max-values' are used to back-propagate into,
149  // the rest of derivatives is zeroed-out by a mask.
150  CuMatrix<BaseFloat> mask;
151  in_p.EqualElementMask(out_q, &mask);
152  src.MulElements(mask);
153  tgt.AddMat(1.0, src);
154 
155  patch_summands[p] += 1;
156  }
157  }
158 
159  // divide diff by #summands (compensate for patches used in more pools)
160  for (int32 p = 0; p < num_patches; p++) {
161  CuSubMatrix<BaseFloat> tgt(in_diff->ColRange(p*pool_stride_, pool_stride_));
162  KALDI_ASSERT(patch_summands[p] > 0); // patch at least in one pool
163  tgt.Scale(1.0/patch_summands[p]);
164  }
165  }
int32 input_dim_
Data members,.
CuSubMatrix< Real > ColRange(const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Definition: cu-matrix.h:544
void SetZero()
Math operations, some calling kernels.
Definition: cu-matrix.cc:474
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
Component* Copy ( ) const
inlinevirtual

Copy component (deep copy),.

Implements Component.

Definition at line 52 of file nnet-max-pooling-component.h.

References MaxPoolingComponent::MaxPoolingComponent().

52 { return new MaxPoolingComponent(*this); }
MaxPoolingComponent(int32 dim_in, int32 dim_out)
ComponentType GetType ( ) const
inlinevirtual

Get Type Identification of the component,.

Implements Component.

Definition at line 53 of file nnet-max-pooling-component.h.

References Component::kMaxPoolingComponent.

void InitData ( std::istream &  is)
inlinevirtual

Virtual interface for initialization and I/O,.

Initialize internal data of a component

Reimplemented from Component.

Definition at line 55 of file nnet-max-pooling-component.h.

References KALDI_ASSERT, KALDI_ERR, MaxPoolingComponent::pool_size_, MaxPoolingComponent::pool_step_, MaxPoolingComponent::pool_stride_, kaldi::ReadBasicType(), and kaldi::ReadToken().

55  {
56  // parse config
57  std::string token;
58  while (is >> std::ws, !is.eof()) {
59  ReadToken(is, false, &token);
60  if (token == "<PoolSize>") ReadBasicType(is, false, &pool_size_);
61  else if (token == "<PoolStep>") ReadBasicType(is, false, &pool_step_);
62  else if (token == "<PoolStride>") ReadBasicType(is, false, &pool_stride_);
63  else KALDI_ERR << "Unknown token " << token << ", a typo in config?"
64  << " (PoolSize|PoolStep|PoolStride)";
65  }
66  // check
67  KALDI_ASSERT(pool_size_ != 0 && pool_step_ != 0 && pool_stride_ != 0);
68  }
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 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
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void PropagateFnc ( const CuMatrixBase< BaseFloat > &  in,
CuMatrixBase< BaseFloat > *  out 
)
inlinevirtual

Abstract interface for propagation/backpropagation.

Forward pass transformation (to be implemented by descending class...)

Implements Component.

Definition at line 103 of file nnet-max-pooling-component.h.

References CuMatrixBase< Real >::ColRange(), Component::input_dim_, MaxPoolingComponent::pool_size_, MaxPoolingComponent::pool_step_, MaxPoolingComponent::pool_stride_, and CuMatrixBase< Real >::Set().

104  {
105  // useful dims
106  int32 num_patches = input_dim_ / pool_stride_;
107  int32 num_pools = 1 + (num_patches - pool_size_) / pool_step_;
108 
109  // do the max-pooling (pools indexed by q)
110  for (int32 q = 0; q < num_pools; q++) {
111  // get output buffer of the pool
112  CuSubMatrix<BaseFloat> pool(out->ColRange(q*pool_stride_, pool_stride_));
113  pool.Set(-1e20); // reset (large negative value)
114  for (int32 r = 0; r < pool_size_; r++) { // max
115  int32 p = r + q * pool_step_; // p = input patch
116  pool.Max(in.ColRange(p*pool_stride_, pool_stride_));
117  }
118  }
119  }
int32 input_dim_
Data members,.
CuSubMatrix< Real > ColRange(const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Definition: cu-matrix.h:544
void ReadData ( std::istream &  is,
bool  binary 
)
inlinevirtual

Reads the component content.

Reimplemented from Component.

Definition at line 70 of file nnet-max-pooling-component.h.

References kaldi::ExpectToken(), Component::input_dim_, KALDI_ASSERT, Component::output_dim_, MaxPoolingComponent::pool_size_, MaxPoolingComponent::pool_step_, MaxPoolingComponent::pool_stride_, and kaldi::ReadBasicType().

70  {
71  // pooling hyperparameters
72  ExpectToken(is, binary, "<PoolSize>");
73  ReadBasicType(is, binary, &pool_size_);
74  ExpectToken(is, binary, "<PoolStep>");
75  ReadBasicType(is, binary, &pool_step_);
76  ExpectToken(is, binary, "<PoolStride>");
77  ReadBasicType(is, binary, &pool_stride_);
78 
79  //
80  // Sanity checks:
81  //
82  // number of patches:
84  int32 num_patches = input_dim_ / pool_stride_;
85  // number of pools:
86  KALDI_ASSERT((num_patches - pool_size_) % pool_step_ == 0);
87  int32 num_pools = 1 + (num_patches - pool_size_) / pool_step_;
88  // check output dim:
89  KALDI_ASSERT(output_dim_ == num_pools * pool_stride_);
90  //
91  }
int32 input_dim_
Data members,.
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 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:188
int32 output_dim_
Dimension of the output of the Component,.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void WriteData ( std::ostream &  os,
bool  binary 
) const
inlinevirtual

Writes the component content.

Reimplemented from Component.

Definition at line 93 of file nnet-max-pooling-component.h.

References MaxPoolingComponent::pool_size_, MaxPoolingComponent::pool_step_, MaxPoolingComponent::pool_stride_, kaldi::WriteBasicType(), and kaldi::WriteToken().

93  {
94  // pooling hyperparameters
95  WriteToken(os, binary, "<PoolSize>");
96  WriteBasicType(os, binary, pool_size_);
97  WriteToken(os, binary, "<PoolStep>");
98  WriteBasicType(os, binary, pool_step_);
99  WriteToken(os, binary, "<PoolStride>");
100  WriteBasicType(os, binary, pool_stride_);
101  }
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
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

Member Data Documentation


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