Faiss supports different metrics for measuring embedding similarity:
- Euclidean Distance (
METRIC_L2): Calculates the square root of the sum of squared differences across all dimensions. This is standard for most spatial distance calculations. - Inner Product (
METRIC_INNER_PRODUCT): Often used as Cosine Similarity when vectors are L2-normalized. This is common in models like word2vec or ArcFace.
To perform L2 normalization on a vector X using numpy (to prepare for Inner Product/Cosine Similarity), use the following pattern to avoid division by zero:
# X_normed = X / max(eps, ||X||_2)
X_normed = X / np.maximum(eps, np.linalg.norm(X, ord=2, axis=-1, keepdims=True))
X_normed = X / np.maximum(eps, np.linalg.norm(X, ord=2, axis=-1, keepdims=True))