PyTorch torch.clip function
PyTorch torch reference manual
torch.clip is a function in PyTorch used to clip the values in a tensor to a specified range.
Function definition
torch.clip(input, min, max, out=None)
Usage examples
Example
import
torch
Creating tensors
x
=
torch.
tensor
(
[
1.0
,
2.0
,
3.0
,
4.0
,
5.0
]
)
Clip values to the range [2, 4]
y
=
torch.
clip
(
x
,
2
,
4
)
(
y
)
The output is:
tensor([2., 2., 3., 4., 4.])