PyTorch torch.clamp function


Pytorch torch 参考手册Pytorch torch reference manual

torch.clampis a function in PyTorch used to clamp tensor values within a specified range.

Function definition

torch.clamp(input, min, max, out)

Usage examples

Example

import torch

x = torch.tensor([-2, -1, 0, 1, 2, 3])

# Clamp between -1 and 2
result = torch.clamp(x, min=-1, max=2)

print(result)

The output result is:

tensor([-1, -1,  0,  1,  2,  2])

Pytorch torch 参考手册Pytorch torch reference manual

Other extensions