Class: DNN::Layers::Pool2D

Inherits:
Layer
  • Object
show all
Includes:
Conv2DUtils
Defined in:
lib/dnn/core/cnn_layers.rb

Overview

Super class of all pooling2D class.

Direct Known Subclasses

AvgPool2D, MaxPool2D

Instance Attribute Summary collapse

Attributes inherited from Layer

#input_shape, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Layer

#backward, #built?, #call, call, #forward

Constructor Details

#initialize(pool_size, strides: nil, padding: false) ⇒ Pool2D

Returns a new instance of Pool2D.

Parameters:

  • pool_size (Array | Integer) —

    Pooling size. Pooling size is of the form [height, width].

  • strides (Array | Integer | NilClass) (defaults to: nil) —

    Stride length. Stride length is of the form [height, width]. If you set nil, treat pool_size as strides.

  • padding (Array | Boolean) (defaults to: false) —

    Padding size or whether to padding. Padding size is of the form [height, width].



311
312
313
314
315
316
317
318
319
320
# File 'lib/dnn/core/cnn_layers.rb', line 311

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

Instance Attribute Details

#padding ⇒ Object (readonly)

Returns the value of attribute padding.



301
302
303
# File 'lib/dnn/core/cnn_layers.rb', line 301

def padding
  @padding
end

#pool_size ⇒ Object (readonly)

Returns the value of attribute pool_size.



299
300
301
# File 'lib/dnn/core/cnn_layers.rb', line 299

def pool_size
  @pool_size
end

#strides ⇒ Object (readonly)

Returns the value of attribute strides.



300
301
302
# File 'lib/dnn/core/cnn_layers.rb', line 300

def strides
  @strides
end

Class Method Details

.from_hash(hash) ⇒ Object



303
304
305
# File 'lib/dnn/core/cnn_layers.rb', line 303

def self.from_hash(hash)
  self.new(hash[:pool_size], strides: hash[:strides], padding: hash[:padding])
end

Instance Method Details

#build(input_shape) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/dnn/core/cnn_layers.rb', line 322

def build(input_shape)
  unless input_shape.length == 3
    raise DNN_ShapeError.new("Input shape is #{input_shape}. But input shape must be 3 dimensional.")
  end
  super
  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)
end

#output_shape ⇒ Object



339
340
341
# File 'lib/dnn/core/cnn_layers.rb', line 339

def output_shape
  [*@out_size, @num_channel]
end

#to_hash ⇒ Object



343
344
345
346
347
# File 'lib/dnn/core/cnn_layers.rb', line 343

def to_hash
  super(pool_size: @pool_size,
        strides: @strides,
        padding: @padding)
end