随机奇异值分解(Randomized SVD, rSVD)

博客围绕RSVD和SVD算法展开,给出rSVD算法流程,进行代码详解和案例分析。以原始图片为例,对比了SVD和RSVD的耗时,SVD耗时8.07s,而RSVD仅耗时0.46s,体现出RSVD速度优势。还列出了参考文献。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

假设 X ∈ R n × m X \in \mathbb{R}^{n\times m} XRn×m 为瘦高矩阵, n > m n > m n>m

rSVD 算法流程如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

# Define randomized SVD function
def rSVD(X,r,q=0,p=0):
    # Step 1: Sample column space of X with P matrix
    ny = X.shape[1]
    P = np.random.randn(ny,r+p)
    Z = X @ P
    for k in range(q):
        Z = X @ (X.T @ Z)

    Q, R = np.linalg.qr(Z,mode='reduced')

    # Step 2: Compute SVD on projected Y = Q.T @ X
    Y = Q.T @ X
    UY, S, VT = np.linalg.svd(Y,full_matrices=0)
    U = Q @ UY

    return U, S, VT

代码详解

在这里插入图片描述

案例

原始图片,大小: (3207, 2260)
在这里插入图片描述

A = imread(os.path.join('..','DATA','jupiter.jpg'))
X = np.mean(A,axis=2) # Convert RGB -> grayscale

U, S, VT = np.linalg.svd(X,full_matrices=0) # Deterministic SVD, [time spent: 8.07s]


r = 400 # Target rank
q = 1   # Power iterations
p = 5   # Oversampling parameter

rU, rS, rVT = rSVD(X,r,q,p)  # [time spent: 0.46s]

SVD 耗时 8.07 s

RSVD 耗时 0.46s,很快啊

## Reconstruction
XSVD = U[:,:(r+1)] @ np.diag(S[:(r+1)]) @ VT[:(r+1),:] # SVD approximation
errSVD = np.linalg.norm(X-XSVD,ord=2) / np.linalg.norm(X,ord=2)

XrSVD = rU[:,:(r+1)] @ np.diag(rS[:(r+1)]) @ rVT[:(r+1),:] # SVD approximation
errSVD = np.linalg.norm(X-XrSVD,ord=2) / np.linalg.norm(X,ord=2)

## Plot

fig, axs = plt.subplots(1,3,figsize=(20,10))

plt.set_cmap('gray')
axs[0].imshow(X)
axs[0].axis('off')
axs[0].set_title('original image')
axs[1].imshow(XSVD)
axs[1].axis('off')
axs[1].set_title('SVD')
axs[2].imshow(XrSVD)
axs[2].axis('off')
axs[2].set_title('rSVD')

plt.show()

在这里插入图片描述

参考文献

  • http://databookuw.com/
  • 《Data-Driven Science and Engineering —— Machine Learning, Dynamical Systems, and Control》
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

颹蕭蕭

白嫖?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值