Skip to content

Instantly share code, notes, and snippets.

@agtbaskara
Last active July 1, 2024 12:36
Show Gist options
  • Save agtbaskara/e60ac859e0c2d586c94e9acb12800932 to your computer and use it in GitHub Desktop.
Save agtbaskara/e60ac859e0c2d586c94e9acb12800932 to your computer and use it in GitHub Desktop.
FAISS Cosine similarity example
# 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))
@Barbery
Copy link

Barbery commented Jul 1, 2024

add import numpy as np

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment