Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/data/users/matthijs/github_faiss/faiss/Clustering.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 /* Copyright 2004-present Facebook. All Rights Reserved.
11 kmeans clustering routines
12 */
13
14 #include "Clustering.h"
15
16
17
18 #include <cmath>
19 #include <cstdio>
20 #include <cstring>
21
22 #include "utils.h"
23 #include "FaissAssert.h"
24 #include "IndexFlat.h"
25
26 namespace faiss {
27
28 ClusteringParameters::ClusteringParameters ():
29 niter(25),
30 nredo(1),
31 verbose(false), spherical(false),
32 update_index(false),
33 min_points_per_centroid(39),
34 max_points_per_centroid(256),
35 seed(1234)
36 {}
37 // 39 corresponds to 10000 / 256 -> to avoid warnings on PQ tests with randu10k
38
39
40 Clustering::Clustering (int d, int k):
41 d(d), k(k) {}
42
43 Clustering::Clustering (int d, int k, const ClusteringParameters &cp):
44 ClusteringParameters (cp), d(d), k(k) {}
45
46
47
48 static double imbalance_factor (int n, int k, long *assign) {
49 std::vector<int> hist(k, 0);
50 for (int i = 0; i < n; i++)
51 hist[assign[i]]++;
52
53 double tot = 0, uf = 0;
54
55 for (int i = 0 ; i < k ; i++) {
56 tot += hist[i];
57 uf += hist[i] * (double) hist[i];
58 }
59 uf = uf * k / (tot * tot);
60
61 return uf;
62 }
63
64
65
66
67 void Clustering::train (idx_t nx, const float *x_in, Index & index) {
68 FAISS_ASSERT (nx >= k ||
69 !"need at least as many training points as clusters");
70
71 double t0 = getmillisecs();
72
73 // yes it is the user's responsibility, but it may spare us some
74 // hard-to-debug reports.
75 for (size_t i = 0; i < nx * d; i++) {
76 FAISS_ASSERT (finite (x_in[i]) ||
77 !"input contains NaN's or Inf's");
78 }
79
80 const float *x = x_in;
81
82 if (nx > k * max_points_per_centroid) {
83 if (verbose)
84 printf("Sampling a subset of %ld / %ld for training\n",
85 k * max_points_per_centroid, nx);
86 int *perm = new int[nx];
87 rand_perm (perm, nx, seed);
88 nx = k * max_points_per_centroid;
89 float * x_new = new float [nx * d];
90 for (idx_t i = 0; i < nx; i++)
91 memcpy (x_new + i * d, x + perm[i] * d, sizeof(x_new[0]) * d);
92 delete [] perm;
93 x = x_new;
94 } else if (nx < k * min_points_per_centroid) {
95 fprintf (stderr,
96 "WARNING clustering %ld points to %ld centroids: "
97 "please provide at least %ld training points\n",
98 nx, k, idx_t(k) * min_points_per_centroid);
99 }
100
101 if (verbose)
102 printf("Clustering %d points in %ldD to %ld clusters, "
103 "redo %d times, %d iterations\n",
104 int(nx), d, k, nredo, niter);
105
106
107 idx_t * assign = new idx_t[nx];
108 float * dis = new float[nx];
109
110 float best_err = 1e50;
111 double t_search_tot = 0;
112 if (verbose) {
113 printf(" Preprocessing in %.2f s\n",
114 (getmillisecs() - t0)/1000.);
115 }
116 t0 = getmillisecs();
117
118 for (int redo = 0; redo < nredo; redo++) {
119
120 std::vector<float> buf_centroids;
121
122 std::vector<float> &cur_centroids =
123 nredo == 1 ? centroids : buf_centroids;
124
125 if (verbose && nredo > 1) {
126 printf("Outer iteration %d / %d\n", redo, nredo);
127 }
128
129 if (cur_centroids.size() == 0) {
130 // initialize centroids with random points from the dataset
131 cur_centroids.resize (d * k);
132 int *perm = new int[nx];
133 rand_perm (perm, nx, seed + 1 + redo * 15486557L);
134 #pragma omp parallel for
135 for (int i = 0; i < k ; i++)
136 memcpy (&cur_centroids[i * d], x + perm[i] * d,
137 d * sizeof (float));
138 delete [] perm;
139 } else { // assume user provides some meaningful initialization
140 FAISS_ASSERT (cur_centroids.size() == d * k);
141 FAISS_ASSERT (nredo == 1 ||
142 !"will redo with same initialization");
143 }
144
145 if (spherical)
146 fvec_renorm_L2 (d, k, cur_centroids.data());
147
148 if (!index.is_trained)
149 index.train (k, cur_centroids.data());
150
151 FAISS_ASSERT (index.ntotal == 0);
152 index.add (k, cur_centroids.data());
153 float err = 0;
154 for (int i = 0; i < niter; i++) {
155 double t0s = getmillisecs();
156 index.search (nx, x, 1, dis, assign);
157 t_search_tot += getmillisecs() - t0s;
158
159 err = 0;
160 for (int j = 0; j < nx; j++)
161 err += dis[j];
162 obj.push_back (err);
163
164 int nsplit = km_update_centroids (x, cur_centroids.data(),
165 assign, d, k, nx);
166
167 if (verbose) {
168 printf (" Iteration %d (%.2f s, search %.2f s): "
169 "objective=%g imbalance=%.3f nsplit=%d \r",
170 i, (getmillisecs() - t0) / 1000.0,
171 t_search_tot / 1000,
172 err, imbalance_factor (nx, k, assign),
173 nsplit);
174 fflush (stdout);
175 }
176
177 if (spherical)
178 fvec_renorm_L2 (d, k, cur_centroids.data());
179
180 index.reset ();
181 if (update_index)
182 index.train (k, cur_centroids.data());
183
184 assert (index.ntotal == 0);
185 index.add (k, cur_centroids.data());
186 }
187 if (verbose) printf("\n");
188 if (nredo > 1) {
189 if (err < best_err) {
190 if (verbose)
191 printf ("Objective improved: keep new clusters\n");
192 centroids = buf_centroids;
193 best_err = err;
194 }
195 index.reset ();
196 }
197 }
198
199 delete [] assign;
200 delete [] dis;
201 if (x_in != x) delete [] x;
202 }
203
204 float kmeans_clustering (size_t d, size_t n, size_t k,
205 const float *x,
206 float *centroids)
207 {
208 Clustering clus (d, k);
209 clus.verbose = d * n * k > (1L << 30);
210 // display logs if > 1Gflop per iteration
211 IndexFlatL2 index (d);
212 clus.train (n, x, index);
213 memcpy(centroids, clus.centroids.data(), sizeof(*centroids) * d * k);
214 return clus.obj.back();
215 }
216
217 } // namespace faiss
faiss::ClusteringParameters::niter
int niter
clustering iterations
Definition: Clustering.h:26
faiss::km_update_centroids
int km_update_centroids(const float *x, float *centroids, long *assign, size_t d, size_t k, size_t n)
Definition: utils.cpp:1286
faiss::ClusteringParameters::nredo
int nredo
redo clustering this many times and keep best
Definition: Clustering.h:27
faiss::ClusteringParameters::ClusteringParameters
ClusteringParameters()
sets reasonable defaults
Definition: Clustering.cpp:28
faiss::Index::reset
virtual void reset()=0
removes all elements from the database.
faiss::IndexFlatL2
Definition: IndexFlat.h:81
faiss::Clustering::Clustering
Clustering(int d, int k)
the only mandatory parameters are k and d
Definition: Clustering.cpp:40
faiss::Clustering::k
size_t k
nb of centroids
Definition: Clustering.h:60
faiss::ClusteringParameters::seed
int seed
seed for the random number generator
Definition: Clustering.h:36
faiss::ClusteringParameters::min_points_per_centroid
int min_points_per_centroid
otherwise you get a warning
Definition: Clustering.h:33
faiss::Clustering
Definition: Clustering.h:57
faiss::Index::add
virtual void add(idx_t n, const float *x)=0
faiss::Clustering::obj
std::vector< float > obj
Definition: Clustering.h:67
faiss::kmeans_clustering
float kmeans_clustering(size_t d, size_t n, size_t k, const float *x, float *centroids)
Definition: Clustering.cpp:204
faiss::Index::ntotal
idx_t ntotal
total nb of indexed vectors
Definition: Index.h:67
faiss::getmillisecs
double getmillisecs()
ms elapsed since some arbitrary epoch
Definition: utils.cpp:71
faiss::Clustering::centroids
std::vector< float > centroids
centroids (k * d)
Definition: Clustering.h:63
faiss::Clustering::d
size_t d
dimension of the vectors
Definition: Clustering.h:59
faiss::Index::search
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const =0
faiss::ClusteringParameters::update_index
bool update_index
update index after each iteration?
Definition: Clustering.h:31
faiss::ClusteringParameters
Definition: Clustering.h:25
faiss::Index
Definition: Index.h:61
faiss::Clustering::train
virtual void train(idx_t n, const float *x, faiss::Index &index)
Index is used during the assignment stage.
Definition: Clustering.cpp:67
faiss::Index::is_trained
bool is_trained
set if the Index does not require training, or if training is done already
Definition: Index.h:71
faiss::Index::train
virtual void train(idx_t n, const float *x)
Definition: Index.h:92
faiss::ClusteringParameters::spherical
bool spherical
do we want normalized centroids?
Definition: Clustering.h:30
faiss::ClusteringParameters::max_points_per_centroid
int max_points_per_centroid
to limit size of dataset
Definition: Clustering.h:34