zeros

paddle. zeros ( shape: ShapeLike, dtype: DTypeLike | None = None, name: str | None = None, *, out: paddle.Tensor | None = None, device: PlaceLike | None = None, requires_grad: bool = False, pin_memory: bool = False ) paddle.Tensor [source]

Creates a tensor of specified shape and dtype, and fills it with 0.

Note

Alias Support: The parameter name size can be used as an alias for shape. shape can be a variable number of arguments. For example:

System Message: ERROR/3 (/usr/local/lib/python3.10/site-packages/paddle/tensor/creation.py:docstring of paddle.tensor.creation.zeros, line 7)

Unexpected indentation.

paddle.ones(1, 2, 3, dtype=paddle.float32) paddle.ones(size=[1, 2, 3], dtype=paddle.float32)

Parameters
  • shape (tuple|list|Tensor|variable number of arguments) – Shape of the Tensor to be created. The data type is int32 or int64 . alias: size. If shape is a list or tuple, each element of it should be integer or 0-D Tensor with shape []. If shape is an Tensor, it should be an 1-D Tensor which represents a list. shape can be a variable number of arguments.

  • dtype (np.dtype|str, optional) – Data type of output Tensor, it supports bool, float16, float32, float64, int32 and int64. Default: if None, the data type is float32. property. For more information, please refer to api_guide_Name.

  • out (Tensor, optional) – The output tensor.

  • device (PlaceLike|None, optional) – The desired device of returned tensor. if None, uses the current device for the default tensor type (see paddle.device.set_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types. Default: None.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

  • name (str|None, optional) – The default value is None. Normally there is no need for user to set this

  • pin_memory (bool, optional) – If set, return tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False

Returns

A tensor of data type dtype with shape shape and all elements set to 0.

Return type

Tensor

Examples

>>> import paddle

>>> # shape is a list/tuple
>>> data1 = paddle.zeros(shape=[3, 2])
>>> print(data1.numpy())
[[0. 0.]
 [0. 0.]
 [0. 0.]]

>>> # shape is a Tensor
>>> shape = paddle.to_tensor([3, 2])
>>> data2 = paddle.zeros(shape=shape)
>>> print(data2.numpy())
[[0. 0.]
 [0. 0.]
 [0. 0.]]

>>> # shape is a Tensor List
>>> shape = [paddle.to_tensor(3), paddle.to_tensor(2)]
>>> data3 = paddle.zeros(shape=shape)
>>> print(data3.numpy())
[[0. 0.]
 [0. 0.]
 [0. 0.]]

Used in the guide/tutorials