Pytorch Torch Linspace
## PyTorch torch.linspace
`torch.linspace` is a fundamental PyTorch function used to create a one-dimensional tensor containing a sequence of evenly spaced values over a specified interval.
Unlike `torch.arange`, which generates values based on a user-defined step size, `torch.linspace` generates values based on a user-defined **number of steps (elements)**. This makes it highly useful when you need precise control over the exact count of elements in your tensor.
---
### Syntax and Parameters
```python
torch.linspace(start, end, steps, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
```
#### Parameters:
* **`start`** *(float/int)*: The starting value of the sequence.
* **`end`** *(float/int)*: The end value of the sequence (inclusive).
* **`steps`** *(int)*: The number of elements to generate. It must be a non-negative integer.
* **`out`** *(Tensor, optional)*: The output tensor.
* **`dtype`** *(torch.dtype, optional)*: The desired data type of the returned tensor. If not specified, PyTorch infers the default floating-point type (usually `torch.float32`).
* **`layout`** *(torch.layout, optional)*: The desired layout of the returned Tensor. Default is `torch.strided`.
* **`device`** *(torch.device, optional)*: The device on which the tensor is allocated (e.g., `'cpu'` or `'cuda'`).
* **`requires_grad`** *(bool, optional)*: If `True`, autograd will record operations on the returned tensor. Default is `False`.
#### Return Value:
* **`torch.Tensor`**: A 1-D tensor containing the evenly spaced sequence.
---
### Code Examples
#### Example 1: Creating 5 Evenly Spaced Points
This example demonstrates how to generate 5 evenly spaced values between `0` and `10`.
```python
import torch
# Create a sequence from 0 to 10 containing exactly 5 points
x = torch.linspace(0, 10, 5)
print(x)
```
**Output:**
```text
tensor([ 0.0000, 2.5000, 5.0000, 7.5000, 10.0000])
```
---
#### Example 2: Generating Negative and Fractional Sequences
You can easily generate sequences spanning negative ranges. Here, we generate 10 points between `-1` and `1`.
```python
import torch
# Create a sequence from -1 to 1 containing exactly 10 points
x = torch.linspace(-1, 1, 10)
print(x)
```
**Output:**
```text
tensor([-1.0000, -0.7778, -0.5556, -0.3333, -0.1111, 0.1111, 0.3333, 0.5556,
0.7778, 1.0000])
```
---
#### Example 3: Practical Application β Learning Rate Decay Scheduling
In deep learning, `torch.linspace` is frequently used to schedule hyperparameters, such as decaying a learning rate linearly over a set number of epochs.
```python
import torch
# Simulate a learning rate decaying from 0.1 down to 0.001 over 100 steps
learning_rates = torch.linspace(0.1, 0.001, 100)
print("Initial Learning Rate:", learning_rates.item())
print("Final Learning Rate:", learning_rates.item())
```
**Output:**
```text
Initial Learning Rate: 0.10000000149011612
Final Learning Rate: 0.0010000000474974513
```
---
### Key Differences: `torch.arange` vs. `torch.linspace`
Understanding when to use `torch.arange` versus `torch.linspace` is essential for clean PyTorch code:
| Function | Control Parameter | Inclusion of End Value | Use Case |
| :--- | :--- | :--- | :--- |
| **`torch.arange(start, end, step)`** | **Step size**: You define the interval between elements. | **Exclusive**: The `end` value is not included in the output. | When the exact distance between points matters (e.g., indexing, loops). |
| **`torch.linspace(start, end, steps)`** | **Element count**: You define the total number of elements. | **Inclusive**: The `end` value is always included in the output. | When the exact number of points matters (e.g., plotting, interpolation, scheduling). |
YouTip