Pytorch torch.range() and torch.arange() can generate a 1-D tensor. In this tutorial, we will disucss the difference between them.
torch.range() Vs torch.arange()
torch.range() is defined as:
torch.range(start=0, end, step=1, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
torch.arange() is defined as:
torch.arange(start=0, end, step=1, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
Both of them can create a 1-D tensor based on start and step.
However, they are different.
- torch.range() will return [start, end], the data type is torch.float32
- torch.arange() will return [start, end), the data type is torch.int64
For example:
import torch x = torch.range(0,5) print(x, x.dtype) x = torch.arange(0,5) print(x, x.dtype)
We will see:
tensor([0., 1., 2., 3., 4., 5.]) torch.float32 tensor([0, 1, 2, 3, 4]) torch.int64
We should notice: torch.range() is deprecated and will be removed in a future.