Class: DNN::Layers::Conv2D
Instance Attribute Summary collapse
Attributes inherited from Connection
#l1_lambda, #l2_lambda
#params, #trainable
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Connection
#dlasso, #dridge, #lasso, #ridge
#update
Methods inherited from Layer
#built?, #prev_layer
Constructor Details
#initialize(num_filters, filter_size, weight_initializer: Initializers::RandomNormal.new, bias_initializer: Initializers::RandomNormal.new, strides: 1, padding: false, l1_lambda: 0, l2_lambda: 0) ⇒ Conv2D
Returns a new instance of Conv2D.
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/dnn/core/cnn_layers.rb', line 70
def initialize(num_filters, filter_size,
weight_initializer: Initializers::RandomNormal.new,
bias_initializer: Initializers::RandomNormal.new,
strides: 1,
padding: false,
l1_lambda: 0,
l2_lambda: 0)
super(weight_initializer: weight_initializer, bias_initializer: bias_initializer,
l1_lambda: l1_lambda, l2_lambda: l2_lambda)
@num_filters = num_filters
@filter_size = filter_size.is_a?(Integer) ? [filter_size, filter_size] : filter_size
@strides = strides.is_a?(Integer) ? [strides, strides] : strides
@padding = padding
end
|
Instance Attribute Details
#filter_size ⇒ Object
Returns the value of attribute filter_size.
67
68
69
|
# File 'lib/dnn/core/cnn_layers.rb', line 67
def filter_size
@filter_size
end
|
#num_filters ⇒ Object
Returns the value of attribute num_filters.
66
67
68
|
# File 'lib/dnn/core/cnn_layers.rb', line 66
def num_filters
@num_filters
end
|
#strides ⇒ Object
Returns the value of attribute strides.
68
69
70
|
# File 'lib/dnn/core/cnn_layers.rb', line 68
def strides
@strides
end
|
Class Method Details
.load_hash(hash) ⇒ Object
85
86
87
88
89
90
91
92
93
|
# File 'lib/dnn/core/cnn_layers.rb', line 85
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],
l1_lambda: hash[:l1_lambda],
l2_lambda: hash[:l2_lambda])
end
|
Instance Method Details
#backward(dout) ⇒ Object
114
115
116
117
118
119
120
121
|
# File 'lib/dnn/core/cnn_layers.rb', line 114
def backward(dout)
dout = dout.reshape(dout.shape[0..2].reduce(:*), dout.shape[3])
@weight.grad = @col.transpose.dot(dout)
@bias.grad = dout.sum(0)
dcol = dout.dot(@weight.data.transpose)
dx = col2im(dcol, @x_shape, *@out_size, *@filter_size, @strides)
@padding ? back_padding(dx, @pad) : dx
end
|
#build(model) ⇒ Object
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/dnn/core/cnn_layers.rb', line 95
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
106
107
108
109
110
111
112
|
# File 'lib/dnn/core/cnn_layers.rb', line 106
def forward(x)
x = padding(x, @pad) if @padding
@x_shape = x.shape
@col = im2col(x, *@out_size, *@filter_size, @strides)
out = @col.dot(@weight.data) + @bias.data
out.reshape(x.shape[0], *@out_size, out.shape[3])
end
|
#shape ⇒ Object
123
124
125
|
# File 'lib/dnn/core/cnn_layers.rb', line 123
def shape
[*@out_size, @num_filters]
end
|
#to_hash ⇒ Object
127
128
129
130
131
132
|
# File 'lib/dnn/core/cnn_layers.rb', line 127
def to_hash
super({num_filters: @num_filters,
filter_size: @filter_size,
strides: @strides,
padding: @padding})
end
|