Randomized SVD

I am implement the randomized SVD without power iteration and I am just getting a black and white dots as the output of the SVD.

I can’t seem to spot the problem. Here is the code

function rsvd(X,k)
    (m,n)=size(X)
    Phi=rand(n,k)
    Y=X*(Phi)
    Q,R = qr(Y)
    B=transpose(Matrix(Q))*X
    Uhat,S,Vt=svd(B)
    Uk=Uhat[:, 1:k]
    Vk=Vt'[1:k, :]
    U=Matrix(Q)*Uk
    M = U * diagm(S) * Vk
    return M
    println(size(M))
    
end 

Let me know if you can spot the error.

What do you mean by black and white dots? Can you give a screenshot?
It works on my machine.

I am using the gray version of the mandrill image from the Julia TestImage packages. This is the result. Attached is the image after I use 5 singular values.

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

Sorry about the code.

I think it’s working now.

But I don’t see the difference between your code and mine. The only difference is diagm(S) and Diagonal(S) which I think doesn’t make any difference.

Or I maybe wrong?

How are you plotting the output?

It’s really crucial to include an example that could be reproduced. Otherwise, how can we know it’s not just a plotting issue, for example.

1 Like

Here is the code

using LinearAlgebra
using FundamentalsNumericalComputation
using TestImages
using Images

image=TestImages.testimage("mandrill")
grayimg=Gray.(image
X=channelview(grayimg)

function rsvd(X,k)
    (m,n)=size(X)
    Phi=rand(n,k)
    Y=X*Phi
    Q,R = qr(Y)
    B=transpose(Matrix(Q))*X
    Uhat,S,Vt=svd(B)
    Uk=Uhat[:, 1:k]
    Vk=Vt'[1:k, :]
    U=Matrix(Q)*Uk
    M = U * diagm(S) * Vk
    return M 
end 

M=rsvd(X,5)
Gray.(M)