pad#

ivy.pad(input, pad_width, /, *, mode='constant', stat_length=1, constant_values=0, end_values=0, reflect_type='even', **kwargs)[source]#

Pad an array.

Parameters:
  • input (Union[Array, NativeArray]) – Input array to pad.

  • pad_width (Union[Iterable[Tuple[int]], int]) –

    Number of values padded to the edges of each axis.
    • ((before_1, after_1), … (before_N, after_N)) yields unique pad widths for each axis.

    • ((before, after),) yields same before and after pad for each axis.

    • pad (integer) is shortcut for before = after = pad width for all axes.

  • mode (Union[Literal['constant', 'dilated', 'edge', 'linear_ramp', 'maximum', 'mean', 'median', 'minimum', 'reflect', 'symmetric', 'wrap', 'empty'], Callable], default: 'constant') –

    One of the following string values or a user-supplied function.
    • ”constant”: Pads with a constant value.

    • ”edge”: Pads with the input’s edge values.

    • ”linear_ramp”: Pads with the linear ramp between end_value and the input’s edge value.

    • ”maximum”: Pads with the maximum value of all or part of the vector along each axis.

    • ”mean”: Pads with the mean value of all or part of the vector along each axis.

    • ”median”: Pads with the median value of all or part of the vector along each axis.

    • ”minimum”: Pads with the minimum value of all or part of the vector along each axis.

    • ”reflect”: Pads with the reflection mirrored on the first and last values of the vector along each axis.

    • ”symmetric”: Pads with the reflection of the vector mirrored along the edge of the input.

    • ”wrap”: Pads with the wrap of the vector along the axis. The first values are used to pad the end and the end values are used to pad the beginning.

    • ”empty”: Pads with undefined values.

    • <function>: Pads with a user-defined padding function. The padding function should modify a rank 1 array following the signature padding_func(vector, iaxis_pad_width, iaxis, kwargs), where:

      • vector is a rank 1 array already padded with zeros. Padded values are vector[:iaxis_pad_width[0]] and vector[-iaxis_pad_width[1]:].

      • iaxis_pad_width is a 2-tuple of ints, where iaxis_pad_width[0] represents the number of values padded at the beginning of vector and iaxis_pad_width[1] represents the number of values padded at the end of vector.

      • iaxis is the axis currently being calculated.

      • kwargs is a dict of keyword arguments the function requires.

  • stat_length (Union[Iterable[Tuple[int]], int], default: 1) –

    Used in “maximum”, “mean”, “median”, and “minimum”. Number of values at edge of each axis used to calculate the statistic value.

    • ((before_1, after_1), … (before_N, after_N)) yields unique statistic lengths for each axis.

    • ((before, after),) yields same before and after statistic lengths for each axis.

    • stat_length (integer) is a shortcut for before = after = stat_length length for all axes.

    • None uses the entire axis.

  • constant_values (Union[Iterable[Tuple[Number]], Number], default: 0) –

    Used in “constant”. The values to set the padded values for each axis.
    • ((before_1, after_1), … (before_N, after_N)) yields unique pad constants for each axis.

    • ((before, after),) yields same before and after constants for each axis.

    • constant (integer) is a shortcut for before = after = constant for all axes.

  • end_values (Union[Iterable[Tuple[Number]], Number], default: 0) –

    Used in “linear_ramp”. The values used for the ending value of the linear_ramp and that will form the edge of the padded array.

    • ((before_1, after_1), … (before_N, after_N)) yields unique end values for each axis.

    • ((before, after),) yields same before and after end values for each axis

    • end (integer) is a shortcut for before = after = end for all axes.

  • reflect_type (Literal['even', 'odd'], default: 'even') – Used in “reflect”, and “symmetric”. The “even” style is the default with an unaltered reflection around the edge value. For the “odd” style, the extended part of the array is created by subtracting the reflected values from two times the edge value.

Return type:

Array

Returns:

ret – Padded array of the same rank as the input but with shape increased according to pad_width.

Both the description and the type hints above assume an array input for simplicity, but this function is nestable, and therefore also accepts ivy.Container instances in place of any of the arguments.

Examples

With ivy.Array input:

>>> x = ivy.array([[1, 2, 3], [4, 5, 6]])
>>> padding = ((1, 1), (2, 2))
>>> y = ivy.pad(x, padding, mode="constant", constant_values=0)
>>> print(y)
ivy.array([[0, 0, 0, 0, 0, 0, 0],
           [0, 0, 1, 2, 3, 0, 0],
           [0, 0, 4, 5, 6, 0, 0],
           [0, 0, 0, 0, 0, 0, 0]])
>>> x = ivy.array([[1, 2, 3], [4, 5, 6]])
>>> padding = ((1, 1), (2, 2))
>>> y = ivy.pad(x, padding, mode="reflect")
>>> print(y)
ivy.array([[6, 5, 4, 5, 6, 5, 4],
           [3, 2, 1, 2, 3, 2, 1],
           [6, 5, 4, 5, 6, 5, 4],
           [3, 2, 1, 2, 3, 2, 1]])
>>> x = ivy.array([[1, 2, 3], [4, 5, 6]])
>>> padding = ((1, 1), (2, 2))
>>> y = ivy.pad(x, padding, mode="symmetric")
>>> print(y)
ivy.array([[2, 1, 1, 2, 3, 3, 2],
           [2, 1, 1, 2, 3, 3, 2],
           [5, 4, 4, 5, 6, 6, 5],
           [5, 4, 4, 5, 6, 6, 5]])

With ivy.NativeArray input:

>>> x = ivy.native_array([[1, 2, 3], [4, 5, 6]])
>>> padding = ((1, 1), (2, 2))
>>> y = ivy.pad(x, padding, mode="constant", constant_values=7)
>>> print(y)
ivy.array([[7, 7, 7, 7, 7, 7, 7],
           [7, 7, 1, 2, 3, 7, 7],
           [7, 7, 4, 5, 6, 7, 7],
           [7, 7, 7, 7, 7, 7, 7]])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([0, 1, 2]), b=ivy.array([4, 5, 6]))
>>> padding = (1, 1)
>>> y = ivy.pad(x, padding, mode="constant")
>>> print(y)
{
    a: ivy.array([0, 0, 1, 2, 0]),
    b: ivy.array([0, 4, 5, 6, 0])
}
Array.pad(self, pad_width, /, *, mode='constant', stat_length=1, constant_values=0, end_values=0, reflect_type='even', out=None, **kwargs)[source]#

ivy.Array instance method variant of ivy.pad.

This method simply wraps the function, and so the docstring for ivy.pad also applies to this method with minimal changes.

Return type:

Array

Container.pad(self, pad_width, /, *, mode='constant', stat_length=1, constant_values=0, end_values=0, reflect_type='even', key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None, **kwargs)[source]#

ivy.Container instance method variant of ivy.pad.

This method simply wraps the function, and so the docstring for ivy.pad also applies to this method with minimal changes.

Return type:

Container