torch.clamp
时间: 2023-10-12 22:21:12 浏览: 66
torch.clamp(input, min, max) function returns a new tensor with all elements of the input tensor bounded between the minimum and maximum values. If an element in the input tensor is less than the minimum value, it will be replaced by the minimum value. If an element in the input tensor is greater than the maximum value, it will be replaced by the maximum value. The function takes three arguments:
- input (Tensor): the input tensor
- min (Number): the minimum value to clamp to
- max (Number): the maximum value to clamp to
Example:
```
import torch
x = torch.tensor([-1, 0, 1, 2, 3, 4, 5, 6])
y = torch.clamp(x, min=1, max=5)
print(y) # Output: tensor([1, 1, 1, 2, 3, 4, 5, 5])
```
In the example above, the input tensor x is clamped between the minimum value of 1 and the maximum value of 5. Elements less than 1 are replaced with 1, and elements greater than 5 are replaced with 5. The resulting tensor y contains the clamped values.
阅读全文