Browse Source

Merge pull request #401 from LosReturn:patch-1

GitOrigin-RevId: 440af8bd3d
tags/v1.8.0
Megvii Engine Team 3 years ago
parent
commit
3252016e05
1 changed files with 22 additions and 19 deletions
  1. +22
    -19
      imperative/python/megengine/functional/elemwise.py

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

@@ -531,33 +531,36 @@ def clip(x: Tensor, lower=None, upper=None) -> Tensor:
\end{cases} \end{cases}


Args: Args:
x: input tensor.
lower: lower-bound of the range to be clamped to.
upper: upper-bound of the range to be clamped to.
x: (Tensor): The input tensor.
lower: (Numberic,optional): lower-bound of the range to be clamped to.
upper: (Numberic,optional): upper-bound of the range to be clamped to.
Note:
* If both `lower` and `upper` are None, raises an AssertionError.
* If `lower` is bigger than `upper`, the result is same as `clip(Tensor(), upper, upper)`.


Returns: Returns:
output clamped tensor.
output clamped tensor. The result must have a data type determined by :ref:`dtype-promotion`.


Examples: Examples:


.. testcode::
>>> import numpy as np
>>> x = Tensor([0,1,2,3,4])
>>> F.clip(x, 2, 4)
Tensor([2 2 2 3 4], dtype=int32, device=xpux:0)


import numpy as np
from megengine import tensor
import megengine.functional as F
>>> x = Tensor([0,1,2,3,4])
>>> F.clip(x, 4, 3)
Tensor([3 3 3 3 3], dtype=int32, device=xpux:0)


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())

Outputs:

.. testoutput::
>>> x = F.arange(5)
>>> F.clip(x, lower=3)
Tensor([3. 3. 3. 3. 4.], device=xpux:0)


[2 2 2 3 4]
[3 3 3 3 4]
[0 1 2 3 3]
>>> x = F.arange(5, dtype=np.int32)
>>> F.clip(x, upper=2.1)
Tensor([0. 1. 2. 2.1 2.1], device=xpux:0)
""" """
assert ( assert (
lower is not None or upper is not None lower is not None or upper is not None


Loading…
Cancel
Save