0
0
PytorchHow-ToBeginner · 3 min read

How to Create a Zeros Tensor in PyTorch: Simple Guide

In PyTorch, you can create a tensor filled with zeros using torch.zeros(). You specify the shape as a tuple, like torch.zeros((2, 3)), which creates a 2 by 3 tensor filled with zeros.
📐

Syntax

The basic syntax to create a zeros tensor in PyTorch is torch.zeros(size, dtype=None, device=None, requires_grad=False).

  • size: A tuple defining the shape of the tensor (e.g., (2, 3) for 2 rows and 3 columns).
  • dtype: (Optional) The data type of the tensor elements, like torch.float32 or torch.int64. Defaults to torch.float32.
  • device: (Optional) The device where the tensor will be stored, such as CPU or GPU.
  • requires_grad: (Optional) If set to True, PyTorch will track operations on the tensor for automatic differentiation.
python
torch.zeros(size, dtype=None, device=None, requires_grad=False)
💻

Example

This example creates a 2x3 tensor filled with zeros and prints it. It also shows how to specify the data type and device.

python
import torch

# Create a 2x3 zeros tensor
zeros_tensor = torch.zeros((2, 3))
print('Zeros tensor on CPU:')
print(zeros_tensor)

# Create a 2x3 zeros tensor with integer type
zeros_int_tensor = torch.zeros((2, 3), dtype=torch.int64)
print('\nZeros tensor with int64 type:')
print(zeros_int_tensor)

# Create a zeros tensor on GPU if available
if torch.cuda.is_available():
    zeros_cuda = torch.zeros((2, 3), device='cuda')
    print('\nZeros tensor on GPU:')
    print(zeros_cuda)
else:
    print('\nGPU not available, skipping GPU tensor creation.')
Output
Zeros tensor on CPU: tensor([[0., 0., 0.], [0., 0., 0.]]) Zeros tensor with int64 type: tensor([[0, 0, 0], [0, 0, 0]]) GPU not available, skipping GPU tensor creation.
⚠️

Common Pitfalls

Some common mistakes when creating zeros tensors in PyTorch include:

  • Forgetting to pass the size as a tuple, e.g., writing torch.zeros(2, 3) instead of torch.zeros((2, 3)). The former works but is less clear and can cause confusion.
  • Not specifying dtype when integer zeros are needed, which defaults to floating point.
  • Trying to create a tensor on GPU without checking if CUDA is available, which causes errors.
python
import torch

# Wrong: size not in tuple (still works but less clear)
wrong_tensor = torch.zeros(2, 3)
print(wrong_tensor)

# Right: size as tuple
right_tensor = torch.zeros((2, 3))
print(right_tensor)

# Wrong: create tensor on GPU without checking
# Uncommenting the next line may cause error if no GPU
# gpu_tensor = torch.zeros((2, 3), device='cuda')

# Right: check before creating on GPU
if torch.cuda.is_available():
    gpu_tensor = torch.zeros((2, 3), device='cuda')
    print(gpu_tensor)
else:
    print('CUDA not available, skipping GPU tensor creation.')
Output
tensor([[0., 0., 0.], [0., 0., 0.]]) tensor([[0., 0., 0.], [0., 0., 0.]]) CUDA not available, skipping GPU tensor creation.
📊

Quick Reference

Here is a quick summary of how to create zeros tensors in PyTorch:

FunctionDescriptionExample
torch.zeros(size)Creates a tensor of zeros with given shapetorch.zeros((2, 3))
torch.zeros(size, dtype=torch.int64)Zeros tensor with integer typetorch.zeros((2, 3), dtype=torch.int64)
torch.zeros(size, device='cuda')Zeros tensor on GPU devicetorch.zeros((2, 3), device='cuda')
torch.zeros(size, requires_grad=True)Zeros tensor tracked for gradientstorch.zeros((2, 3), requires_grad=True)

Key Takeaways

Use torch.zeros() with a size tuple to create a zeros tensor in PyTorch.
Specify dtype to control the data type of the zeros tensor.
Check for CUDA availability before creating tensors on GPU to avoid errors.
Passing size as a tuple is the clearest and recommended way.
Use requires_grad=True if you want to track operations for gradient computation.