PyTorch torch.clip function
PyTorch torch Reference Manual
torch.clipis a function in PyTorch used to clip the values in a tensor to a specified range.
Function Definition
torch.clip(input, min, max, out=None)
Usage Examples
Example
import torch
# Create tensor
x = torch.tensor([1.0, 2.0, 3.0, 4.0, 5.0])
# Clip values to the range [2, 4]
y = torch.clip(x, 2, 4)
print(y)
# Create tensor
x = torch.tensor([1.0, 2.0, 3.0, 4.0, 5.0])
# Clip values to the range [2, 4]
y = torch.clip(x, 2, 4)
print(y)
The output result is:
tensor([2., 2., 3., 4., 4.])
Other Extensions