Class: DNN::Layers::Conv2D
- Inherits:
-
HasParamLayer
- Object
- Layer
- HasParamLayer
- DNN::Layers::Conv2D
- Includes:
- Initializers, Conv2DModule
- Defined in:
- lib/dnn/core/cnn_layers.rb
Instance Attribute Summary collapse
-
#filter_size ⇒ Object
readonly
Returns the value of attribute filter_size.
-
#num_filters ⇒ Object
readonly
Returns the value of attribute num_filters.
-
#strides ⇒ Object
readonly
Returns the value of attribute strides.
-
#weight_decay ⇒ Object
readonly
Returns the value of attribute weight_decay.
Attributes inherited from HasParamLayer
Class Method Summary collapse
Instance Method Summary collapse
- #backward(dout) ⇒ Object
- #build(model) ⇒ Object
- #forward(x) ⇒ Object
-
#initialize(num_filters, filter_size, weight_initializer: nil, bias_initializer: nil, strides: 1, padding: false, weight_decay: 0) ⇒ Conv2D
constructor
A new instance of Conv2D.
- #ridge ⇒ Object
- #shape ⇒ Object
- #to_hash ⇒ Object
Methods inherited from HasParamLayer
Methods inherited from Layer
Constructor Details
#initialize(num_filters, filter_size, weight_initializer: nil, bias_initializer: nil, strides: 1, padding: false, weight_decay: 0) ⇒ Conv2D
Returns a new instance of Conv2D.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/dnn/core/cnn_layers.rb', line 72 def initialize(num_filters, filter_size, weight_initializer: nil, bias_initializer: nil, strides: 1, padding: false, weight_decay: 0) super() @num_filters = num_filters @filter_size = filter_size.is_a?(Integer) ? [filter_size, filter_size] : filter_size @weight_initializer = (weight_initializer || RandomNormal.new) @bias_initializer = (bias_initializer || Zeros.new) @strides = strides.is_a?(Integer) ? [strides, strides] : strides @padding = padding @weight_decay = weight_decay end |
Instance Attribute Details
#filter_size ⇒ Object (readonly)
Returns the value of attribute filter_size.
68 69 70 |
# File 'lib/dnn/core/cnn_layers.rb', line 68 def filter_size @filter_size end |
#num_filters ⇒ Object (readonly)
Returns the value of attribute num_filters.
67 68 69 |
# File 'lib/dnn/core/cnn_layers.rb', line 67 def num_filters @num_filters end |
#strides ⇒ Object (readonly)
Returns the value of attribute strides.
69 70 71 |
# File 'lib/dnn/core/cnn_layers.rb', line 69 def strides @strides end |
#weight_decay ⇒ Object (readonly)
Returns the value of attribute weight_decay.
70 71 72 |
# File 'lib/dnn/core/cnn_layers.rb', line 70 def weight_decay @weight_decay end |
Class Method Details
.load_hash(hash) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/dnn/core/cnn_layers.rb', line 88 def self.load_hash(hash) Conv2D.new(hash[:num_filters], hash[:filter_size], weight_initializer: Util.load_hash(hash[:weight_initializer]), bias_initializer: Util.load_hash(hash[:bias_initializer]), strides: hash[:strides], padding: hash[:padding], weight_decay: hash[:weight_decay]) end |
Instance Method Details
#backward(dout) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/dnn/core/cnn_layers.rb', line 116 def backward(dout) dout = dout.reshape(dout.shape[0..2].reduce(:*), dout.shape[3]) @grads[:weight] = @col.transpose.dot(dout) if @weight_decay > 0 dridge = @weight_decay * @params[:weight] @grads[:weight] += dridge end @grads[:bias] = dout.sum(0) dcol = dout.dot(@params[:weight].transpose) dx = col2im(dcol, @x_shape, *@out_size, *@filter_size, @strides) @padding ? back_padding(dx, @pad) : dx end |
#build(model) ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/dnn/core/cnn_layers.rb', line 97 def build(model) super prev_h, prev_w = prev_layer.shape[0..1] @out_size = out_size(prev_h, prev_w, *@filter_size, @strides) out_w, out_h = @out_size if @padding @pad = [prev_h - out_h, prev_w - out_w] @out_size = [prev_h, prev_w] end end |
#forward(x) ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/dnn/core/cnn_layers.rb', line 108 def forward(x) x = padding(x, @pad) if @padding @x_shape = x.shape @col = im2col(x, *@out_size, *@filter_size, @strides) out = @col.dot(@params[:weight]) out.reshape(x.shape[0], *@out_size, out.shape[3]) end |
#ridge ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/dnn/core/cnn_layers.rb', line 133 def ridge if @weight_decay > 0 0.5 * @weight_decay * (@params[:weight]**2).sum else 0 end end |
#shape ⇒ Object
129 130 131 |
# File 'lib/dnn/core/cnn_layers.rb', line 129 def shape [*@out_size, @num_filters] end |
#to_hash ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'lib/dnn/core/cnn_layers.rb', line 141 def to_hash super({num_filters: @num_filters, filter_size: @filter_size, weight_initializer: @weight_initializer.to_hash, bias_initializer: @bias_initializer.to_hash, strides: @strides, padding: @padding, weight_decay: @weight_decay}) end |