Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

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