简介
将输入input
张量每个元素的夹紧到区间 [min,max]
内,并返回结果到一个新张量。
torch.clamp(input, min, max, out=None) → Tensor
参数解释
input(Tensor):输入张量
min:限制范围下限
max:限制范围上限
out(Tensor):输出张量
数学原理:
| min, if input < min
output = | input, if min <= input <= max
| max, if input > max
举例
代码示例如下:
a=torch.randint(low=0,high=10,size=(10,1))
print(a)
a=torch.clamp(a,3,9)
print(a)
输出如下:
tensor([[9.],
[3.],
[0.],
[4.],
[4.],
[2.],
[4.],
[1.],
[2.],
[9.]])
tensor([[9.],
[3.],
[3.],
[4.],
[4.],
[3.],
[4.],
[3.],
[3.],
[9.]])
参考
https://blog.csdn.net/qq_40263477/article/details/103881062
https://blog.csdn.net/u013230189/article/details/82627375