Class: DNN::Layers::Conv2D
- Inherits:
-
HasParamLayer
- Object
- Layer
- HasParamLayer
- DNN::Layers::Conv2D
- Includes:
- Initializers, Convert
- Defined in:
- lib/dnn/core/layers.rb
Instance Attribute Summary
Attributes inherited from HasParamLayer
Instance Method Summary collapse
- #backward(dout) ⇒ Object
- #forward(x) ⇒ Object
- #init(model) ⇒ Object
-
#initialize(num_filters, filter_height, filter_width, weight_initializer: nil, bias_initializer: nil, strides: [1, 1], padding: 0, weight_decay: 0) ⇒ Conv2D
constructor
A new instance of Conv2D.
- #shape ⇒ Object
Methods included from Convert
Methods inherited from HasParamLayer
Methods inherited from Layer
Constructor Details
#initialize(num_filters, filter_height, filter_width, weight_initializer: nil, bias_initializer: nil, strides: [1, 1], padding: 0, weight_decay: 0) ⇒ Conv2D
Returns a new instance of Conv2D.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/dnn/core/layers.rb', line 165 def initialize(num_filters, filter_height, filter_width, weight_initializer: nil, bias_initializer: nil, strides: [1, 1], padding: 0, weight_decay: 0) super() @num_filters = num_filters @filter_height = filter_height @filter_width = filter_width @weight_initializer = (weight_initializer || RandomNormal.new) @bias_initializer = (bias_initializer || Zeros.new) @strides = strides @weight_decay = weight_decay @padding = padding end |
Instance Method Details
#backward(dout) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/dnn/core/layers.rb', line 197 def backward(dout) dout = dout.transpose(0, 2, 3, 1) 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) col2im(dcol, @x_shape, @out_height, @out_width, @filter_height, @filter_width, @strides) end |
#forward(x) ⇒ Object
189 190 191 192 193 194 195 |
# File 'lib/dnn/core/layers.rb', line 189 def forward(x) x = padding(x, 2) if @padding > 0 @x_shape = x.shape @col = im2col(x, @out_height, @out_width, @filter_height, @filter_width, @strides) out = @col.dot(@params[:weight]) out.reshape(@model.batch_size, @out_height, @out_width, out.shape[3]).transpose(0, 3, 1, 2) end |
#init(model) ⇒ Object
182 183 184 185 186 187 |
# File 'lib/dnn/core/layers.rb', line 182 def init(model) super prev_height, prev_width = prev_layer.shape[1], prev_layer.shape[2] @out_height = (prev_height + @padding * 2 - @filter_height) / @strides[0] + 1 @out_width = (prev_width + @padding * 2 - @filter_width) / @strides[1] + 1 end |
#shape ⇒ Object
210 211 212 |
# File 'lib/dnn/core/layers.rb', line 210 def shape [@num_filters, @out_height, @out_width] end |