Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
2-IVFFlat.cpp
1 /**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the CC-by-NC license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9 #include <cstdio>
10 #include <cstdlib>
11 #include <cassert>
12
13 #include <faiss/IndexFlat.h>
14 #include <faiss/IndexIVF.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
41 faiss::IndexFlatL2 quantizer(d); // the other index
42 faiss::IndexIVFFlat index(&quantizer, d, nlist, faiss::METRIC_L2);
43 // here we specify METRIC_L2, by default it performs inner-product search
44 assert(!index.is_trained);
45 index.train(nb, xb);
46 assert(index.is_trained);
47 index.add(nb, xb);
48
49 { // search xq
50 long *I = new long[k * nq];
51 float *D = new float[k * nq];
52
53 index.search(nq, xq, k, D, I);
54
55 printf("I=\n");
56 for(int i = nq - 5; i < nq; i++) {
57 for(int j = 0; j < k; j++)
58 printf("%5ld ", I[i * k + j]);
59 printf("\n");
60 }
61
62 index.nprobe = 10;
63 index.search(nq, xq, k, D, I);
64
65 printf("I=\n");
66 for(int i = nq - 5; i < nq; i++) {
67 for(int j = 0; j < k; j++)
68 printf("%5ld ", I[i * k + j]);
69 printf("\n");
70 }
71
72 delete [] I;
73 delete [] D;
74 }
75
76
77
78 delete [] xb;
79 delete [] xq;
80
81 return 0;
82 }
faiss::IndexFlatL2
Definition: IndexFlat.h:79
faiss::IndexIVFFlat
Definition: IndexIVF.h:129