megengine.functional.clip#

clip(x, lower=None, upper=None)[源代码]#

逐元素裁切函数。

将输入张量 \(x\) 的所有元素 \(x_i\) 裁切(限制)到 [ lower, upper ] 范围内。例如,如果范围被指定为 [0, 1] ,那么小于 0 的值将变为 0 ,而大于 1 的值将变为 1

\[\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}\]

目前等价于 F.minimum(upper, np.maximum(x, upper))

参数:
  • x (Tensor) – 输入张量。

  • lower – 裁切范围的下界。应具有数值数据类型。

  • upper – 裁切范围的上界。应具有数值数据类型。

备注

  • 如果 lowerupper 都是 None,抛出 AssertionError

  • lower 是 None,则等价于 F.minimum(x, upper)

  • upper 是 None,则等价于 F.maximum(x, lower)

  • 如果 lower 大于 `upper ,那么结果与 clip(Tensor(), upper, upper) 相同。

返回类型:

Tensor

返回:

输出被限制区间后的张量。 返回结果的数据类型由 类型提升规则 决定。