Skip to content

_pow

pow

Raise a tensor to a given power.

This method can be used with Numpy data:

n = np.array([-2, 7, -19])
b = fe.backend.pow(n, 2)  # [4, 49, 361]

This method can be used with TensorFlow tensors:

t = tf.constant([-2, 7, -19])
b = fe.backend.pow(t, 2)  # [4, 49, 361]

This method can be used with PyTorch tensors:

p = torch.tensor([-2, 7, -19])
b = fe.backend.pow(p, 2)  # [4, 49, 361]

Parameters:

Name Type Description Default
tensor Tensor

The input value.

required
power Union[int, float, Tensor]

The exponent to raise tensor by.

required

Returns:

Type Description
Tensor

The exponentiated tensor.

Raises:

Type Description
ValueError

If tensor is an unacceptable data type.

Source code in fastestimator/fastestimator/backend/_pow.py
def pow(tensor: Tensor, power: Union[int, float, Tensor] ) -> Tensor:
    """Raise a `tensor` to a given `power`.

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

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

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

    Args:
        tensor: The input value.
        power: The exponent to raise `tensor` by.

    Returns:
        The exponentiated `tensor`.

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