# megengine.functional.abs¶

abs(x)[源代码]

Element-wise $$\operatorname{abs}(x)$$ function.

Calculates the absolute value for each element $$x_i$$ of the input tensor $$x$$. (i.e., the element-wise result has the same magnitude as the respective element in x but has positive sign).

x – input tensor. Should have a numeric data type.

a tensor containing the absolute value of each element in $$x$$. The returned tensor must have the same data type as $$x$$.

Special cases

For floating-point operands,

• If $$x_i$$ is NaN, the result is NaN.

• If $$x_i$$ is -0, the result is +0.

• If $$x_i$$ is -infinity, the result is +infinity.

>>> F.abs(-2)
Tensor(2, dtype=int32, device=xpux:0)


Element-wise absolution:

>>> x = Tensor([1, -2, 3, -4, 5])
>>> F.abs(x)
Tensor([1 2 3 4 5], dtype=int32, device=xpux:0)