Rate this Page

torch.clamp#

torch.clamp(input,min=None,max=None,*,out=None)Tensor#

Clamps all elements ininput into the range[min,max].Letting min_value and max_value bemin andmax, respectively, this returns:

yi=min(max(xi,min_valuei),max_valuei)y_i = \min(\max(x_i, \text{min\_value}_i), \text{max\_value}_i)

Ifmin isNone, there is no lower bound.Or, ifmax isNone there is no upper bound.

Note

Ifmin is greater thanmaxtorch.clamp(...,min,max)sets all elements ininput to the value ofmax.

Parameters
  • input (Tensor) – the input tensor.

  • min (Number orTensor,optional) – lower-bound of the range to be clamped to

  • max (Number orTensor,optional) – upper-bound of the range to be clamped to

Keyword Arguments

out (Tensor,optional) – the output tensor.

Example:

>>>a=torch.randn(4)>>>atensor([-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])