padding.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. from .module import Module
  2. from .utils import _pair, _quadruple, _ntuple
  3. from .. import functional as F
  4. from torch import Tensor
  5. from ..common_types import _size_2_t, _size_4_t, _size_6_t
  6. from typing import Sequence, Tuple
  7. # TODO: grad_output size asserts in THNN
  8. __all__ = ['ConstantPad1d', 'ConstantPad2d', 'ConstantPad3d', 'ReflectionPad1d', 'ReflectionPad2d',
  9. 'ReflectionPad3d', 'ReplicationPad1d', 'ReplicationPad2d', 'ReplicationPad3d', 'ZeroPad2d']
  10. class _ConstantPadNd(Module):
  11. __constants__ = ['padding', 'value']
  12. value: float
  13. padding: Sequence[int]
  14. def __init__(self, value: float) -> None:
  15. super().__init__()
  16. self.value = value
  17. def forward(self, input: Tensor) -> Tensor:
  18. return F.pad(input, self.padding, 'constant', self.value)
  19. def extra_repr(self) -> str:
  20. return 'padding={}, value={}'.format(self.padding, self.value)
  21. class ConstantPad1d(_ConstantPadNd):
  22. r"""Pads the input tensor boundaries with a constant value.
  23. For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`.
  24. Args:
  25. padding (int, tuple): the size of the padding. If is `int`, uses the same
  26. padding in both boundaries. If a 2-`tuple`, uses
  27. (:math:`\text{padding\_left}`, :math:`\text{padding\_right}`)
  28. Shape:
  29. - Input: :math:`(C, W_{in})` or :math:`(N, C, W_{in})`.
  30. - Output: :math:`(C, W_{out})` or :math:`(N, C, W_{out})`, where
  31. :math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
  32. Examples::
  33. >>> # xdoctest: +IGNORE_WANT("non-deterministic")
  34. >>> m = nn.ConstantPad1d(2, 3.5)
  35. >>> input = torch.randn(1, 2, 4)
  36. >>> input
  37. tensor([[[-1.0491, -0.7152, -0.0749, 0.8530],
  38. [-1.3287, 1.8966, 0.1466, -0.2771]]])
  39. >>> m(input)
  40. tensor([[[ 3.5000, 3.5000, -1.0491, -0.7152, -0.0749, 0.8530, 3.5000,
  41. 3.5000],
  42. [ 3.5000, 3.5000, -1.3287, 1.8966, 0.1466, -0.2771, 3.5000,
  43. 3.5000]]])
  44. >>> m = nn.ConstantPad1d(2, 3.5)
  45. >>> input = torch.randn(1, 2, 3)
  46. >>> input
  47. tensor([[[ 1.6616, 1.4523, -1.1255],
  48. [-3.6372, 0.1182, -1.8652]]])
  49. >>> m(input)
  50. tensor([[[ 3.5000, 3.5000, 1.6616, 1.4523, -1.1255, 3.5000, 3.5000],
  51. [ 3.5000, 3.5000, -3.6372, 0.1182, -1.8652, 3.5000, 3.5000]]])
  52. >>> # using different paddings for different sides
  53. >>> m = nn.ConstantPad1d((3, 1), 3.5)
  54. >>> m(input)
  55. tensor([[[ 3.5000, 3.5000, 3.5000, 1.6616, 1.4523, -1.1255, 3.5000],
  56. [ 3.5000, 3.5000, 3.5000, -3.6372, 0.1182, -1.8652, 3.5000]]])
  57. """
  58. padding: Tuple[int, int]
  59. def __init__(self, padding: _size_2_t, value: float):
  60. super().__init__(value)
  61. self.padding = _pair(padding)
  62. class ConstantPad2d(_ConstantPadNd):
  63. r"""Pads the input tensor boundaries with a constant value.
  64. For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`.
  65. Args:
  66. padding (int, tuple): the size of the padding. If is `int`, uses the same
  67. padding in all boundaries. If a 4-`tuple`, uses (:math:`\text{padding\_left}`,
  68. :math:`\text{padding\_right}`, :math:`\text{padding\_top}`, :math:`\text{padding\_bottom}`)
  69. Shape:
  70. - Input: :math:`(N, C, H_{in}, W_{in})` or :math:`(C, H_{in}, W_{in})`.
  71. - Output: :math:`(N, C, H_{out}, W_{out})` or :math:`(C, H_{out}, W_{out})`, where
  72. :math:`H_{out} = H_{in} + \text{padding\_top} + \text{padding\_bottom}`
  73. :math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
  74. Examples::
  75. >>> # xdoctest: +IGNORE_WANT("non-deterministic")
  76. >>> m = nn.ConstantPad2d(2, 3.5)
  77. >>> input = torch.randn(1, 2, 2)
  78. >>> input
  79. tensor([[[ 1.6585, 0.4320],
  80. [-0.8701, -0.4649]]])
  81. >>> m(input)
  82. tensor([[[ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000, 3.5000],
  83. [ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000, 3.5000],
  84. [ 3.5000, 3.5000, 1.6585, 0.4320, 3.5000, 3.5000],
  85. [ 3.5000, 3.5000, -0.8701, -0.4649, 3.5000, 3.5000],
  86. [ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000, 3.5000],
  87. [ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000, 3.5000]]])
  88. >>> # using different paddings for different sides
  89. >>> m = nn.ConstantPad2d((3, 0, 2, 1), 3.5)
  90. >>> m(input)
  91. tensor([[[ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000],
  92. [ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000],
  93. [ 3.5000, 3.5000, 3.5000, 1.6585, 0.4320],
  94. [ 3.5000, 3.5000, 3.5000, -0.8701, -0.4649],
  95. [ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000]]])
  96. """
  97. __constants__ = ['padding', 'value']
  98. padding: Tuple[int, int, int, int]
  99. def __init__(self, padding: _size_4_t, value: float) -> None:
  100. super().__init__(value)
  101. self.padding = _quadruple(padding)
  102. class ConstantPad3d(_ConstantPadNd):
  103. r"""Pads the input tensor boundaries with a constant value.
  104. For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`.
  105. Args:
  106. padding (int, tuple): the size of the padding. If is `int`, uses the same
  107. padding in all boundaries. If a 6-`tuple`, uses
  108. (:math:`\text{padding\_left}`, :math:`\text{padding\_right}`,
  109. :math:`\text{padding\_top}`, :math:`\text{padding\_bottom}`,
  110. :math:`\text{padding\_front}`, :math:`\text{padding\_back}`)
  111. Shape:
  112. - Input: :math:`(N, C, D_{in}, H_{in}, W_{in})` or :math:`(C, D_{in}, H_{in}, W_{in})`.
  113. - Output: :math:`(N, C, D_{out}, H_{out}, W_{out})` or
  114. :math:`(C, D_{out}, H_{out}, W_{out})`, where
  115. :math:`D_{out} = D_{in} + \text{padding\_front} + \text{padding\_back}`
  116. :math:`H_{out} = H_{in} + \text{padding\_top} + \text{padding\_bottom}`
  117. :math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
  118. Examples::
  119. >>> m = nn.ConstantPad3d(3, 3.5)
  120. >>> input = torch.randn(16, 3, 10, 20, 30)
  121. >>> output = m(input)
  122. >>> # using different paddings for different sides
  123. >>> m = nn.ConstantPad3d((3, 3, 6, 6, 0, 1), 3.5)
  124. >>> output = m(input)
  125. """
  126. padding: Tuple[int, int, int, int, int, int]
  127. def __init__(self, padding: _size_6_t, value: float) -> None:
  128. super().__init__(value)
  129. self.padding = _ntuple(6)(padding)
  130. class _ReflectionPadNd(Module):
  131. __constants__ = ['padding']
  132. padding: Sequence[int]
  133. def forward(self, input: Tensor) -> Tensor:
  134. return F.pad(input, self.padding, 'reflect')
  135. def extra_repr(self) -> str:
  136. return '{}'.format(self.padding)
  137. class ReflectionPad1d(_ReflectionPadNd):
  138. r"""Pads the input tensor using the reflection of the input boundary.
  139. For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`.
  140. Args:
  141. padding (int, tuple): the size of the padding. If is `int`, uses the same
  142. padding in all boundaries. If a 2-`tuple`, uses
  143. (:math:`\text{padding\_left}`, :math:`\text{padding\_right}`)
  144. Shape:
  145. - Input: :math:`(C, W_{in})` or :math:`(N, C, W_{in})`.
  146. - Output: :math:`(C, W_{out})` or :math:`(N, C, W_{out})`, where
  147. :math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
  148. Examples::
  149. >>> m = nn.ReflectionPad1d(2)
  150. >>> # xdoctest: +IGNORE_WANT("other tests seem to modify printing styles")
  151. >>> input = torch.arange(8, dtype=torch.float).reshape(1, 2, 4)
  152. >>> input
  153. tensor([[[0., 1., 2., 3.],
  154. [4., 5., 6., 7.]]])
  155. >>> m(input)
  156. tensor([[[2., 1., 0., 1., 2., 3., 2., 1.],
  157. [6., 5., 4., 5., 6., 7., 6., 5.]]])
  158. >>> # using different paddings for different sides
  159. >>> m = nn.ReflectionPad1d((3, 1))
  160. >>> m(input)
  161. tensor([[[3., 2., 1., 0., 1., 2., 3., 2.],
  162. [7., 6., 5., 4., 5., 6., 7., 6.]]])
  163. """
  164. padding: Tuple[int, int]
  165. def __init__(self, padding: _size_2_t) -> None:
  166. super().__init__()
  167. self.padding = _pair(padding)
  168. class ReflectionPad2d(_ReflectionPadNd):
  169. r"""Pads the input tensor using the reflection of the input boundary.
  170. For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`.
  171. Args:
  172. padding (int, tuple): the size of the padding. If is `int`, uses the same
  173. padding in all boundaries. If a 4-`tuple`, uses (:math:`\text{padding\_left}`,
  174. :math:`\text{padding\_right}`, :math:`\text{padding\_top}`, :math:`\text{padding\_bottom}`)
  175. Shape:
  176. - Input: :math:`(N, C, H_{in}, W_{in})` or :math:`(C, H_{in}, W_{in})`.
  177. - Output: :math:`(N, C, H_{out}, W_{out})` or :math:`(C, H_{out}, W_{out})` where
  178. :math:`H_{out} = H_{in} + \text{padding\_top} + \text{padding\_bottom}`
  179. :math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
  180. Examples::
  181. >>> # xdoctest: +IGNORE_WANT("not sure why xdoctest is choking on this")
  182. >>> m = nn.ReflectionPad2d(2)
  183. >>> input = torch.arange(9, dtype=torch.float).reshape(1, 1, 3, 3)
  184. >>> input
  185. tensor([[[[0., 1., 2.],
  186. [3., 4., 5.],
  187. [6., 7., 8.]]]])
  188. >>> m(input)
  189. tensor([[[[8., 7., 6., 7., 8., 7., 6.],
  190. [5., 4., 3., 4., 5., 4., 3.],
  191. [2., 1., 0., 1., 2., 1., 0.],
  192. [5., 4., 3., 4., 5., 4., 3.],
  193. [8., 7., 6., 7., 8., 7., 6.],
  194. [5., 4., 3., 4., 5., 4., 3.],
  195. [2., 1., 0., 1., 2., 1., 0.]]]])
  196. >>> # using different paddings for different sides
  197. >>> m = nn.ReflectionPad2d((1, 1, 2, 0))
  198. >>> m(input)
  199. tensor([[[[7., 6., 7., 8., 7.],
  200. [4., 3., 4., 5., 4.],
  201. [1., 0., 1., 2., 1.],
  202. [4., 3., 4., 5., 4.],
  203. [7., 6., 7., 8., 7.]]]])
  204. """
  205. padding: Tuple[int, int, int, int]
  206. def __init__(self, padding: _size_4_t) -> None:
  207. super().__init__()
  208. self.padding = _quadruple(padding)
  209. class ReflectionPad3d(_ReflectionPadNd):
  210. r"""Pads the input tensor using the reflection of the input boundary.
  211. For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`.
  212. Args:
  213. padding (int, tuple): the size of the padding. If is `int`, uses the same
  214. padding in all boundaries. If a 6-`tuple`, uses
  215. (:math:`\text{padding\_left}`, :math:`\text{padding\_right}`,
  216. :math:`\text{padding\_top}`, :math:`\text{padding\_bottom}`,
  217. :math:`\text{padding\_front}`, :math:`\text{padding\_back}`)
  218. Shape:
  219. - Input: :math:`(N, C, D_{in}, H_{in}, W_{in})` or :math:`(C, D_{in}, H_{in}, W_{in})`.
  220. - Output: :math:`(N, C, D_{out}, H_{out}, W_{out})` or :math:`(C, D_{out}, H_{out}, W_{out})`,
  221. where
  222. :math:`D_{out} = D_{in} + \text{padding\_front} + \text{padding\_back}`
  223. :math:`H_{out} = H_{in} + \text{padding\_top} + \text{padding\_bottom}`
  224. :math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
  225. Examples::
  226. >>> # xdoctest: +IGNORE_WANT("not sure why xdoctest is choking on this")
  227. >>> m = nn.ReflectionPad3d(1)
  228. >>> input = torch.arange(8, dtype=torch.float).reshape(1, 1, 2, 2, 2)
  229. >>> m(input)
  230. tensor([[[[[7., 6., 7., 6.],
  231. [5., 4., 5., 4.],
  232. [7., 6., 7., 6.],
  233. [5., 4., 5., 4.]],
  234. [[3., 2., 3., 2.],
  235. [1., 0., 1., 0.],
  236. [3., 2., 3., 2.],
  237. [1., 0., 1., 0.]],
  238. [[7., 6., 7., 6.],
  239. [5., 4., 5., 4.],
  240. [7., 6., 7., 6.],
  241. [5., 4., 5., 4.]],
  242. [[3., 2., 3., 2.],
  243. [1., 0., 1., 0.],
  244. [3., 2., 3., 2.],
  245. [1., 0., 1., 0.]]]]])
  246. """
  247. padding: Tuple[int, int, int, int, int, int]
  248. def __init__(self, padding: _size_6_t) -> None:
  249. super().__init__()
  250. self.padding = _ntuple(6)(padding)
  251. class _ReplicationPadNd(Module):
  252. __constants__ = ['padding']
  253. padding: Sequence[int]
  254. def forward(self, input: Tensor) -> Tensor:
  255. return F.pad(input, self.padding, 'replicate')
  256. def extra_repr(self) -> str:
  257. return '{}'.format(self.padding)
  258. class ReplicationPad1d(_ReplicationPadNd):
  259. r"""Pads the input tensor using replication of the input boundary.
  260. For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`.
  261. Args:
  262. padding (int, tuple): the size of the padding. If is `int`, uses the same
  263. padding in all boundaries. If a 2-`tuple`, uses
  264. (:math:`\text{padding\_left}`, :math:`\text{padding\_right}`)
  265. Shape:
  266. - Input: :math:`(C, W_{in})` or :math:`(N, C, W_{in})`.
  267. - Output: :math:`(C, W_{out})` or :math:`(N, C, W_{out})`, where
  268. :math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
  269. Examples::
  270. >>> # xdoctest: +IGNORE_WANT("not sure why xdoctest is choking on this")
  271. >>> m = nn.ReplicationPad1d(2)
  272. >>> input = torch.arange(8, dtype=torch.float).reshape(1, 2, 4)
  273. >>> input
  274. tensor([[[0., 1., 2., 3.],
  275. [4., 5., 6., 7.]]])
  276. >>> m(input)
  277. tensor([[[0., 0., 0., 1., 2., 3., 3., 3.],
  278. [4., 4., 4., 5., 6., 7., 7., 7.]]])
  279. >>> # using different paddings for different sides
  280. >>> m = nn.ReplicationPad1d((3, 1))
  281. >>> m(input)
  282. tensor([[[0., 0., 0., 0., 1., 2., 3., 3.],
  283. [4., 4., 4., 4., 5., 6., 7., 7.]]])
  284. """
  285. padding: Tuple[int, int]
  286. def __init__(self, padding: _size_2_t) -> None:
  287. super().__init__()
  288. self.padding = _pair(padding)
  289. class ReplicationPad2d(_ReplicationPadNd):
  290. r"""Pads the input tensor using replication of the input boundary.
  291. For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`.
  292. Args:
  293. padding (int, tuple): the size of the padding. If is `int`, uses the same
  294. padding in all boundaries. If a 4-`tuple`, uses (:math:`\text{padding\_left}`,
  295. :math:`\text{padding\_right}`, :math:`\text{padding\_top}`, :math:`\text{padding\_bottom}`)
  296. Shape:
  297. - Input: :math:`(N, C, H_{in}, W_{in})` or :math:`(C, H_{in}, W_{in})`.
  298. - Output: :math:`(N, C, H_{out}, W_{out})` or :math:`(C, H_{out}, W_{out})`, where
  299. :math:`H_{out} = H_{in} + \text{padding\_top} + \text{padding\_bottom}`
  300. :math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
  301. Examples::
  302. >>> m = nn.ReplicationPad2d(2)
  303. >>> # xdoctest: +IGNORE_WANT("non-deterministic")
  304. >>> input = torch.arange(9, dtype=torch.float).reshape(1, 1, 3, 3)
  305. >>> input
  306. tensor([[[[0., 1., 2.],
  307. [3., 4., 5.],
  308. [6., 7., 8.]]]])
  309. >>> m(input)
  310. tensor([[[[0., 0., 0., 1., 2., 2., 2.],
  311. [0., 0., 0., 1., 2., 2., 2.],
  312. [0., 0., 0., 1., 2., 2., 2.],
  313. [3., 3., 3., 4., 5., 5., 5.],
  314. [6., 6., 6., 7., 8., 8., 8.],
  315. [6., 6., 6., 7., 8., 8., 8.],
  316. [6., 6., 6., 7., 8., 8., 8.]]]])
  317. >>> # using different paddings for different sides
  318. >>> m = nn.ReplicationPad2d((1, 1, 2, 0))
  319. >>> m(input)
  320. tensor([[[[0., 0., 1., 2., 2.],
  321. [0., 0., 1., 2., 2.],
  322. [0., 0., 1., 2., 2.],
  323. [3., 3., 4., 5., 5.],
  324. [6., 6., 7., 8., 8.]]]])
  325. """
  326. padding: Tuple[int, int, int, int]
  327. def __init__(self, padding: _size_4_t) -> None:
  328. super().__init__()
  329. self.padding = _quadruple(padding)
  330. class ReplicationPad3d(_ReplicationPadNd):
  331. r"""Pads the input tensor using replication of the input boundary.
  332. For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`.
  333. Args:
  334. padding (int, tuple): the size of the padding. If is `int`, uses the same
  335. padding in all boundaries. If a 6-`tuple`, uses
  336. (:math:`\text{padding\_left}`, :math:`\text{padding\_right}`,
  337. :math:`\text{padding\_top}`, :math:`\text{padding\_bottom}`,
  338. :math:`\text{padding\_front}`, :math:`\text{padding\_back}`)
  339. Shape:
  340. - Input: :math:`(N, C, D_{in}, H_{in}, W_{in})` or :math:`(C, D_{in}, H_{in}, W_{in})`.
  341. - Output: :math:`(N, C, D_{out}, H_{out}, W_{out})` or :math:`(C, D_{out}, H_{out}, W_{out})`,
  342. where
  343. :math:`D_{out} = D_{in} + \text{padding\_front} + \text{padding\_back}`
  344. :math:`H_{out} = H_{in} + \text{padding\_top} + \text{padding\_bottom}`
  345. :math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
  346. Examples::
  347. >>> # xdoctest: +IGNORE_WANT("non-deterministic")
  348. >>> m = nn.ReplicationPad3d(3)
  349. >>> input = torch.randn(16, 3, 8, 320, 480)
  350. >>> output = m(input)
  351. >>> # using different paddings for different sides
  352. >>> m = nn.ReplicationPad3d((3, 3, 6, 6, 1, 1))
  353. >>> output = m(input)
  354. """
  355. padding: Tuple[int, int, int, int, int, int]
  356. def __init__(self, padding: _size_6_t) -> None:
  357. super().__init__()
  358. self.padding = _ntuple(6)(padding)
  359. class ZeroPad2d(ConstantPad2d):
  360. r"""Pads the input tensor boundaries with zero.
  361. For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`.
  362. Args:
  363. padding (int, tuple): the size of the padding. If is `int`, uses the same
  364. padding in all boundaries. If a 4-`tuple`, uses (:math:`\text{padding\_left}`,
  365. :math:`\text{padding\_right}`, :math:`\text{padding\_top}`, :math:`\text{padding\_bottom}`)
  366. Shape:
  367. - Input: :math:`(N, C, H_{in}, W_{in})` or :math:`(C, H_{in}, W_{in})`.
  368. - Output: :math:`(N, C, H_{out}, W_{out})` or :math:`(C, H_{out}, W_{out})`, where
  369. :math:`H_{out} = H_{in} + \text{padding\_top} + \text{padding\_bottom}`
  370. :math:`W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}`
  371. Examples::
  372. >>> # xdoctest: +IGNORE_WANT("non-deterministic")
  373. >>> m = nn.ZeroPad2d(2)
  374. >>> input = torch.randn(1, 1, 3, 3)
  375. >>> input
  376. tensor([[[[-0.1678, -0.4418, 1.9466],
  377. [ 0.9604, -0.4219, -0.5241],
  378. [-0.9162, -0.5436, -0.6446]]]])
  379. >>> m(input)
  380. tensor([[[[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
  381. [ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
  382. [ 0.0000, 0.0000, -0.1678, -0.4418, 1.9466, 0.0000, 0.0000],
  383. [ 0.0000, 0.0000, 0.9604, -0.4219, -0.5241, 0.0000, 0.0000],
  384. [ 0.0000, 0.0000, -0.9162, -0.5436, -0.6446, 0.0000, 0.0000],
  385. [ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
  386. [ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]]])
  387. >>> # using different paddings for different sides
  388. >>> m = nn.ZeroPad2d((1, 1, 2, 0))
  389. >>> m(input)
  390. tensor([[[[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
  391. [ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
  392. [ 0.0000, -0.1678, -0.4418, 1.9466, 0.0000],
  393. [ 0.0000, 0.9604, -0.4219, -0.5241, 0.0000],
  394. [ 0.0000, -0.9162, -0.5436, -0.6446, 0.0000]]]])
  395. """
  396. padding: Tuple[int, int, int, int]
  397. def __init__(self, padding: _size_4_t) -> None:
  398. super().__init__(padding, 0.)
  399. def extra_repr(self) -> str:
  400. return '{}'.format(self.padding)