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

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

#include <nnet-average-pooling-2d-component.h>

Inheritance diagram for AveragePooling2DComponent:
Collaboration diagram for AveragePooling2DComponent:

Public Member Functions

 AveragePooling2DComponent (int32 dim_in, int32 dim_out)
 
 ~AveragePooling2DComponent ()
 
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 fmap_x_len_
 
int32 fmap_y_len_
 
int32 pool_x_len_
 
int32 pool_y_len_
 
int32 pool_x_step_
 
int32 pool_y_step_
 

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

AveragePoolingComponent : 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 41 of file nnet-average-pooling-2d-component.h.

Constructor & Destructor Documentation

Definition at line 49 of file nnet-average-pooling-2d-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 151 of file nnet-average-pooling-2d-component.h.

References CuMatrixBase< Real >::AddMat(), CuMatrixBase< Real >::ColRange(), AveragePooling2DComponent::fmap_x_len_, AveragePooling2DComponent::fmap_y_len_, rnnlm::i, Component::input_dim_, rnnlm::j, KALDI_ASSERT, rnnlm::n, AveragePooling2DComponent::pool_x_len_, AveragePooling2DComponent::pool_x_step_, AveragePooling2DComponent::pool_y_len_, AveragePooling2DComponent::pool_y_step_, CuMatrixBase< Real >::Scale(), and CuMatrixBase< Real >::SetZero().

154  {
155  // useful dims
156  int32 num_input_fmaps = input_dim_ / (fmap_x_len_ * fmap_y_len_);
157  int32 inp_fmap_size = fmap_x_len_ * fmap_y_len_;
158  //
159  // here we note how many diff matrices are summed for each input patch,
160  std::vector<int32> patch_summands(inp_fmap_size, 0);
161  // this metainfo will be used to divide diff of patches
162  // used in more than one pool.
163  //
164 
165  in_diff->SetZero(); // reset
166  int out_fmap_cnt = 0;
167  for (int32 m = 0; m < fmap_x_len_-pool_x_len_+1; m = m+pool_x_step_) {
168  for (int32 n = 0; n < fmap_y_len_-pool_y_len_+1; n = n+pool_y_step_) {
169  int32 st = 0;
170  st = (m * fmap_y_len_ + n) * num_input_fmaps;
171  CuSubMatrix<BaseFloat> src(out_diff.ColRange(out_fmap_cnt * num_input_fmaps, num_input_fmaps));
172  for (int32 i = 0; i < pool_x_len_; i++) {
173  for (int32 j = 0; j < pool_y_len_; j++) {
174  int32 c = 0;
175  c = st + i * (num_input_fmaps * fmap_y_len_)
176  + j * num_input_fmaps;
177  CuSubMatrix<BaseFloat> tgt(in_diff->ColRange(c, num_input_fmaps));
178  tgt.AddMat(1.0, src);
179  patch_summands[c / num_input_fmaps] += 1;
180  }
181  }
182  out_fmap_cnt++;
183  }
184  }
185 
186  // divide diff by average-pooling-dim (derivative of averaging)
187  in_diff->Scale(1.0 / (pool_x_len_ * pool_y_len_));
188 
189  // divide diff by #summands (compensate for patches used in more pools)
190  for (int i = 0; i < fmap_x_len_; i++) {
191  for (int32 j = 0; j < fmap_y_len_; j++) {
192  int32 c = i * fmap_y_len_ + j;
193  CuSubMatrix<BaseFloat> tgt(in_diff->ColRange(c*num_input_fmaps, num_input_fmaps));
194  KALDI_ASSERT(patch_summands[c] > 0); // patch at least in one pool
195  tgt.Scale(1.0 / patch_summands[c]);
196  }
197  }
198  }
int32 input_dim_
Data members,.
void Scale(Real value)
Definition: cu-matrix.cc:608
CuSubMatrix< Real > ColRange(const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Definition: cu-matrix.h:544
struct rnnlm::@11::@12 n
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-average-pooling-2d-component.h.

References AveragePooling2DComponent::AveragePooling2DComponent().

52 { return new AveragePooling2DComponent(*this); }
ComponentType GetType ( ) const
inlinevirtual

Get Type Identification of the component,.

Implements Component.

Definition at line 53 of file nnet-average-pooling-2d-component.h.

References Component::kAveragePooling2DComponent.

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-average-pooling-2d-component.h.

References AveragePooling2DComponent::fmap_x_len_, AveragePooling2DComponent::fmap_y_len_, KALDI_ASSERT, KALDI_ERR, AveragePooling2DComponent::pool_x_len_, AveragePooling2DComponent::pool_x_step_, AveragePooling2DComponent::pool_y_len_, AveragePooling2DComponent::pool_y_step_, 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 == "<FmapXLen>") ReadBasicType(is, false, &fmap_x_len_);
61  else if (token == "<FmapYLen>") ReadBasicType(is, false, &fmap_y_len_);
62  else if (token == "<PoolXLen>") ReadBasicType(is, false, &pool_x_len_);
63  else if (token == "<PoolYLen>") ReadBasicType(is, false, &pool_y_len_);
64  else if (token == "<PoolXStep>") ReadBasicType(is, false, &pool_x_step_);
65  else if (token == "<PoolYStep>") ReadBasicType(is, false, &pool_y_step_);
66  else KALDI_ERR << "Unknown token " << token << ", a typo in config?"
67  << " (FmapXLen|FmapYLen|PoolXLen|PoolYLen|PoolXStep|PoolYStep)";
68  }
69  // check
73  }
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 126 of file nnet-average-pooling-2d-component.h.

