Python PyTorch linalg.svd() 方法
PyTorch linalg.svd()方法计算矩阵的奇异值分解 ( SVD )。 2D 张量是 PyTorch 中的矩阵。此方法支持实值和复值矩阵(float、double、cfloat 和 cdouble dtypes)。它接受输入一个矩阵或一批矩阵,并以命名元组(U,S,VT)的形式返回分解。我列出了 U、S 和 VT 的一些属性,如下所示:
- 如果输入矩阵 A 是实值矩阵,则 U 和 VT 都是正交的。
- 如果输入矩阵 A 是复值矩阵,则 U 和 VT 是酉的。
- 对于实值或复值输入矩阵 A,S 始终为实值。奇异值(S 的元素)按降序排列。
以下是语法 -
Syntax: torch.linalg.svd(A, full_matrices=True, *, out=None)
Parameters:
- A: the input matrix (tensor or batch of tensors).
- full_matrices: an optional boolean value. If True full SVD is computed. If False, reduced SVD is computed.
- out: the output tuple of three tensors.
Return: It returns three tensors as a named tuple (U, S, VT).
让我们借助一些Python程序示例来了解torch.linalg.svd()方法。
示例 1:
在此示例中,我们使用 torch.linalg.svd() 计算实值矩阵的完整奇异值分解。这里,U和VT矩阵是方阵,S的大小是min(2,3)。矩阵 U 和 VT 是正交的。您可以使用 U@Ut() 和 VT@VT.t() 检查这些矩阵的正交性。
Python3
# Python program to demonstrate torch.linalg.svd()
# method for a real-valued matrix
# importing torch
import torch
# creating a real-valued matrix (2-D tensor)
Mat = torch.tensor([[1.,2.,3.],
[3.,-2,-7]])
# printing the matrix
print("Matrix:\n", Mat)
# computing SVD of the matrix Mat
U, S, VT = torch.linalg.svd(Mat)
# printing U, S, and Vh
print("U:\n",U)
print("Singular Values (S):\n",S)
print("VT:\n",VT)
print("Shape of U, S and VT:\n", U.shape, S.shape, VT.shape)
Python3
# Python program to demonstrate torch.linalg.svd()
# method for a real-valued matrix with
# full_matrices=False
# importing torch
import torch
# creating a real-valued matrix (2-D tensor)
Mat = torch.rand(3, 4)
# printing the matrix
print("Matrix:\n", Mat)
# computing SVD of the matrix Mat
U, S, VT = torch.linalg.svd(Mat, full_matrices=False)
# printing U, S, and Vh
print("U:\n", U)
print("Singular Values (S):\n", S)
print("VT:\n", VT)
print("Shape of U, S and VT:\n", U.shape, S.shape, VT.shape)
Python3
# Python program to demonstrate torch.linalg.svd()
# method for a complex-valued matrix
# importing torch
import torch
# creating a complex-valued matrix (2-D tensor)
Mat = torch.randn(3, 2, dtype=torch.cfloat)
# printing the matrix
print("Matrix:\n", Mat)
# computing SVD of the matrix Mat
U, S, VT = torch.linalg.svd(Mat)
# printing U, S, and Vh
print("U:\n", U)
print("Singular Values (S):\n", S)
print("VT:\n", VT)
print("Shape of U, S and VT:\n", U.shape, S.shape, VT.shape)
输出:
Matrix:
tensor([[ 1., 2., 3.],
[ 3., -2., -7.]])
U:
tensor([[-0.3625, -0.9320],
[ 0.9320, -0.3625]])
Singular Values (S):
tensor([8.3999, 2.3329])
VT:
tensor([[ 0.2897, -0.3082, -0.9061],
[-0.8657, -0.4882, -0.1107],
[ 0.4082, -0.8165, 0.4082]])
Shape of U, S and VT:
torch.Size([2, 2]) torch.Size([2]) torch.Size([3, 3])
示例 2:
在下面的示例中,我们使用 torch.linalg.svd() 计算实值矩阵的简化奇异值分解。在这里,计算简化的 SVD。请注意,U 是正方形,而 VT 不是,S 的大小是 min(3,4)。由于输入矩阵是实值矩阵 U 和 VT 应该是正交的。您可以使用 U@Ut() 和 VT@VT.t() 检查这些矩阵的正交性。如果上面代码中的输入矩阵 Mat 是一个 4×3 的矩阵呢?是的!!!。 VT 是方形的,U 不是。
Python3
# Python program to demonstrate torch.linalg.svd()
# method for a real-valued matrix with
# full_matrices=False
# importing torch
import torch
# creating a real-valued matrix (2-D tensor)
Mat = torch.rand(3, 4)
# printing the matrix
print("Matrix:\n", Mat)
# computing SVD of the matrix Mat
U, S, VT = torch.linalg.svd(Mat, full_matrices=False)
# printing U, S, and Vh
print("U:\n", U)
print("Singular Values (S):\n", S)
print("VT:\n", VT)
print("Shape of U, S and VT:\n", U.shape, S.shape, VT.shape)
输出:
请注意,当我们使用随机数生成器创建输入矩阵时,您可能会从输出中得到不同的矩阵 -
Matrix:
tensor([[0.5280, 0.3108, 0.1215, 0.8151],
[0.9640, 0.4199, 0.3913, 0.2239],
[0.7886, 0.0702, 0.7123, 0.6120]])
U:
tensor([[-0.4984, -0.7911, -0.3546],
[-0.5810, 0.6083, -0.5407],
[-0.6435, 0.0634, 0.7628]])
Singular Values (S):
tensor([1.8411, 0.5512, 0.4225])
VT:
tensor([[-0.7227, -0.2412, -0.4053, -0.5052],
[ 0.3969, 0.0254, 0.3395, -0.8524],
[-0.2531, -0.6715, 0.6834, 0.1343]])
Shape of U, S and VT:
torch.Size([3, 3]) torch.Size([3]) torch.Size([3, 4])
示例 3:
在下面的示例中,我们计算复值矩阵的完整 SVD。请注意,S 是实值,U 和 VT 是复值。
Python3
# Python program to demonstrate torch.linalg.svd()
# method for a complex-valued matrix
# importing torch
import torch
# creating a complex-valued matrix (2-D tensor)
Mat = torch.randn(3, 2, dtype=torch.cfloat)
# printing the matrix
print("Matrix:\n", Mat)
# computing SVD of the matrix Mat
U, S, VT = torch.linalg.svd(Mat)
# printing U, S, and Vh
print("U:\n", U)
print("Singular Values (S):\n", S)
print("VT:\n", VT)
print("Shape of U, S and VT:\n", U.shape, S.shape, VT.shape)
输出:
请注意,当我们使用随机数生成器创建输入矩阵时,您可能会从下面的输出中得到不同的矩阵
Matrix:
tensor([[-0.4095-0.8878j, 0.3983+0.5446j],
[-1.3408+1.1268j, 0.3193+0.9775j],
[ 0.8876+0.6970j, -0.9217-0.5416j]])
U:
tensor([[-0.2687-0.3263j, -0.3146+0.2721j, 0.7890+0.1605j],
[-0.6525+0.3324j, -0.1834-0.6499j, 0.0518+0.0713j],
[ 0.5008+0.1853j, 0.3777-0.4778j, 0.5800-0.0863j]])
Singular Values (S):
tensor([2.4929, 1.3183])
VT:
tensor([[ 0.8916+0.0000j, -0.2928-0.3453j],
[-0.4527+0.0000j, -0.5767-0.6800j]])
Shape of U, S and VT:
torch.Size([3, 3]) torch.Size([2]) torch.Size([2, 2])