.linspace()
Published Oct 10, 2024
Contribute to Docs
The .linspace()
function in PyTorch is used to return a one-dimensional tensor with a specified number of evenly spaced values between the given start and end points.
Syntax
torch.linspace(start, end, steps)
start
: The starting value to be used.end
: The ending value to be used.steps
: The number of steps to be taken between the starting and ending values.
This function is particularly useful when there is a need to create a tensor of equally spaced points for plotting graphs or for performing other numerical computations.
Example
The following example shows how to use the .linspace()
function in PyTorch:
import torch# Create a tensor of 5 equally spaced points between 0 and 1x = torch.linspace(0, 1, 5)print(x)
The code above generates the following output:
tensor([0.0000, 0.2500, 0.5000, 0.7500, 1.0000])
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
- Free course
Intro to PyTorch and Neural Networks
Learn how to use PyTorch to build, train, and test artificial neural networks in this course.Intermediate3 hours - Course
PyTorch for Classification
Build AI classification models with PyTorch using binary and multi-label techniques.With CertificateBeginner Friendly3 hours