Class: DNN::Layers::AvgPool2D

Inherits:
Pool2D show all
Defined in:
lib/dnn/core/cnn_layers.rb

Instance Attribute Summary

Attributes inherited from Pool2D

#pool_size, #strides

Attributes inherited from Layer

#input_shape

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pool2D

#build, #initialize, #output_shape, #to_hash

Methods inherited from Layer

#build, #built?, #initialize, #output_shape, #to_hash

Constructor Details

This class inherits a constructor from DNN::Layers::Pool2D

Class Method Details

.load_hash(hash) ⇒ Object



216
217
218
# File 'lib/dnn/core/cnn_layers.rb', line 216

def self.load_hash(hash)
  Pool2D.load_hash(self, hash)
end

Instance Method Details

#backward(dout) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/dnn/core/cnn_layers.rb', line 228

def backward(dout)
  row_length = @pool_size.reduce(:*)
  dout /= row_length
  davg = Xumo::SFloat.zeros(dout.size, row_length)
  row_length.times do |i|
    davg[true, i] = dout.flatten
  end
  dcol = davg.reshape(dout.shape[0..2].reduce(:*), dout.shape[3] * @pool_size.reduce(:*))
  dx = col2im(dcol, @x_shape, *@out_size, *@pool_size, @strides)
  @padding ? back_padding(dx, @pad) : dx
end

#forward(x) ⇒ Object



220
221
222
223
224
225
226
# File 'lib/dnn/core/cnn_layers.rb', line 220

def forward(x)
  x = padding(x, @pad) if @padding
  @x_shape = x.shape
  col = im2col(x, *@out_size, *@pool_size, @strides)
  col = col.reshape(x.shape[0] * @out_size.reduce(:*) * x.shape[3], @pool_size.reduce(:*))
  col.mean(1).reshape(x.shape[0], *@out_size, x.shape[3])
end