Pytorch Torch Linalg Norm
# PyTorch torch.linalg.norm Function
* * PyTorch torch Reference Manual](#)
`torch.linalg.norm` is a function in PyTorch's linear algebra module for calculating matrix or vector norms. It supports multiple norm types such as L1, L2, Frobenius norms, etc.
### Function Definition
torch.linalg.norm(A, ord=None, dim=None, keepdim=False, out=None, dtype=None)
**Parameters**:
* `A` (Tensor): Input tensor.
* `ord` (int, float, inf, -inf, optional): Norm type. Default is 'fro'.
* `dim` (int, tuple, optional): Dimension(s) for norm calculation.
* `keepdim` (bool, optional): Whether to retain dimensions. Default is False.
* `dtype` (torch.dtype, optional): Output data type.
**Returns**:
* `torch.Tensor`: Returns the norm value.
* * *
## Usage Examples
## Example - Frobenius Norm
import torch
# Create matrix
A = torch.tensor([[1.0,2.0],[3.0,4.0]])
# Frobenius norm
norm_fro = torch.linalg.norm(A)
print("Matrix A:")
print(A)
print("Frobenius norm:", norm_fro)
Output result:
Matrix A: tensor([[1., 2.], [3., 4.]])Frobenius norm: tensor(5.4772)
## Example - Vector L2 Norm
import torch
# Create vector
v = torch.tensor([3.0,4.0])
# L2 norm (default)
norm_l2 = torch.linalg.norm(v)
# L1 norm
norm_l1 = torch.linalg.norm(v,ord=1)
print("Vector v:", v)
print("L2 norm:", norm_l2)
print("L1 norm:", norm_l1)
* * PyTorch torch Reference Manual](#)
YouTip