PyTorch .arange()
Anonymous contributor
Published Sep 30, 2024
Contribute to Docs
The .arange() method returns a tensor containing values from a given interval [start, end) with a specified step size. When the step size is not an integer, floating-point rounding errors may occur, so it is recommended to subtract a small epsilon from the end value for consistency.
Syntax
torch.arange(start=0, end, step=1, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
The parameters are as follows:
start: The starting value of the range, inclusive. Defaults to0.end: The ending value of the range, exclusive. This parameter is required.step: The difference between each consecutive value in the range. The default value is1.out: A tensor in which to store the output. IfNone, a new tensor is created. The default value isNone.dtype: The desired data type of the output tensor (torch.dtype). IfNone, the data type will be inferred from other input arguments. The default value isNone.layout: The desired layout of the output tensor. Default:torch.strided.device: The device on which the tensor will be allocated (torch.device). Default:None.requires_grad: A boolean indicating whether autograd should track operations on the output tensor. Default:False.
Example
import torch# Return a tensor with only an end valuet0 = torch.arange(3)# Return a tensor of a specified range and step countt1 = torch.arange(5, 35, 10)print(t0)print(t1)
The returned tensors are as follows:
tensor([0, 1, 2])tensor([5, 15, 25])
Codebyte Example
Run the following code to know how the .arange() method works:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn how to use PyTorch to build, train, and test artificial neural networks in this course.
- Intermediate.3 hours