clip

paddle. clip ( x, min=None, max=None, name=None ) [source]

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)\]
Parameters
  • x (Tensor) – An N-D Tensor with data type float16, float32, float64, int32 or int64.

  • min (float|int|Tensor, optional) – The lower bound with type float , int or a 0-D Tensor with shape [] and type int32, float16, float32, float64.

  • max (float|int|Tensor, optional) – The upper bound with type float, int or a 0-D Tensor with shape [] and type int32, float16, float32, float64.

  • name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.

Returns

A Tensor with the same data type and data shape as 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]])