Skip to content

_abs

abs

Compute the absolute value of a tensor.

This method can be used with Numpy data:

n = np.array([-2, 7, -19])
b = fe.backend.abs(n)  # [2, 7, 19]

This method can be used with TensorFlow tensors:

t = tf.constant([-2, 7, -19])
b = fe.backend.abs(t)  # [2, 7, 19]

This method can be used with PyTorch tensors:

p = torch.tensor([-2, 7, -19])
b = fe.backend.abs(p)  # [2, 7, 19]

Parameters:

Name Type Description Default
tensor ArrayT

The input value.

required

Returns:

Type Description
ArrayT

The absolute value of tensor.

Raises:

Type Description
ValueError

If tensor is an unacceptable data type.

Source code in fastestimator/fastestimator/backend/_abs.py
def abs(tensor: ArrayT) -> ArrayT:
    """Compute the absolute value of a tensor.

    This method can be used with Numpy data:
    ```python
    n = np.array([-2, 7, -19])
    b = fe.backend.abs(n)  # [2, 7, 19]
    ```

    This method can be used with TensorFlow tensors:
    ```python
    t = tf.constant([-2, 7, -19])
    b = fe.backend.abs(t)  # [2, 7, 19]
    ```

    This method can be used with PyTorch tensors:
    ```python
    p = torch.tensor([-2, 7, -19])
    b = fe.backend.abs(p)  # [2, 7, 19]
    ```

    Args:
        tensor: The input value.

    Returns:
        The absolute value of `tensor`.

    Raises:
        ValueError: If `tensor` is an unacceptable data type.
    """
    if tf.is_tensor(tensor):
        return tf.abs(tensor)
    elif isinstance(tensor, torch.Tensor):
        return torch.abs(tensor)
    elif isinstance(tensor, np.ndarray):
        return np.abs(tensor)
    else:
        raise ValueError("Unrecognized tensor type {}".format(type(tensor)))