Class: DNN::Layers::UnPool2D
Instance Attribute Summary collapse
Attributes inherited from Layer
#input_shape
Instance Method Summary
collapse
Methods included from LayerNode
#backward, #forward
Methods inherited from Layer
#built?, #call, call, #clean, #forward, from_hash
Constructor Details
#initialize(unpool_size) ⇒ UnPool2D
Returns a new instance of UnPool2D.
415
416
417
418
|
# File 'lib/dnn/core/layers/cnn_layers.rb', line 415
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
Returns the value of attribute unpool_size.
412
413
414
|
# File 'lib/dnn/core/layers/cnn_layers.rb', line 412
def unpool_size
@unpool_size
end
|
Instance Method Details
#backward_node(dy) ⇒ Object
445
446
447
448
449
450
|
# File 'lib/dnn/core/layers/cnn_layers.rb', line 445
def backward_node(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])
col.sum(1).reshape(dy.shape[0], *in_size, dy.shape[3])
end
|
#build(input_shape) ⇒ Object
420
421
422
423
424
425
426
427
428
429
430
431
|
# File 'lib/dnn/core/layers/cnn_layers.rb', line 420
def build(input_shape)
unless input_shape.length == 3
raise DNN_ShapeError, "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_node(x) ⇒ Object
433
434
435
436
437
438
439
440
441
442
443
|
# File 'lib/dnn/core/layers/cnn_layers.rb', line 433
def forward_node(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
|
#load_hash(hash) ⇒ Object
460
461
462
|
# File 'lib/dnn/core/layers/cnn_layers.rb', line 460
def load_hash(hash)
initialize(hash[:unpool_size])
end
|
#output_shape ⇒ Object
452
453
454
|
# File 'lib/dnn/core/layers/cnn_layers.rb', line 452
def output_shape
[*@out_size, @num_channel]
end
|
#to_hash ⇒ Object
456
457
458
|
# File 'lib/dnn/core/layers/cnn_layers.rb', line 456
def to_hash
super(unpool_size: @unpool_size)
end
|