PyTorch torch.clamp function
PyTorch torch reference manual
torch.clamp is a function in PyTorch used to clamp tensor values to a specified range.
Function definition
torch.clamp(input, min, max, out)
Usage examples
Example
import
torch
x
=
torch.
tensor
(
[
2
,
1
,
0
,
1
,
2
,
3
]
)
Clamp between -1 and 2
result
=
torch.
clamp
(
x
,
min
=
1
,
max
=
2
)
(
result
)
The output is:
tensor([-1, -1, 0, 1, 2, 2])