📜  Python PyTorch – linalg.norm() 方法

📅  最后修改于: 2022-05-13 01:55:00.603000             🧑  作者: Mango

Python PyTorch – linalg.norm() 方法

PyTorch linalg.norm() 方法计算向量或矩阵范数。范数始终是一个非负实数,它是矩阵大小的度量。它接受一个向量或矩阵或一批矩阵作为输入。它仅支持 float、double、cfloat 和 cdouble dtype 的输入。我们将使用以下语法来计算向量或矩阵范数。

如果dim是 int,则此方法计算向量范数,如果dim是 2 元组,则计算矩阵范数。如果dimord都为None ,则输入张量A将被展平为 1D 向量并计算 2-范数。如果ord != Nonedim= None ,则 A 必须是 1D 或 2D。

示例 1:

在下面的示例中,我们使用torch.linalg.norm()方法找到向量范数。我们定义一维张量(向量)并使用该方法的默认参数计算范数。我们还将 1D 张量重塑为 2D 并计算范数。在这里,一维张量和二维张量的范数是相同的。两个张量的元素相同,默认参数为 ord = None, dim =None。因此 2D 张量被展平为 1D 并计算向量范数。请注意,范数是非负实数值。

Python3
# Python program to compute vector norm
# importing libraries
import torch
import torch.linalg as la
  
# define input a 1D tensor
a = torch.tensor([-3., -4., 1., 0., 3., 2., -1., -7.])
print("1D Tensor:\n", a)
  
# computing the norm
norm = la.norm(a)
  
# print the computed norm
print("Norm:", norm)
  
# reshape the tensor to 2D tensor
a = a.reshape(2, 4)
print("2D Tensor:\n", a)
  
# again compute the norm
norm = la.norm(a)
  
# print the norm
print("Norm:", norm)


Python3
# Python program to compute norm
# importing libraries
import torch
import torch.linalg as la
  
# define input a 2D tensor (matrix)
a = torch.tensor([[1., 2., -3.],
                  [-4., 5., 6.],
                  [9., -7., 8.]])
print("Tensor:\n", a)
  
# compute and print the computed norm
print("\nNorm with different ord:\n")
print("Norm with ord= None:",
      la.norm(a, ord=None))
print("Norm ord= 'fro':",
      la.norm(a, ord='fro'))
print("Norm with ord= float('inf'):",
      la.norm(a, ord=float('inf')))
print("Norm with ord= -float('inf'):",
      la.norm(a, ord=-float('inf')))
print("Norm with ord= 'nuc'):",
      la.norm(a, ord='nuc'))
  
# print("Norm with ord= 0:", la.norm(a, ord= 0))
# ord=0 not supported for matrix norm
print("Norm with ord= 1:",
      la.norm(a, ord=1))
print("Norm with ord= -1:",
      la.norm(a, ord=-1))
print("Norm with ord= 2:",
      la.norm(a, ord=2))
print("Norm with ord= -2:",
      la.norm(a, ord=-1))


Python3
# Python program to compute matrix norm
# importing libraries
import torch
import torch.linalg as la
  
# define input a 2D tensor (matrix)
a = torch.tensor([[1., 2., -3.],
                  [-4., 5., 6.]])
print("Tensor:\n", a)
  
# compute and print the computed norm
# with different ord and dim
print("Norm with dim=0:",
      la.norm(a, dim=0))
print("Norm with dim=1:",
      la.norm(a, dim=1))
print("Norm with ord=1, dim=1:",
      la.norm(a, ord=1, dim=1))
print("Norm with ord=-1, dim=1:",
      la.norm(a, ord=-1, dim=1))
print("Norm with ord=1, dim=0:",
      la.norm(a, ord=1, dim=0))
print("Norm with ord=-1, dim=0:",
      la.norm(a, ord=1, dim=1))
print("Norm with dim=(0,1):",
      la.norm(a, dim=(0, 1)))


输出:

1D Tensor:
 tensor([-3., -4.,  1.,  0.,  3.,  2., -1., -7.])
Norm: tensor(9.4340)
2D Tensor:
 tensor([[-3., -4.,  1.,  0.],
        [ 3.,  2., -1., -7.]])
Norm: tensor(9.4340)

示例 2:

