zero_pad#

ivy.zero_pad(x, /, pad_width, *, out=None)[source]#

Pad an array with zeros.

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

  • pad_width (Iterable[Tuple[int]]) – Number of values padded to the edges of each axis. Specified as ((before_1, after_1), … (before_N, after_N)), where N is number of axes of x.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

  • ret – Padded array of rank equal to x with shape increased according to pad_width.

  • This function conforms to the `Array API Standard

  • <https (//data-apis.org/array-api/latest/>`_. This docstring is an extension of the)

  • `docstring <https (//data-apis.org/array-api/latest/)

  • API_specification/generated/array_api.concat.html>`_

  • in the standard.

  • Both the description and the type hints above assumes 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])
>>> y = ivy.zero_pad(x, pad_width = [[2, 3]])
>>> print(y)
ivy.array([0., 0., 1., 2., 3., 4., 5., 6., 0., 0., 0.])
>>> x = ivy.array([[1., 2., 3.],[4, 5, 6]])
>>> y = ivy.zero_pad(x, pad_width = [[2, 3], [2, 3]])
>>> print(y)
ivy.array([[0., 0., 0., 0., 0., 0., 0., 0.],
   [0., 0., 0., 0., 0., 0., 0., 0.],
   [0., 0., 1., 2., 3., 0., 0., 0.],
   [0., 0., 4., 5., 6., 0., 0., 0.],
   [0., 0., 0., 0., 0., 0., 0., 0.],
   [0., 0., 0., 0., 0., 0., 0., 0.],
   [0., 0., 0., 0., 0., 0., 0., 0.]])
>>> x = ivy.Container(a = ivy.array([1., 2., 3.]), b = ivy.array([3., 4., 5.]))
>>> y = ivy.zero_pad(x, pad_width = [[2, 3]])
>>> print(y)
{
    a: ivy.array([0., 0., 1., 2., 3., 0., 0., 0.]),
    b: ivy.array([0., 0., 3., 4., 5., 0., 0., 0.])
}
Array.zero_pad(self, /, pad_width, *, out=None)[source]#

ivy.Array instance method variant of ivy.zero_pad. This method simply wraps the function, and so the docstring for ivy.zero_pad also applies to this method with minimal changes.

Parameters:
  • self (Array) – Input array to pad.

  • pad_width (Iterable[Tuple[int]]) – Number of values padded to the edges of each axis. Specified as ((before_1, after_1), … (before_N, after_N)), where N is number of axes of x.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – Padded array of rank equal to x with shape increased according to pad_width.

Examples

With ivy.Array input:

>>> x = ivy.array([1., 2., 3.,4, 5, 6])
>>> y = x.zero_pad(pad_width = [[2, 3]])
>>> print(y)
ivy.array([0., 0., 1., 2., 3., 4., 5., 6., 0., 0., 0.])
Container.zero_pad(self, /, pad_width, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container instance method variant of ivy.zero_pad. This method simply wraps the function, and so the docstring for ivy.zero_pad also applies to this method with minimal changes.

Parameters:
  • self (Container) – Input array to pad.

  • pad_width (Union[Iterable[Tuple[int]], Container]) – Number of values padded to the edges of each axis. Specified as ((before_1, after_1), … (before_N, after_N)), where N is number of axes of x.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

  • out (Optional[Container], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Container

Returns:

ret – Padded array of rank equal to x with shape increased according to pad_width.

Examples

With ivy.Container input:

>>> x = ivy.Container(a = ivy.array([1., 2., 3.]), b = ivy.array([3., 4., 5.]))
>>> y = x.zero_pad(pad_width = [[2, 3]])
>>> print(y)
{
    a: ivy.array([0., 0., 1., 2., 3., 0., 0., 0.]),
    b: ivy.array([0., 0., 3., 4., 5., 0., 0., 0.])
}