clamp

paddle.fluid.layers.clamp(input, min=None, max=None, output=None, name=None)[source]

clampe layer

This operator clamps 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
  • input (Variable) – An input N-D Tensor or LoDTensor with data type float32, float64.

  • min (float32|Variable) – The lower bound with type float32 or a Tensor with shape [1] and type int32, float32, float64.

  • max (float32|Variable) – The upper bound with type float32 or a Tensor with shape [1] and type int32, float32, float64.

  • output (Variable, optional) – A tensor or LoDTensor. If output is None, a new tensor will be created as output. Default: None.

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name.

Returns

A Tensor or LodTensor with the same data type and data shape as input’s.

Return type

Variable

Examples

import paddle
import paddle.fluid as fluid
import numpy as np

in1 = np.array([[1.2,3.5],
                [4.5,6.4]]).astype('float32')
with fluid.dygraph.guard():
    x1 = fluid.dygraph.to_variable(in1)
    out1 = fluid.layers.clamp(x1, min=3.5, max=5.0)
    out2 = fluid.layers.clamp(x1, min=2.5)
    print(out1.numpy())
    # [[3.5, 3.5]
    # [4.5, 5.0]]
    print(out2.numpy())
    # [[2.5, 3.5]
    # [[4.5, 6.4]