diff --git a/imperative/python/megengine/functional/elemwise.py b/imperative/python/megengine/functional/elemwise.py index 85fc27276cbb8088d7f74c644bc24d056829d22a..08436ac547a7db6900c35c0dc02becf73c685366 100644 --- a/imperative/python/megengine/functional/elemwise.py +++ b/imperative/python/megengine/functional/elemwise.py @@ -26,7 +26,7 @@ __all__ = [ "acosh", "atanh", "ceil", - "clamp", + "clip", "cos", "cosh", "div", @@ -547,7 +547,7 @@ def sigmoid(x): return _elwise(x, mode="sigmoid") -def clamp(x: Tensor, lower=None, upper=None) -> Tensor: +def clip(x: Tensor, lower=None, upper=None) -> Tensor: r"""Clamps all elements in input tensor into the range `[` :attr:`lower`, :attr:`upper` `]` and returns a resulting tensor: @@ -572,9 +572,9 @@ def clamp(x: Tensor, lower=None, upper=None) -> Tensor: import megengine.functional as F a = tensor(np.arange(5).astype(np.int32)) - print(F.clamp(a, 2, 4).numpy()) - print(F.clamp(a, lower=3).numpy()) - print(F.clamp(a, upper=3).numpy()) + print(F.clip(a, 2, 4).numpy()) + print(F.clip(a, lower=3).numpy()) + print(F.clip(a, upper=3).numpy()) Outputs: @@ -590,7 +590,7 @@ def clamp(x: Tensor, lower=None, upper=None) -> Tensor: ), "At least one of 'lower' or 'upper' must not be None" if lower is not None: if upper is not None: - assert lower <= upper, "clamp lower bound is bigger that upper bound" + assert lower <= upper, "clip lower bound is bigger that upper bound" return minimum(maximum(x, lower), upper) else: return maximum(x, lower) diff --git a/imperative/python/megengine/functional/math.py b/imperative/python/megengine/functional/math.py index 77f7f0a6ec9adb2a791b09bdcaf12581b1c553bb..a61ea8dd7e439505a2bf404f526d1d78c62d6109 100644 --- a/imperative/python/megengine/functional/math.py +++ b/imperative/python/megengine/functional/math.py @@ -18,7 +18,7 @@ from ..core.ops.special import Const from ..core.tensor import utils from ..core.tensor.core import TensorBase, TensorWrapperBase, apply from ..tensor import Tensor -from .elemwise import clamp, exp, log, log1p +from .elemwise import clip, exp, log, log1p from .tensor import add_axis, remove_axis, reshape __all__ = [ @@ -85,7 +85,7 @@ def isinf(inp: Tensor) -> Tensor: print(F.isinf(x).numpy()) Outputs: - + .. testoutput:: [False True False] @@ -109,7 +109,7 @@ def sign(inp: Tensor): x = tensor([1, -1, 0]) print(F.sign(x).numpy()) - + Outputs: .. testoutput:: @@ -557,9 +557,9 @@ def normalize( :return: normalized output tensor. """ if axis is None: - return inp / clamp(norm(inp, p, axis), lower=eps) + return inp / clip(norm(inp, p, axis), lower=eps) else: - return inp / clamp(norm(inp, p, axis, keepdims=True), lower=eps) + return inp / clip(norm(inp, p, axis, keepdims=True), lower=eps) def argsort(inp: Tensor, descending: bool = False) -> Tensor: diff --git a/imperative/python/test/unit/functional/test_elemwise.py b/imperative/python/test/unit/functional/test_elemwise.py index 0b167942929c89f3b6e5080b16cb46e0fb7fd6f2..f0b51a56f221902a031f2f1939c7fdcb24628a8b 100644 --- a/imperative/python/test/unit/functional/test_elemwise.py +++ b/imperative/python/test/unit/functional/test_elemwise.py @@ -47,14 +47,14 @@ def test_multiply(): def test_clamp(): """Fix an issue when `lower` or `upper` is 0, it will be recognized as `False` and - `F.clamp` will fall into wrong conditions unexpectedly. + `F.clip` will fall into wrong conditions unexpectedly. """ x = np.linspace(-6, 6, dtype="float32") np.testing.assert_allclose( - F.clamp(tensor(x) + 3, 0, 6).numpy(), np.clip(x + 3, 0, 6) + F.clip(tensor(x) + 3, 0, 6).numpy(), np.clip(x + 3, 0, 6) ) np.testing.assert_allclose( - F.clamp(tensor(x) - 3, -6, 0).numpy(), np.clip(x - 3, -6, 0) + F.clip(tensor(x) - 3, -6, 0).numpy(), np.clip(x - 3, -6, 0) )