亚欧色一区w666天堂,色情一区二区三区免费看,少妇特黄A片一区二区三区,亚洲人成网站999久久久综合,国产av熟女一区二区三区

  • 發布文章
  • 消息中心
點贊
收藏
評論
分享
原創

SVD分解

2024-11-26 09:11:04
12
0
import torch

# 原始評分矩陣 5x4
A = torch.tensor([[5, 3, 0, 1],
[4, 0, 0, 1],
[1, 1, 0, 5],
[1, 0, 0, 4],
[0, 1, 5, 4]], dtype=torch.float32)

# 1.對矩陣A進行SVD分解
U, sigma, Vt = torch.svd(A)
# 打印結果
print("U:", U.shape) # torch.Size([5, 4]) 為什么不是 5x5 ?
print("S:", sigma.shape) # S: torch.Size([4])
print("V:", Vt.shape) # V: torch.Size([4, 4])

# 2.構造奇異值矩陣
Sigma = torch.diag(sigma)

# 3.重構原始矩陣
reconstructed_A = torch.mm(torch.mm(U, Sigma), Vt.t())

print("Original matrix A:")
print(A)
print("\nReconstructed matrix A:")
print(reconstructed_A)

# torch.svd_lowrank ()是PyTorch中的一個函數,用于計算矩陣的低秩奇異值分解(Low-rank Singular Value Decomposition,簡稱LSVD)。
import torch

# 創建一個隨機矩陣 (復用 A)

# 計算低秩 SVD
U, S, V = torch.svd_lowrank(A, q=4) # 不配置q 會報錯 ,why ?

# 打印結果
print("U:", U.shape) # U: torch.Size([5, 4])
print("S:", S.shape) # S: torch.Size([4])
print("V:", V.shape) # V: torch.Size([4, 4])

# 重構矩陣
A_reconstructed = U @ torch.diag(S) @ V.T

# 打印重構矩陣
print("Original A:\n", A)
print("Reconstructed A:\n", A_reconstructed)

"""
torch.svd:
計算復雜度: 計算完整 SVD,時間復雜度為 (O(mn^2)),適用于較小的矩陣。
適用場景: 適用于需要精確 SVD 的場景,特別是在矩陣規模較小時。

torch.svd_lowrank:
計算復雜度: 計算低秩 SVD,時間復雜度較低,適用于大規模矩陣。
適用場景: 適用于大規模矩陣的近似 SVD,特別是在需要減少計算復雜度和內存使用時。
"""
0條評論
作者已關閉評論
Top123
32文章數
3粉絲數
Top123
32 文章 | 3 粉絲
Top123
32文章數
3粉絲數
Top123
32 文章 | 3 粉絲
原創

SVD分解

2024-11-26 09:11:04
12
0
import torch

# 原始評分矩陣 5x4
A = torch.tensor([[5, 3, 0, 1],
[4, 0, 0, 1],
[1, 1, 0, 5],
[1, 0, 0, 4],
[0, 1, 5, 4]], dtype=torch.float32)

# 1.對矩陣A進行SVD分解
U, sigma, Vt = torch.svd(A)
# 打印結果
print("U:", U.shape) # torch.Size([5, 4]) 為什么不是 5x5 ?
print("S:", sigma.shape) # S: torch.Size([4])
print("V:", Vt.shape) # V: torch.Size([4, 4])

# 2.構造奇異值矩陣
Sigma = torch.diag(sigma)

# 3.重構原始矩陣
reconstructed_A = torch.mm(torch.mm(U, Sigma), Vt.t())

print("Original matrix A:")
print(A)
print("\nReconstructed matrix A:")
print(reconstructed_A)

# torch.svd_lowrank ()是PyTorch中的一個函數,用于計算矩陣的低秩奇異值分解(Low-rank Singular Value Decomposition,簡稱LSVD)。
import torch

# 創建一個隨機矩陣 (復用 A)

# 計算低秩 SVD
U, S, V = torch.svd_lowrank(A, q=4) # 不配置q 會報錯 ,why ?

# 打印結果
print("U:", U.shape) # U: torch.Size([5, 4])
print("S:", S.shape) # S: torch.Size([4])
print("V:", V.shape) # V: torch.Size([4, 4])

# 重構矩陣
A_reconstructed = U @ torch.diag(S) @ V.T

# 打印重構矩陣
print("Original A:\n", A)
print("Reconstructed A:\n", A_reconstructed)

"""
torch.svd:
計算復雜度: 計算完整 SVD,時間復雜度為 (O(mn^2)),適用于較小的矩陣。
適用場景: 適用于需要精確 SVD 的場景,特別是在矩陣規模較小時。

torch.svd_lowrank:
計算復雜度: 計算低秩 SVD,時間復雜度較低,適用于大規模矩陣。
適用場景: 適用于大規模矩陣的近似 SVD,特別是在需要減少計算復雜度和內存使用時。
"""
文章來自個人專欄
文章 | 訂閱
0條評論
作者已關閉評論
作者已關閉評論
0
0