How to Create a Range of Numbers in PyTorch with torch.arange
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
torch.arange(start, end, step)
| Part | What it means |
|---|---|
start | Optional. The first value. Left out, it's 0 |
end | The value to stop before: it's never in the result |
step | Optional. 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 numpyadds 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
import torch
print(torch.arange(5))
print(torch.arange(2, 10, 2))
print(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
uv run python arange.py
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]).](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/ai/pytorch/2026/pytorch-arange-02-output.webp)
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
| Mistake | What happens | Do this instead |
|---|---|---|
| Expecting the end in the result | torch.arange(5) stopped at 4 | Give one more than the last value you want |
| Expecting decimals from whole numbers | torch.arange(5) gave whole numbers | Give a decimal: torch.arange(5.0) printed tensor([0., 1., 2., 3., 4.]) |
| A decimal step that should land exactly on the end | PyTorch'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
- torch.arange — PyTorch's reference for the function.
- torch.linspace — a set number of values, both ends included.
- Tensors tutorial — PyTorch's introduction to tensors.
