lucid.abs

lucid.abs(a: Tensor, /) Tensor

The abs function computes the element-wise absolute value of each element in the input tensor.

Function Signature

def abs(a: Tensor) -> Tensor

Parameters

  • a (Tensor): The input tensor whose absolute values are computed.

Returns

  • Tensor:

    A new tensor containing the absolute values of the elements in the input tensor. If a requires gradients, the resulting tensor will also require gradients.

Forward Calculation

\[\mathbf{out}_i = |\mathbf{a}_i|\]

Backward Gradient Calculation

\[\begin{split}\frac{\partial \mathbf{out}_i}{\partial \mathbf{a}_i} = \begin{cases} 1 & \text{if } \mathbf{a}_i > 0 \\ -1 & \text{if } \mathbf{a}_i < 0 \\ 0 & \text{if } \mathbf{a}_i = 0 \end{cases}\end{split}\]

Example

>>> import lucid
>>> a = Tensor([-1, -0.5, 0, 0.5, 1], requires_grad=True)
>>> out = lucid.abs(a)
>>> print(out)
Tensor([1. 0.5 0. 0.5 1.], grad=None)