clamp
- paddle. clamp ( x: Tensor, min: float | None = None, max: float | None = None, name: str | None = None, *, out: Tensor | None = None ) Tensor
-
This operator clip all elements in input into the range [ min, max ] and return a resulting tensor as the following equation:
\[Out = MIN(MAX(x, min), max)\]Note
Alias Support: The parameter name
input
can be used as an alias forx
. For example,clip(input=tensor_x)
is equivalent toclip(x=tensor_x)
.- Parameters
-
x (Tensor) – An N-D Tensor with data type bfloat16, float16, float32, float64, int32 or int64. alias:
input
.min (float|int|Tensor, optional) – The lower bound with type
float
,int
or a0-D Tensor
with shape [] and typebfloat16
,float16
,float32
,float64
,int32
.max (float|int|Tensor, optional) – The upper bound with type
float
,int
or a0-D Tensor
with shape [] and typebfloat16
,float16
,float32
,float64
,int32
.name (str|None, optional) – Name for the operation (optional, default is None). For more information, please refer to api_guide_Name.
out (Tensor, optional) – The output Tensor. If set, the result will be stored in this Tensor. Default: None.
- Returns
-
A Tensor with the same data shape as input. If either min or max is a floating-point value/Tensor, the output tensor will have a data type of
float32
. Otherwise, the output tensor will inherit the same data type as the input. - Return type
-
Tensor
Examples
>>> import paddle >>> x1 = paddle.to_tensor([[1.2, 3.5], [4.5, 6.4]], 'float32') >>> out1 = paddle.clip(x1, min=3.5, max=5.0) >>> out1 Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, [[3.50000000, 3.50000000], [4.50000000, 5. ]]) >>> out2 = paddle.clip(x1, min=2.5) >>> out2 Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, [[2.50000000, 3.50000000], [4.50000000, 6.40000010]])