Class: DNN::Layers::Conv2DTranspose

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

Instance Attribute Summary collapse

Attributes inherited from Connection

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

Attributes inherited from HasParamLayer

#trainable

Attributes inherited from Layer

#input_shape, #name

Instance Method Summary collapse

Methods inherited from Connection

#get_params, #regularizers, #use_bias

Methods inherited from HasParamLayer

#get_params

Methods inherited from Layer

#built?, #call, call, 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].



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/dnn/core/cnn_layers.rb', line 200

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.



192
193
194
# File 'lib/dnn/core/cnn_layers.rb', line 192

def filter_size
  @filter_size
end

#num_filtersObject (readonly)

Returns the value of attribute num_filters.



191
192
193
# File 'lib/dnn/core/cnn_layers.rb', line 191

def num_filters
  @num_filters
end

#paddingObject (readonly)

Returns the value of attribute padding.



194
195
196
# File 'lib/dnn/core/cnn_layers.rb', line 194

def padding
  @padding
end

#stridesObject (readonly)

Returns the value of attribute strides.



193
194
195
# File 'lib/dnn/core/cnn_layers.rb', line 193

def strides
  @strides
end

Instance Method Details

#backward(dy) ⇒ Object



246
247
248
249
250
251
252
253
254
255
# File 'lib/dnn/core/cnn_layers.rb', line 246

def backward(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



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/dnn/core/cnn_layers.rb', line 216

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, num_prev_filters = *input_shape
  @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
  @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)
end

#filtersNumo::SFloat

Returns Convert weight to filter and return.

Returns:

  • (Numo::SFloat)

    Convert weight to filter and return.



262
263
264
265
# File 'lib/dnn/core/cnn_layers.rb', line 262

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.



268
269
270
271
# File 'lib/dnn/core/cnn_layers.rb', line 268

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

#forward(x) ⇒ Object



235
236
237
238
239
240
241
242
243
244
# File 'lib/dnn/core/cnn_layers.rb', line 235

def forward(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



280
281
282
283
284
285
286
287
288
289
# File 'lib/dnn/core/cnn_layers.rb', line 280

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

#output_shapeObject



257
258
259
# File 'lib/dnn/core/cnn_layers.rb', line 257

def output_shape
  [*@out_size, @num_filters]
end

#to_hashObject



273
274
275
276
277
278
# File 'lib/dnn/core/cnn_layers.rb', line 273

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