Main Content

averagePooling2dLayer

R2026b

Average pooling layer

Description

A 2-D average pooling layer performs downsampling by dividing the input into rectangular pooling regions, then computing the average of each region.

Creation

Description

layer = averagePooling2dLayer(poolSize) creates an average pooling layer and sets the PoolSize property.

layer = averagePooling2dLayer(poolSize,Name=Value) specifies options using one or more name-value arguments. For example, averagePooling2dLayer(2,Stride=2) creates an average pooling layer with pool size of two and a stride of two.

example

Input Arguments

expand all

Size of the pooling regions, specified as one of these:

  • Positive integer sz — Use sz-by-sz pooling regions.

  • Vector of two positive integers [h w] — Use h-by-w pooling regions.

If the stride sizes Stride are less than the corresponding pooling sizes, then the pooling regions overlap.

The sizes of the pooling regions must be greater than or equal to the corresponding PaddingSize values.

This argument sets the PoolSize property.

Example: [2 1] specifies pooling regions of height 2 and width 1.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Name-Value Arguments

expand all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: averagePooling2dLayer(2,Stride=2) creates an average pooling layer with pool size [2 2] and stride [2 2].

Input edge padding, specified as one of these values:

  • "same" — Add padding of size calculated by the software at training or prediction time so that the output has the same size as the input when the stride equals 1. If the stride is larger than 1, then the output size is ceil(inputSize/stride), where inputSize is the height or width of the input and stride is the stride in the corresponding dimension. The software adds the same amount of padding to the top and bottom, and to the left and right, if possible. If the padding that must be added vertically has an odd value, then the software adds extra padding to the bottom. If the padding that must be added horizontally has an odd value, then the software adds extra padding to the right.

  • Nonnegative integer p — Add padding of size p to all the edges of the input.

  • Vector [a b] of nonnegative integers — Add padding of size a to the top and bottom of the input and padding of size b to the left and right.

  • Vector [t b l r] of nonnegative integers — Add padding of size t to the top, b to the bottom, l to the left, and r to the right of the input.

Example: Padding=1 adds one row of padding to the top and bottom, and one column of padding to the left and right of the input.

Example: Padding="same" adds padding so that the output has the same size as the input (if the stride equals 1).

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char | string

Step size for traversing the input, specified as one of these:

  • Positive integer sz — Traverse input with vertical and horizontal step sizes of sz.

  • Vector of two positive integers [a b] — Traverse input with a vertical step size of a and a horizontal step size of b.

If the stride sizes are less than the corresponding pooling window sizes, then the pooling regions overlap.

This argument sets the Stride property.

Example: [2 3] specifies a vertical step size of 2 and a horizontal step size of 3.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Value used to pad input, specified as one of these:

  • 0 — Pad the input with zeros.

  • "mean" — Pad the input with the mean of the pooling region.

Specify the padding approach using the Padding name-value argument.

The layer includes the padded areas when it calculates of the average value of the pooling regions along the edges. Using the mean value as the padding value reduces the effect of the padding values on the calculated average values.

This argument sets the PaddingValue property

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char | string

Layer name, specified as a character vector or a string scalar. For Layer array input, the trainnet and dlnetwork functions automatically assign names to unnamed layers.

This argument sets the Name property.

Data Types: char | string

Properties

expand all

Average Pooling

This property is read-only after object creation. To set this property, use the corresponding positional input argument when you create the AveragePooling2DLayer object.

Size of the pooling regions, represented as a vector of two positive integers [h w], where h is the height and w is the width.

If the stride dimensions Stride are less than the corresponding pooling dimensions, then the pooling regions overlap.

Example: [2 1] specifies pooling regions of height 2 and width 1.

Data Types: double

Step size for traversing the input vertically and horizontally, specified as a vector [a b] of two positive integers, where a is the vertical step size and b is the horizontal step size.

When you set this property, you can also specify a scalar value to use the same value for both dimensions.

If the stride sizes are less than the corresponding pooling window sizes, then the pooling regions overlap.

Example: [2 3] specifies a vertical step size of 2 and a horizontal step size of 3.

Data Types: double

Size of padding to apply to input borders, specified as a vector [t b l r] of four nonnegative integers, where t is the padding applied to the top, b is the padding applied to the bottom, l is the padding applied to the left, and r is the padding applied to the right.

When you create a layer, use the Padding name-value argument to specify the padding size.

Example: [1 1 2 2] adds one row of padding to the top and bottom, and two columns of padding to the left and right of the input.

Data Types: double

This property is read-only.

Method to determine padding size, represented as one of these:

  • 'manual' – Pad using the integer or vector specified by the Padding name-value argument.

  • 'same' – Apply padding such that the output has the same size as the input for a stride of one. If the stride is larger than 1, then the output size is ceil(inputSize/stride), where inputSize is the height or width of the input and stride is the stride in the corresponding dimension. The software adds the same amount of padding to the top and bottom, and to the left and right, if possible. If the padding that must be added vertically has an odd value, then the software adds extra padding to the bottom. If the padding that must be added horizontally has an odd value, then the software adds extra padding to the right.

When you create a layer, use the Padding name-value argument to specify the method to determine padding size.

