mindspore.ops.clamp
- mindspore.ops.clamp(input, min=None, max=None)[source]
Clamp all elements of the input tensor within the range [min, max].
Note
min and max cannot be None at the same time;
If min is
None
, there is no lower bound.if max is
None
, there is no upper bound.If min is greater than max, the value of all elements in Tensor will be set to max;
- Parameters
- Returns
Tensor
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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]])