Randomized SVD

Related question: Compressed svd algorithm implementation - #4 by odow

@Desmond it’s easier to help if you can provide a reproducible example that people can copy-and-paste.

import TestImages
import Images
import ImageInTerminal
import LinearAlgebra

function rsvd(X, k)
    m, n = size(X)
    Φ = rand(n, k)
    Y = X * Φ
    Q, R = LinearAlgebra.qr(Y)
    Qm = Matrix(Q)
    B = Qm' * X
    Uhat, S, Vt = LinearAlgebra.svd(B)
    Uk = Uhat[:, 1:k]
    Vk = Vt'[1:k, :]
    U = Qm * Uk
    return U * LinearAlgebra.Diagonal(S) * Vk
end

image = TestImages.testimage("mandrill")
gray_image = Images.Gray.(image);
image_matrix = Images.channelview(gray_image);
M = rsvd(image_matrix, 5);
Images.colorview(Images.Gray, M)

1 Like