Class: DNN::Layers::UnPool2D
Instance Attribute Summary collapse
Attributes inherited from Layer
#input_shape
Instance Method Summary
collapse
Methods inherited from Layer
#built?, #call, call, #clean, from_hash
Constructor Details
#initialize(unpool_size) ⇒ UnPool2D
Returns a new instance of UnPool2D.
417
418
419
420
|
# File 'lib/dnn/core/layers/cnn_layers.rb', line 417
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.
414
415
416
|
# File 'lib/dnn/core/layers/cnn_layers.rb', line 414
def unpool_size
@unpool_size
end
|
Instance Method Details
#backward(dy) ⇒ Object
447
448
449
450
451
452
|
# File 'lib/dnn/core/layers/cnn_layers.rb', line 447
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])
col.sum(1).reshape(dy.shape[0], *in_size, dy.shape[3])
end
|
#build(input_shape) ⇒ Object
422
423
424
425
426
427
428
429
430
431
432
433
|
# File 'lib/dnn/core/layers/cnn_layers.rb', line 422
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(x) ⇒ Object
435
436
437
438
439
440
441
442
443
444
445
|
# File 'lib/dnn/core/layers/cnn_layers.rb', line 435
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
|
#load_hash(hash) ⇒ Object
462
463
464
|
# File 'lib/dnn/core/layers/cnn_layers.rb', line 462
def load_hash(hash)
initialize(hash[:unpool_size])
end
|
#output_shape ⇒ Object
454
455
456
|
# File 'lib/dnn/core/layers/cnn_layers.rb', line 454
def output_shape
[*@out_size, @num_channel]
end
|
#to_hash ⇒ Object
458
459
460
|
# File 'lib/dnn/core/layers/cnn_layers.rb', line 458
def to_hash
super(unpool_size: @unpool_size)
end
|