Skip to content
class

LPPool2d

extendsModule
LPPool2d(norm_type: float, kernel_size: int | tuple[int, int], stride: int | tuple[int, int] | None = None, ceil_mode: bool = False)

Applies 2-D power-average (Lp-norm) pooling over a spatial feature map.

For each kH×kWk_H \times k_W window the module computes:

y[n,c,h,w]=(∑kh=0kH−1∑kw=0kW−1x ⁣[n, c, h⋅sH+kh, w⋅sW+kw]p)1/py[n,c,h,w] = \left( \sum_{k_h=0}^{k_H-1} \sum_{k_w=0}^{k_W-1} x\!\left[n,\,c,\,h \cdot s_H + k_h,\,w \cdot s_W + k_w\right]^p \right)^{1/p}

with pp = norm_type. The power keeps the sign of xx, and the special cases p=1,2,∞p = 1, 2, \infty are those of LPPool1d.

Parameters

norm_typefloat
The exponent pp. Must be positive; inf is allowed.
kernel_sizeint or tuple[int, int]
Window size (k_H, k_W). A scalar is broadcast.
strideint or tuple[int, int] or None= None
Step between windows. Defaults to kernel_size.
ceil_modebool= False
Use ceiling for output sizes. Default: False.

Attributes

norm_typefloat
khint
Kernel height.
kwint
Kernel width.
shint
Stride along the height dimension.
swint
Stride along the width dimension.
ceil_modebool

Notes

  • Input: (N, C, H_in, W_in)
  • Output: (N, C, H_out, W_out) where
Hout=⌊Hin−kHsH⌋+1,Wout=⌊Win−kWsW⌋+1H_{out} = \left\lfloor \frac{H_{in} - k_H}{s_H} \right\rfloor + 1, \quad W_{out} = \left\lfloor \frac{W_{in} - k_W}{s_W} \right\rfloor + 1
  • Lp pooling with p=2p = 2 appears in energy-based models and certain biologically-inspired convolutional architectures.

Examples

L2 energy pooling:
>>> import lucid
>>> import lucid.nn as nn
>>> pool = nn.LPPool2d(norm_type=2, kernel_size=2, stride=2)
>>> x = lucid.ones((1, 32, 16, 16))
>>> y = pool(x)
>>> y.shape
(1, 32, 8, 8)
Asymmetric window with p=1 (sum pooling):
>>> pool = nn.LPPool2d(norm_type=1, kernel_size=(3, 1), stride=(1, 1))
>>> x = lucid.ones((2, 16, 10, 8))
>>> y = pool(x)
>>> y.shape
(2, 16, 8, 8)

Used by 1

Constructors

1
dunder

__init__

→None
__init__(norm_type: float, kernel_size: int | tuple[int, int], stride: int | tuple[int, int] | None = None, ceil_mode: bool = False)

Initialise the LPPool2d module. See the class docstring for parameter semantics.

Instance methods

2
fn

extra_repr

→str
extra_repr()

Return a string representation of the layer's configuration.

fn

forward

→Tensor
forward(input: Tensor)

Apply the pooling operation to the input tensor.

Parameters

inputTensor
Input tensor of shape (N,C,∗)(N, C, *) where ∗* are the spatial dimensions appropriate for this pooling layer.

Returns

Tensor

Pooled output tensor.