Class: DNN::Layers::MaxPool2D
- Includes:
- Conv2DModule
- Defined in:
- lib/dnn/core/cnn_layers.rb
Instance Attribute Summary collapse
-
#pool_size ⇒ Object
readonly
Returns the value of attribute pool_size.
-
#strides ⇒ Object
readonly
Returns the value of attribute strides.
Class Method Summary collapse
Instance Method Summary collapse
- #backward(dout) ⇒ Object
- #build(model) ⇒ Object
- #forward(x) ⇒ Object
-
#initialize(pool_size, strides: nil, padding: false) ⇒ MaxPool2D
constructor
A new instance of MaxPool2D.
- #shape ⇒ Object
- #to_hash ⇒ Object
Methods inherited from Layer
Constructor Details
#initialize(pool_size, strides: nil, padding: false) ⇒ MaxPool2D
Returns a new instance of MaxPool2D.
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/dnn/core/cnn_layers.rb', line 165 def initialize(pool_size, strides: nil, padding: false) super() @pool_size = pool_size.is_a?(Integer) ? [pool_size, pool_size] : pool_size @strides = if strides strides.is_a?(Integer) ? [strides, strides] : strides else @pool_size.clone end @padding = padding end |
Instance Attribute Details
#pool_size ⇒ Object (readonly)
Returns the value of attribute pool_size.
158 159 160 |
# File 'lib/dnn/core/cnn_layers.rb', line 158 def pool_size @pool_size end |
#strides ⇒ Object (readonly)
Returns the value of attribute strides.
159 160 161 |
# File 'lib/dnn/core/cnn_layers.rb', line 159 def strides @strides end |
Class Method Details
Instance Method Details
#backward(dout) ⇒ Object
197 198 199 200 201 202 203 |
# File 'lib/dnn/core/cnn_layers.rb', line 197 def backward(dout) dmax = SFloat.zeros(dout.size * @pool_size.reduce(:*)) dmax[@max_index] = dout.flatten dcol = dmax.reshape(dout.shape[0..2].reduce(:*), dout.shape[3] * @pool_size.reduce(:*)) dx = col2im(dcol, @x_shape, *@out_size, *@pool_size, @strides) @padding ? back_padding(dx, @pad) : dx end |
#build(model) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/dnn/core/cnn_layers.rb', line 176 def build(model) super prev_w, prev_h = prev_layer.shape[0..1] @num_channel = prev_layer.shape[2] @out_size = out_size(prev_h, prev_w, *@pool_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
188 189 190 191 192 193 194 195 |
# File 'lib/dnn/core/cnn_layers.rb', line 188 def forward(x) x = padding(x, @pad) if @padding @x_shape = x.shape col = im2col(x, *@out_size, *@pool_size, @strides) col = col.reshape(x.shape[0] * @out_size.reduce(:*) * x.shape[3], @pool_size.reduce(:*)) @max_index = col.max_index(1) col.max(1).reshape(x.shape[0], *@out_size, x.shape[3]) end |
#shape ⇒ Object
205 206 207 |
# File 'lib/dnn/core/cnn_layers.rb', line 205 def shape [*@out_size, @num_channel] end |
#to_hash ⇒ Object
209 210 211 212 213 214 |
# File 'lib/dnn/core/cnn_layers.rb', line 209 def to_hash super({pool_width: @pool_width, pool_height: @pool_height, strides: @strides, padding: @padding}) end |