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

PyTorch · Function

torch.zeros_like

Creates a zero-filled tensor with the same size as an input tensor. By default, it also follows the input's dtype, layout, device, and preserved memory format.

How it works

torch.zeros_like accepts a reference tensor and creates a zero-filled tensor with the same size. Unless overridden, it also follows the reference tensor's data type, layout, device, and preserved memory format; requires_grad controls whether autograd records operations on the result.

Syntax

torch.zeros_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format) → Tensor

Parameters

NameTypeDescription
inputTensorTensor whose size determines the output tensor's size.
dtypetorch.dtype, optionalDesired data type; None uses input.dtype.
layouttorch.layout, optionalDesired layout; None uses input.layout.
devicetorch.device, optionalDesired device; None uses input.device.
requires_gradbool, optionalWhether autograd records operations on the returned tensor; defaults to False.
memory_formattorch.memory_format, optionalDesired memory format; defaults to torch.preserve_format.

Returns

Tensor

Key points

  • The result contains zeros and has the same dimensions as the reference tensor.
  • The input tensor's size determines the output shape; no separate shape tuple is required.
  • Use dtype= to request a different data type for the created tensor.
  • Use layout= to select the output tensor layout instead of inheriting the input layout.
  • Use device= to place the result on a selected device rather than the input's device.
  • Set requires_grad=True when the result should participate in autograd recording.
  • memory_format= controls the output memory format and defaults to preserving the input format.

Common mistakes

  • Do not pass out to torch.zeros_like; use torch.zeros with the input size and out when an output buffer is required.

Example: Create a matching zero tensor

import torch

x = torch.empty((2, 3), dtype=torch.float64, device="cpu")
z = torch.zeros_like(x)

print(z)
print(z.dtype, z.device, z.shape)

Creates a zero-filled tensor that matches x in shape, data type, layout, and device.

When to use it

Shape-matched initialization

Initialize a tensor-shaped buffer without manually copying dimensions.

Device-local buffers

Create zeros alongside an existing CPU, CUDA, or other-device tensor.

Controlled tensor properties

Build a zero baseline while retaining or overriding numeric type and layout.

Interview questions

QWhat does torch.zeros_like do?

It returns a tensor filled with zeros whose size matches the input tensor. By default, its dtype, layout, and device come from the input, while optional keyword arguments can override those choices.

Follow-up: How would you create the same shape on another device? Pass the target device with device=, for example torch.zeros_like(x, device="cuda").

QWhich properties are inherited, and which options can be overridden?

The input tensor determines the output size. None defaults dtype, layout, and device to the corresponding input attributes, while memory_format defaults to torch.preserve_format.

Follow-up: What controls gradient tracking on the result? requires_grad does; its documented default is False.

QHow is the function called, and what does it return?

Its signature has one input tensor followed by keyword-only dtype, layout, device, requires_grad, and memory_format arguments. It returns a Tensor.

Follow-up: What is the documented limitation involving out? zeros_like does not accept out; use torch.zeros(input.size(), out=output) instead.

  • torch.zeros
  • torch.ones_like
  • torch.empty_like
  • torch.full_like

Source: torch.zeros_like — PyTorch 2.14 documentation. Checked 2026-10-16.