Class: DNN::Layers::Pool2D

Inherits:
Layer
  • Object
show all
Includes:
Conv2D_Utils
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, #learning_phase

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Layer

#backward, #built?, #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 | Bool) (defaults to: false) —

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



318
319
320
321
322
323
324
325
326
327
# File 'lib/dnn/core/cnn_layers.rb', line 318

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 ⇒ Array | Bool (readonly)

Return padding size or whether to padding.

Returns:

  • (Array | Bool) —

    Return padding size or whether to padding.



308
309
310
# File 'lib/dnn/core/cnn_layers.rb', line 308

def padding
  @padding
end

#pool_size ⇒ Array (readonly)

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

Returns:

  • (Array) —

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



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

def pool_size
  @pool_size
end

#strides ⇒ Array (readonly)

Return stride length. Stride length is of the form [height, width].

Returns:

  • (Array) —

    Return stride length. Stride length is of the form [height, width].



306
307
308
# File 'lib/dnn/core/cnn_layers.rb', line 306

def strides
  @strides
end

Class Method Details

.from_hash(pool2d_class, hash) ⇒ Object



310
311
312
# File 'lib/dnn/core/cnn_layers.rb', line 310

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

Instance Method Details

#build(input_shape) ⇒ Object



329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/dnn/core/cnn_layers.rb', line 329

def build(input_shape)
  super
  prev_h, prev_w = input_shape[0..1]
  @num_channel = input_shape[2]
  if @padding == true
    out_h, out_w = calc_conv2d_out_size(prev_h, prev_w, *@pool_size, 0, 0, @strides)
    @pad_size = calc_padding_size(prev_h, prev_w, out_h, out_w, @strides)
  elsif @padding.is_a?(Array)
    @pad_size = @padding
  else
    @pad_size = [0, 0]
  end
  @out_size = calc_conv2d_out_size(prev_h, prev_w, *@pool_size, *@pad_size, @strides)
end

#output_shape ⇒ Object



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

def output_shape
  [*@out_size, @num_channel]
end

#to_hash ⇒ Object



348
349
350
351
352
# File 'lib/dnn/core/cnn_layers.rb', line 348

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