How to Create a Tensor of Zeros in PyTorch with torch.zeros
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
torch.zeros(*size, dtype=None, device=None)
| Part | What it means |
|---|---|
*size | The 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 |
dtype | Optional, given by name. The data type, such as torch.int64 for whole numbers. Left out, it's PyTorch's default, torch.float32 |
device | Optional, 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 numpyadds 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
import torch
print(torch.zeros(4))
print(torch.zeros(2, 3))
print(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
uv run python zeros.py
tensor([0., 0., 0., 0.])
tensor([[0., 0., 0.],
[0., 0., 0.]])
tensor([[0, 0, 0],
[0, 0, 0]])

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
| Mistake | What happens | Do this instead |
|---|---|---|
| Expecting whole numbers | torch.zeros(2, 3) makes decimals, printed 0. | Add dtype=torch.int64 |
Reading torch.zeros(2, 3) as two columns | It's two rows of three columns: the first number is the rows | Give 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
- torch.zeros — PyTorch's reference for the function.
- torch.zeros_like — zeros in the shape of another tensor.
- Tensors tutorial — PyTorch's introduction to tensors.
