Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Function

torch.zeros

Creates a tensor filled with zeros, using dimensions supplied as separate arguments or as a collection. Optional keywords select its dtype, layout, device, output buffer, and autograd behavior.

How it works

torch.zeros accepts dimensions either as separate integer arguments or as a collection, then allocates a tensor filled with zero. Optional keyword arguments control its output buffer, data type, layout, device, and whether autograd records operations on it.

Syntax

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

Parameters

NameTypeDescription
sizeint ...Dimensions defining the output shape; pass integer dimensions individually or as a list or tuple.
outTensor, optionalOptional destination tensor for the result.
dtypetorch.dtype, optionalOptional data type of the returned tensor; defaults to the global default dtype when omitted.
layouttorch.layout, optionalOptional layout of the returned tensor; defaults to torch.strided.
devicetorch.device, optionalOptional device for the returned tensor; omitted values follow the current default device.
requires_gradbool, optionalWhether autograd records operations on the returned tensor; defaults to False.

Returns

Tensor

Key points

  • The function returns a tensor whose every element is zero.
  • The size argument determines the complete output shape.
  • Shape dimensions may be passed individually or grouped in a list or tuple.
  • The optional dtype argument selects the output tensor's data type.
  • The optional layout argument selects the output tensor's layout.
  • The optional out argument designates a tensor for receiving the result.

Common mistakes

  • Leaving dtype unset delegates type selection to PyTorch's global default dtype.
  • Leaving device unset does not universally mean CPU; it follows the current default-device rules.
  • Set requires_grad=True only when the newly created tensor should participate in autograd tracking.

Example: Create a zero-filled tensor

import torch

x = torch.zeros(2, 3, dtype=torch.float32)
print(x)

Creates a 2-by-3 CPU tensor whose six elements are zero and whose dtype is explicitly float32.

When to use it

Initialize storage

Preallocating a numeric buffer with a known shape before filling it during a computation.

Build zero-valued tensors

Creating a zero-valued matrix or vector for model inputs, masks, counters, or accumulators.

Control tensor placement and type

Selecting dtype and device explicitly when preparing tensors for a model or accelerator.

Interview questions

QWhat does torch.zeros create, and how is its shape specified?

It creates and returns a tensor filled with scalar zero. The requested shape comes from size, which may be supplied as separate dimensions or as a list or tuple.

Follow-up: How would you preserve an existing tensor's shape and properties instead? Use torch.zeros_like(input), which is the corresponding shape-and-properties-oriented factory.

QWhat are the important dtype, layout, and device defaults?

dtype controls the returned tensor's data type, while layout controls its tensor layout. If dtype is None, PyTorch uses its global default dtype; the default layout is torch.strided.

Follow-up: What determines the device when device is omitted? PyTorch uses the current device for the default tensor type; CPU tensor types use CPU, while CUDA types use the current CUDA device.

QHow do requires_grad and out affect the operation?

requires_grad defaults to False. When enabled, autograd records operations on the returned tensor; out can provide an output tensor instead of allocating a new result.

Follow-up: When is zeros_like preferable? When an existing input tensor defines the desired size, dtype, layout, and device.

  • torch.zeros_like
  • torch.ones
  • torch.empty
  • torch.full

Source: torch.zeros — PyTorch main documentation. Checked 2026-10-16.