Class: DNN::Layers::Conv2DTranspose

Inherits:
Connection show all
Includes:
Conv2DUtils, LayerNode
Defined in:
lib/dnn/core/layers/cnn_layers.rb

Instance Attribute Summary collapse

Attributes inherited from Connection

#bias, #bias_initializer, #bias_regularizer, #weight, #weight_initializer, #weight_regularizer

Attributes inherited from TrainableLayer

#trainable

Attributes inherited from Layer

#input_shape, #output_shape

Instance Method Summary collapse

Methods included from Conv2DUtils

calc_conv2d_out_size, calc_conv2d_padding_size, calc_conv2d_transpose_out_size, calc_conv2d_transpose_padding_size, col2im, col2im_cpu, col2im_gpu, im2col, im2col_cpu, im2col_gpu, zero_padding, zero_padding_bwd

Methods included from LayerNode

#forward

Methods inherited from Connection

#get_params, #regularizers, #use_bias

Methods inherited from TrainableLayer

#clean, #get_params

Methods inherited from Layer

#<<, #built?, #call, call, #clean, #forward, from_hash

Constructor Details

#initialize(num_filters, filter_size, weight_initializer: Initializers::RandomNormal.new, bias_initializer: Initializers::Zeros.new, weight_regularizer: nil, bias_regularizer: nil, use_bias: true, strides: 1, padding: false) ⇒ Conv2DTranspose

Returns a new instance of Conv2DTranspose.

Parameters:

  • num_filters (Integer)

    Number of filters.

  • filter_size (Array | Integer)

    Filter size. Filter size is of the form [height, width].

  • strides (Array | Integer) (defaults to: 1)

    Stride length. Stride length is of the form [height, width].

  • padding (Array) (defaults to: false)

    Padding size. Padding size is of the form [height, width].



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/dnn/core/layers/cnn_layers.rb', line 230

def initialize(num_filters, filter_size,
               weight_initializer: Initializers::RandomNormal.new,
               bias_initializer: Initializers::Zeros.new,
               weight_regularizer: nil,
               bias_regularizer: nil,
               use_bias: true,
               strides: 1,
               padding: false)
  super(weight_initializer: weight_initializer, bias_initializer: bias_initializer,
        weight_regularizer: weight_regularizer, bias_regularizer: bias_regularizer, use_bias: use_bias)
  @num_filters = num_filters
  @filter_size = filter_size.is_a?(Integer) ? [filter_size, filter_size] : filter_size
  @strides = strides.is_a?(Integer) ? [strides, strides] : strides
  @padding = padding.is_a?(Integer) ? [padding, padding] : padding
end

Instance Attribute Details

#filter_sizeObject (readonly)

Returns the value of attribute filter_size.



222
223
224
# File 'lib/dnn/core/layers/cnn_layers.rb', line 222

def filter_size
  @filter_size
end

#num_filtersObject (readonly)

Returns the value of attribute num_filters.



221
222
223
# File 'lib/dnn/core/layers/cnn_layers.rb', line 221

def num_filters
  @num_filters
end

#paddingObject (readonly)

Returns the value of attribute padding.



224
225
226
# File 'lib/dnn/core/layers/cnn_layers.rb', line 224

def padding
  @padding
end

#stridesObject (readonly)

Returns the value of attribute strides.



223
224
225
# File 'lib/dnn/core/layers/cnn_layers.rb', line 223

def strides
  @strides
end

Instance Method Details

#backward_node(dy) ⇒ Object



276
277
278
279
280
281
282
283
284
285
# File 'lib/dnn/core/layers/cnn_layers.rb', line 276

def backward_node(dy)
  dy = zero_padding(dy, @pad_size) if @padding
  col = im2col(dy, *@input_shape[0..1], *@filter_size, @strides)
  if @trainable
    @weight.grad += col.transpose.dot(@x)
    @bias.grad += col.reshape(col.shape[0] * @filter_size.reduce(:*), @num_filters).sum(0) if @bias
  end
  dx = col.dot(@weight.data)
  dx.reshape(dy.shape[0], *@input_shape)
end

#build(input_shape) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/dnn/core/layers/cnn_layers.rb', line 246

def build(input_shape)
  unless input_shape.length == 3
    raise DNNShapeError, "Input shape is #{input_shape}. But input shape must be 3 dimensional."
  end
  prev_h, prev_w, num_prev_filters = *input_shape
  @pad_size = if @padding == true
    calc_conv2d_transpose_padding_size(prev_h, prev_w, *@filter_size, @strides)
  elsif @padding.is_a?(Array)
    @padding
  else
    [0, 0]
  end
  @out_size = calc_conv2d_transpose_out_size(prev_h, prev_w, *@filter_size, *@pad_size, @strides)
  super
  @weight.data = Xumo::SFloat.new(@filter_size.reduce(:*) * @num_filters, num_prev_filters)
  @bias.data = Xumo::SFloat.new(@num_filters) if @bias
  init_weight_and_bias
end

#compute_output_shapeObject



287
288
289
# File 'lib/dnn/core/layers/cnn_layers.rb', line 287

def compute_output_shape
  [*@out_size, @num_filters]
end

#filtersNumo::SFloat

Returns Convert weight to filter and return.

Returns:

  • (Numo::SFloat)

    Convert weight to filter and return.



292
293
294
295
# File 'lib/dnn/core/layers/cnn_layers.rb', line 292

def filters
  num_prev_filters = @input_shape[2]
  @weight.data.reshape(*@filter_size, @num_filters, num_prev_filters)
end

#filters=(filters) ⇒ Object

Parameters:

  • filters (Numo::SFloat)

    Convert weight to filters and set.



298
299
300
301
# File 'lib/dnn/core/layers/cnn_layers.rb', line 298

def filters=(filters)
  num_prev_filters = @input_shape[2]
  @weight.data = filters.reshape(@filter_size.reduce(:*) * @num_filters, num_prev_filters)
end

#forward_node(x) ⇒ Object



265
266
267
268
269
270
271
272
273
274
# File 'lib/dnn/core/layers/cnn_layers.rb', line 265

def forward_node(x)
  bsize = x.shape[0]
  x = x.reshape(x.shape[0..2].reduce(:*), x.shape[3])
  @x = x
  col = x.dot(@weight.data.transpose)
  img_shape = [bsize, @out_size[0] + @pad_size[0], @out_size[1] + @pad_size[1], @num_filters]
  y = col2im(col, img_shape, *@input_shape[0..1], *@filter_size, @strides)
  y += @bias.data if @bias
  @padding ? zero_padding_bwd(y, @pad_size) : y
end

#load_hash(hash) ⇒ Object



310
311
312
313
314
315
316
317
318
319
# File 'lib/dnn/core/layers/cnn_layers.rb', line 310

def load_hash(hash)
  initialize(hash[:num_filters], hash[:filter_size],
             weight_initializer: Initializers::Initializer.from_hash(hash[:weight_initializer]),
             bias_initializer: Initializers::Initializer.from_hash(hash[:bias_initializer]),
             weight_regularizer: Regularizers::Regularizer.from_hash(hash[:weight_regularizer]),
             bias_regularizer: Regularizers::Regularizer.from_hash(hash[:bias_regularizer]),
             use_bias: hash[:use_bias],
             strides: hash[:strides],
             padding: hash[:padding])
end

#to_hashObject



303
304
305
306
307
308
# File 'lib/dnn/core/layers/cnn_layers.rb', line 303

def to_hash
  super(num_filters: @num_filters,
        filter_size: @filter_size,
        strides: @strides,
        padding: @padding)
end