PyTorch torch.linspace Function


Pytorch torch 参考手册Pytorch torch Reference Manual

torch.linspaceIt is a function in PyTorch used to create equally spaced sequence tensors. It creates a one-dimensional tensor containing an equally spaced sequence from the start value to the end value.

andtorch.arangeDifferent,torch.linspaceIt specifies the number of elements rather than the step size, which is very useful when you need precise control over the number of elements.

Function Definition

torch.linspace(start, end, steps, dtype=None, device=None, requires_grad=False)

Parameters:

  • start(float): The starting value of the sequence.
  • end(float): The ending value of the sequence (inclusive).
  • steps(int): The number of elements in the sequence, must be a positive integer.
  • dtype(torch.dtype, optional): Specifies the data type of the tensor.
  • device(torch.device, optional): Specifies the device on which the tensor is stored.
  • requires_grad(bool, optional): Whether gradients need to be computed.

Return value:

  • torch.Tensor: Returns a one-dimensional tensor.

Usage Examples

Example 1: Create 5 equally spaced points

Example

import torch

# Create an equally spaced sequence from 0 to 10 with a total of 5 points
x = torch.linspace(0, 10, 5)

print(x)

The output result is:

tensor([ 0.0000,  2.5000,  5.0000,  7.5000, 10.0000])

Example 2: Create 10 points

Example

import torch

# Create an equally spaced sequence from -1 to 1 with a total of 10 points
x = torch.linspace(-1, 1, 10)

print(x)

The output result is:

tensor([-1.0000, -0.7778, -0.5556, -0.3333, -0.1111,  0.1111,  0.3333,  0.5556,
         0.7778,  1.0000])

Example 3: Used for neural network learning rate scheduling

Example

import torch

# Simulate the learning rate gradually decreasing from 0.1 to 0.001
learning_rates = torch.linspace(0.1, 0.001, 100)

print("Initial learning rate:", learning_rates[0].item())
print("Final learning rate:", learning_rates[-1].item())

The output result is:

初始学习率: 0.10000000149011612
最终学习率: 0.0010000000474974513

In this example, we created a sequence containing 100 learning rate values, which is commonly used for learning rate scheduling.


Difference between torch.arange and torch.linspace

  • torch.arange(start, end, step): Creates a sequence based on the step size; the number of elements is determined by the range and step size.
  • torch.linspace(start, end, steps): Creates a sequence based on the number of elements; the interval is determined by the range and the number of elements.

Pytorch torch 参考手册Pytorch torch Reference Manual

Other Extensions