Value used to pad input, specified as one of these:

  • 0 — Pad the input with zeros.

  • 'mean' — Pad the input with the mean of the pooling region.

Specify the padding approach using the Padding name-value argument.

The layer includes the padded areas when it calculates of the average value of the pooling regions along the edges. Using the mean value as the padding value reduces the effect of the padding values on the calculated average values.

Data Types: double | char

Layer

Layer name, specified as a character vector. For Layer array input, the trainnet and dlnetwork functions automatically assign names to unnamed layers.

Data Types: char

This property is read-only.

Number of inputs to the layer, represented as 1. This layer has a single input only.

Data Types: double

This property is read-only.

Input name, represented as {'in'}. This layer has a single input only.

This property is read-only.

Number of outputs from the layer, represented as 1. This layer has a single output only.

Data Types: double

This property is read-only.

Output name, represented as {'out'}. This layer has a single output only.

Examples

collapse all

Create an average pooling layer with the name avg1.

layer = averagePooling2dLayer(2,Name="avg1")
layer = 
  AveragePooling2DLayer with properties:

            Name: 'avg1'

   Hyperparameters
        PoolSize: [2 2]
          Stride: [1 1]
     PaddingMode: 'manual'
     PaddingSize: [0 0 0 0]
    PaddingValue: 0

Include an average pooling layer in a Layer array.

layers = [ ...
    imageInputLayer([28 28 1])
    convolution2dLayer(5,20)
    reluLayer
    averagePooling2dLayer(2)
    fullyConnectedLayer(10)
    softmaxLayer]
layers = 
  6×1 Layer array with layers:

     1   ''   Image Input           28×28×1 images with 'zerocenter' normalization
     2   ''   2-D Convolution       20 5×5 convolutions with stride [1  1] and padding [0  0  0  0]
     3   ''   ReLU                  ReLU
     4   ''   2-D Average Pooling   2×2 average pooling with stride [1  1] and padding [0  0  0  0]
     5   ''   Fully Connected       Fully connected layer with output size 10
     6   ''   Softmax               Softmax

Create an average pooling layer with nonoverlapping pooling regions.

layer = averagePooling2dLayer(2,'Stride',2)
layer = 
  AveragePooling2DLayer with properties:

            Name: ''

   Hyperparameters
        PoolSize: [2 2]
          Stride: [2 2]
     PaddingMode: 'manual'
     PaddingSize: [0 0 0 0]
    PaddingValue: 0

The height and width of the rectangular regions (pool size) are both 2. The pooling regions do not overlap because the step size for traversing the images vertically and horizontally (stride) is also 2.

Include an average pooling layer with nonoverlapping regions in a Layer array.

layers = [ ...
    imageInputLayer([28 28 1])
    convolution2dLayer(5,20)
    reluLayer
    averagePooling2dLayer(2,'Stride',2)
    fullyConnectedLayer(10)
    softmaxLayer]
layers = 
  6×1 Layer array with layers:

     1   ''   Image Input           28×28×1 images with 'zerocenter' normalization
     2   ''   2-D Convolution       20 5×5 convolutions with stride [1  1] and padding [0  0  0  0]
     3   ''   ReLU                  ReLU
     4   ''   2-D Average Pooling   2×2 average pooling with stride [2  2] and padding [0  0  0  0]
     5   ''   Fully Connected       Fully connected layer with output size 10
     6   ''   Softmax               Softmax

Create an average pooling layer with overlapping pooling regions.

layer = averagePooling2dLayer([3 2],'Stride',2)
layer = 
  AveragePooling2DLayer with properties:

            Name: ''

   Hyperparameters
        PoolSize: [3 2]
          Stride: [2 2]
     PaddingMode: 'manual'
     PaddingSize: [0 0 0 0]
    PaddingValue: 0

This layer creates pooling regions of size [3 2] and takes the average of the six elements in each region. The pooling regions overlap because Stride includes dimensions that are less than the respective pooling dimensions PoolSize.

Include an average pooling layer with overlapping pooling regions in a Layer array.

layers = [ ...
    imageInputLayer([28 28 1])
    convolution2dLayer(5,20)
    reluLayer
    averagePooling2dLayer([3 2],'Stride',2)
    fullyConnectedLayer(10)
    softmaxLayer]
layers = 
  6×1 Layer array with layers:

     1   ''   Image Input           28×28×1 images with 'zerocenter' normalization
     2   ''   2-D Convolution       20 5×5 convolutions with stride [1  1] and padding [0  0  0  0]
     3   ''   ReLU                  ReLU
     4   ''   2-D Average Pooling   3×2 average pooling with stride [2  2] and padding [0  0  0  0]
     5   ''   Fully Connected       Fully connected layer with output size 10
     6   ''   Softmax               Softmax

Algorithms

expand all

References

[1] Nagi, J., F. Ducatelle, G. A. Di Caro, D. Ciresan, U. Meier, A. Giusti, F. Nagi, J. Schmidhuber, L. M. Gambardella. ''Max-Pooling Convolutional Neural Networks for Vision-based Hand Gesture Recognition''. IEEE International Conference on Signal and Image Processing Applications (ICSIPA2011), 2011.

Extended Capabilities

expand all

Version History

Introduced in R2016a

expand all