Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow diffusion map to take as input a Gram matrix? #27

Closed
SimonEnsemble opened this issue Feb 4, 2022 · 3 comments
Closed

allow diffusion map to take as input a Gram matrix? #27

SimonEnsemble opened this issue Feb 4, 2022 · 3 comments

Comments

@SimonEnsemble
Copy link
Contributor

SimonEnsemble commented Feb 4, 2022

hi. wanting to learn a diffusion map with a custom kernel.
I have my own Gram matrix. (can't explicitly build the feature vectors...)
are you able / can I make a PR to allow this to take as input a Gram matrix whose element (i,j) is k(x_i, x_j) with k the kernel?

https://github.com/wildart/ManifoldLearning.jl/blob/master/src/diffmaps.jl#L66

something like the below?

function fit(::Type{DiffMap}, X::AbstractMatrix{T}; maxoutdim::Int=2, t::Int=1, α::Real=0.0, ɛ::Real=1.0, custom_gram::Bool=false) where {T<:Real}
    if ! custom_gram
      sumX = sum(X.^ 2, dims=1)
      K = exp.(-( transpose(sumX) .+ sumX .- 2*transpose(X) * X ) 
   else
      @assert size(K)[1] == size(K)[2] "Gram matrix must be square!"
      @assert all(K .>= 0.0)
      @assert K == K'
   end
   L = K ./ convert(T, ɛ))

https://inside.mines.edu/~whereman/talks/delaPorte-Herbst-Hereman-vanderWalt-DiffusionMaps-PRASA2008.pdf
going off of the above reference.

edit

I now understand that

    D = Diagonal(vec(sum(L, dims=1)))
    M = inv(D)*L

will normalize the rows :)

thx!

@wildart
Copy link
Owner

wildart commented Feb 6, 2022

Sure, PR is quite welcome. I think the better design for custom kernel would be similar to how KernelPCA fit handles different kernels, see here: https://github.com/JuliaStats/MultivariateStats.jl/blob/99ee965df3a8e136ff2d0fcb10456b434e1f9001/src/kpca.jl#L157-L165

@SimonEnsemble
Copy link
Contributor Author

thx, wanted to change this for our research project anyway so glad I could try to help out. let me know if you want me to change anything or have improvements you want me to make.

[we are using it for when the gram matrix is from a graph kernel so the X input argument does not work for us.]

https://github.com/wildart/ManifoldLearning.jl/pull/28/files

@wildart
Copy link
Owner

wildart commented Feb 16, 2022

This is included in the new release.

@wildart wildart closed this as completed Feb 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants