Python Pytorch linspace() method Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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.linspace() returns a one-dimensional tensor of steps equally spaced points between start and end. The output tensor is 1-D of size steps. Syntax: torch.linspace(start, end, steps=100, out=None) Parameters: start: the starting value for the set of point. end: the ending value for the set of points steps: the gap between each pair of adjacent points. Default: 100. out(Tensor, optional): the output tensor Return type: A tensor Code #1: Python3 # Importing the PyTorch library import torch # Applying the linspace function and # storing the resulting tensor in 't' a = torch.linspace(3, 10, 5) print("a = ", a) b = torch.linspace(start =-10, end = 10, steps = 5) print("b = ", b) Output: a = tensor([ 3.0000, 4.7500, 6.5000, 8.2500, 10.0000]) b = tensor([-10., -5., 0., 5., 10.]) Code #2: Visualization Python3 # Importing the PyTorch library import torch # Importing the NumPy library import numpy as np # Importing the matplotlib.pyplot function import matplotlib.pyplot as plt # Applying the linspace function to get a tensor of size 15 with values from -5 to 5 a = torch.linspace(-5, 5, 15) print(a) # Plotting plt.plot(a.numpy(), np.zeros(a.numpy().shape), color = 'red', marker = "o") plt.title("torch.linspace") plt.xlabel("X") plt.ylabel("Y") plt.show() Output: tensor([-5.0000, -4.2857, -3.5714, -2.8571, -2.1429, -1.4286, -0.7143, 0.0000, 0.7143, 1.4286, 2.1429, 2.8571, 3.5714, 4.2857, 5.0000]) [torch.FloatTensor of size 15] Comment More infoAdvertise with us Next Article Python PyTorch log2() method S sanskar27jain Follow Improve Article Tags : Python Python-PyTorch Practice Tags : python Similar Reads Python Pytorch logspace() 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.logspace() returns a one-dimensional tensor of steps points logarithmically spaced with base base between {\text{base}}^{\text{sta 2 min read Python - PyTorch frac() method PyTorch torch.frac() method computes the fractional portion of each element in input. Syntax: torch.frac(input, out=None) Arguments input: This is input tensor. out: The output tensor. Return: It returns a Tensor. Let's see this concept with the help of few examples: Example 1: Python3 # Importing t 1 min read Python - PyTorch clamp() method PyTorch torch.clamp() method clamps all the input elements into the range [ min, max ] and return a resulting tensor. Syntax: torch.clamp(inp, min, max, out=None) Arguments inp: This is input tensor. min: This is a number and specifies the lower-bound of the range to which input to be clamped. max: 2 min read Python PyTorch log2() method PyTorch log2() method computes the logarithm to the base 2 of the elements of an input tensor. Â It computes the logarithm values element-wise. Â It takes a tensor as an input and returns a new tensor with computed logarithm values. The elements of the input tensor must be between zero and the positiv 4 min read Python - PyTorch floor() method PyTorch torch.floor() method returns a new tensor which is floor of the elements of input, the largest integer less than or equal to each element. Syntax: torch.floor(input, out=None) Arguments input: This is input tensor. out: The output tensor. Return: It returns a Tensor. Let's see this concept w 1 min read Python String lstrip() Method The lstrip() method removes leading whitespace characters from a string. We can also specify custom characters to remove from the beginning/starting of the string.Let's take an example to remove whitespace from the starting of a string.Pythons = " Hello Python!" res = s.lstrip() print(res)OutputHell 2 min read Like