torch.arange — PyTorch 2.7 documentation (original) (raw)

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

Returns a 1-D tensor of size ⌈end−startstep⌉\left\lceil \frac{\text{end} - \text{start}}{\text{step}} \right\rceilwith values from the interval [start, end) taken with common differencestep beginning from start.

Note: When using floating-point dtypes (especially reduced precision types like bfloat16), the results may be affected by floating-point rounding behavior. Some values in the sequence might not be exactly representable in certain floating-point formats, which can lead to repeated values or unexpected rounding. For precise sequences, it is recommended to use integer dtypes instead of floating-point dtypes.

Note that non-integer step is subject to floating point rounding errors when comparing against end; to avoid inconsistency, we advise subtracting a small epsilon from endin such cases.

outi+1=outi+step\text{out}_{{i+1}} = \text{out}_{i} + \text{step}

Parameters

Keyword Arguments

Example:

torch.arange(5) tensor([ 0, 1, 2, 3, 4]) torch.arange(1, 4) tensor([ 1, 2, 3]) torch.arange(1, 2.5, 0.5) tensor([ 1.0000, 1.5000, 2.0000])