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 bemin
andmax
, respectively, this returns:If
min
isNone
, there is no lower bound. Or, ifmax
isNone
there is no upper bound.Note
If
min
is greater thanmax
torch.clamp(..., min, max)
sets all elements ininput
to the value ofmax
.- Parameters
- Keyword Arguments
out (Tensor, optional) – the output tensor.
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])