Beispiel #1
0
 def run_simple_dis_test(self, ref_func, metric_type):
     xq, yb = self.make_example()
     ref_dis = np.array([
         [ref_func(x, y) for y in yb]
         for x in xq
     ])
     new_dis = faiss.pairwise_distances(xq, yb, metric_type)
     self.assertTrue(np.allclose(ref_dis, new_dis))
Beispiel #2
0
 def test_Lp(self):
     p = 1.5
     xq, yb = self.make_example()
     ref_dis = np.array(
         [[scipy.spatial.distance.minkowski(x, y, p) for y in yb]
          for x in xq])
     new_dis = faiss.pairwise_distances(xq, yb, faiss.METRIC_Lp, p)
     new_dis = new_dis**(1 / p)  # post processing
     self.assertTrue(np.allclose(ref_dis, new_dis))
Beispiel #3
0
    def test_L2(self):
        xq, yb = self.make_example()
        ref_dis = np.array(
            [[scipy.spatial.distance.sqeuclidean(x, y) for y in yb]
             for x in xq])
        new_dis = faiss.pairwise_distances(xq, yb, faiss.METRIC_L2)
        self.assertTrue(np.allclose(ref_dis, new_dis))

        ref_dis = np.array(
            [[scipy.spatial.distance.euclidean(x, y) for y in yb] for x in xq])
        new_dis = np.sqrt(new_dis)  # post processing
        self.assertTrue(np.allclose(ref_dis, new_dis))
Beispiel #4
0
    def do_test_knn(self, mt):
        d = 10
        nb = 100
        nq = 50
        nt = 0
        xt, xb, xq = get_dataset_2(d, nt, nb, nq)

        index = faiss.IndexFlat(d, mt)
        index.add(xb)

        D, I = index.search(xq, 10)

        dis = faiss.pairwise_distances(xq, xb, mt)
        o = dis.argsort(axis=1)
        assert np.all(I == o[:, :10])

        for q in range(nq):
            assert np.all(D[q] == dis[q, I[q]])
Beispiel #5
0
    def test_hnsw(self):

        d = 10
        nb = 1000
        nq = 100
        nt = 0
        xt, xb, xq = get_dataset_2(d, nt, nb, nq)

        mt = faiss.METRIC_L1

        index = faiss.IndexHNSW(faiss.IndexFlat(d, mt))
        index.add(xb)

        D, I = index.search(xq, 10)

        dis = faiss.pairwise_distances(xq, xb, mt)

        for q in range(nq):
            assert np.all(D[q] == dis[q, I[q]])
"""small test script to benchmark the SIMD implementation of the
distance computations for the additional metrics. Call eg. with L1 to
get L1 distance computations.
"""

import faiss

import sys
import time

d = 64
nq = 4096
nb = 16384

print("sample")

xq = faiss.randn((nq, d), 123)
xb = faiss.randn((nb, d), 123)

mt_name = "L2" if len(sys.argv) < 2 else sys.argv[1]

mt = getattr(faiss, "METRIC_" + mt_name)

print("distances")
t0 = time.time()
dis = faiss.pairwise_distances(xq, xb, mt)
t1 = time.time()

print("nq=%d nb=%d d=%d %s: %.3f s" % (nq, nb, d, mt_name, t1 - t0))