Class: Chainer::Links::Connection::Convolution2D
- Inherits:
-
Chainer::Link
- Object
- Chainer::Link
- Chainer::Links::Connection::Convolution2D
- Defined in:
- lib/chainer/links/connection/convolution_2d.rb
Instance Attribute Summary
Attributes inherited from Chainer::Link
Instance Method Summary collapse
-
#call(x) ⇒ Chainer::Variable
Applies the convolution layer.
-
#initialize(in_channels, out_channels, ksize = nil, stride: 1, pad: 0, nobias: false, initial_w: nil, initial_bias: nil) ⇒ Convolution2D
constructor
Two-dimensional convolutional layer.
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
-
Give the first three arguments explicitly:
> l = Chainer::Links::Connection::Convolution2D.new(3, 7, 5) > y = l.(x) > y.shape
- 1, 7, 6, 6
-
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
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.
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 |