Friday, November 21, 2025
HomeLanguagesPython Pytorch range() method

Python Pytorch range() method

PyTorch is an open-source machine learning library developed by Facebook. It is used for deep neural network and natural language processing purposes.

The function torch.range() returns a 1-D tensor of size \left\lfloor \frac{\text{end} - \text{start}}{\text{step}} \right\rfloor + 1
with values from start to end with step step. Step is the gap between two values in the tensor.

 out_{i+1} = out_i + step

This function is deprecated in favor of torch.arange().

Syntax: torch.range(start=0, end, step=1, out=None)

Parameters:
start: the starting value for the set of points. Default: 0.
end: the ending value for the set of points
step: the gap between each pair of adjacent points. Default: 1.
out(Tensor, optional): the output tensor

Return type: A tensor

Code #1:




# Importing the PyTorch library
import torch
  
# Applying the range function and
# storing the resulting tensor in 't'
a = torch.range(1, 6)
print("a = ", a)
  
b = torch.range(1, 5, 0.5)
print("b = ", b)


Output:

a =  tensor([1., 2., 3., 4., 5., 6.])
b =  tensor([1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000, 4.5000, 5.0000])

 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32405 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6781 POSTS0 COMMENTS
Nicole Veronica
11928 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11997 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7166 POSTS0 COMMENTS
Thapelo Manthata
6862 POSTS0 COMMENTS
Umr Jansen
6847 POSTS0 COMMENTS