Class: DNN::Layers::AvgPool2D

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

Instance Attribute Summary

Attributes inherited from Pool2D

#padding, #pool_size, #strides

Attributes inherited from Layer

#input_shape, #name

Instance Method Summary collapse

Methods inherited from Pool2D

#build, #initialize, #load_hash, #output_shape, #to_hash

Methods inherited from Layer

#build, #built?, call, #call, from_hash, #initialize, #load_hash, #output_shape, #to_hash

Constructor Details

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

Instance Method Details

#backward(dy) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
# File 'lib/dnn/core/cnn_layers.rb', line 375

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

#forward(x) ⇒ Object



367
368
369
370
371
372
373
# File 'lib/dnn/core/cnn_layers.rb', line 367

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