from .module import Module
from .utils import _pair, _quadruple, _ntuple
from .. import functional as F
from torch import Tensor
from ..common_types import _size_2_t, _size_4_t, _size_6_t
from typing import Sequence, Tuple
# TODO: grad_output size asserts in THNN
__all__ = ['CircularPad1d', 'CircularPad2d', 'CircularPad3d', 'ConstantPad1d', 'ConstantPad2d',
'ConstantPad3d', 'ReflectionPad1d', 'ReflectionPad2d', 'ReflectionPad3d',
'ReplicationPad1d', 'ReplicationPad2d', 'ReplicationPad3d', 'ZeroPad1d', 'ZeroPad2d', 'ZeroPad3d']
class _CircularPadNd(Module):
__constants__ = ['padding']
padding: Sequence[int]
def _check_input_dim(self, input):
raise NotImplementedError
def forward(self, input: Tensor) -> Tensor:
self._check_input_dim(input)
return F.pad(input, self.padding, 'circular')
def extra_repr(self) -> str:
return f'{self.padding}'
[docs]class CircularPad1d(_CircularPadNd):
r"""使用输入边界的循环填充来填充输入张量。
维度开始处的张量值用于填充末尾,
末尾处的值用于填充开始处。如果应用了负填充,
则张量的末端将被移除。
对于 `N` 维填充,请使用 :func:`torch.nn.functional.pad()`。
参数:
padding (int, tuple): 填充的大小。如果是 `int`,则在所有边界使用相同的填充。
如果是 2-`tuple`,则使用
(:math:`\text{padding\_left}`, :math:`\text{padding\_right}`)
形状:
- 输入: :math:`(C, W_{in})` 或 :math:`(N, C, W_{in})`。
- 输出: :math:`(C, W_{out})` 或 :math:`(N, C, W_{out})`,其中
:math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
示例::
>>> # xdoctest: +IGNORE_WANT("not sure why xdoctest is choking on this")
>>> m = nn.CircularPad1d(2)
>>> input = torch.arange(8, dtype=torch.float).reshape(1, 2, 4)
>>> input
tensor([[[0., 1., 2., 3.],
[4., 5., 6., 7.]]])
>>> m(input)
tensor([[[2., 3., 0., 1., 2., 3., 0., 1.],
[6., 7., 4., 5., 6., 7., 4., 5.]]])
>>> # 为不同边使用不同的填充
>>> m = nn.CircularPad1d((3, 1))
>>> m(input)
tensor([[[1., 2., 3., 0., 1., 2., 3., 0.],
[5., 6., 7., 4., 5., 6., 7., 4.]]])
"""
padding: Tuple[int, int]
def __init__(self, padding: _size_2_t) -> None:
super().__init__()
self.padding = _pair(padding)
def _check_input_dim(self, input):
if input.dim() != 2 and input.dim() != 3:
raise ValueError(
f"expected 2D or 3D input (got {input.dim()}D input)"
)
[docs]class CircularPad2d(_CircularPadNd):
r"""使用输入边界的循环填充来填充输入张量。
维度开始处的张量值用于填充末尾,
末尾处的值用于填充开始处。如果应用了负填充,
则张量的末端将被移除。
对于 `N` 维填充,请使用 :func:`torch.nn.functional.pad()`。
参数:
padding (int, tuple): 填充的大小。如果是 `int`,则在所有边界使用相同的填充。
如果是 4-`tuple`,则使用 (:math:`\text{padding\_left}`,
:math:`\text{padding\_right}`, :math:`\text{padding\_top}`, :math:`\text{padding\_bottom}`)
形状:
- 输入: :math:`(N, C, H_{in}, W_{in})` 或 :math:`(C, H_{in}, W_{in})`。
- 输出: :math:`(N, C, H_{out}, W_{out})` 或 :math:`(C, H_{out}, W_{out})`,其中
:math:`H_{out} = H_{in} + \text{padding\_top} + \text{padding\_bottom}`
:math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
示例::
>>> m = nn.CircularPad2d(2)
>>> input = torch.arange(9, dtype=torch.float).reshape(1, 1, 3, 3)
>>> input
tensor([[[[0., 1., 2.],
[3., 4., 5.],
[6., 7., 8.]]]])
>>> m(input)
tensor([[[[4., 5., 3., 4., 5., 3., 4.],
[7., 8., 6., 7., 8., 6., 7.],
[1., 2., 0., 1., 2., 0., 1.],
[4., 5., 3., 4., 5., 3., 4.],
[7., 8., 6., 7., 8., 6., 7.],
[1., 2., 0., 1., 2., 0., 1.],
[4., 5., 3., 4., 5., 3., 4.]]]])
>>> # 为不同边使用不同的填充
>>> m = nn.CircularPad2d((1, 1, 2, 0))</