.zeros()

Anonymous contributor's avatar
Anonymous contributor
Published Aug 23, 2024
Contribute to Docs

The .zeros() method returns a tensor filled with zeros that has a specified shape. The output tensor, its datatype, layout, device, and whether autograd records operations can me specified.

Syntax

torch.zeros(size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)

The parameters are as follows:

  • size: The shape of the tensor, specified as a variable, tuple, or list of integers.
  • out: The output Tensor, defaults to None.
  • dtype: The datatype (torch.dtype) of the zeros, defaults to None.
  • layout: The layout (torch.layout) of the output tensor, defaults to torch.strided.
  • device: The device (torch.device) of the output tensor, defaults to None.
  • requires_grad: A boolean indicating whether autograd will record operations on the output tensor, defaults to False.

Example

import torch
# Define a tensor with three values in one row
t0 = torch.zeros(3)
# Define a tensor with four rows, two values per row. All values have the datatype torch.int16
t1 = torch.zeros((4, 2), dtype=torch.int16)
print(t0)
print(t1)

The returned tensors are as follows:

tensor([0., 0., 0.])
tensor([[0, 0],
[0, 0],
[0, 0],
[0, 0]], dtype=torch.int16)

Codebyte Example

Run the following code to know how the .zeros() method works:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn PyTorch on Codecademy