.clamp()
Published Jun 28, 2025
Contribute to Docs
The .clamp()
method in PyTorch restricts each tensor element to a specified range, setting values below the minimum to the minimum and values above the maximum to the maximum. It is commonly used for normalization, gradient clipping, activation constraints, or to keep values within safe or interpretable ranges during model training and evaluation.
Syntax
torch.clamp(input, min=None, max=None, *, out=None)
Parameters:
input
: Tensor with values to clamp.min
: Lower bound to clamp values to. IfNone
, no minimum is applied.max
: Upper bound to clamp values to. IfNone
, no maximum is applied.out
(optional): Optional output tensor fortorch.clamp
.
Note: At least one of the
min
ormax
values must be provided—otherwise, the tensor remains unchanged
Return value:
Returns a new tensor where each element is limited to the specified range [min, max]
.
Example
This example creates a tensor with values from -5.0
to 5.0
, then uses the .clamp()
method to restrict all values to the range [-1.5, 1.5]
as follows:
import torch# Create a tensor with a range of valuestensor = torch.tensor([-5.00, -1.25, 0.00, 1.25, 5.00])# Clamp values to the range [-1.50, 1.50]clamped = tensor.clamp(min=-1.50, max=1.50)# Print the clamped tensorprint(clamped)
The output of this code is:
tensor([-1.5000, -1.2500, 0.0000, 1.2500, 1.5000])
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.