YouTip LogoYouTip

Pytorch Torch Range

## PyTorch torch.range The `torch.range` function in PyTorch is used to create a 1D tensor containing a sequence of values spaced at equal intervals within a specified range. Unlike many Python range functions, `torch.range` **includes** the `end` value in the generated sequence (it is a closed interval $[start, end]$). > **Deprecation Warning:** `torch.range` is deprecated in modern versions of PyTorch and may be removed in future releases. It is highly recommended to use `torch.arange` instead, which uses a half-open interval $[start, end)$ to match standard Python conventions. --- ### Syntax ```python torch.range(start=0, end, step=1, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) ``` ### Parameter Definitions | Parameter | Type | Description | | :--- | :--- | :--- | | `start` | *float/int* | The starting value of the sequence. Default: `0`. | | `end` | *float/int* | The ending value of the sequence (inclusive). | | `step` | *float/int* | The spacing between each pair of adjacent values. Default: `1`. | | `out` | *Tensor, optional* | The alternative output tensor to store the result. | | `dtype` | *torch.dtype, optional* | The desired data type of the returned tensor. If not specified, infers the type from other input arguments. | | `layout` | *torch.layout, optional* | The desired layout of the returned Tensor. Default: `torch.strided`. | | `device` | *torch.device, optional* | The desired device of the returned tensor (e.g., `'cpu'`, `'cuda'`). | | `requires_grad` | *bool, optional* | If autograd should record operations on the returned tensor. Default: `False`. | --- ### Code Examples #### Example 1: Basic Usage (Default Start and Step) If you only provide one positional argument, it is treated as the `end` value. The sequence starts at `0` with a step size of `1`. ```python import torch # Create a sequence from 0 to 5 (inclusive) x = torch.range(5.0) print(x) # Output: tensor([0., 1., 2., 3., 4., 5.]) ``` #### Example 2: Specifying Start, End, and Step You can customize the starting point and the step interval. ```python import torch # Create a sequence from 1 to 10 with a step size of 2 (inclusive of 10 if reachable) y = torch.range(1, 10, 2) print(y) # Output: tensor([1., 3., 5., 7., 9.]) ``` #### Example 3: Specifying Device and Data Type You can explicitly define the tensor's data type and place it on a specific device (like a GPU). ```python import torch # Create an integer sequence on the GPU (if CUDA is available) device = "cuda" if torch.cuda.is_available() else "cpu" z = torch.range(start=1, end=5, step=1, dtype=torch.int32, device=device) print(z) # Output: tensor([1, 2, 3, 4, 5], device='cuda:0', dtype=torch.int32) ``` --- ### Key Considerations & Best Practices 1. **Inclusive End Value:** `torch.range(start, end)` includes the `end` value in the output tensor. For example, `torch.range(1, 3)` yields `[1, 2, 3]`. 2. **Deprecation Warning:** Because the inclusive behavior of `torch.range` is inconsistent with Python's built-in `range()` and NumPy's `arange()`, PyTorch has deprecated this function. 3. **Recommended Alternative (`torch.arange`):** You should use `torch.arange` for modern PyTorch development. It excludes the `end` value (half-open interval $[start, end)$). ```python # Deprecated (Inclusive) torch.range(1, 3) # Returns: tensor([1., 2., 3.]) # Recommended (Exclusive) torch.arange(1, 3) # Returns: tensor([1, 2]) ```
← Pytorch Torch RealPytorch Torch Randn_Like β†’