torch.clamp¶
- torch.clamp(input, min=None, max=None, *, out=None) Tensor¶
 Clamps all elements in
inputinto the range [min,max]. Letting min_value and max_value beminandmax, respectively, this returns:If
minisNone, there is no lower bound. Or, ifmaxisNonethere is no upper bound.Note
If
minis greater thanmaxtorch.clamp(..., min, max)sets all elements ininputto 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])