Last active
July 1, 2024 12:36
-
-
Save agtbaskara/e60ac859e0c2d586c94e9acb12800932 to your computer and use it in GitHub Desktop.
FAISS Cosine similarity example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Just adding example if noob like me came here to find how to calculate the Cosine similarity from scratch | |
# Source https://github.com/facebookresearch/faiss/issues/95 | |
import numpy as np | |
import faiss | |
dataSetI = [.1, .2, .3] | |
dataSetII = [.4, .5, .6] | |
#dataSetII = [.1, .2, .3] | |
x = np.array([dataSetI]).astype(np.float32) | |
q = np.array([dataSetII]).astype(np.float32) | |
index = faiss.index_factory(3, "Flat", faiss.METRIC_INNER_PRODUCT) | |
index.ntotal | |
faiss.normalize_L2(x) | |
index.add(x) | |
faiss.normalize_L2(q) | |
distance, index = index.search(q, 5) | |
print('Distance by FAISS:{}'.format(distance)) | |
# To Tally the results check the cosine similarity of the following example | |
from scipy import spatial | |
result = 1 - spatial.distance.cosine(dataSetI, dataSetII) | |
print('Distance by FAISS:{}'.format(result)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
add
import numpy as np