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 |
Tensor
|
The input value. |
required
|
Returns:
Type |
Description |
Tensor
|
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: Tensor) -> Tensor:
"""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)))
|