References CuMatrixBase< Real >::ColRange(), AveragePooling2DComponent::fmap_x_len_, AveragePooling2DComponent::fmap_y_len_, rnnlm::i, Component::input_dim_, rnnlm::j, rnnlm::n, AveragePooling2DComponent::pool_x_len_, AveragePooling2DComponent::pool_x_step_, AveragePooling2DComponent::pool_y_len_, AveragePooling2DComponent::pool_y_step_, and CuMatrixBase< Real >::SetZero().

127  {
128  // useful dims
129  int32 num_input_fmaps = input_dim_ / (fmap_x_len_ * fmap_y_len_);
130  int out_fmap_cnt = 0;
131  for (int32 m = 0; m < fmap_x_len_-pool_x_len_+1; m = m+pool_x_step_) {
132  for (int32 n = 0; n < fmap_y_len_-pool_y_len_+1; n = n+pool_y_step_) {
133  int32 st = 0;
134  st = (m * fmap_y_len_ + n) * num_input_fmaps;
135  CuSubMatrix<BaseFloat> pool(out->ColRange(out_fmap_cnt * num_input_fmaps, num_input_fmaps));
136  pool.SetZero(); // reset
137  for (int32 i = 0; i < pool_x_len_; i++) {
138  for (int32 j = 0; j < pool_y_len_; j++) {
139  int32 c = 0;
140  c = st + i * (num_input_fmaps * fmap_y_len_)
141  + j * num_input_fmaps;
142  pool.AddMat(1.0, in.ColRange(c, num_input_fmaps));
143  }
144  }
145  pool.Scale(1.0 / (pool_x_len_ * pool_y_len_));
146  out_fmap_cnt++;
147  }
148  }
149  }
int32 input_dim_
Data members,.
CuSubMatrix< Real > ColRange(const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Definition: cu-matrix.h:544
struct rnnlm::@11::@12 n
void ReadData ( std::istream &  is,
bool  binary 
)
inlinevirtual

Reads the component content.

Reimplemented from Component.

Definition at line 75 of file nnet-average-pooling-2d-component.h.

References kaldi::ExpectToken(), AveragePooling2DComponent::fmap_x_len_, AveragePooling2DComponent::fmap_y_len_, Component::input_dim_, KALDI_ASSERT, KALDI_LOG, Component::output_dim_, AveragePooling2DComponent::pool_x_len_, AveragePooling2DComponent::pool_x_step_, AveragePooling2DComponent::pool_y_len_, AveragePooling2DComponent::pool_y_step_, and kaldi::ReadBasicType().

75  {
76  // pooling hyperparameters
77  ExpectToken(is, binary, "<FmapXLen>");
78  ReadBasicType(is, binary, &fmap_x_len_);
79  ExpectToken(is, binary, "<FmapYLen>");
80  ReadBasicType(is, binary, &fmap_y_len_);
81  ExpectToken(is, binary, "<PoolXLen>");
82  ReadBasicType(is, binary, &pool_x_len_);
83  ExpectToken(is, binary, "<PoolYLen>");
84  ReadBasicType(is, binary, &pool_y_len_);
85  ExpectToken(is, binary, "<PoolXStep>");
86  ReadBasicType(is, binary, &pool_x_step_);
87  ExpectToken(is, binary, "<PoolYStep>");
88  ReadBasicType(is, binary, &pool_y_step_);
89 
90  //
91  // Sanity checks:
92  //
93  // input sanity checks
94  // input_dim_ should be multiple of (fmap_x_len_ * fmap_y_len_)
96  int32 num_input_fmaps = input_dim_ / (fmap_x_len_ * fmap_y_len_);
97  KALDI_LOG << "num_fmaps " << num_input_fmaps;
98  // check if step is in sync with fmap_len and filt_len
101  int32 out_fmap_x_len = (fmap_x_len_ - pool_x_len_)/pool_x_step_ + 1;
102  int32 out_fmap_y_len = (fmap_y_len_ - pool_y_len_)/pool_y_step_ + 1;
103  // int32 out_fmap_size = out_fmap_x_len*out_fmap_y_len;
104  // output sanity checks
105  KALDI_ASSERT(output_dim_ % (out_fmap_x_len * out_fmap_y_len) == 0);
106  int32 num_output_fmaps = output_dim_ / (out_fmap_x_len * out_fmap_y_len);
107  KALDI_ASSERT(num_input_fmaps == num_output_fmaps);
108  }
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
#define KALDI_LOG
Definition: kaldi-error.h:133
void WriteData ( std::ostream &  os,
bool  binary 
) const
inlinevirtual

Writes the component content.

Reimplemented from Component.

Definition at line 110 of file nnet-average-pooling-2d-component.h.

References AveragePooling2DComponent::fmap_x_len_, AveragePooling2DComponent::fmap_y_len_, AveragePooling2DComponent::pool_x_len_, AveragePooling2DComponent::pool_x_step_, AveragePooling2DComponent::pool_y_len_, AveragePooling2DComponent::pool_y_step_, kaldi::WriteBasicType(), and kaldi::WriteToken().

110  {
111  // pooling hyperparameters
112  WriteToken(os, binary, "<FmapXLen>");
113  WriteBasicType(os, binary, fmap_x_len_);
114  WriteToken(os, binary, "<FmapYLen>");
115  WriteBasicType(os, binary, fmap_y_len_);
116  WriteToken(os, binary, "<PoolXLen>");
117  WriteBasicType(os, binary, pool_x_len_);
118  WriteToken(os, binary, "<PoolYLen>");
119  WriteBasicType(os, binary, pool_y_len_);
120  WriteToken(os, binary, "<PoolXStep>");
121  WriteBasicType(os, binary, pool_x_step_);
122  WriteToken(os, binary, "<PoolYStep>");
123  WriteBasicType(os, binary, pool_y_step_);
124  }
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: