Pytorch Torch Dist
## PyTorch `torch.dist`
The `torch.dist` function in PyTorch is a utility used to compute the $p$-norm distance (also known as Minkowski distance) between two input tensors. It is widely used in machine learning tasks such as calculating loss, measuring similarity, and evaluating spatial distances between embeddings.
---
### Function Definition
```python
torch.dist(input, other, p=2)
```
### Parameter Breakdown
| Parameter | Type | Description |
| :--- | :--- | :--- |
| `input` | *Tensor* | The source input tensor. |
| `other` | *Tensor* | The target tensor to compare against. |
| `p` | *float / int* | The norm type to be computed. Default is `2` (Euclidean distance). |
#### Mathematical Formulation
The distance is calculated using the following formula:
$$\text{distance} = \left( \sum_{i=1}^{n} |x_i - y_i|^p \right)^{1/p}$$
Where:
* $x$ is the `input` tensor.
* $y$ is the `other` tensor.
* $p$ is the norm degree.
---
### Supported Norm Types (`p`)
* **`p = 2` (Euclidean Distance / $L_2$ Norm):** Measures the straight-line distance between two points in Euclidean space.
* **`p = 1` (Manhattan Distance / $L_1$ Norm):** Measures the sum of absolute differences along each dimension.
* **`p = float('inf')` (Chebyshev Distance / $L_\infty$ Norm):** Measures the maximum absolute difference along any single dimension.
---
### Code Examples
The following example demonstrates how to compute different types of distances between two 1D tensors.
```python
import torch
# Create two sample tensors
x = torch.tensor([1.0, 2.0, 3.0])
y = torch.tensor([4.0, 5.0, 6.0])
# 1. Compute Euclidean Distance (p = 2)
dist_l2 = torch.dist(x, y, p=2)
print("Euclidean Distance (L2):", dist_l2.item()) # Output: ~5.1962
# 2. Compute Manhattan Distance (p = 1)
dist_l1 = torch.dist(x, y, p=1)
print("Manhattan Distance (L1):", dist_l1.item()) # Output: 9.0
# 3. Compute Chebyshev Distance (p = infinity)
dist_inf = torch.dist(x, y, p=float('inf'))
print("Chebyshev Distance (L-inf):", dist_inf.item()) # Output: 3.0
```
---
### Important Considerations
1. **Broadcasting Support:** The shapes of `input` and `other` must be broadcastable to a common shape. If they are not directly matching, PyTorch will attempt to broadcast them according to standard broadcasting rules.
2. **Data Types:** Both tensors should have matching floating-point or complex data types (e.g., `torch.float32`, `torch.float64`). If you pass integer tensors, you may need to cast them using `.float()` to avoid precision errors.
3. **Output Type:** The function returns a 0-dimensional tensor containing a single scalar value. Use `.item()` to extract it as a standard Python number.
YouTip