Class: DNN::Layers::Conv2D
- Inherits:
-
HasParamLayer
- Object
- Layer
- HasParamLayer
- DNN::Layers::Conv2D
- Includes:
- Initializers, Conv2DModule
- Defined in:
- lib/dnn/core/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.
- #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.
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/dnn/core/layers.rb', line 231 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.
227 228 229 |
# File 'lib/dnn/core/layers.rb', line 227 def filter_size @filter_size end |
#num_filters ⇒ Object (readonly)
Returns the value of attribute num_filters.
226 227 228 |
# File 'lib/dnn/core/layers.rb', line 226 def num_filters @num_filters end |
#strides ⇒ Object (readonly)
Returns the value of attribute strides.
228 229 230 |
# File 'lib/dnn/core/layers.rb', line 228 def strides @strides end |
#weight_decay ⇒ Object (readonly)
Returns the value of attribute weight_decay.
229 230 231 |
# File 'lib/dnn/core/layers.rb', line 229 def weight_decay @weight_decay end |
Class Method Details
.load_hash(hash) ⇒ Object
247 248 249 250 251 252 253 254 |
# File 'lib/dnn/core/layers.rb', line 247 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
275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/dnn/core/layers.rb', line 275 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
256 257 258 259 260 261 262 263 264 265 |
# File 'lib/dnn/core/layers.rb', line 256 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
267 268 269 270 271 272 273 |
# File 'lib/dnn/core/layers.rb', line 267 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 |
#shape ⇒ Object
288 289 290 |
# File 'lib/dnn/core/layers.rb', line 288 def shape [*@out_size, @num_filters] end |
#to_hash ⇒ Object
292 293 294 295 296 297 298 299 300 |
# File 'lib/dnn/core/layers.rb', line 292 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 |