Skip to content

WIP: supervised learning, data views #36

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

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ deps/deps.jl

*.ipynb_checkpoints

Manifest.toml
/Manifest.toml
/*.png
32 changes: 32 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Contains code modified from umap (https://raw.githubusercontent.com/lmcinnes/umap/)
which is available under the following License.

BSD 3-Clause License

Copyright (c) 2017, Leland McInnes
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 changes: 45 additions & 0 deletions examples/MNIST_demo.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using UMAP
using MLDatasets
using PyCall
const py_umap = pyimport_conda("umap", "umap-learn")

include("plotting.jl")

# First, let's get a small sample of the MNIST data.
n_points = 10_000
X = reshape(MNIST.traintensor(Float64)[:, :, 1:n_points], 28^2, :)

# We'll also get the labels
y = MNIST.trainlabels()[1:n_points]

# Now we run UMAP super and unsupervised, via PyCall and via UMAP.jl

## try to match py_umap's number of nndescent iterations
nndescent_kwargs = (max_iters = max(5, round(Int, log2(n_points))), sample_rate = 1)
unsup = UMAP_(X; n_neighbors=10, min_dist=0.001, n_epochs=200,
nndescent_kwargs = nndescent_kwargs)
py_unsup = py_umap.UMAP(min_dist=0.001, n_epochs=200, n_neighbors=10).fit(permutedims(X))

scene = plot_umap_comparison((permutedims(py_unsup.embedding_), y), (unsup.embedding, y);
titles=("PyUMAP (unsupervised)", "UMAP.jl (unsupervised)"))
save("MNIST_py_vs_jl_unsupervised.png", scene, px_per_unit=3, resolution=(1440, 810))


sup = UMAP_(X, y; n_neighbors=10, min_dist=0.001, n_epochs=200, far_dist=5.0,
nndescent_kwargs = nndescent_kwargs)
py_sup = py_umap.UMAP(min_dist=0.001, n_epochs=200, n_neighbors=10).fit(permutedims(X), y)

scene = plot_umap_comparison((permutedims(py_sup.embedding_), y), (sup.embedding, y);
titles=("PyUMAP (supervised)", "UMAP.jl (supervised)"))
save("MNIST_py_vs_jl_supervised.png", scene, px_per_unit=3, resolution=(1440, 810))


scene = plot_umap_comparison((unsup.embedding, y), (sup.embedding, y);
titles=("UMAP.jl (unsupervised)", "UMAP.jl (supervised)"))
save("MNIST_jl_unsup_vs_supervised.png", scene, px_per_unit=3, resolution=(1440, 810))


scene = plot_umap_comparison((permutedims(py_unsup.embedding_), y),
(permutedims(py_sup.embedding_), y);
titles=("PyUMAP (unsupervised)", "PyUMAP (supervised)"))
save("MNIST_py_unsup_vs_supervised.png", scene, px_per_unit=3, resolution=(1440, 810))
50 changes: 50 additions & 0 deletions examples/MNIST_no_python.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using UMAP
using MLDatasets
using Distances

include("plotting.jl")

# First, let's get a small sample of the MNIST data.
n_points = 1_000
X = reshape(MNIST.traintensor(Float64)[:, :, 1:n_points], 28^2, :)

# We'll also get the labels
y = MNIST.trainlabels()[1:n_points]

# Now we run UMAP super and unsupervised, via PyCall and via UMAP.jl

## try to match py_umap's number of nndescent iterations
nndescent_kwargs = (max_iters = max(5, round(Int, log2(n_points))), sample_rate = 1)
n_neighbors = 20
n_neighbors_meta = 200

unsup = Embedding(DataView(X; metric=Euclidean(), n_neighbors, nndescent_kwargs); min_dist=0.001, n_epochs=200)

sup = Embedding(DataView(X; metric=Euclidean(), n_neighbors, nndescent_kwargs), DataView(y;metric=Categorical(far_dist=5.0), n_neighbors=n_neighbors_meta, nndescent_kwargs); min_dist=0.001, n_epochs=200)

# Bug in NearestNeighbors or Distances?
# NN calls `result_type(metric, data[1], data[2])` instead of `result_type(metric, typeof(data[1]), typeof(data[2])`.
# But this works for arrays...
Distances.result_type(M, ::Int, ::Int) = Distances.result_type(M, Int, Int)
#
let
scene = plot_umap_comparison((unsup.embedding, y), (sup.embedding, y);
titles=("UMAP.jl (unsupervised)", "UMAP.jl (supervised)"), markersize=5px)
save("MNIST_jl_unsup_vs_supervised.png", scene, px_per_unit=3, resolution=(1440, 810))
end



for mix_weight in (0.001, 0.25, 0.5, 0.75, 0.999), n_neighbors_meta in (20, 200)
sup_cts = Embedding(DataView(X; metric=Euclidean(), n_neighbors, nndescent_kwargs), DataView(y;metric=Euclidean(), n_neighbors=n_neighbors_meta, nndescent_kwargs); min_dist=0.001, n_epochs=200, mix_weight)

scene = plot_umap_comparison((sup.embedding, y), (sup_cts.embedding, y);
titles=("UMAP.jl (supervised; categorical)", "UMAP.jl (supervised; continuous) $(n_neighbors_meta) neighbors), mix_weight=$(mix_weight)"), markersize=5px)
save("MNIST_jl_vs_jl_supervised_cat_vs_cts_max_weight_$(mix_weight)_neighbors_$(n_neighbors_meta).png", scene, px_per_unit=3, resolution=(1440, 810))


scene = plot_umap_comparison((unsup.embedding, y), (sup_cts.embedding, y);
titles=("UMAP.jl (unsupervised)", "UMAP.jl (supervised; continuous) $(n_neighbors_meta) neighbors), mix_weight=$(mix_weight)"), markersize=5px)
save("MNIST_jl_vs_jl_unsupervised_vs_cts_max_weight_$(mix_weight)_neighbors_$(n_neighbors_meta).png", scene, px_per_unit=3, resolution=(1440, 810))

end
Loading