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

PyTorch · Function

torch.ones

Creates a tensor filled with 1, using the supplied dimensions as its shape. Optional keywords control its output tensor, dtype, layout, device, and autograd behavior.

How it works

torch.ones creates a new tensor whose dimensions come from the supplied size arguments or a single shape collection. It fills every element with 1 and lets you choose the data type, layout, device, and whether autograd records operations involving the result.

Syntax

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

Parameters

NameTypeDescription
sizeint...Dimensions of the output; provide multiple integers or one collection such as a list or tuple.
outTensor, optionalOptional output tensor to receive the result.
dtypetorch.dtype, optionalData type of the returned tensor; None uses the global default dtype.
layouttorch.layout, optionalLayout of the returned tensor; defaults to torch.strided.
devicetorch.device, optionalDevice on which the returned tensor is allocated; None uses the current default-tensor device.
requires_gradbool, optionalWhether autograd records operations on the returned tensor; defaults to False.

Returns

Tensor

Key points

  • Creates a new tensor in which every element is 1, with dimensions determined by size.
  • The shape may be written as separate dimensions, such as torch.ones(2, 3), or as one list or tuple, such as torch.ones((2, 3)).
  • The default layout is torch.strided, while the layout keyword can request a different supported tensor layout.
  • requires_grad determines whether autograd records operations involving the returned tensor, and its default is False.

Common mistakes

  • Leaving dtype unset uses PyTorch's current default dtype, so the result's type is not fixed by the function call alone.
  • Leaving device unset selects the current device associated with the default tensor type; specify it explicitly when placement matters.

Example: Create a typed tensor of ones

import torch

x = torch.ones((2, 3), dtype=torch.float64, device="cpu")
print(x)
# tensor([[1., 1., 1.],
#         [1., 1., 1.]], dtype=torch.float64)

Creates a 2-by-3 CPU tensor filled with ones using 64-bit floating-point values.

When to use it

Build constant-one inputs or parameters

Model initialization

Create an all-ones mask with a chosen shape

Mask construction

Allocate tensors directly on a target device

Device-aware setup

Create constants with an explicit dtype

Typed computation

Interview questions

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

torch.ones returns a tensor filled with the scalar value 1. Its shape is defined by size, which can be passed as multiple integer arguments or as a list or tuple.

Follow-up: How would you create a 2×3 tensor without relying on the default floating-point type? Pass a shape and an explicit dtype, such as torch.ones((2, 3), dtype=torch.int32).

QWhich options control the properties of the returned tensor?

The function accepts dtype, layout, device, and requires_grad as keyword options. dtype controls the returned data type, layout controls tensor layout, device controls placement, and requires_grad controls autograd recording.

Follow-up: How can placement be made deterministic? Supply an explicit torch.device, such as device="cuda" or device="cpu", rather than relying on the default.

QWhat is the default behavior of requires_grad?

requires_grad defaults to False, so autograd does not record operations on the returned tensor unless requested. Set requires_grad=True when the tensor should participate in recorded gradient computation.

Follow-up: What other allocation-related option can reuse storage? The optional out argument supplies an output tensor for the operation.

  • torch.empty
  • torch.full
  • torch.ones_like
  • torch.rand
  • torch.zeros

Source: torch.ones — PyTorch 2.12 documentation. Checked 2026-10-16.