Skip to content

_sign

sign

Compute the sign of a tensor.

This method can be used with Numpy data:

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

This method can be used with TensorFlow tensors:

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

This method can be used with PyTorch tensors:

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

Parameters:

Name Type Description Default
tensor Tensor

The input value.

required

Returns:

Type Description
Tensor

The sign of each value of the tensor.

Raises:

Type Description
ValueError

If tensor is an unacceptable data type.

Source code in fastestimator/fastestimator/backend/_sign.py
def sign(tensor: Tensor) -> Tensor:
    """Compute the sign of a tensor.

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

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

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

    Args:
        tensor: The input value.

    Returns:
        The sign of each value of the `tensor`.

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