MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / pad

Function pad

imperative/python/megengine/functional/nn.py:1813–1889  ·  view source on GitHub ↗

r"""Pads the input tensor. Args: pad_width: A tuple. Each element in the tuple is the tuple of 2-elements, the 2 elements represent the padding size on both sides of the current dimension, ``(front_offset, back_offset)`` mode: One of the following string values. Defa

(
    src: Tensor,
    pad_width: Tuple[Tuple[int, int], ...],
    mode: str = "constant",
    constant_value: float = 0.0,
)

Source from the content-addressed store, hash-verified

1811
1812
1813def pad(
1814 src: Tensor,
1815 pad_width: Tuple[Tuple[int, int], ...],
1816 mode: str = "constant",
1817 constant_value: float = 0.0,
1818) -> Tensor:
1819 r"""Pads the input tensor.
1820
1821 Args:
1822 pad_width: A tuple. Each element in the tuple is the tuple of 2-elements,
1823 the 2 elements represent the padding size on both sides of the current dimension, ``(front_offset, back_offset)``
1824 mode: One of the following string values. Default: ``'constant'``
1825
1826 * ``'constant'``: Pads with a constant value.
1827 * ``'reflect'``: Pads with the reflection of the tensor mirrored on the first and last values of the tensor along each axis.
1828 * ``'replicate'``: Pads with the edge values of tensor.
1829 constant_val: Fill value for ``'constant'`` padding. Default: 0
1830
1831 Examples:
1832 >>> import numpy as np
1833 >>> inp = Tensor([[1., 2., 3.],[4., 5., 6.]])
1834 >>> inp
1835 Tensor([[1. 2. 3.]
1836 [4. 5. 6.]], device=xpux:0)
1837 >>> F.nn.pad(inp, pad_width=((1, 1),), mode="constant")
1838 Tensor([[0. 0. 0.]
1839 [1. 2. 3.]
1840 [4. 5. 6.]
1841 [0. 0. 0.]], device=xpux:0)
1842 >>> F.nn.pad(inp, pad_width=((1, 1),), mode="constant", constant_value=9)
1843 Tensor([[9. 9. 9.]
1844 [1. 2. 3.]
1845 [4. 5. 6.]
1846 [9. 9. 9.]], device=xpux:0)
1847 >>> F.nn.pad(inp, pad_width=((1, 1), (1, 2)), mode="reflect")
1848 Tensor([[5. 4. 5. 6. 5. 4.]
1849 [2. 1. 2. 3. 2. 1.]
1850 [5. 4. 5. 6. 5. 4.]
1851 [2. 1. 2. 3. 2. 1.]], device=xpux:0)
1852 >>> F.nn.pad(inp, pad_width=((1, 1), (1, 2)), mode="replicate")
1853 Tensor([[1. 1. 2. 3. 3. 3.]
1854 [1. 1. 2. 3. 3. 3.]
1855 [4. 4. 5. 6. 6. 6.]
1856 [4. 4. 5. 6. 6. 6.]], device=xpux:0)
1857
1858 """
1859 p_offsets = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
1860
1861 assert mode.lower() in ["constant", "edge", "replicate", "reflect"]
1862
1863 if mode.lower() == "edge":
1864 mode = "replicate"
1865
1866 for i in range(0, len(pad_width)):
1867 p_offsets[i * 2] = pad_width[i][0]
1868 p_offsets[i * 2 + 1] = pad_width[i][1]
1869
1870 op = builtin.Padding(

Callers 4

forwardMethod · 0.50
calc_convMethod · 0.50
calc_convMethod · 0.50
calc_conv_quantizedMethod · 0.50

Calls 1

applyFunction · 0.50

Tested by

no test coverage detected