torch.norm [deprecated ]
在torch.norm中,通过参数p来定制order
主要有如下几类
- L1 norm
计算张量中所有数值之和 - L2 norm
计算张量中所有数值的平方和开根 - Frobenius norm
计算张量中所有维度上所有数值的平方和开根 - Infinity norm
计算张量中有所数值绝对值最大 - Negative infinity norm
计算张量中所有数值绝对值最小
python">import torch# Create a tensor
x = torch.tensor([1.0, -2.0, 3.0])# L1 norm
l1_norm = torch.norm(x, p=1)
print("L1 norm(p=1):", l1_norm) # Output: 6.0# L2 norm
l2_norm = torch.norm(x, p=2)
print("L2 norm(p=2):", l2_norm) # Output: 3.7416573867739413# Frobenius norm (for matrices)
X = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
fro_norm = torch.norm(X, p='fro')
print("Frobenius norm(p='fro'):", fro_norm) # Output: 5.477225575051661# Infinity norm
inf_norm = torch.norm(x, p=float('inf'))
print("Infinity norm(p=float('inf')):", inf_norm) # Output: 3.0# Negative infinity norm
neg_inf_norm = torch.norm(x, p=float('-inf'))
print("Negative infinity norm(p=float('-inf')):", neg_inf_norm) # Output: 1.0
torch.linalg
python">import torch
import torch.nn.functional as F# Create a tensor
x = torch.tensor([1.0, -2.0, 3.0])# L1 norm
l1_norm = torch.linalg.vector_norm(x, ord=1)
print("L1 norm(p=1):", l1_norm) # Output: 6.0# L2 norm
l2_norm = torch.linalg.vector_norm(x, ord=2)
print("L2 norm(p=2):", l2_norm) # Output: 3.7416573867739413# Frobenius norm (for matrices)
X = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
fro_norm = torch.linalg.matrix_norm(X, ord='fro')
print("Frobenius norm(p='fro'):", fro_norm) # Output: 5.477225575051661# Infinity norm
inf_norm = torch.linalg.vector_norm(x, ord=float('inf'))
print("Infinity norm(p=float('inf')):", inf_norm) # Output: 3.0# Negative infinity norm
neg_inf_norm = torch.linalg.vector_norm(x, ord=float('-inf'))
print("Negative infinity norm(p=float('-inf')):", neg_inf_norm) # Output: 1.0
torch.nn.functional.normalize
注意,dim是必须要指定的,不然默认是1
python">import torch
import torch.nn.functional as F# Create a tensor
x = torch.tensor([1.0, -2.0, 3.0])
X = torch.tensor([[1.0, 2.0], [3.0, 4.0]])# L1 norm using F.normalize
l1_norm = F.normalize(x, p=1,dim=0)
print("L1 norm(p=1):", l1_norm) # Output: tensor([ 0.1667, -0.3333, 0.5000])# L2 norm using F.normalize
l2_norm = F.normalize(x, p=2,dim=0)
print("L2 norm(p=2):", l2_norm) # Output: [[0.3162, 0.4472],# [0.9487, 0.8944]]# Frobenius norm (for matrices) using F.normalize
fro_norm = F.normalize(X, p='fro',dim=0 )
print("Frobenius norm(p='fro'):", fro_norm) # Output: tensor([[0.3162, 0.4472],# [0.9487, 0.8944]])# Infinity norm using F.normalize
inf_norm = F.normalize(x, p=float('inf'),dim=0)
print("Infinity norm(p=float('inf')):", inf_norm) # Output: [ 0.3333, -0.6667, 1.0000]# Negative infinity norm using F.normalize
neg_inf_norm = F.normalize(x, p=float('-inf'),dim=0)
print("Negative infinity norm(p=float('-inf')):", neg_inf_norm) # Output: [ 1., -2., 3.]# Examples showing the usage of dim
# Example 1: L2 norm along dimension 0
x_2d = torch.tensor([[1.0, -2.0, 3.0], [4.0, -5.0, 6.0]])
l2_norm_dim0 = F.normalize(x_2d, p=2, dim=0)
print("L2 norm along dim=0:", l2_norm_dim0) # Output: [[ 0.2425, -0.3714, 0.4472],# [ 0.9701, -0.9285, 0.8944]]# Example 2: L2 norm along dimension 1
l2_norm_dim1 = F.normalize(x_2d, p=2, dim=1)
print("L2 norm along dim=1:", l2_norm_dim1) # Output: [[ 0.2673, -0.5345, 0.8018],# [ 0.4558, -0.5698, 0.6838]]