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

PyTorch · Function

torch.arange

Creates a 1-D tensor containing values from start up to, but excluding, end, separated by step. start defaults to 0 and step to 1; dtype is inferred unless specified.

How it works

torch.arange returns a one-dimensional tensor whose values begin at start and advance by step while remaining below end. The result size is determined by the interval and step, and omitted start or step values default to 0 and 1 respectively.

Syntax

torch.arange(start=0, end, step=1, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

Parameters

NameTypeDescription
startNumber, optionalStarting value of the sequence; defaults to 0.
endNumberExclusive upper endpoint of the sequence.
stepNumber, optionalDifference between adjacent values; defaults to 1.
outTensor, optionalOptional destination tensor.
dtypetorch.dtype, optionalDesired dtype; if omitted, PyTorch infers it from the inputs.
layouttorch.layout, optionalDesired tensor layout; defaults to torch.strided.
devicetorch.device, optionalDevice on which to create the tensor.
requires_gradbool, optionalWhether autograd records operations on the returned tensor; defaults to false.

Returns

A one-dimensional Tensor containing the generated range.

Key points

  • The result is always one-dimensional, with its length determined by the interval and step.
  • The end boundary is exclusive, so a value equal to end is not included.
  • Adjacent values differ by step, starting with start.
  • When the relevant inputs are not floating-point and dtype is omitted, the result uses int64.

Common mistakes

  • Floating-point bounds or steps cause dtype inference to use the current default floating-point dtype; specify dtype when exact type control matters.

Example: Create an evenly spaced range

import torch

steps = torch.arange(0, 10, 2, dtype=torch.float32)
print(steps)  # tensor([0., 2., 4., 6., 8.])

Creates floating-point values starting at 0, advancing by 2, and stopping before 10.

When to use it

Index sequences

Generate integer indices for tensor operations or iteration.

Stepped numeric ranges

Build regularly spaced numeric coordinates with a chosen increment.

Device- and dtype-specific initialization

Create a range directly on a selected device and with a chosen dtype.

Interview questions

QWhat does torch.arange generate?

It creates a one-dimensional tensor containing values in the half-open interval [start, end), separated by step. The number of elements is the ceiling of (end - start) divided by step.

Follow-up: What happens when start is omitted? start defaults to 0, so torch.arange(end) generates values from 0 up to, but excluding, end.

QWhich arguments are optional?

The required argument is end; start defaults to 0 and step defaults to 1. Thus torch.arange(5) produces integer values 0 through 4.

Follow-up: How can the output type be controlled? Pass dtype explicitly, such as dtype=torch.float32.

QHow is the result dtype selected?

If dtype is omitted, PyTorch infers it from the inputs: floating-point inputs use the default floating-point dtype, while otherwise the result defaults to torch.int64. The function also accepts device, layout, out, and requires_grad keyword arguments.

Follow-up: Why should dtype often be explicit for fractional ranges? Floating-point inference depends on the configured default dtype, so explicit dtype makes the result predictable.

  • torch.linspace
  • torch.zeros
  • torch.ones
  • torch.tensor

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