vector_norm(input: Tensor, ord: int | float = 2, dim: int | list[int] | tuple[int, ...] | None = None, keepdim: bool = False, dtype: object = None)Compute a vector -norm along an axis.
Reduces along dim (or all elements if dim is None) using
for any positive real . Special-cased values:
- — count of non-zero entries (not a true norm).
- — .
- — Euclidean norm.
- — .
- — .
Parameters
inputTensorordint or float= 22.dimint, list of int, tuple of int or None= NoneNone reduces over every axis — the
rank is retained, so keepdim=True yields one size-1 axis per
input axis rather than a single one.keepdimbool= FalseTrue, reduced dimensions are retained with size 1.dtype(dtype, keyword - only)= Noneinput to this dtype before reducing, so the whole computation
— and the result — is in it; a complex dtype gives its real
counterpart. It must be complex for a complex input and real
otherwise, and must not narrow input. None (default) reduces
in input's own dtype.Returns
TensorNorm along the specified axes — real, also for a complex input.
Raises
TypeErrorinput is an integer or bool tensor (a DtypeMismatch), or if
dtype would narrow it or its kind (real / complex) differs.ValueErrordim names one axis twice.Notes
Gradients flow for non-integer too. A slice whose -th
powers leave the float range keeps its norm and its derivatives
(float32 near 1e-20 gives 2e-20): they are taken relative to
its largest entry. A zero norm has a zero gradient — the subgradient the
reference picks — rather than the nan there would give; so is a zero
entry's for .
A float16 or bfloat16 input is reduced in float32 and the
result cast back, so a norm that fits the input dtype is not lost to
an intermediate sum that does not: the 2-norm of 65 536 half-precision
ones is 256, not inf.
A complex input is reduced through its magnitudes , as
the definition says. Squaring the entries themselves gave a complex
"norm" for ord=2 on Metal and an error on the CPU.
Examples
>>> import lucid
>>> from lucid.linalg import vector_norm
>>> vector_norm(lucid.tensor([3.0, 4.0]))
tensor(5.)
>>> vector_norm(lucid.tensor([1.0, -2.0, 3.0]), ord=1)
tensor(6.)
>>> vector_norm(lucid.tensor([3.0, 4.0]), dtype=lucid.float64)
tensor(5., dtype=lucid.float64)
>>> vector_norm(lucid.tensor([3 + 4j, 0j]))
tensor(5.)Used by 3
- lucid._ops.composite.reductions
- lucid.nn.functional.normalization
- lucid.peft.tuners.delora._layer