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:
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:
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)