View Source Scholar.Neighbors.NNDescent (Scholar v0.3.1)

Nearest Neighbors Descent (NND) is an algorithm that calculates Approximated Nearest Neighbors (ANN) for a given set of points[1].

It is implemented using Random Projection Forest[2].

References

[1] Efficient K-Nearest Neighbor Graph Construction for Generic Similarity Measures. [2] Randomized partition trees for nearest neighbor search

Summary

Functions

Calculates the approximate nearest neighbors for a given set of points. It returns a struct containing two tensors

Query the training data for the nearest neighbors of the query data.

Query the training data for the nearest neighbors of the query data.

Functions

Calculates the approximate nearest neighbors for a given set of points. It returns a struct containing two tensors:

  • nearest_neighbors - the indices of the nearest neighbors
  • distances - the distances to the nearest neighbors

Examples

iex> data = Nx.iota({10, 5})
iex> key = Nx.Random.key(12)
iex> Scholar.Neighbors.NNDescent.fit(data, num_neighbors: 3, key: key)
Link to this function

min_heap_insert(arg1, val, id)

View Source
Link to this function

query(nn, query_data, opts)

View Source

Query the training data for the nearest neighbors of the query data.

Use this function if you query for the first time on a training data. It will compute search graph that can be reused in the future queries.

Examples

iex> data = Nx.iota({10, 5})
iex> query_data = Nx.tensor([[1,7,32,6,2], [1,4,5,2,5], [1,3,67,12,4]])
iex> key = Nx.Random.key(12)
iex> nn = Scholar.Neighbors.NNDescent.fit(data, num_neighbors: 3)
iex> Scholar.Neighbors.NNDescent.query(nn, query_data, num_neighbors: 5, key: key)
Link to this function

query(nn, query_data, search_graph, opts)

View Source

Query the training data for the nearest neighbors of the query data.

Use this function if you have already queried training data. It will compute search graph that can be reused in the future queries.

Examples

iex> data = Nx.iota({10, 5})
iex> query_data = Nx.tensor([[1,7,32,6,2], [1,4,5,2,5], [1,3,67,12,4]])
iex> key = Nx.Random.key(12)
iex> nn = Scholar.Neighbors.NNDescent.fit(data, num_neighbors: 3)
iex> {_indices, _distances, search_graph} = Scholar.Neighbors.NNDescent.query(nn, query_data, num_neighbors: 5, key: key)
iex> query_data2 = Nx.tensor([[2,7,32,6,2], [1,4,6,2,5], [1,3,67,12,3]])
iex> Scholar.Neighbors.NNDescent.query(nn, query_data2, search_graph, num_neighbors: 5, key: key)
Link to this function

query_n(arg1, query_data, train_data, search_graph, forest, rng_key, opts)

View Source