Class: DNN::Layers::UnPool2D

Inherits:
Layer
  • Object
show all
Includes:
Convert
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.



390
391
392
393
# File 'lib/dnn/core/layers.rb', line 390

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.



388
389
390
# File 'lib/dnn/core/layers.rb', line 388

def unpool_size
  @unpool_size
end

Class Method Details

.load_hash(hash) ⇒ Object



395
396
397
# File 'lib/dnn/core/layers.rb', line 395

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

Instance Method Details

#backward(dout) ⇒ Object



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

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

#build(model) ⇒ Object



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

def build(model)
  super
  prev_w, prev_h = prev_layer.shape[0..1]
  unpool_w, unpool_h = @unpool_size
  out_w = prev_w * unpool_w
  out_h = prev_h * unpool_h
  @out_size = [out_w, out_h]
  @num_channel = prev_layer.shape[2]
end

#forward(x) ⇒ Object



409
410
411
412
413
414
415
# File 'lib/dnn/core/layers.rb', line 409

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

#shapeObject



423
424
425
# File 'lib/dnn/core/layers.rb', line 423

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

#to_hashObject



427
428
429
430
431
432
# File 'lib/dnn/core/layers.rb', line 427

def to_hash
  {
    name: self.class.name,
    unpool_size: @unpool_size,
  }
end