Skip to main content

How to Create a Tensor of Zeros in PyTorch with torch.zeros

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A tensor of zeros is the usual starting point when you'll fill in the values later, such as counts or running totals. You give PyTorch the shape, and it makes the tensor. This guide uses torch.zeros for a row, a table and a table of whole numbers in PyTorch 2.15.0.

Syntax​

Syntax
torch.zeros(*size, dtype=None, device=None)
PartWhat it means
*sizeThe shape: one whole number per dimension, such as 2, 3 for two rows of three. A tuple or list, such as (2, 3), works too
dtypeOptional, given by name. The data type, such as torch.int64 for whole numbers. Left out, it's PyTorch's default, torch.float32
deviceOptional, given by name. Where to make the tensor, such as the GPU. Left out, it's the CPU

PyTorch's reference describes it as returning "a tensor filled with the scalar value 0, with the shape defined by the variable argument size."

The Example​

A script makes three tensors of zeros: a row of four, a table of two rows and three columns, and the same table as whole numbers.

Prerequisites​

  • A uv project with PyTorch and NumPy. uv add torch numpy adds both; without NumPy, PyTorch 2.15.0 prints a warning each time it starts. This guide uses PyTorch 2.15.0 and NumPy 2.5.5 on Python 3.15.2, with uv 0.13.10, on an Apple M1 Pro.

Step 1: Write the Script​

zeros.py
import torch

print(torch.zeros(4))
print(torch.zeros(2, 3))
print(torch.zeros(2, 3, dtype=torch.int64))

The zeros.py script in a macOS-style code window: print of torch.zeros(4), torch.zeros(2, 3) and torch.zeros(2, 3, dtype=torch.int64).

One number makes a single row; two numbers make a table, rows first. The last line asks for whole numbers with dtype=torch.int64.

Step 2: Run It​

Terminal
uv run python zeros.py
Output
tensor([0., 0., 0., 0.])
tensor([[0., 0., 0.],
[0., 0., 0.]])
tensor([[0, 0, 0],
[0, 0, 0]])

The terminal after uv run python zeros.py: a row of four 0. values, a two-by-three table of 0. values, and a two-by-three table of 0 values.

The first two tensors print 0. with a dot: they're decimals, torch.float32, the default. The third prints 0 without one, because dtype=torch.int64 made whole numbers. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting whole numberstorch.zeros(2, 3) makes decimals, printed 0.Add dtype=torch.int64
Reading torch.zeros(2, 3) as two columnsIt's two rows of three columns: the first number is the rowsGive the rows first

FAQ​

How do I make the zeros on the GPU?​

Give the device: with device picked as in How to Check Whether PyTorch Can Use a GPU (CUDA or Apple MPS), torch.zeros(2, 3, device=device) printed the table with device='mps:0' on this Mac.

How do I make zeros in the shape of another tensor?​

torch.zeros_like() copies the shape and the data type: for a two-by-three table of whole-number scores, torch.zeros_like(scores) printed a two-by-three table of 0.

Conclusion​

torch.zeros(4) made a row of four zeros, torch.zeros(2, 3) a table of two rows and three columns, both as torch.float32 decimals, and dtype=torch.int64 made the same table as whole numbers.

Additional Resources​