torch.clamp
- torch.clamp(input, min=None, max=None, *, out=None) → Tensor
Clamps all elements in input into the range [ min, max ].
Letting min_value and max_value be min and max, respectively, this returns:
Example:
>>> a = torch.randn(4)
>>> a
tensor([-1.7120, 0.1734, -0.0478, -0.0922])
>>> torch.clamp(a, min=-0.5, max=0.5)
tensor([-0.5000, 0.1734, -0.0478, -0.0922])
>>> min = torch.linspace(-1, 1, steps=4)
>>> torch.clamp(a, min=min)
tensor([-1.0000, 0.1734, 0.3333, 1.0000])