megengine.functional.clip¶
- clip(x, lower=None, upper=None)[source]¶
Element-wise clipping function.
Clamps(limits) all elements \(x_i\) of the input tensor \(x\) into the range
[ lower, upper ]
. For example, if a range of[0, 1]
is specified, values smaller than0
become0
, and values larger than1
become1
.\[\begin{split}y_i = \begin{cases} \text{lower} & \text{if } x_i < \text{lower} \\ x_i & \text{if } \text{lower} \leq x_i \leq \text{upper} \\ \text{upper} & \text{if } x_i > \text{upper} \end{cases}\end{split}\]Equivalent to
F.minimum(upper, np.maximum(x, upper))
right now.- Parameters
x (
Tensor
) – The input tensor.lower – lower-bound of the range to be clamped to. Should have a numeric data type.
upper – upper-bound of the range to be clamped to. Should have a numeric data type.
Note
If both
lower
andupper
are None, raises anAssertionError
.If
lower
is None, equivalent toF.minimum(x, upper)
.If
upper
is None, equivalent toF.maximum(x, lower)
.If
lower
is bigger than`upper
, the result is same asclip(Tensor(), upper, upper)
.
- Return type
- Returns
output clamped tensor. The result must have a data type determined by Type promotion rules.