Class: Chainer::Functions::Pooling::AveragePooling2D

Inherits:
Pooling2D show all
Defined in:
lib/chainer/functions/pooling/average_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/average_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/average_pooling_2d.rb', line 5

def in_shape
  @in_shape
end

Class Method Details

.average_pooling_2d(x, ksize, stride: nil, pad: 0) ⇒ Chainer::Variable

Spatial average pooling function.

This function acts similarly to :class:‘Convolution2D`, but it computes the average of input spatial patch for each channel without any parameter instead of computing the inner products.

Parameters:

  • x (Chainer::Variable)

    Input variable.

  • ksize (integer)

    Size of pooling window. ‘ksize=k` and `ksize=[k, k]` are equivalent.

  • stride (integer) (defaults to: nil)

    Stride of pooling applications. ‘stride=s` and `stride=[s, s]` are equivalent. If `nil` is specified, then it uses same stride as the pooling window size.

  • pad (integer) (defaults to: 0)

    Spatial padding width for the input array. ‘pad=p` and `pad=[p, p]` are equivalent.

Returns:



18
19
20
# File 'lib/chainer/functions/pooling/average_pooling_2d.rb', line 18

def self.average_pooling_2d(x, ksize, stride: nil, pad: 0)
  self.new(ksize, stride: stride, pad: pad, cover_all: false).apply([x])[0]
end

Instance Method Details

#backward(indexes, gy) ⇒ Object



33
34
35
# File 'lib/chainer/functions/pooling/average_pooling_2d.rb', line 33

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

#forward(x) ⇒ Object

Average pooling over a set of 2d planes.



23
24
25
26
27
28
29
30
31
# File 'lib/chainer/functions/pooling/average_pooling_2d.rb', line 23

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)
  y = col.mean(axis: [2, 3])

  [y]
end