比较与torch.clamp的功能差异
torch.clamp
torch.clamp(
    input,
    min,
    max,
    out=None
)
更多内容详见torch.clamp。
mindspore.ops.clip_by_value
mindspore.ops.clip_by_value(
    x,
    clip_value_min,
    clip_value_max
)
更多内容详见mindspore.ops.clip_by_value。
使用方式
PyTorch:将输入中的所有元素限制在 ‘min’、’max’ 范围内并返回结果张量。 支持指定两个参数 ‘min’、’max’ 之一。
MindSpore:将’x’的值限制在一个范围内,其下限为’clip_value_min’,上限为’clip_value_max’。 两个参数’clip_value_min’,’clip_value_max’是必要的。
代码示例
import mindspore
from mindspore import Tensor
import mindspore.ops as ops
import torch
import numpy as np
min_value = Tensor(5, mindspore.float32)
max_value = Tensor(20, mindspore.float32)
x = Tensor(np.array([[1., 25., 5., 7.], [4., 11., 6., 21.]]), mindspore.float32)
output = ops.clip_by_value(x, min_value, max_value)
print(output)
# Out:
# [[ 5. 20.  5.  7.]
#  [ 5. 11.  6. 20.]]
a = torch.randn(4)
print(a)
# Out:
#tensor([-1.7120,  0.1734, -0.0478, -0.0922])
print(torch.clamp(a, min=-0.5, max=0.5))
# Out:
# tensor([-0.5000,  0.1734, -0.0478, -0.0922])
a = torch.randn(4)
print(a)
# Out:
# tensor([-0.0299, -2.3184,  2.1593, -0.8883])
print(torch.clamp(a, min=0.5))
# Out:
# tensor([ 0.5000,  0.5000,  2.1593,  0.5000])
a = torch.randn(4)
print(a)
# Out:
# tensor([ 0.7753, -0.4702, -0.4599,  1.1899])
print(torch.clamp(a, max=0.5))
# Out:
# tensor([ 0.5000, -0.4702, -0.4599,  0.5000])
