class DNN::Layers::Pool2D

Super class of all pooling2D class.

Attributes

padding[R]
pool_size[R]
strides[R]

Public Class Methods

new(pool_size, strides: nil, padding: false) click to toggle source

@param [Array | Integer] pool_size Pooling size. Pooling size is of the form [height, width]. @param [Array | Integer | NilClass] strides Stride length. Stride length is of the form [height, width].

If you set nil, treat pool_size as strides.

@param [Array | Boolean] padding Padding size or whether to padding. Padding size is of the form [height, width].

Calls superclass method DNN::Layers::Layer::new
# File lib/dnn/core/layers/cnn_layers.rb, line 334
def initialize(pool_size, strides: nil, padding: false)
  super()
  @pool_size = pool_size.is_a?(Integer) ? [pool_size, pool_size] : pool_size
  @strides = if strides
               strides.is_a?(Integer) ? [strides, strides] : strides
             else
               @pool_size.clone
             end
  @padding = padding.is_a?(Integer) ? [padding, padding] : padding
end

Public Instance Methods

build(input_shape) click to toggle source
Calls superclass method DNN::Layers::Layer#build
# File lib/dnn/core/layers/cnn_layers.rb, line 345
def build(input_shape)
  unless input_shape.length == 3
    raise DNNShapeError, "Input shape is #{input_shape}. But input shape must be 3 dimensional."
  end
  prev_h, prev_w = input_shape[0..1]
  @num_channel = input_shape[2]
  @pad_size = if @padding == true
                calc_conv2d_padding_size(prev_h, prev_w, *@pool_size, @strides)
              elsif @padding.is_a?(Array)
                @padding
              else
                [0, 0]
              end
  @out_size = calc_conv2d_out_size(prev_h, prev_w, *@pool_size, *@pad_size, @strides)
  super
end
compute_output_shape() click to toggle source
# File lib/dnn/core/layers/cnn_layers.rb, line 362
def compute_output_shape
  [*@out_size, @num_channel]
end
load_hash(hash) click to toggle source
# File lib/dnn/core/layers/cnn_layers.rb, line 372
def load_hash(hash)
  initialize(hash[:pool_size], strides: hash[:strides], padding: hash[:padding])
end
to_hash() click to toggle source
Calls superclass method DNN::Layers::Layer#to_hash
# File lib/dnn/core/layers/cnn_layers.rb, line 366
def to_hash
  super(pool_size: @pool_size,
        strides: @strides,
        padding: @padding)
end