mindspore.ops.clamp
- mindspore.ops.clamp(input, min=None, max=None)[源代码]
将输入tensor的所有元素限制在范围 [min, max] 内。
说明
min 和 max 不能同时为None;
当 min 为
None
时, 无最小值限制当 max 为
None
时, 无最大值限制.当 min 大于 max 时,Tensor中所有元素的值会被置为 max;
- 参数:
input (Tensor) - 输入tensor。
min (Union(Tensor, float, int),可选) - 指定最小值。默认
None
。max (Union(Tensor, float, int),可选) - 指定最大值。默认
None
。
- 返回:
Tensor
- 支持平台:
Ascend
GPU
CPU
样例:
>>> import mindspore >>> # case 1: `min` and `max` are integer >>> input = mindspore.tensor([[1, 25, 5, 7], [4, 11, 6, 21]]) >>> mindspore.ops.clamp(input, 5, 20) Tensor(shape=[2, 4], dtype=Int64, value= [[ 5, 20, 5, 7], [ 5, 11, 6, 20]]) >>> >>> # case 2: If `min` and `max` are tensors, their shapes need to be broadcastable with input. >>> min = mindspore.tensor([2, 4, 6, 8]) >>> max = mindspore.tensor([10, 12, 14, 18]) >>> mindspore.ops.clamp(input, min, max) Tensor(shape=[2, 4], dtype=Int64, value= [[ 2, 12, 6, 8], [ 4, 11, 6, 18]])