megengine.functional.clip¶
- clip(x, lower=None, upper=None)[源代码]¶
Clamps all elements in input tensor into the range
[ lower, upper ]
and returns a resulting tensor:\[\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}\]实际案例
import numpy as np from megengine import tensor import megengine.functional as F a = tensor(np.arange(5).astype(np.int32)) print(F.clip(a, 2, 4).numpy()) print(F.clip(a, lower=3).numpy()) print(F.clip(a, upper=3).numpy())
输出:
[2 2 2 3 4] [3 3 3 3 4] [0 1 2 3 3]