zeros_like#

ivy.zeros_like(x, /, *, dtype=None, device=None, out=None)[source]#

Return a new array filled with zeros and having the same shape as an input array x.

Parameters:
  • x (Union[Array, NativeArray]) – input array from which to derive the output array shape.

  • dtype (Optional[Union[Dtype, NativeDtype]], default: None) – output array data type. If dtype is None, the output array data type must be inferred from x. Default: None.

  • device (Optional[Union[Device, NativeDevice]], default: None) – device on which to place the created array. If device is None, the output array device must be inferred from x. Default: None.

  • 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 – an array having the same shape as x and filled with zeros.

This function conforms to the Array API Standard. This docstring is an extension of the docstring 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.zeros_like(x)
>>> print(y)
ivy.array([0, 0, 0, 0, 0, 0])
>>> x = ivy.array([[0, 1, 2],[3, 4, 5]], dtype = ivy.float32)
>>> y = ivy.zeros_like(x)
>>> print(y)
ivy.array([[0., 0., 0.],
        [0., 0., 0.]])
>>> x = ivy.array([3., 2., 1.])
>>> y = ivy.ones(3)
>>> ivy.zeros_like(x, out=y)
>>> print(y)
ivy.array([0., 0., 0.])

With ivy.NativeArray input:

>>> x = ivy.native_array([[3, 8, 2],[2, 8, 3]])
>>> y = ivy.zeros_like(x)
>>> print(y)
ivy.array([[0, 0, 0],[0, 0, 0]])
>>> x = ivy.native_array([3, 8, 2, 0, 0, 2])
>>> y = ivy.zeros_like(x, dtype=ivy.IntDtype('int32'), device=ivy.Device('cpu'))
>>> print(y)
ivy.array([0, 0, 0, 0, 0, 0])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([3, 2, 1]), b=ivy.array([8, 2, 3]))
>>> y = ivy.zeros_like(x)
>>> print(y)
{
    a: ivy.array([0, 0, 0]),
    b: ivy.array([0, 0, 0])
}

With ivy.Array input:

>>> x = ivy.array([2, 3, 8, 2, 1])
>>> y = x.zeros_like()
>>> print(y)
ivy.array([0, 0, 0, 0, 0])

With :class:’ivy.Container’ input:

>>> x = ivy.Container(a=ivy.array([3., 8.]), b=ivy.array([2., 2.]))
>>> y = x.zeros_like()
>>> print(y)
{
    a: ivy.array([0., 0.]),
    b: ivy.array([0., 0.])
}
Array.zeros_like(self, /, *, dtype=None, device=None, out=None)[source]#

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

Parameters:
  • self (Array) – input array from which to derive the output array shape.

  • dtype (Optional[Union[Dtype, NativeDtype]], default: None) – output array data type. If dtype is None, the output array data type must be inferred from self. Default: None.

  • device (Optional[Union[Device, NativeDevice]], default: None) – device on which to place the created array. If device is None, the output array device must be inferred from self. Default: None.

  • 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 – an array having the same shape as self and filled with zeros.

Container.zeros_like(self, /, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, *, dtype=None, device=None, out=None)[source]#

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

Parameters:
  • self (Container) – input array or container from which to derive the output container shape.

  • 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.

  • dtype (Optional[Union[Dtype, NativeDtype, Container]], default: None) – output array data type. If dtype is None, the output container data type must be inferred from self. Default: None.

  • device (Optional[Union[Device, NativeDevice, Container]], default: None) – device on which to place the created array. If device is None, the output container device must be inferred from self. Default: None.

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

Return type:

Container

Returns:

ret – an container having the same shape as x and filled with zeros.