Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
3-IVFPQ.cpp
1 /**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD+Patents license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9 #include <cstdio>
10 #include <cstdlib>
11
12 #include <faiss/IndexFlat.h>
13 #include <faiss/IndexIVFPQ.h>
14
15
16 int main() {
17 int d = 64; // dimension
18 int nb = 100000; // database size
19 int nq = 10000; // nb of queries
20
21 float *xb = new float[d * nb];
22 float *xq = new float[d * nq];
23
24 for(int i = 0; i < nb; i++) {
25 for(int j = 0; j < d; j++)
26 xb[d * i + j] = drand48();
27 xb[d * i] += i / 1000.;
28 }
29
30 for(int i = 0; i < nq; i++) {
31 for(int j = 0; j < d; j++)
32 xq[d * i + j] = drand48();
33 xq[d * i] += i / 1000.;
34 }
35
36
37 int nlist = 100;
38 int k = 4;
39 int m = 8; // bytes per vector
40 faiss::IndexFlatL2 quantizer(d); // the other index
41 faiss::IndexIVFPQ index(&quantizer, d, nlist, m, 8);
42 // here we specify METRIC_L2, by default it performs inner-product search
43 index.train(nb, xb);
44 index.add(nb, xb);
45
46 { // sanity check
47 long *I = new long[k * 5];
48 float *D = new float[k * 5];
49
50 index.search(5, xb, k, D, I);
51
52 printf("I=\n");
53 for(int i = 0; i < 5; i++) {
54 for(int j = 0; j < k; j++)
55 printf("%5ld ", I[i * k + j]);
56 printf("\n");
57 }
58
59 printf("D=\n");
60 for(int i = 0; i < 5; i++) {
61 for(int j = 0; j < k; j++)
62 printf("%7g ", D[i * k + j]);
63 printf("\n");
64 }
65
66 delete [] I;
67 delete [] D;
68 }
69
70 { // search xq
71 long *I = new long[k * nq];
72 float *D = new float[k * nq];
73
74 index.nprobe = 10;
75 index.search(nq, xq, k, D, I);
76
77 printf("I=\n");
78 for(int i = nq - 5; i < nq; i++) {
79 for(int j = 0; j < k; j++)
80 printf("%5ld ", I[i * k + j]);
81 printf("\n");
82 }
83
84 delete [] I;
85 delete [] D;
86 }
87
88
89
90 delete [] xb;
91 delete [] xq;
92
93 return 0;
94 }
faiss::IndexFlatL2
Definition: IndexFlat.h:79
faiss::IndexIVFPQ
Definition: IndexIVFPQ.h:29