Skip to main content

How to Create a Range of Numbers in PyTorch with torch.arange

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A tensor of counting numbers, such as 0 to 9, is handy for positions, labels and quick test data. PyTorch makes one in a single call, the way Python's range counts. This guide uses torch.arange three ways in PyTorch 2.15.0.

Syntax​

Syntax
torch.arange(start, end, step)
PartWhat it means
startOptional. The first value. Left out, it's 0
endThe value to stop before: it's never in the result
stepOptional. The gap between values. Left out, it's 1

With one number, that number is end: torch.arange(5) counts from 0. PyTorch's reference says it returns "values from the interval [start, end) taken with common difference step beginning from start."

The Example​

A script makes three ranges: 0 up to 5, the even numbers from 2 up to 10, and quarters from 0 up to 1.

Prerequisites​

  • A uv project with PyTorch and NumPy. uv add torch numpy adds both; without NumPy, PyTorch 2.15.0 prints a warning each time it starts. This guide uses PyTorch 2.15.0 and NumPy 2.5.5 on Python 3.15.2, with uv 0.13.10, on an Apple M1 Pro.

Step 1: Write the Script​

arange.py
import torch

print(torch.arange(5))
print(torch.arange(2, 10, 2))
print(torch.arange(0, 1, 0.25))

The arange.py script in a macOS-style code window: print of torch.arange(5), torch.arange(2, 10, 2) and torch.arange(0, 1, 0.25).

The first call gives only end, the second gives start, end and a step of 2, and the third uses a decimal step.

Step 2: Run It​

Terminal
uv run python arange.py
Output
tensor([0, 1, 2, 3, 4])
tensor([2, 4, 6, 8])
tensor([0.0000, 0.2500, 0.5000, 0.7500])

The terminal after uv run python arange.py: tensor([0, 1, 2, 3, 4]), tensor([2, 4, 6, 8]) and tensor([0.0000, 0.2500, 0.5000, 0.7500]).

Each range stops before its end: 5, 10 and 1 are missing. The first two are whole numbers, because every number given was whole. A decimal step made the third tensor decimals. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting the end in the resulttorch.arange(5) stopped at 4Give one more than the last value you want
Expecting decimals from whole numberstorch.arange(5) gave whole numbersGive a decimal: torch.arange(5.0) printed tensor([0., 1., 2., 3., 4.])
A decimal step that should land exactly on the endPyTorch's reference warns that a "non-integer step is subject to floating point rounding errors when comparing against end"Use torch.linspace, as the FAQ shows

FAQ​

How do I get evenly spaced numbers that include the end?​

torch.linspace(start, end, steps) makes steps values from start to end, both included: torch.linspace(0, 1, 5) printed tensor([0.0000, 0.2500, 0.5000, 0.7500, 1.0000]).

How is it different from Python's range?​

range counts in whole numbers, while torch.arange makes a tensor and takes decimal steps too: torch.arange(0, 1, 0.1) printed ten values, 0.0000 to 0.9000.

Conclusion​

torch.arange(5) gave 0 to 4, torch.arange(2, 10, 2) the even numbers 2 to 8, and torch.arange(0, 1, 0.25) the quarters 0.0000 to 0.7500: each stopped before its end.

Additional Resources​