Approximate Nearest Neighbor (ANN)
Last updated
Copyright Continuum Labs - 2023
Last updated
Approximate Nearest Neighbor (ANN) is a search algorithms used for extracting high-dimensional unstructured data such as images, text, and audio from vector databases.
These algorithms, by focusing on approximation rather than exact matches, strike a balance between speed and accuracy, enabling efficient similarity searches in vast datasets.
ANN search algorithms operate on the principle that in many applications, an exact nearest neighbor search is unnecessarily precise and computationally expensive.
The nearest neighbour problem involves finding the closest point in a set to a given query point in a metric space.
To enhance efficiency in terms of computation and memory usage, the concept of approximate nearest neighbour (ANN) is introduced, where the goal is to find a point in such that its distance to is within a factor (approximation factor) of the actual nearest neighbour's distance.
Early solutions to the nearest neighbour problem demonstrated a trade-off between the time it takes to answer a query (query time) and the amount of memory used (space).
The challenge has been to develop data structures that reduce both these factors effectively.
Data-independent Approaches: These approaches do not rely on the specific dataset but instead use methods like dimension reduction and randomized space partitions to pre-process the space, making the search for nearest neighbours more efficient.
Dimension Reduction: One key strategy for ANN involves reducing the dimensionality of the data before applying nearest neighbour algorithms. This can significantly decrease space and time complexity, as seen in the Johnson-Lindenstrauss lemma, which facilitates this reduction while preserving distance relationships to a certain degree.
Locality-Sensitive Hashing (LSH): LSH is a method in constructing efficient ANN data structures, characterised by hashing that ensures closer points have a higher probability of colliding in the hashed space. This method balances the space and query time more favourably and is adaptable to various metric spaces.
Here are some key equations representing how Approximate Nearest Neighbour (ANN) works:
For a set of points in a metric space , and a query point , the nearest neighbour is defined as:
The goal in ANN is to find a point such that: where is the approximation factor.
These equations encapsulate the core mathematical concepts underpinning the ANN search methodology, illustrating the trade-offs between accuracy and computational efficiency inherent in the problem.
By accepting a degree of approximation, ANN searches significantly reduce the time and resources required to find similar items within large datasets. This trade-off is particularly valuable in real-world applications where speed and efficiency are paramount, and an exact match is not strictly necessary.
Locality-Sensitive Hashing (LSH) is a technique used to solve the Approximate Nearest Neighbour (ANN) search problem efficiently, especially in high-dimensional spaces.
Hashing Families: LSH uses a family of hash functions designed so that closer points in the space are more likely to collide (have the same hash value) than points that are far apart.
This family is defined by four parameters
where:
If the distance then
If the distance , then
Here, is the approximation factor, and is a scale parameter.
Quality Parameter : The effectiveness of an LSH family is often measured by the parameter , where . The smaller is, the more effective the LSH family is at distinguishing between close and distant neighbours.
Below is a great article on LSH:
Data Structure Construction: LSH-based data structures for ANN are built by using multiple hash tables. Each table uses a hash function (or a composite hash function) drawn from the LSH family.
Query Processing: To find the approximate nearest neighbours of a query point , LSH computes the hash values of using the same hash functions and looks up points in the corresponding buckets across the hash tables. The points in these buckets are then considered as candidate nearest neighbours.
Enhancing Accuracy: By using multiple hash tables (each with potentially a composite hash function created by concatenating multiple LSH functions), the probability of missing the true nearest neighbour is reduced. Essentially, even if a neighbor is missed in one table, it might be caught in another.
Limitations: The effectiveness of LSH is inherently tied to the dimensionality and intrinsic structure of the data. While LSH offers a powerful framework for ANN, its performance can degrade in ultra-high-dimensional spaces or when the data does not align well with the chosen LSH family.
LSH provides a scalable and efficient framework for performing approximate nearest neighbour searches in high-dimensional spaces, balancing the trade-offs between space, accuracy, and computational time.
ANN algorithms can be categorized based on their underlying mechanisms and structures. Each category has its unique advantages and is suited to particular types of data or applications.
Tree-based Methods
Tree-based algorithms, such as k-d trees, ball trees, and ANNOY, use tree data structures to partition the search space, facilitating efficient navigation and search. While these methods are effective for lower-dimensional data, they tend to struggle with the "curse of dimensionality," making them less suitable for very high-dimensional spaces.
Quantization-based Methods
Quantization involves compressing the search space to reduce both storage and computational demands.
Techniques like IVF_FLAT, IVF_SQ8, and IVF_PQ work by first dividing the vector space into clusters using a predefined number of centroids.
During the encoding process, each vector is assigned to its nearest centroid, and the vectors within each cluster are encoded using different quantization strategies.
IVF_FLAT stores vectors directly, IVF_SQ8 applies scalar quantization to reduce dimension size, and IVF_PQ uses product quantization to split vectors into subspaces.
This clustering and encoding reduce both storage requirements and search complexity, enabling efficient retrieval in high-dimensional spaces.
Graph-based Methods
Graph-based algorithms represent the search space as a graph, with nodes corresponding to vectors and edges linking similar vectors.
Techniques like HNSW and NGT leverage graph traversal to efficiently locate nearest neighbours. These methods are known for their rapid search capabilities and resilience against the curse of dimensionality.
Deep Learning-based Methods
Deep learning approaches, including autoencoders and specialized neural networks, learn compact vector representations for efficient similarity searches. While offering high accuracy, these methods require significant computational resources and training time.
The distinction between vector databases and ANN libraries lies in their scope and functionality.
Vector databases provide a comprehensive solution for managing unstructured data, featuring capabilities like cloud-nativity, multi-tenancy, and scalability.
These platforms offer a robust environment for data ingestion, indexing, querying, and persistence, alongside advanced functionalities such as data sharding and hybrid search.
Conversely, ANN libraries like FAISS, ScaNN, and HNSW focus on constructing efficient vector indices to expedite the nearest neighbour search.
These libraries are optimised for performance, offering fast search capabilities with minimal resource usage. While sufficient for smaller, less complex datasets, ANN libraries may not adequately address the scaling challenges posed by larger datasets and multi-user environments.
The debate between the comprehensive features of vector databases and the focused efficiency of ANN libraries underscores the diverse needs of modern data applications.
While vector databases excel in providing a full-fledged environment for managing and querying unstructured data at scale, ANN libraries offer streamlined, efficient solutions for specific nearest neighbour search tasks.
The choice between the two depends on the specific requirements of the application, including dataset size, complexity, and operational scalability.