提交 e034874a 编写于 作者: M Megvii Engine Team

feat(mge): rename clamp -> clip

GitOrigin-RevId: 2b594a611ebc158c85b86f05c2307e0b9685548c
上级 912d733e
......@@ -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)
......
......@@ -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:
......
......@@ -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)
)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册