Class: DNN::Layers::UnPool2D

Inherits:
Layer
  • Object
show all
Defined in:
lib/dnn/core/layers.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Layer

#built?, #prev_layer

Constructor Details

#initialize(unpool_size) ⇒ UnPool2D

Returns a new instance of UnPool2D.



380
381
382
383
# File 'lib/dnn/core/layers.rb', line 380

def initialize(unpool_size)
  super()
  @unpool_size = unpool_size.is_a?(Integer) ? [unpool_size, unpool_size] : unpool_size
end

Instance Attribute Details

#unpool_sizeObject (readonly)

Returns the value of attribute unpool_size.



378
379
380
# File 'lib/dnn/core/layers.rb', line 378

def unpool_size
  @unpool_size
end

Class Method Details

.load_hash(hash) ⇒ Object



385
386
387
# File 'lib/dnn/core/layers.rb', line 385

def self.load_hash(hash)
  UnPool2D.new(hash[:unpool_size])
end

Instance Method Details

#backward(dout) ⇒ Object



407
408
409
410
411
# File 'lib/dnn/core/layers.rb', line 407

def backward(dout)
  unpool_h, unpool_w = @unpool_size
  dout = dout.reshape(dout.shape[0], @x_shape[0], unpool_h, @x_shape[1], unpool_w, @num_channel)
  dout[true, true, 0, true, 0, true].clone
end

#build(model) ⇒ Object



389
390
391
392
393
394
395
396
397
# File 'lib/dnn/core/layers.rb', line 389

def build(model)
  super
  prev_h, prev_w = prev_layer.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 = prev_layer.shape[2]
end

#forward(x) ⇒ Object



399
400
401
402
403
404
405
# File 'lib/dnn/core/layers.rb', line 399

def forward(x)
  @x_shape = x.shape
  unpool_h, unpool_w = @unpool_size
  x2 = SFloat.zeros(x.shape[0], x.shape[1], unpool_h, x.shape[2], unpool_w, @num_channel)
  x2[true, true, 0, true, 0, true] = x
  x2.reshape(x.shape[0], *@out_size, x.shape[3])
end

#shapeObject



413
414
415
# File 'lib/dnn/core/layers.rb', line 413

def shape
  [@out_width, @out_height, @num_channel]
end

#to_hashObject



417
418
419
# File 'lib/dnn/core/layers.rb', line 417

def to_hash
  super({unpool_size: @unpool_size})
end