megengine.functional.abs#

abs(x)[source]#

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

Parameters:

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

Returns:

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.

Examples

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