Browse Source

feat(mge): rename clamp -> clip

GitOrigin-RevId: 2b594a611e
release-1.1
Megvii Engine Team 4 years ago
parent
commit
e034874ae7
3 changed files with 14 additions and 14 deletions
  1. +6
    -6
      imperative/python/megengine/functional/elemwise.py
  2. +5
    -5
      imperative/python/megengine/functional/math.py
  3. +3
    -3
      imperative/python/test/unit/functional/test_elemwise.py

+ 6
- 6
imperative/python/megengine/functional/elemwise.py View File

@@ -26,7 +26,7 @@ __all__ = [
"acosh", "acosh",
"atanh", "atanh",
"ceil", "ceil",
"clamp",
"clip",
"cos", "cos",
"cosh", "cosh",
"div", "div",
@@ -547,7 +547,7 @@ def sigmoid(x):
return _elwise(x, mode="sigmoid") 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 r"""Clamps all elements in input tensor into the range `[` :attr:`lower`, :attr:`upper` `]` and returns
a resulting tensor: a resulting tensor:


@@ -572,9 +572,9 @@ def clamp(x: Tensor, lower=None, upper=None) -> Tensor:
import megengine.functional as F import megengine.functional as F


a = tensor(np.arange(5).astype(np.int32)) 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: 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" ), "At least one of 'lower' or 'upper' must not be None"
if lower is not None: if lower is not None:
if upper 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) return minimum(maximum(x, lower), upper)
else: else:
return maximum(x, lower) return maximum(x, lower)


+ 5
- 5
imperative/python/megengine/functional/math.py View File

@@ -18,7 +18,7 @@ from ..core.ops.special import Const
from ..core.tensor import utils from ..core.tensor import utils
from ..core.tensor.core import TensorBase, TensorWrapperBase, apply from ..core.tensor.core import TensorBase, TensorWrapperBase, apply
from ..tensor import Tensor 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 from .tensor import add_axis, remove_axis, reshape


__all__ = [ __all__ = [
@@ -85,7 +85,7 @@ def isinf(inp: Tensor) -> Tensor:
print(F.isinf(x).numpy()) print(F.isinf(x).numpy())


Outputs: Outputs:
.. testoutput:: .. testoutput::


[False True False] [False True False]
@@ -109,7 +109,7 @@ def sign(inp: Tensor):


x = tensor([1, -1, 0]) x = tensor([1, -1, 0])
print(F.sign(x).numpy()) print(F.sign(x).numpy())
Outputs: Outputs:


.. testoutput:: .. testoutput::
@@ -557,9 +557,9 @@ def normalize(
:return: normalized output tensor. :return: normalized output tensor.
""" """
if axis is None: if axis is None:
return inp / clamp(norm(inp, p, axis), lower=eps)
return inp / clip(norm(inp, p, axis), lower=eps)
else: 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: def argsort(inp: Tensor, descending: bool = False) -> Tensor:


+ 3
- 3
imperative/python/test/unit/functional/test_elemwise.py View File

@@ -47,14 +47,14 @@ def test_multiply():


def test_clamp(): def test_clamp():
"""Fix an issue when `lower` or `upper` is 0, it will be recognized as `False` and """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") x = np.linspace(-6, 6, dtype="float32")
np.testing.assert_allclose( 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( 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)
) )






Loading…
Cancel
Save