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

transform nn #64

Merged
merged 1 commit into from
Jul 18, 2020
Merged

transform nn #64

merged 1 commit into from
Jul 18, 2020

Conversation

yuhanH
Copy link

@yuhanH yuhanH commented Jul 17, 2020

Thanks for answering my issue. #63
Here is an application.
First, I build training umap with precalculated nearest neighbor input and return its model.
Then, I build the test-train nearest neighbor, and put it into umap_transform which avoids annoy_search for X, and use precalculated test-train nearest neighbor as input.
This NN input can give users flexibilities to add/explore their specific kernel into umap and transform_umap, not limited to the features input. Thanks.

This is an example:

devtools::install_github("jlmelville/vizier")
devtools::install_github("jlmelville/snedata")
fashion <- snedata::download_fashion_mnist()
fashion_train <- head(fashion, 60000)
fashion_test <- tail(fashion, 10000)

fashion_train.nn <- uwot:::annoy_nn(X = as.matrix(fashion_train[, 1:784]),
                                    k = 15, 
                                    metric = "cosine",
                                    ret_index = T)
# return umap map with annoy_nn input
set.seed(1337)
fashion_umap <- umap(X = NULL, nn_method = fashion_train.nn, ret_model = T)

# compute the query-reference annoy_nn
query_ref.nn <- annoy_search(X = as.matrix(fashion_test[, 1:784]), 
                             k = 15, 
                             ann = fashion_train.nn$index  )

# use the query-reference annoy_nn to transform query to reference
fashion_umap_test <- umap_transform(X = NULL,  model = fashion_umap, nn_method = query_ref.nn)


vizier::embed_plot(fashion_umap$embedding, fashion, cex = 0.5, title = "Fashion UMAP", alpha_scale = 0.075)
vizier::embed_plot(fashion_umap_test, fashion_test, cex = 0.5, title = "Fashion Test UMAP", alpha_scale = 0.075)

@jlmelville jlmelville merged commit d4ecda6 into jlmelville:master Jul 18, 2020
@jlmelville
Copy link
Owner

I understand now. Thank you for the PR. Happy to merge it if it will be useful to you. I will also add you as a contributor to the DESCRIPTION.

@yuhanH
Copy link
Author

yuhanH commented Jul 18, 2020

Cool. Thanks!

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

Successfully merging this pull request may close these issues.

2 participants