Class: DNN::Layers::UnPool2D
- Includes:
- Conv2DUtils
- Defined in:
- lib/dnn/core/cnn_layers.rb
Instance Attribute Summary collapse
-
#unpool_size ⇒ Object
readonly
Returns the value of attribute unpool_size.
Attributes inherited from Layer
Class Method Summary collapse
Instance Method Summary collapse
- #backward(dy) ⇒ Object
- #build(input_shape) ⇒ Object
- #forward(x) ⇒ Object
-
#initialize(unpool_size) ⇒ UnPool2D
constructor
A new instance of UnPool2D.
- #output_shape ⇒ Object
- #to_hash ⇒ Object
Methods inherited from Layer
Constructor Details
#initialize(unpool_size) ⇒ UnPool2D
Returns a new instance of UnPool2D.
406 407 408 409 |
# File 'lib/dnn/core/cnn_layers.rb', line 406 def initialize(unpool_size) super() @unpool_size = unpool_size.is_a?(Integer) ? [unpool_size, unpool_size] : unpool_size end |
Instance Attribute Details
#unpool_size ⇒ Object (readonly)
Returns the value of attribute unpool_size.
399 400 401 |
# File 'lib/dnn/core/cnn_layers.rb', line 399 def unpool_size @unpool_size end |
Class Method Details
.from_hash(hash) ⇒ Object
401 402 403 |
# File 'lib/dnn/core/cnn_layers.rb', line 401 def self.from_hash(hash) self.new(hash[:unpool_size]) end |
Instance Method Details
#backward(dy) ⇒ Object
436 437 438 439 440 441 442 |
# File 'lib/dnn/core/cnn_layers.rb', line 436 def backward(dy) in_size = input_shape[0..1] col = im2col(dy, *in_size, *@unpool_size, @unpool_size) col = col.reshape(dy.shape[0] * in_size.reduce(:*), @unpool_size.reduce(:*), dy.shape[3]).transpose(0, 2, 1) .reshape(dy.shape[0] * in_size.reduce(:*) * dy.shape[3], @unpool_size.reduce(:*)) col.sum(1).reshape(dy.shape[0], *in_size, dy.shape[3]) end |
#build(input_shape) ⇒ Object
411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/dnn/core/cnn_layers.rb', line 411 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] unpool_h, unpool_w = @unpool_size out_h = prev_h * unpool_h out_w = prev_w * unpool_w @out_size = [out_h, out_w] @num_channel = input_shape[2] end |
#forward(x) ⇒ Object
424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/dnn/core/cnn_layers.rb', line 424 def forward(x) @x_shape = x.shape unpool_h, unpool_w = @unpool_size x2 = Xumo::SFloat.zeros(x.shape[0], x.shape[1], unpool_h, x.shape[2], unpool_w, @num_channel) unpool_h.times do |i| unpool_w.times do |j| x2[true, true, i, true, j, true] = x end end x2.reshape(x.shape[0], *@out_size, x.shape[3]) end |
#output_shape ⇒ Object
444 445 446 |
# File 'lib/dnn/core/cnn_layers.rb', line 444 def output_shape [*@out_size, @num_channel] end |
#to_hash ⇒ Object
448 449 450 |
# File 'lib/dnn/core/cnn_layers.rb', line 448 def to_hash super(unpool_size: @unpool_size) end |