8000 GitHub - missing-user/jaxkd: Minimal JAX implementation of k-nearest neighbors using a k-d tree.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

missing-user/jaxkd

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JAX k-D

Find k-nearest neighbors using a k-d tree in JAX!

This is an XLA version of two GPU-friendly tree algorithms [1, 2]. It is convenient and lightweight, but the original CUDA implementation [3] may be a better choice depending on the application.

The build_tree, query_neighbors, and count_neighbors operations are compatible with JIT and automatic differentiation. They are reasonably fast when vectorized on GPU, but will be much slower than scipy.spatial.KDTree on CPU. The main advantage is to avoid the complexity of using non-JAX libraries and potentially leaving JIT and the GPU when a scalable nearest neighbor search is needed as part of a larger JAX program.

Usage

import jax
import jaxkd as jk

kp, kq = jax.random.split(jax.random.key(83))
points = jax.random.normal(kp, shape=(100_000, 3))
queries = jax.random.normal(kq, shape=(10_000, 3))

tree = jk.build_tree(points)
neighbors, distances = jk.query_neighbors(tree, queries, 10)
counts = jk.count_neighbors(tree, queries, 0.1)

There is also a simple k-means implementation in jaxkd.extras. More suggestions welcome!

Installation

To install, use pip. The only dependency is jax.

python -m pip install jaxkd

Or just grab tree.py.

Notes

  • The demo.ipynb notebook in the source repository has some additional examples, including gradient-based optimization using neighbors and iterative clustering with $k$-means.
  • The query_neighbors function is intended for small values of k and does not use a max heap for simplicity.
  • Some common k-d tree operations such as ball search are not implemented because they do not return a fixed size array. But there are probably others which could be implemented if there is a need. Suggestions welcome!
  • Only the Euclidean distance is currently supported, this relatively easy to change if needed.

About

Minimal JAX implementation of k-nearest neighbors using a k-d tree.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Jupyter Notebook 89.3%
  • Python 10.7%
0