Class: DNN::Layers::Conv2D
- Inherits:
-
Connection
- Object
- Layer
- TrainableLayer
- Connection
- DNN::Layers::Conv2D
- Includes:
- Conv2DUtils, LayerNode
- Defined in:
- lib/dnn/core/layers/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.
-
#padding ⇒ Object
readonly
Returns the value of attribute padding.
-
#strides ⇒ Object
readonly
Returns the value of attribute strides.
Attributes inherited from Connection
#bias, #bias_initializer, #bias_regularizer, #weight, #weight_initializer, #weight_regularizer
Attributes inherited from TrainableLayer
Attributes inherited from Layer
Instance Method Summary collapse
- #backward_node(dy) ⇒ Object
- #build(input_shape) ⇒ Object
- #compute_output_shape ⇒ Object
-
#filters ⇒ Numo::SFloat
Convert weight to filter and return.
- #filters=(filters) ⇒ Object
- #forward_node(x) ⇒ Object
-
#initialize(num_filters, filter_size, weight_initializer: Initializers::RandomNormal.new, bias_initializer: Initializers::Zeros.new, weight_regularizer: nil, bias_regularizer: nil, use_bias: true, strides: 1, padding: false) ⇒ Conv2D
constructor
A new instance of Conv2D.
- #load_hash(hash) ⇒ Object
- #to_hash ⇒ Object
Methods included from Conv2DUtils
calc_conv2d_out_size, calc_conv2d_padding_size, calc_conv2d_transpose_out_size, calc_conv2d_transpose_padding_size, col2im, col2im_cpu, col2im_gpu, im2col, im2col_cpu, im2col_gpu, zero_padding, zero_padding_bwd
Methods included from LayerNode
Methods inherited from Connection
#get_params, #regularizers, #use_bias
Methods inherited from TrainableLayer
Methods inherited from Layer
#<<, #built?, #call, call, #clean, #forward, from_hash
Constructor Details
#initialize(num_filters, filter_size, weight_initializer: Initializers::RandomNormal.new, bias_initializer: Initializers::Zeros.new, weight_regularizer: nil, bias_regularizer: nil, use_bias: true, strides: 1, padding: false) ⇒ Conv2D
Returns a new instance of Conv2D.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 127 def initialize(num_filters, filter_size, weight_initializer: Initializers::RandomNormal.new, bias_initializer: Initializers::Zeros.new, weight_regularizer: nil, bias_regularizer: nil, use_bias: true, strides: 1, padding: false) super(weight_initializer: weight_initializer, bias_initializer: bias_initializer, weight_regularizer: weight_regularizer, bias_regularizer: bias_regularizer, use_bias: use_bias) @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.is_a?(Integer) ? [padding, padding] : padding end |
Instance Attribute Details
#filter_size ⇒ Object (readonly)
Returns the value of attribute filter_size.
119 120 121 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 119 def filter_size @filter_size end |
#num_filters ⇒ Object (readonly)
Returns the value of attribute num_filters.
118 119 120 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 118 def num_filters @num_filters end |
#padding ⇒ Object (readonly)
Returns the value of attribute padding.
121 122 123 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 121 def padding @padding end |
#strides ⇒ Object (readonly)
Returns the value of attribute strides.
120 121 122 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 120 def strides @strides end |
Instance Method Details
#backward_node(dy) ⇒ Object
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 171 def backward_node(dy) dy = dy.reshape(dy.shape[0..2].reduce(:*), dy.shape[3]) if @trainable @weight.grad += @col.transpose.dot(dy) @bias.grad += dy.sum(0) if @bias end dcol = dy.dot(@weight.data.transpose) dx = col2im(dcol, @x_shape, *@out_size, *@filter_size, @strides) @padding ? zero_padding_bwd(dx, @pad_size) : dx end |
#build(input_shape) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 143 def build(input_shape) unless input_shape.length == 3 raise DNNShapeError, "Input shape is #{input_shape}. But input shape must be 3 dimensional." end prev_h, prev_w, num_prev_filters = *input_shape @pad_size = if @padding == true calc_conv2d_padding_size(prev_h, prev_w, *@filter_size, @strides) elsif @padding.is_a?(Array) @padding else [0, 0] end @out_size = calc_conv2d_out_size(prev_h, prev_w, *@filter_size, *@pad_size, @strides) super @weight.data = Xumo::SFloat.new(@filter_size.reduce(:*) * num_prev_filters, @num_filters) @bias.data = Xumo::SFloat.new(@num_filters) if @bias init_weight_and_bias end |
#compute_output_shape ⇒ Object
182 183 184 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 182 def compute_output_shape [*@out_size, @num_filters] end |
#filters ⇒ Numo::SFloat
Returns Convert weight to filter and return.
187 188 189 190 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 187 def filters num_prev_filters = @input_shape[2] @weight.data.reshape(*@filter_size, num_prev_filters, @num_filters) end |
#filters=(filters) ⇒ Object
193 194 195 196 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 193 def filters=(filters) num_prev_filters = @input_shape[2] @weight.data = filters.reshape(@filter_size.reduce(:*) * num_prev_filters, @num_filters) end |
#forward_node(x) ⇒ Object
162 163 164 165 166 167 168 169 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 162 def forward_node(x) x = zero_padding(x, @pad_size) if @padding @x_shape = x.shape @col = im2col(x, *@out_size, *@filter_size, @strides) y = @col.dot(@weight.data) y += @bias.data if @bias y.reshape(x.shape[0], *@out_size, y.shape[3]) end |
#load_hash(hash) ⇒ Object
205 206 207 208 209 210 211 212 213 214 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 205 def load_hash(hash) initialize(hash[:num_filters], hash[:filter_size], weight_initializer: Initializers::Initializer.from_hash(hash[:weight_initializer]), bias_initializer: Initializers::Initializer.from_hash(hash[:bias_initializer]), weight_regularizer: Regularizers::Regularizer.from_hash(hash[:weight_regularizer]), bias_regularizer: Regularizers::Regularizer.from_hash(hash[:bias_regularizer]), use_bias: hash[:use_bias], strides: hash[:strides], padding: hash[:padding]) end |
#to_hash ⇒ Object
198 199 200 201 202 203 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 198 def to_hash super(num_filters: @num_filters, filter_size: @filter_size, strides: @strides, padding: @padding) end |