Class: Chainer::Functions::Pooling::MaxPooling2D

Inherits:
Pooling2D show all
Defined in:
lib/chainer/functions/pooling/max_pooling_2d.rb

Instance Attribute Summary collapse

Attributes inherited from Pooling2D

#cover_all, #kh, #kw, #ph, #pw, #sx, #sy

Attributes inherited from Chainer::FunctionNode

#inputs, #outputs, #rank

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pooling2D

#initialize

Methods inherited from Chainer::FunctionNode

#apply, #backward_accumulate, #forward_cpu, #get_retained_inputs, #get_retained_outputs, #initialize, #label, #output_data, #retain_inputs, #retain_outputs, #unchain

Constructor Details

This class inherits a constructor from Chainer::Functions::Pooling::Pooling2D

Instance Attribute Details

#in_dtypeObject (readonly)

Returns the value of attribute in_dtype.



5
6
7
# File 'lib/chainer/functions/pooling/max_pooling_2d.rb', line 5

def in_dtype
  @in_dtype
end

#in_shapeObject (readonly)

Returns the value of attribute in_shape.



5
6
7
# File 'lib/chainer/functions/pooling/max_pooling_2d.rb', line 5

def in_shape
  @in_shape
end

#indexesObject (readonly)

Returns the value of attribute indexes.



5
6
7
# File 'lib/chainer/functions/pooling/max_pooling_2d.rb', line 5

def indexes
  @indexes
end

Class Method Details

.max_pooling_2d(x, ksize, stride: nil, pad: 0, cover_all: true) ⇒ Chainer::Variable

Spatial max pooling function

Parameters:

  • x (Chainer::Variable)

    Input variable

  • ksize (integer || 2D integer array)

    Size of pooling window

  • stride (integer || 2D integer array) (defaults to: nil)

    Stride of pooling applications

  • pad (integer || 2D integer array) (defaults to: 0)

    Spatial padding width for the input array

  • cover_all (boolean) (defaults to: true)

    If ‘true`, all spatial locations are pooled int some output pixels

Returns:



14
15
16
# File 'lib/chainer/functions/pooling/max_pooling_2d.rb', line 14

def self.max_pooling_2d(x, ksize, stride: nil, pad: 0, cover_all: true)
  self.new(ksize, stride: stride, pad: pad, cover_all: cover_all).apply([x]).first
end

Instance Method Details

#backward(indexes, gy) ⇒ Object



36
37
38
# File 'lib/chainer/functions/pooling/max_pooling_2d.rb', line 36

def backward(indexes, gy)
  MaxPooling2DGrad.new(self).apply(gy)
end

#forward(x) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/chainer/functions/pooling/max_pooling_2d.rb', line 18

def forward(x)
  @in_shape = x[0].shape
  @in_dtype = x[0].class

  col = Chainer::Utils::Conv.im2col(x[0], @kh, @kw, @sy, @sx, @ph, @pw, pval: -Float::INFINITY, cover_all: @cover_all)
  n, c, kh, kw, out_h, out_w = col.shape
  col = col.reshape(n , c, kh * kw, out_h, out_w)

  # TODO: numpy.argmax(axis=2)
  d = col.shape[3..-1].reduce(:*) || 1
  dx = col.shape[2..-1].reduce(:*) || 1
  max_index = col.max_index(2)
  @indexes = max_index.flatten.map_with_index { |val, idx| (val - (dx * (idx / d))) / d }.reshape(*max_index.shape)

  y = col.max(axis: 2)
  [y]
end