Uniform interval sampling
Sampling a numeric interval for plotting or numerical algorithms.
PyTorch · Function
Creates a one-dimensional tensor with a chosen number of evenly spaced values from start to end, including both endpoints. Optional arguments configure its dtype, layout, device, storage, and autograd behavior.
torch.linspace constructs a one-dimensional tensor containing a requested number of evenly spaced values between start and end, including both endpoints. Optional arguments control output storage, data type, layout, device, and whether autograd records operations.
torch.linspace(start, end, steps, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)| Name | Type | Description |
|---|---|---|
start | float or Tensor | Starting value; a float or a zero-dimensional tensor. |
end | float or Tensor | Ending value; a float or a zero-dimensional tensor. |
steps | int | Number of values in the constructed tensor. |
out | Tensor, optional | Optional destination tensor to receive the output. |
dtype | torch.dtype, optional | Optional dtype used for computation and the result. |
layout | torch.layout, optional | Optional layout of the returned tensor; defaults to torch.strided. |
device | torch.device, optional | Optional device for the returned tensor. |
requires_grad | bool, optional | Whether autograd records operations on the returned tensor; defaults to False. |
A Tensor containing the requested one-dimensional sequence.
import torch
values = torch.linspace(3, 10, steps=5)
print(values) # tensor([ 3.0000, 4.7500, 6.5000, 8.2500, 10.0000])Creates five evenly spaced values, including both endpoints.
Sampling a numeric interval for plotting or numerical algorithms.
Building phase, time, or coordinate vectors with a fixed number of samples.
Creating tensors directly on a chosen device with a selected dtype.
It returns a one-dimensional tensor with exactly steps values distributed evenly from start through end. Both start and end are included, so the spacing is computed across steps minus one intervals when steps is greater than one.
Follow-up: How would you generate values that exclude the endpoint? Use a different construction or remove the final element, because this function explicitly includes end.
start and end may be floating-point values or zero-dimensional tensors, while steps specifies the output size as an integer. The endpoint arguments cannot be higher-dimensional tensors.
Follow-up: Why might scalar tensors be useful? They allow endpoint values to participate in tensor-device or tensor-dtype workflows while retaining scalar shape.
dtype selects the computation and output data type; if omitted, PyTorch uses the global default dtype for real endpoints and the corresponding complex dtype when either endpoint is complex. device selects where the returned tensor is placed, and requires_grad controls autograd recording.
Follow-up: What happens when device is omitted? The function uses the current device associated with the default tensor type.
torch.arangetorch.logspacetorch.get_default_dtypetorch.set_default_device