Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b1a72d2

Browse files
Create 3. FAISS Advanced explaination.md
1 parent 9cdd6c9 commit b1a72d2

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
To expand on the advanced explanation of Facebook AI Similarity Search (FAISS) and incorporate mathematical expressions, we will delve into the underlying mechanisms and algorithms used in FAISS, using the example query vector "I like to play football".
2+
3+
## Advanced Explanation of FAISS
4+
5+
FAISS is designed for efficient similarity search and clustering of dense vectors, typically in high-dimensional spaces. The core idea is to index a large dataset of vectors so that we can quickly retrieve the most similar vectors to a given query vector.
6+
7+
### Key Components of FAISS
8+
9+
1. **Vector Representation**:
10+
Each sentence or item is represented as a vector in a high-dimensional space. For example, the sentence "I like to play football" might be encoded into a vector $$\mathbf{q}$$ of dimension $$d$$ (e.g., $$d = 768$$ for sentence embeddings).
11+
12+
2. **Distance Metrics**:
13+
FAISS supports various distance metrics for measuring similarity between vectors, including:
14+
15+
- **L2 (Euclidean) Distance**:
16+
$$
17+
D(\mathbf{x}, \mathbf{y}) = \sqrt{\sum_{i=1}^{d} (x_i - y_i)^2}
18+
$$
19+
- **Inner Product** (used for cosine similarity when vectors are normalized):
20+
$$
21+
D(\mathbf{x}, \mathbf{y}) = \sum_{i=1}^{d} x_i \cdot y_i
22+
$$
23+
24+
3. **Index Structures**:
25+
FAISS employs several indexing strategies to optimize search performance:
26+
27+
- **Flat Index**: This is the simplest form, where all vectors are stored, and the search is performed using brute force. For a query vector $$\mathbf{q}$$, the search involves calculating the distance to every vector in the index.
28+
29+
- **Inverted File Index (IVF)**: This partitions the vector space into clusters. Each cluster is represented by a centroid, and vectors are assigned to these clusters. The search process involves:
30+
1. **Cluster Assignment**: For a query vector $$\mathbf{q}$$, find the nearest centroids using a coarse quantizer (e.g., using L2 distance).
31+
2. **Refined Search**: Only search within the nearest clusters.
32+
33+
- **Product Quantization (PQ)**: This technique compresses the vector representation to save memory. It divides each vector into $$M$$ subvectors and quantizes each subvector separately. The distance computation for a query vector $$\mathbf{q}$$ involves:
34+
$$
35+
D(\mathbf{q}, \mathbf{c}) \approx \sum_{m=1}^{M} D(\mathbf{q}_m, \mathbf{c}_m)
36+
$$
37+
where $$\mathbf{c}_m$$ is the quantized representation of the $$m^{th}$$ subvector.
38+
39+
- **Hierarchical Navigable Small World (HNSW)**: This is a graph-based approach that allows for fast nearest neighbor searches. It constructs a multi-layer graph where each layer contains a subset of the vectors, enabling efficient traversal to find nearest neighbors.
40+
41+
### Example Search Process
42+
43+
1. **Index Creation**:
44+
Suppose we have a dataset of vectors representing various sentences, including our example. We would first create an index:
45+
```python
46+
import faiss
47+
d = 768 # Example dimension
48+
index = faiss.IndexIVFPQ(faiss.IndexFlatL2(d), d, nlist=100, M=16, nbits=8)
49+
index.train(training_vectors) # Train the index with a subset of vectors
50+
index.add(vectors) # Add all vectors to the index
51+
```
52+
53+
2. **Query Vector**:
54+
For the query "I like to play football", we encode it into a vector $$\mathbf{q}$$:
55+
```python
56+
xq = model.encode(["I like to play football"])
57+
```
58+
59+
3. **Search Execution**:
60+
To find the 4 nearest neighbors, we execute:
61+
```python
62+
k = 4
63+
D, I = index.search(xq, k) # D contains distances, I contains indices of neighbors
64+
```
65+
66+
4. **Distance Calculation**:
67+
The distances $$D$$ are computed using the selected metric (e.g., L2 distance or inner product) based on the indexed vectors.
68+
69+
5. **Results Interpretation**:
70+
The indices returned in $$I$$ correspond to the most similar sentences in the dataset. For instance, if the results are:
71+
```
72+
I = [4586, 10252, 12465, 190]
73+
```
74+
These indices can be used to retrieve the actual sentences that are most similar to the query vector.
75+
76+
### Performance Optimization
77+
78+
FAISS is optimized for performance through various means:
79+
80+
- **GPU Acceleration**: FAISS can leverage GPU resources to handle large-scale datasets, significantly speeding up both indexing and searching processes.
81+
82+
- **Parameter Tuning**: Users can adjust parameters such as the number of clusters ($$nlist$$) and the number of probes ($$nprobe$$) to balance between speed and accuracy. For example, increasing $$nprobe$$ allows for more clusters to be searched, potentially improving accuracy at the cost of speed.
83+
84+
- **Memory Management**: By using quantization techniques like PQ, FAISS reduces memory usage, allowing it to handle larger datasets efficiently.
85+
86+
By combining these techniques, FAISS achieves state-of-the-art performance in similarity search, making it suitable for applications such as semantic search, recommendation systems, and more.
87+
88+
Citations:
89+
[1] https://github.com/facebookresearch/faiss/wiki/Faiss-indexes
90+
[2] https://unfoldai.com/effortless-large-scale-image-retrieval-with-faiss-a-hands-on-tutorial/
91+
[3] https://www.youtube.com/watch?v=sKyvsdEv6rk
92+
[4] https://www.pinecone.io/learn/series/faiss/faiss-tutorial/
93+
[5] https://ai.meta.com/tools/faiss/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /