Class: Chainer::Links::Connection::Convolution2D

Inherits:
Chainer::Link show all
Defined in:
lib/chainer/links/connection/convolution_2d.rb

Instance Attribute Summary

Attributes inherited from Chainer::Link

#name

Instance Method Summary collapse

Methods inherited from Chainer::Link

#cleargrads, #del_attr, #init_scope, #namedlinks, #namedparams, #params, #register_persistent, #serialize, #set_attr, #within_init_scope

Constructor Details

#initialize(in_channels, out_channels, ksize = nil, stride: 1, pad: 0, nobias: false, initial_w: nil, initial_bias: nil) ⇒ Convolution2D

Two-dimensional convolutional layer.

This link wraps the :func:‘chainer.functions.convolution_2d` function and holds the filter weight and bias vector as parameters.

Example There are several ways to make a Convolution2D link. Let an input vector ‘x` be: > x = Numo::DFloat.new(1, 3, 10, 10).seq

  1. Give the first three arguments explicitly:

> l = Chainer::Links::Connection::Convolution2D.new(3, 7, 5) > y = l.(x) > y.shape

1, 7, 6, 6
  1. Omit ‘in_channels` or fill it with `nil`: The below two cases are the same.

> l = Chainer::Links::Connection::Convolution2D.new(7, 5) > y = l.(x) > y.shape

1, 7, 6, 6

> l = Chainer::Links::Connection::Convolution2D.new(nil, 7, 5) > y = l.(x) > y.shape

1, 7, 6, 6

When you omit the first argument, you need to specify the other subsequent arguments from ‘stride` as keyword auguments.

> l = Chainer::Links::Connection::Convolution2D.new(7, 5, stride: 1, pad: 0) > y = l.(x) > y.shape

1, 7, 6, 6

Parameters:

  • in_channels (integer or nil)

    Number of channels of input arrays. If ‘nil`, parameter initialization will be deferred until the first forward data pass at which time the size will be determined.

  • out_channels (integer)

    Number of channels of output arrays.

  • ksize (integer or 2-d int array) (defaults to: nil)

    Size of filters (a.k.a. kernels).

  • stride (integer or 2-d int array) (defaults to: 1)

    Stride of filter applications.

  • pad (integer or 2-d int array) (defaults to: 0)

    Spatial padding width for input arrays.

  • nobias (boolean) (defaults to: false)

    If ‘true`, then this link does not use the bias term.

  • initial_w (Numo::NArray or Cumo::NArray) (defaults to: nil)

    Initial weight value. If ‘nil`, the default initializer is used.

  • initial_bias (Numo::NArray or Cumo::NArray) (defaults to: nil)

    Initial bias value. If ‘nil`, the bias is set to 0.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/chainer/links/connection/convolution_2d.rb', line 50

def initialize(in_channels, out_channels, ksize=nil, stride: 1, pad: 0, nobias: false, initial_w: nil, initial_bias: nil)
  super()
  if ksize.nil?
    out_channels, ksize, in_channels = in_channels, out_channels, nil
  end

  @ksize = ksize
  @stride = stride.is_a?(Array) ? stride : [stride, stride]
  @pad = pad.is_a?(Array) ? pad : [pad, pad]
  @out_channels = out_channels
  
  init_scope do
    w_initializer = Chainer::Initializers.get_initializer(initial_w)
    @w = Chainer::Parameter.new(initializer: w_initializer)
    if in_channels
      initialize_params(in_channels)
    end

    if nobias
      @b = nil
    else
      initial_bias = 0 if initial_bias.nil?
      bias_initializer = Chainer::Initializers.get_initializer(initial_bias)
      @b = Chainer::Parameter.new(initializer: bias_initializer, shape: out_channels)
    end
  end
end

Instance Method Details

#call(x) ⇒ Chainer::Variable

Applies the convolution layer.

Parameters:

Returns:



81
82
83
84
# File 'lib/chainer/links/connection/convolution_2d.rb', line 81

def call(x)
  initialize_params(x.shape[1]) if @w.data.nil?
  Chainer::Functions::Connection::Convolution2DFunction.convolution_2d(x, @w, b: @b, stride: @stride, pad: @pad)
end