Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Function

torch.linspace

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.

How it works

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.

Syntax

torch.linspace(start, end, steps, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)

Parameters

NameTypeDescription
startfloat or TensorStarting value; a float or a zero-dimensional tensor.
endfloat or TensorEnding value; a float or a zero-dimensional tensor.
stepsintNumber of values in the constructed tensor.
outTensor, optionalOptional destination tensor to receive the output.
dtypetorch.dtype, optionalOptional dtype used for computation and the result.
layouttorch.layout, optionalOptional layout of the returned tensor; defaults to torch.strided.
devicetorch.device, optionalOptional device for the returned tensor.
requires_gradbool, optionalWhether autograd records operations on the returned tensor; defaults to False.

Returns

A Tensor containing the requested one-dimensional sequence.

Key points

  • The result always has one dimension and contains exactly the number of elements requested by steps.
  • Values have uniform spacing and include both boundary values.
  • start defines the first value in the generated sequence.
  • end defines the final value in the generated sequence.
  • steps determines how many values the output tensor contains.
  • device can place the output on a selected CPU or accelerator device.

Common mistakes

  • Tensor inputs for start and end must be scalar, zero-dimensional tensors.
  • Without dtype, the result follows the global default dtype for real endpoints, or the corresponding complex dtype when an endpoint is complex.

Example: Create an inclusive linear 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.

When to use it

Uniform interval sampling

Sampling a numeric interval for plotting or numerical algorithms.

Coordinate and time grids

Building phase, time, or coordinate vectors with a fixed number of samples.

Device-aware initialization

Creating tensors directly on a chosen device with a selected dtype.

Interview questions

QWhat does torch.linspace produce?

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.

QWhat types and shapes can the endpoint arguments have?

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.

QWhich keyword arguments control the returned tensor?

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.arange
  • torch.logspace
  • torch.get_default_dtype
  • torch.set_default_device

Source: torch.linspace — PyTorch main documentation. Checked 2026-10-16.