Shortcuts

# torch.clamp¶

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

Clamp all elements in input into the range [ min, max ]. Let min_value and max_value be min and max, respectively, this returns:

$y_i = \min(\max(x_i, \text{min\_value}), \text{max\_value})$
Parameters
• input (Tensor) – the input tensor.

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

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

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])

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

Clamps all elements in input to be larger or equal min.

Parameters

input (Tensor) – the input tensor.

Keyword Arguments
• min (Number) – minimal value of each element in the output

• out (Tensor, optional) – the output tensor.

Example:

>>> a = torch.randn(4)
>>> a
tensor([-0.0299, -2.3184,  2.1593, -0.8883])
>>> torch.clamp(a, min=0.5)
tensor([ 0.5000,  0.5000,  2.1593,  0.5000])

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

Clamps all elements in input to be smaller or equal max.

Parameters

input (Tensor) – the input tensor.

Keyword Arguments
• max (Number) – maximal value of each element in the output

• out (Tensor, optional) – the output tensor.

Example:

>>> a = torch.randn(4)
>>> a
tensor([ 0.7753, -0.4702, -0.4599,  1.1899])
>>> torch.clamp(a, max=0.5)
tensor([ 0.5000, -0.4702, -0.4599,  0.5000])


## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

## Resources

Find development resources and get your questions answered

View Resources