PyTorch .zeros()
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 toNone.dtype: The datatype (torch.dtype) of the zeros, defaults toNone.layout: The layout (torch.layout) of the output tensor, defaults totorch.strided.device: The device (torch.device) of the output tensor, defaults toNone.requires_grad: A boolean indicating whether autograd will record operations on the output tensor, defaults toFalse.
Example
import torch# Define a tensor with three values in one rowt0 = torch.zeros(3)# Define a tensor with four rows, two values per row. All values have the datatype torch.int16t1 = 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:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours