Pytorch Torch Amin
## PyTorch torch.amin
`torch.amin` is a PyTorch function used to compute and return the minimum values of a tensor along specified dimensions.
Unlike `torch.min`, which can return both the minimum values and their corresponding indices (argmax), `torch.amin` is designed specifically to reduce multiple dimensions at once and return only the minimum values.
---
### Syntax and Parameters
```python
torch.amin(input, dim=None, keepdim=False, *, out=None) -> Tensor
```
#### Parameters:
* **`input`** *(Tensor)*: The input tensor containing the elements to evaluate.
* **`dim`** *(int or tuple of ints, optional)*: The dimension or dimensions along which to find the minimum. If `None` (default), the minimum is computed over all dimensions of the input tensor.
* **`keepdim`** *(bool, default=False)*: Whether the output tensor retains the same dimensions as the input. If `True`, the reduced dimensions are retained with a size of 1.
* **`out`** *(Tensor, optional)*: The alternative output tensor in which to write the result.
---
### Code Examples
The following examples demonstrate how to use `torch.amin` for global reduction, single-dimension reduction, and multi-dimension reduction.
```python
import torch
# Create a 2D tensor
x = torch.tensor([[1, 3, 2],
[4, 1, 3]])
# 1. Compute the global minimum (across all dimensions)
global_min = torch.amin(x)
print("Global Minimum:", global_min)
# Output: Global Minimum: tensor(1)
# 2. Compute the minimum along dim=0 (column-wise minimum)
min_dim0 = torch.amin(x, dim=0)
print("Minimum along dim=0:", min_dim0)
# Output: Minimum along dim=0: tensor([1, 1, 2])
# 3. Compute the minimum along dim=1 (row-wise minimum)
min_dim1 = torch.amin(x, dim=1)
print("Minimum along dim=1:", min_dim1)
# Output: Minimum along dim=1: tensor([1, 1])
```
#### Retaining Dimensions with `keepdim=True`
When `keepdim=True` is specified, the reduced dimensions are kept in the output as dimensions with size 1. This is highly useful for broadcasting operations.
```python
import torch
x = torch.tensor([[1, 3, 2],
[4, 1, 3]])
# Compute minimum along dim=1 while keeping the dimensions
min_keepdim = torch.amin(x, dim=1, keepdim=True)
print("Minimum with keepdim=True:\n", min_keepdim)
print("Output shape:", min_keepdim.shape)
# Output:
# Minimum with keepdim=True:
# tensor([,
# ])
# Output shape: torch.Size([2, 1])
```
---
### Key Considerations
1. **Difference between `torch.amin` and `torch.min`**:
* `torch.min` can only reduce a single dimension at a time and returns a named tuple `(values, indices)`.
* `torch.amin` can reduce multiple dimensions simultaneously (by passing a tuple to `dim`) and only returns the minimum values, making it more efficient when indices are not required.
2. **Handling of NaNs**:
* If the input tensor contains `NaN` values, `torch.amin` will propagate the `NaN` to the output for any slice containing it.
YouTip