【pytorch】norm的使用

torch.norm [deprecated ]

在torch.norm中,通过参数p来定制order

主要有如下几类

  • L1 norm
    计算张量中所有数值之和
  • L2 norm
    计算张量中所有数值的平方和开根
  • Frobenius norm
    计算张量中所有维度上所有数值的平方和开根
  • Infinity norm
    计算张量中有所数值绝对值最大
  • Negative infinity norm
    计算张量中所有数值绝对值最小
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

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

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]]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值