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

PyTorch · Function

torch.full

Creates a tensor with a specified shape and fills every element with one scalar value. Its dtype is inferred from the fill value unless dtype is provided explicitly.

How it works

torch.full constructs a tensor with the requested shape and places the same scalar in every element. Unless explicitly specified, the result’s dtype is inferred from the fill value, while layout, device, and gradient tracking can be controlled with keyword arguments.

Syntax

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

Parameters

NameTypeDescription
sizeint...Shape of the output tensor, provided as integers in a list, tuple, or torch.Size.
fill_valueScalarScalar written into every output element.
outTensor, optionalOptional destination tensor.
dtypetorch.dtype, optionalOptional output dtype; if None, the global default is used when applicable.
layouttorch.layout, optionalOptional output layout; defaults to torch.strided.
devicetorch.device, optionalOptional output device; if None, the current default device is used.
requires_gradbool, optionalWhether autograd records operations on the returned tensor; defaults to False.

Returns

A Tensor of the requested size, filled with fill_value.

Key points

  • Use torch.full when every element should start with one identical scalar value.
  • The shape can be supplied as a list, tuple, or torch.Size containing integer dimensions.
  • fill_value is a scalar applied uniformly throughout the newly created tensor.
  • dtype lets callers request the output type explicitly rather than using inferred or default selection.
  • layout selects the tensor memory layout, with torch.strided as the documented default.
  • out can direct the operation to use a supplied output tensor.

Common mistakes

  • The fill value influences the output dtype when dtype is omitted; specify dtype when an exact type is required.
  • For floating-point fill values, omitting dtype delegates dtype selection to the configured global default.
  • With device=None, placement follows the current default device and tensor-type defaults rather than being universally fixed.
  • requires_grad controls whether autograd records operations; its default is False.

Example: Create a filled tensor

import torch

x = torch.full((2, 3), 7, dtype=torch.int64)
print(x)
# tensor([[7, 7, 7],
#         [7, 7, 7]])

Creates and prints a 2-by-3 integer tensor whose every element is 7.

When to use it

Initialize constant tensors

Building masks or indicator arrays with a known constant.

Prepare deterministic fixtures

Creating test inputs or baseline values with a chosen shape.

Control tensor properties

Allocating a tensor directly on a selected device and with a selected dtype.

Interview questions

QWhat does torch.full do, and what forms can size take?

torch.full creates a tensor with the shape described by size and fills every element with fill_value. The size may be an integer sequence, tuple, or torch.Size.

Follow-up: How would you guarantee the result is integer-valued? Pass an explicit integer dtype, such as dtype=torch.int64, instead of relying on inference.

QHow is the output dtype selected?

If dtype is None, PyTorch infers the tensor dtype from fill_value. An explicit dtype overrides that inference and is preferable when a stable type is part of an interface.

Follow-up: What determines dtype when the fill value is floating-point and dtype is omitted? The configured global default dtype is used.

QWhich keyword arguments control the returned tensor?

The function accepts keyword options for out, dtype, layout, device, and requires_grad. layout defaults to torch.strided, requires_grad defaults to False, and device defaults according to the current default tensor settings.

Follow-up: How can placement be made explicit? Supply a torch.device through the device keyword, such as device='cuda'.

  • torch.empty
  • torch.ones
  • torch.zeros
  • torch.full_like

Source: torch.full — PyTorch 2.13 documentation. Checked 2026-10-16.