Class: DNN::Layers::MaxPool2D
- Includes:
- Conv2DModule
- Defined in:
- lib/dnn/core/layers.rb
Instance Attribute Summary collapse
-
#pool_size ⇒ Object
readonly
Returns the value of attribute pool_size.
-
#strides ⇒ Object
readonly
Returns the value of attribute strides.
Class Method Summary collapse
Instance Method Summary collapse
- #backward(dout) ⇒ Object
- #build(model) ⇒ Object
- #forward(x) ⇒ Object
-
#initialize(pool_size, strides: nil, padding: false) ⇒ MaxPool2D
constructor
A new instance of MaxPool2D.
- #shape ⇒ Object
- #to_hash ⇒ Object
Methods inherited from Layer
Constructor Details
#initialize(pool_size, strides: nil, padding: false) ⇒ MaxPool2D
Returns a new instance of MaxPool2D.
324 325 326 327 328 329 330 331 332 333 |
# File 'lib/dnn/core/layers.rb', line 324 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 end |
Instance Attribute Details
#pool_size ⇒ Object (readonly)
Returns the value of attribute pool_size.
317 318 319 |
# File 'lib/dnn/core/layers.rb', line 317 def pool_size @pool_size end |
#strides ⇒ Object (readonly)
Returns the value of attribute strides.
318 319 320 |
# File 'lib/dnn/core/layers.rb', line 318 def strides @strides end |
Class Method Details
.load_hash(hash) ⇒ Object
320 321 322 |
# File 'lib/dnn/core/layers.rb', line 320 def self.load_hash(hash) MaxPool2D.new(hash[:pool_size], strides: hash[:strides], padding: hash[:padding]) end |
Instance Method Details
#backward(dout) ⇒ Object
356 357 358 359 360 361 362 |
# File 'lib/dnn/core/layers.rb', line 356 def backward(dout) dmax = SFloat.zeros(dout.size * @pool_size.reduce(:*)) dmax[@max_index] = dout.flatten dcol = dmax.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 |
#build(model) ⇒ Object
335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/dnn/core/layers.rb', line 335 def build(model) super prev_w, prev_h = prev_layer.shape[0..1] @num_channel = prev_layer.shape[2] @out_size = out_size(prev_h, prev_w, *@pool_size, @strides) out_w, out_h = @out_size if @padding @pad = [prev_h - out_h, prev_w - out_w] @out_size = [prev_h, prev_w] end end |
#forward(x) ⇒ Object
347 348 349 350 351 352 353 354 |
# File 'lib/dnn/core/layers.rb', line 347 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(:*)) @max_index = col.max_index(1) col.max(1).reshape(x.shape[0], *@out_size, x.shape[3]) end |
#shape ⇒ Object
364 365 366 |
# File 'lib/dnn/core/layers.rb', line 364 def shape [*@out_size, @num_channel] end |
#to_hash ⇒ Object
368 369 370 371 372 373 |
# File 'lib/dnn/core/layers.rb', line 368 def to_hash super({pool_width: @pool_width, pool_height: @pool_height, strides: @strides, padding: @padding}) end |