lucid.linalg.norm

lucid.linalg.norm(a: Tensor, /, ord: int = 2, axis: tuple[int, ...] | int | None = None, keepdims: bool = False) Tensor

The norm function computes the \(p\)-norm of a tensor, where \(p\) is specified by the ord parameter.

Function Signature

def norm(a: Tensor, ord: int = 2) -> Tensor

Parameters

  • a (Tensor):

    The input tensor for which the norm is computed.

  • ord (int, optional):

    The order of the norm. Defaults to \(2\) (Euclidean norm).

    Supported values include: - \(1\): Manhattan norm (sum of absolute values). - \(2\): Euclidean norm.

Returns

  • Tensor:

    A scalar tensor representing the computed \(p\)-norm of the input tensor.

Forward Calculation

The \(p\)-norm is calculated as:

\[\| \mathbf{a} \|_p = \left( \sum_i |\mathbf{a}_i|^p \right)^{1/p}\]

For \(p = 1\), this becomes the sum of absolute values. For \(p = 2\), it is the Euclidean norm. For \(p = \infty\), it is the maximum absolute value.

Backward Gradient Calculation

The gradient of the \(p\)-norm with respect to the tensor \(\mathbf{a}\) is computed as:

\[\begin{split}\frac{\partial \| \mathbf{a} \|_p}{\partial \mathbf{a}_i} = \begin{cases} \text{sgn}(\mathbf{a}_i) \cdot |\mathbf{a}_i|^{p-1} \cdot \| \mathbf{a} \|_p^{1-p}, & \text{if } p > 1 \\ \text{sgn}(\mathbf{a}_i), & \text{if } p = 1 \\ 0, & \text{if } \mathbf{a}_i \neq \max(|\mathbf{a}|) \text{ for } p = \infty \end{cases}\end{split}\]

This calculation depends on the value of ord.

Raises

Attention

  • ValueError: If the value of ord is not supported.

  • LinAlgError: If the input tensor does not support norm computation for the specified ord.

Example

>>> import lucid
>>> a = lucid.Tensor([3.0, 4.0])
>>> n = lucid.linalg.norm(a, ord=2)
>>> print(n)
Tensor(5.0)

>>> n1 = lucid.linalg.norm(a, ord=1)
>>> print(n1)
Tensor(7.0)