在下面的示例中,我们使用torch.linalg.norm()方法找到矩阵范数。我们定义了一个 2D 张量(矩阵)并用不同的ord值计算矩阵范数。输入张量是一个二维张量(矩阵)。对于不同的 ord 值,计算出的矩阵范数是不同的。请注意,矩阵范数不支持 ord = 0,向量(一维张量)范数不支持 ord =“fro”,ord =“nuc”。另外,请注意,范数是一个非负实数值。

Python3

# Python program to compute norm
# importing libraries
import torch
import torch.linalg as la
  
# define input a 2D tensor (matrix)
a = torch.tensor([[1., 2., -3.],
                  [-4., 5., 6.],
                  [9., -7., 8.]])
print("Tensor:\n", a)
  
# compute and print the computed norm
print("\nNorm with different ord:\n")
print("Norm with ord= None:",
      la.norm(a, ord=None))
print("Norm ord= 'fro':",
      la.norm(a, ord='fro'))
print("Norm with ord= float('inf'):",
      la.norm(a, ord=float('inf')))
print("Norm with ord= -float('inf'):",
      la.norm(a, ord=-float('inf')))
print("Norm with ord= 'nuc'):",
      la.norm(a, ord='nuc'))
  
# print("Norm with ord= 0:", la.norm(a, ord= 0))
# ord=0 not supported for matrix norm
print("Norm with ord= 1:",
      la.norm(a, ord=1))
print("Norm with ord= -1:",
      la.norm(a, ord=-1))
print("Norm with ord= 2:",
      la.norm(a, ord=2))
print("Norm with ord= -2:",
      la.norm(a, ord=-1))

输出:

Tensor:
 tensor([[ 1.,  2., -3.],
        [-4.,  5.,  6.],
        [ 9., -7.,  8.]])

Norm with different ord:

Norm with ord= None: tensor(16.8819)
Norm ord= 'fro': tensor(16.8819)
Norm with ord= float('inf'): tensor(24.)
Norm with ord= -float('inf'): tensor(6.)
Norm with ord= 'nuc'): tensor(25.4331)
Norm with ord= 1: tensor(17.)
Norm with ord= -1: tensor(14.)
Norm with ord= 2: tensor(14.2151)
Norm with ord= -2: tensor(14.)

示例 3:

在下面的示例中,我们使用torch.linalg.norm()方法找到规范。我们定义一个 2D 张量(矩阵)并使用不同的 dim 和 ord 值计算范数。输入张量是一个二维张量(矩阵)。对于不同的 ord 和 dim 值,计算出的矩阵范数是不同的。请注意,dim = 0 的范数与列一起计算,而 dim=1 的范数沿行计算。另外,请注意 norm 是一个非负实数值。

Python3

# Python program to compute matrix norm
# importing libraries
import torch
import torch.linalg as la
  
# define input a 2D tensor (matrix)
a = torch.tensor([[1., 2., -3.],
                  [-4., 5., 6.]])
print("Tensor:\n", a)
  
# compute and print the computed norm
# with different ord and dim
print("Norm with dim=0:",
      la.norm(a, dim=0))
print("Norm with dim=1:",
      la.norm(a, dim=1))
print("Norm with ord=1, dim=1:",
      la.norm(a, ord=1, dim=1))
print("Norm with ord=-1, dim=1:",
      la.norm(a, ord=-1, dim=1))
print("Norm with ord=1, dim=0:",
      la.norm(a, ord=1, dim=0))
print("Norm with ord=-1, dim=0:",
      la.norm(a, ord=1, dim=1))
print("Norm with dim=(0,1):",
      la.norm(a, dim=(0, 1)))

输出:

Tensor:
 tensor([[ 1.,  2., -3.],
        [-4.,  5.,  6.]])
Norm with dim=0: tensor([4.1231, 5.3852, 6.7082])
Norm with dim=1: tensor([3.7417, 8.7750])
Norm with ord=1, dim=1: tensor([ 6., 15.])
Norm with ord=-1, dim=1: tensor([0.5455, 1.6216])
Norm with ord=1, dim=0: tensor([5., 7., 9.])
Norm with ord=-1, dim=0: tensor([ 6., 15.])
Norm with dim=(0,1): tensor(9.5394)