Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.clamp function

PyTorch torch.clamp function


PyTorch torch reference manual 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

)

print

(

result

)

The output is:

tensor([-1, -1,  0,  1,  2,  2])


PyTorch torch reference manual PyTorch torch reference manual