123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
- 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__ = ['ConstantPad1d', 'ConstantPad2d', 'ConstantPad3d', 'ReflectionPad1d', 'ReflectionPad2d',
- 'ReflectionPad3d', 'ReplicationPad1d', 'ReplicationPad2d', 'ReplicationPad3d', 'ZeroPad2d']
- class _ConstantPadNd(Module):
- __constants__ = ['padding', 'value']
- value: float
- padding: Sequence[int]
- def __init__(self, value: float) -> None:
- super().__init__()
- self.value = value
- def forward(self, input: Tensor) -> Tensor:
- return F.pad(input, self.padding, 'constant', self.value)
- def extra_repr(self) -> str:
- return 'padding={}, value={}'.format(self.padding, self.value)
- class ConstantPad1d(_ConstantPadNd):
- r"""Pads the input tensor boundaries with a constant value.
- For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`.
- Args:
- padding (int, tuple): the size of the padding. If is `int`, uses the same
- padding in both boundaries. If a 2-`tuple`, uses
- (:math:`\text{padding\_left}`, :math:`\text{padding\_right}`)
- Shape:
- - Input: :math:`(C, W_{in})` or :math:`(N, C, W_{in})`.
- - Output: :math:`(C, W_{out})` or :math:`(N, C, W_{out})`, where
- :math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
- Examples::
- >>> # xdoctest: +IGNORE_WANT("non-deterministic")
- >>> m = nn.ConstantPad1d(2, 3.5)
- >>> input = torch.randn(1, 2, 4)
- >>> input
- tensor([[[-1.0491, -0.7152, -0.0749, 0.8530],
- [-1.3287, 1.8966, 0.1466, -0.2771]]])
- >>> m(input)
- tensor([[[ 3.5000, 3.5000, -1.0491, -0.7152, -0.0749, 0.8530, 3.5000,
- 3.5000],
- [ 3.5000, 3.5000, -1.3287, 1.8966, 0.1466, -0.2771, 3.5000,
- 3.5000]]])
- >>> m = nn.ConstantPad1d(2, 3.5)
- >>> input = torch.randn(1, 2, 3)
- >>> input
- tensor([[[ 1.6616, 1.4523, -1.1255],
- [-3.6372, 0.1182, -1.8652]]])
- >>> m(input)
- tensor([[[ 3.5000, 3.5000, 1.6616, 1.4523, -1.1255, 3.5000, 3.5000],
- [ 3.5000, 3.5000, -3.6372, 0.1182, -1.8652, 3.5000, 3.5000]]])
- >>> # using different paddings for different sides
- >>> m = nn.ConstantPad1d((3, 1), 3.5)
- >>> m(input)
- tensor([[[ 3.5000, 3.5000, 3.5000, 1.6616, 1.4523, -1.1255, 3.5000],
- [ 3.5000, 3.5000, 3.5000, -3.6372, 0.1182, -1.8652, 3.5000]]])
- """
- padding: Tuple[int, int]
- def __init__(self, padding: _size_2_t, value: float):
- super().__init__(value)
- self.padding = _pair(padding)
- class ConstantPad2d(_ConstantPadNd):
- r"""Pads the input tensor boundaries with a constant value.
- For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`.
- Args:
- padding (int, tuple): the size of the padding. If is `int`, uses the same
- padding in all boundaries. If a 4-`tuple`, uses (:math:`\text{padding\_left}`,
- :math:`\text{padding\_right}`, :math:`\text{padding\_top}`, :math:`\text{padding\_bottom}`)
- Shape:
- - Input: :math:`(N, C, H_{in}, W_{in})` or :math:`(C, H_{in}, W_{in})`.
- - Output: :math:`(N, C, H_{out}, W_{out})` or :math:`(C, H_{out}, W_{out})`, where
- :math:`H_{out} = H_{in} + \text{padding\_top} + \text{padding\_bottom}`
- :math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
- Examples::
- >>> # xdoctest: +IGNORE_WANT("non-deterministic")
- >>> m = nn.ConstantPad2d(2, 3.5)
- >>> input = torch.randn(1, 2, 2)
- >>> input
- tensor([[[ 1.6585, 0.4320],
- [-0.8701, -0.4649]]])
- >>> m(input)
- tensor([[[ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000, 3.5000],
- [ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000, 3.5000],
- [ 3.5000, 3.5000, 1.6585, 0.4320, 3.5000, 3.5000],
- [ 3.5000, 3.5000, -0.8701, -0.4649, 3.5000, 3.5000],
- [ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000, 3.5000],
- [ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000, 3.5000]]])
- >>> # using different paddings for different sides
- >>> m = nn.ConstantPad2d((3, 0, 2, 1), 3.5)
- >>> m(input)
- tensor([[[ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000],
- [ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000],
- [ 3.5000, 3.5000, 3.5000, 1.6585, 0.4320],
- [ 3.5000, 3.5000, 3.5000, -0.8701, -0.4649],
- [ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000]]])
- """
- __constants__ = ['padding', 'value']
- padding: Tuple[int, int, int, int]
- def __init__(self, padding: _size_4_t, value: float) -> None:
- super().__init__(value)
- self.padding = _quadruple(padding)
- class ConstantPad3d(_ConstantPadNd):
- r"""Pads the input tensor boundaries with a constant value.
- For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`.
- Args:
- padding (int, tuple): the size of the padding. If is `int`, uses the same
- padding in all boundaries. If a 6-`tuple`, uses
- (:math:`\text{padding\_left}`, :math:`\text{padding\_right}`,
- :math:`\text{padding\_top}`, :math:`\text{padding\_bottom}`,
- :math:`\text{padding\_front}`, :math:`\text{padding\_back}`)
- Shape:
- - Input: :math:`(N, C, D_{in}, H_{in}, W_{in})` or :math:`(C, D_{in}, H_{in}, W_{in})`.
- - Output: :math:`(N, C, D_{out}, H_{out}, W_{out})` or
- :math:`(C, D_{out}, H_{out}, W_{out})`, where
- :math:`D_{out} = D_{in} + \text{padding\_front} + \text{padding\_back}`
- :math:`H_{out} = H_{in} + \text{padding\_top} + \text{padding\_bottom}`
- :math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
- Examples::
- >>> m = nn.ConstantPad3d(3, 3.5)
- >>> input = torch.randn(16, 3, 10, 20, 30)
- >>> output = m(input)
- >>> # using different paddings for different sides
- >>> m = nn.ConstantPad3d((3, 3, 6, 6, 0, 1), 3.5)
- >>> output = m(input)
- """
- padding: Tuple[int, int, int, int, int, int]
- def __init__(self, padding: _size_6_t, value: float) -> None:
- super().__init__(value)
- self.padding = _ntuple(6)(padding)
- class _ReflectionPadNd(Module):
- __constants__ = ['padding']
- padding: Sequence[int]
- def forward(self, input: Tensor) -> Tensor:
- return F.pad(input, self.padding, 'reflect')
- def extra_repr(self) -> str:
- return '{}'.format(self.padding)
- class ReflectionPad1d(_ReflectionPadNd):
- r"""Pads the input tensor using the reflection of the input boundary.
- For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`.
- Args:
- padding (int, tuple): the size of the padding. If is `int`, uses the same
- padding in all boundaries. If a 2-`tuple`, uses
- (:math:`\text{padding\_left}`, :math:`\text{padding\_right}`)
- Shape:
- - Input: :math:`(C, W_{in})` or :math:`(N, C, W_{in})`.
- - Output: :math:`(C, W_{out})` or :math:`(N, C, W_{out})`, where
- :math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
- Examples::
- >>> m = nn.ReflectionPad1d(2)
- >>> # xdoctest: +IGNORE_WANT("other tests seem to modify printing styles")
- >>> 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., 1., 0., 1., 2., 3., 2., 1.],
- [6., 5., 4., 5., 6., 7., 6., 5.]]])
- >>> # using different paddings for different sides
- >>> m = nn.ReflectionPad1d((3, 1))
- >>> m(input)
- tensor([[[3., 2., 1., 0., 1., 2., 3., 2.],
- [7., 6., 5., 4., 5., 6., 7., 6.]]])
- """
- padding: Tuple[int, int]
- def __init__(self, padding: _size_2_t) -> None:
- super().__init__()
- self.padding = _pair(padding)
- class ReflectionPad2d(_ReflectionPadNd):
- r"""Pads the input tensor using the reflection of the input boundary.
- For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`.
- Args:
- padding (int, tuple): the size of the padding. If is `int`, uses the same
- padding in all boundaries. If a 4-`tuple`, uses (:math:`\text{padding\_left}`,
- :math:`\text{padding\_right}`, :math:`\text{padding\_top}`, :math:`\text{padding\_bottom}`)
- Shape:
- - Input: :math:`(N, C, H_{in}, W_{in})` or :math:`(C, H_{in}, W_{in})`.
- - Output: :math:`(N, C, H_{out}, W_{out})` or :math:`(C, H_{out}, W_{out})` where
- :math:`H_{out} = H_{in} + \text{padding\_top} + \text{padding\_bottom}`
- :math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
- Examples::
- >>> # xdoctest: +IGNORE_WANT("not sure why xdoctest is choking on this")
- >>> m = nn.ReflectionPad2d(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([[[[8., 7., 6., 7., 8., 7., 6.],
- [5., 4., 3., 4., 5., 4., 3.],
- [2., 1., 0., 1., 2., 1., 0.],
- [5., 4., 3., 4., 5., 4., 3.],
- [8., 7., 6., 7., 8., 7., 6.],
- [5., 4., 3., 4., 5., 4., 3.],
- [2., 1., 0., 1., 2., 1., 0.]]]])
- >>> # using different paddings for different sides
- >>> m = nn.ReflectionPad2d((1, 1, 2, 0))
- >>> m(input)
- tensor([[[[7., 6., 7., 8., 7.],
- [4., 3., 4., 5., 4.],
- [1., 0., 1., 2., 1.],
- [4., 3., 4., 5., 4.],
- [7., 6., 7., 8., 7.]]]])
- """
- padding: Tuple[int, int, int, int]
- def __init__(self, padding: _size_4_t) -> None:
- super().__init__()
- self.padding = _quadruple(padding)
- class ReflectionPad3d(_ReflectionPadNd):
- r"""Pads the input tensor using the reflection of the input boundary.
- For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`.
- Args:
- padding (int, tuple): the size of the padding. If is `int`, uses the same
- padding in all boundaries. If a 6-`tuple`, uses
- (:math:`\text{padding\_left}`, :math:`\text{padding\_right}`,
- :math:`\text{padding\_top}`, :math:`\text{padding\_bottom}`,
- :math:`\text{padding\_front}`, :math:`\text{padding\_back}`)
- Shape:
- - Input: :math:`(N, C, D_{in}, H_{in}, W_{in})` or :math:`(C, D_{in}, H_{in}, W_{in})`.
- - Output: :math:`(N, C, D_{out}, H_{out}, W_{out})` or :math:`(C, D_{out}, H_{out}, W_{out})`,
- where
- :math:`D_{out} = D_{in} + \text{padding\_front} + \text{padding\_back}`
- :math:`H_{out} = H_{in} + \text{padding\_top} + \text{padding\_bottom}`
- :math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
- Examples::
- >>> # xdoctest: +IGNORE_WANT("not sure why xdoctest is choking on this")
- >>> m = nn.ReflectionPad3d(1)
- >>> input = torch.arange(8, dtype=torch.float).reshape(1, 1, 2, 2, 2)
- >>> m(input)
- tensor([[[[[7., 6., 7., 6.],
- [5., 4., 5., 4.],
- [7., 6., 7., 6.],
- [5., 4., 5., 4.]],
- [[3., 2., 3., 2.],
- [1., 0., 1., 0.],
- [3., 2., 3., 2.],
- [1., 0., 1., 0.]],
- [[7., 6., 7., 6.],
- [5., 4., 5., 4.],
- [7., 6., 7., 6.],
- [5., 4., 5., 4.]],
- [[3., 2., 3., 2.],
- [1., 0., 1., 0.],
- [3., 2., 3., 2.],
- [1., 0., 1., 0.]]]]])
- """
- padding: Tuple[int, int, int, int, int, int]
- def __init__(self, padding: _size_6_t) -> None:
- super().__init__()
- self.padding = _ntuple(6)(padding)
- class _ReplicationPadNd(Module):
- __constants__ = ['padding']
- padding: Sequence[int]
- def forward(self, input: Tensor) -> Tensor:
- return F.pad(input, self.padding, 'replicate')
- def extra_repr(self) -> str:
- return '{}'.format(self.padding)
- class ReplicationPad1d(_ReplicationPadNd):
- r"""Pads the input tensor using replication of the input boundary.
- For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`.
- Args:
- padding (int, tuple): the size of the padding. If is `int`, uses the same
- padding in all boundaries. If a 2-`tuple`, uses
- (:math:`\text{padding\_left}`, :math:`\text{padding\_right}`)
- Shape:
- - Input: :math:`(C, W_{in})` or :math:`(N, C, W_{in})`.
- - Output: :math:`(C, W_{out})` or :math:`(N, C, W_{out})`, where
- :math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
- Examples::
- >>> # xdoctest: +IGNORE_WANT("not sure why xdoctest is choking on this")
- >>> m = nn.ReplicationPad1d(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([[[0., 0., 0., 1., 2., 3., 3., 3.],
- [4., 4., 4., 5., 6., 7., 7., 7.]]])
- >>> # using different paddings for different sides
- >>> m = nn.ReplicationPad1d((3, 1))
- >>> m(input)
- tensor([[[0., 0., 0., 0., 1., 2., 3., 3.],
- [4., 4., 4., 4., 5., 6., 7., 7.]]])
- """
- padding: Tuple[int, int]
- def __init__(self, padding: _size_2_t) -> None:
- super().__init__()
- self.padding = _pair(padding)
- class ReplicationPad2d(_ReplicationPadNd):
- r"""Pads the input tensor using replication of the input boundary.
- For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`.
- Args:
- padding (int, tuple): the size of the padding. If is `int`, uses the same
- padding in all boundaries. If a 4-`tuple`, uses (:math:`\text{padding\_left}`,
- :math:`\text{padding\_right}`, :math:`\text{padding\_top}`, :math:`\text{padding\_bottom}`)
- Shape:
- - Input: :math:`(N, C, H_{in}, W_{in})` or :math:`(C, H_{in}, W_{in})`.
- - Output: :math:`(N, C, H_{out}, W_{out})` or :math:`(C, H_{out}, W_{out})`, where
- :math:`H_{out} = H_{in} + \text{padding\_top} + \text{padding\_bottom}`
- :math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
- Examples::
- >>> m = nn.ReplicationPad2d(2)
- >>> # xdoctest: +IGNORE_WANT("non-deterministic")
- >>> 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([[[[0., 0., 0., 1., 2., 2., 2.],
- [0., 0., 0., 1., 2., 2., 2.],
- [0., 0., 0., 1., 2., 2., 2.],
- [3., 3., 3., 4., 5., 5., 5.],
- [6., 6., 6., 7., 8., 8., 8.],
- [6., 6., 6., 7., 8., 8., 8.],
- [6., 6., 6., 7., 8., 8., 8.]]]])
- >>> # using different paddings for different sides
- >>> m = nn.ReplicationPad2d((1, 1, 2, 0))
- >>> m(input)
- tensor([[[[0., 0., 1., 2., 2.],
- [0., 0., 1., 2., 2.],
- [0., 0., 1., 2., 2.],
- [3., 3., 4., 5., 5.],
- [6., 6., 7., 8., 8.]]]])
- """
- padding: Tuple[int, int, int, int]
- def __init__(self, padding: _size_4_t) -> None:
- super().__init__()
- self.padding = _quadruple(padding)
- class ReplicationPad3d(_ReplicationPadNd):
- r"""Pads the input tensor using replication of the input boundary.
- For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`.
- Args:
- padding (int, tuple): the size of the padding. If is `int`, uses the same
- padding in all boundaries. If a 6-`tuple`, uses
- (:math:`\text{padding\_left}`, :math:`\text{padding\_right}`,
- :math:`\text{padding\_top}`, :math:`\text{padding\_bottom}`,
- :math:`\text{padding\_front}`, :math:`\text{padding\_back}`)
- Shape:
- - Input: :math:`(N, C, D_{in}, H_{in}, W_{in})` or :math:`(C, D_{in}, H_{in}, W_{in})`.
- - Output: :math:`(N, C, D_{out}, H_{out}, W_{out})` or :math:`(C, D_{out}, H_{out}, W_{out})`,
- where
- :math:`D_{out} = D_{in} + \text{padding\_front} + \text{padding\_back}`
- :math:`H_{out} = H_{in} + \text{padding\_top} + \text{padding\_bottom}`
- :math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
- Examples::
- >>> # xdoctest: +IGNORE_WANT("non-deterministic")
- >>> m = nn.ReplicationPad3d(3)
- >>> input = torch.randn(16, 3, 8, 320, 480)
- >>> output = m(input)
- >>> # using different paddings for different sides
- >>> m = nn.ReplicationPad3d((3, 3, 6, 6, 1, 1))
- >>> output = m(input)
- """
- padding: Tuple[int, int, int, int, int, int]
- def __init__(self, padding: _size_6_t) -> None:
- super().__init__()
- self.padding = _ntuple(6)(padding)
- class ZeroPad2d(ConstantPad2d):
- r"""Pads the input tensor boundaries with zero.
- For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`.
- Args:
- padding (int, tuple): the size of the padding. If is `int`, uses the same
- padding in all boundaries. If a 4-`tuple`, uses (:math:`\text{padding\_left}`,
- :math:`\text{padding\_right}`, :math:`\text{padding\_top}`, :math:`\text{padding\_bottom}`)
- Shape:
- - Input: :math:`(N, C, H_{in}, W_{in})` or :math:`(C, H_{in}, W_{in})`.
- - Output: :math:`(N, C, H_{out}, W_{out})` or :math:`(C, H_{out}, W_{out})`, where
- :math:`H_{out} = H_{in} + \text{padding\_top} + \text{padding\_bottom}`
- :math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
- Examples::
- >>> # xdoctest: +IGNORE_WANT("non-deterministic")
- >>> m = nn.ZeroPad2d(2)
- >>> input = torch.randn(1, 1, 3, 3)
- >>> input
- tensor([[[[-0.1678, -0.4418, 1.9466],
- [ 0.9604, -0.4219, -0.5241],
- [-0.9162, -0.5436, -0.6446]]]])
- >>> m(input)
- tensor([[[[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [ 0.0000, 0.0000, -0.1678, -0.4418, 1.9466, 0.0000, 0.0000],
- [ 0.0000, 0.0000, 0.9604, -0.4219, -0.5241, 0.0000, 0.0000],
- [ 0.0000, 0.0000, -0.9162, -0.5436, -0.6446, 0.0000, 0.0000],
- [ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]]])
- >>> # using different paddings for different sides
- >>> m = nn.ZeroPad2d((1, 1, 2, 0))
- >>> m(input)
- tensor([[[[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [ 0.0000, -0.1678, -0.4418, 1.9466, 0.0000],
- [ 0.0000, 0.9604, -0.4219, -0.5241, 0.0000],
- [ 0.0000, -0.9162, -0.5436, -0.6446, 0.0000]]]])
- """
- padding: Tuple[int, int, int, int]
- def __init__(self, padding: _size_4_t) -> None:
- super().__init__(padding, 0.)
- def extra_repr(self) -> str:
- return '{}'.format(self.padding)
|