Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/data/users/matthijs/github_faiss/faiss/VectorTransform.h
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 // Copyright 2004-present Facebook. All Rights Reserved.
10 // -*- c++ -*-
11
12 #ifndef FAISS_VECTOR_TRANSFORM_H
13 #define FAISS_VECTOR_TRANSFORM_H
14
15 /** Defines a few objects that apply transformations to a set of
16 * vectors Often these are pre-processing steps.
17 */
18
19 #include <vector>
20
21 #include "Index.h"
22
23
24 namespace faiss {
25
26
27 /** Any transformation applied on a set of vectors */
28 struct VectorTransform {
29
30 typedef Index::idx_t idx_t;
31
32 int d_in; ///! input dimension
33 int d_out; ///! output dimension
34
35 explicit VectorTransform (int d_in = 0, int d_out = 0):
36 d_in(d_in), d_out(d_out), is_trained(true)
37 {}
38
39
40 /// set if the LinearTransform does not require training, or if
41 /// training is done already
42 bool is_trained;
43
44
45 /** Perform training on a representative set of vectors. Does
46 * nothing by default.
47 *
48 * @param n nb of training vectors
49 * @param x training vecors, size n * d
50 */
51 virtual void train (idx_t n, const float *x);
52
53 /** apply the random roation, return new allocated matrix
54 * @param x size n * d_in
55 * @return size n * d_out
56 */
57 float *apply (idx_t n, const float * x) const;
58
59 /// same as apply, but result is pre-allocated
60 virtual void apply_noalloc (idx_t n, const float * x,
61 float *xt) const = 0;
62
63 /// reverse transformation. May not be implemented or may return
64 /// approximate result
65 virtual void reverse_transform (idx_t n, const float * xt,
66 float *x) const;
67
68 virtual ~VectorTransform () {}
69
70 };
71
72
73
74 /** Generic linear transformation, with bias term applied on output
75 * y = A * x + b
76 */
77 struct LinearTransform: VectorTransform {
78
79
80 bool have_bias; ///! whether to use the bias term
81
82 /// Transformation matrix, size d_out * d_in
83 std::vector<float> A;
84
85 /// bias vector, size d_out
86 std::vector<float> b;
87
88
89 /// both d_in > d_out and d_out < d_in are supported
90 explicit LinearTransform (int d_in = 0, int d_out = 0,
91 bool have_bias = false);
92
93 /// same as apply, but result is pre-allocated
94 void apply_noalloc(idx_t n, const float* x, float* xt) const override;
95
96 /// compute x = A^T * (x - b)
97 /// is reverse transform if A has orthonormal lines
98 void transform_transpose (idx_t n, const float * y,
99 float *x) const;
100
101 bool verbose;
102
103 ~LinearTransform() override {}
104 };
105
106
107
108 /// Randomly rotate a set of vectors
109 struct RandomRotationMatrix: LinearTransform {
110
111 /// both d_in > d_out and d_out < d_in are supported
112 RandomRotationMatrix (int d_in, int d_out):
113 LinearTransform(d_in, d_out, false) {}
114
115 /// must be called before the transform is used
116 void init(int seed);
117
118 void reverse_transform(idx_t n, const float* xt, float* x) const override;
119
120 RandomRotationMatrix () {}
121 };
122
123
124 /** Applies a principal component analysis on a set of vectors,
125 * with optionally whitening and random rotation. */
126 struct PCAMatrix: LinearTransform {
127
128 /** after transformation the components are multiplied by
129 * eigenvalues^eigen_power
130 *
131 * =0: no whitening
132 * =-2: full whitening
133 */
134 float eigen_power;
135
136 /// random rotation after PCA
137 bool random_rotation;
138
139 /// ratio between # training vectors and dimension
140 size_t max_points_per_d;
141
142 /// try to distribute output eigenvectors in this many bins
143 int balanced_bins;
144
145 /// Mean, size d_in
146 std::vector<float> mean;
147
148 /// eigenvalues of covariance matrix (= squared singular values)
149 std::vector<float> eigenvalues;
150
151 /// PCA matrix, size d_in * d_in
152 std::vector<float> PCAMat;
153
154 // the final matrix is computed after random rotation and/or whitening
155 explicit PCAMatrix (int d_in = 0, int d_out = 0,
156 float eigen_power = 0, bool random_rotation = false);
157
158 /// train on n vectors. If n < d_in then the eigenvector matrix
159 /// will be completed with 0s
160 void train(Index::idx_t n, const float* x) override;
161
162 void reverse_transform(idx_t n, const float* xt, float* x) const override;
163
164 /// copy pre-trained PCA matrix
165 void copy_from (const PCAMatrix & other);
166
167 /// called after mean, PCAMat and eigenvalues are computed
168 void prepare_Ab();
169
170 };
171
172
173
174 /** Applies a rotation to align the dimensions with a PQ to minimize
175 * the reconstruction error. Can be used before an IndexPQ or an
176 * IndexIVFPQ. The method is the non-parametric version described in:
177 *
178 * "Optimized Product Quantization for Approximate Nearest Neighbor Search"
179 * Tiezheng Ge, Kaiming He, Qifa Ke, Jian Sun, CVPR'13
180 *
181 */
182 struct OPQMatrix: LinearTransform {
183
184 int M; ///< nb of subquantizers
185 int niter; ///< Number of outer training iterations
186 int niter_pq; ///< Number of training iterations for the PQ
187 int niter_pq_0; ///< same, for the first outer iteration
188
189 /// if there are too many training points, resample
190 size_t max_train_points;
191 bool verbose;
192
193 /// if d2 != -1, output vectors of this dimension
194 explicit OPQMatrix (int d = 0, int M = 1, int d2 = -1);
195
196 void train(Index::idx_t n, const float* x) override;
197
198 void reverse_transform(idx_t n, const float* xt, float* x) const override;
199 };
200
201
202 /** remap dimensions for intput vectors, possibly inserting 0s
203 * strictly speaking this is also a linear transform but we don't want
204 * to compute it with matrix multiplies */
205 struct RemapDimensionsTransform: VectorTransform {
206
207
208 /// map from output dimension to input, size d_out
209 /// -1 -> set output to 0
210 std::vector<int> map;
211
212 RemapDimensionsTransform (int d_in, int d_out, const int *map);
213
214 /// remap input to output, skipping or inserting dimensions as needed
215 /// if uniform: distribute dimensions uniformly
216 /// otherwise just take the d_out first ones.
217 RemapDimensionsTransform (int d_in, int d_out, bool uniform = true);
218
219 void apply_noalloc(idx_t n, const float* x, float* xt) const override;
220
221 /// reverse transform correct only when the mapping is a permuation
222 void reverse_transform(idx_t n, const float* xt, float* x) const override;
223
224 RemapDimensionsTransform () {}
225 };
226
227
228 /** Index that applies a LinearTransform transform on vectors before
229 * handing them over to a sub-index */
230 struct IndexPreTransform: Index {
231
232 std::vector<VectorTransform *> chain; ///! chain of tranforms
233 Index * index; ///! the sub-index
234
235 bool own_fields; ///! whether pointers are deleted in destructor
236
237 explicit IndexPreTransform (Index *index);
238
239 IndexPreTransform ();
240
241 /// ltrans is the last transform before the index
242 IndexPreTransform (VectorTransform * ltrans, Index * index);
243
244 void prepend_transform (VectorTransform * ltrans);
245
246 void train(idx_t n, const float* x) override;
247
248 void add(idx_t n, const float* x) override;
249
250 void add_with_ids(idx_t n, const float* x, const long* xids) override;
251
252 void reset() override;
253
254 /** removes IDs from the index. Not supported by all indexes.
255 */
256 long remove_ids(const IDSelector& sel) override;
257
258 void search(
259 idx_t n,
260 const float* x,
261 idx_t k,
262 float* distances,
263 idx_t* labels) const override;
264
265 void reconstruct_n (idx_t i0, idx_t ni, float *recons)
266 const override;
267
268 /// apply the transforms in the chain. The returned float * may be
269 /// equal to x, otherwise it should be deallocated.
270 const float * apply_chain (idx_t n, const float *x) const;
271
272 ~IndexPreTransform() override;
273 };
274
275
276
277 } // namespace faiss
278
279
280
281 #endif
faiss::LinearTransform::transform_transpose
void transform_transpose(idx_t n, const float *y, float *x) const
Definition: VectorTransform.cpp:134
faiss::IndexPreTransform::index
Index * index
! chain of tranforms
Definition: VectorTransform.h:233
faiss::RandomRotationMatrix
Randomly rotate a set of vectors.
Definition: VectorTransform.h:109
faiss::OPQMatrix::niter
int niter
Number of outer training iterations.
Definition: VectorTransform.h:185
faiss::RandomRotationMatrix::RandomRotationMatrix
RandomRotationMatrix(int d_in, int d_out)
both d_in &gt; d_out and d_out &lt; d_in are supported
Definition: VectorTransform.h:112
faiss::RandomRotationMatrix::init
void init(int seed)
must be called before the transform is used
Definition: VectorTransform.cpp:164
faiss::IndexPreTransform::reset
void reset() override
removes all elements from the database.
Definition: VectorTransform.cpp:842
faiss::OPQMatrix::niter_pq
int niter_pq
Number of training iterations for the PQ.
Definition: VectorTransform.h:186
faiss::LinearTransform::A
std::vector< float > A
! whether to use the bias term
Definition: VectorTransform.h:83
faiss::PCAMatrix::eigen_power
float eigen_power
Definition: VectorTransform.h:134
faiss::IDSelector
Definition: AuxIndexStructures.h:57
faiss::LinearTransform::LinearTransform
LinearTransform(int d_in=0, int d_out=0, bool have_bias=false)
both d_in &gt; d_out and d_out &lt; d_in are supported
Definition: VectorTransform.cpp:99
faiss::PCAMatrix
Definition: VectorTransform.h:126
faiss::VectorTransform::VectorTransform
VectorTransform(int d_in=0, int d_out=0)
! output dimension
Definition: VectorTransform.h:35
faiss::OPQMatrix
Definition: VectorTransform.h:182
faiss::PCAMatrix::train
void train(Index::idx_t n, const float *x) override
Definition: VectorTransform.cpp:271
faiss::IndexPreTransform
Definition: VectorTransform.h:230
faiss::PCAMatrix::mean
std::vector< float > mean
Mean, size d_in.
Definition: VectorTransform.h:146
faiss::RemapDimensionsTransform::map
std::vector< int > map
Definition: VectorTransform.h:210
faiss::IndexPreTransform::apply_chain
const float * apply_chain(idx_t n, const float *x) const
Definition: VectorTransform.cpp:795
faiss::PCAMatrix::PCAMat
std::vector< float > PCAMat
PCA matrix, size d_in * d_in.
Definition: VectorTransform.h:152
faiss::IndexPreTransform::train
void train(idx_t n, const float *x) override
Definition: VectorTransform.cpp:766
faiss::LinearTransform::b
std::vector< float > b
bias vector, size d_out
Definition: VectorTransform.h:86
faiss::LinearTransform
Definition: VectorTransform.h:77
faiss::OPQMatrix::train
void train(Index::idx_t n, const float *x) override
Definition: VectorTransform.cpp:547
faiss::PCAMatrix::balanced_bins
int balanced_bins
try to distribute output eigenvectors in this many bins
Definition: VectorTransform.h:143
faiss::VectorTransform::is_trained
bool is_trained
Definition: VectorTransform.h:42
faiss::Index::idx_t
long idx_t
all indices are this type
Definition: Index.h:62
faiss::IndexPreTransform::reconstruct_n
void reconstruct_n(idx_t i0, idx_t ni, float *recons) const override
Definition: VectorTransform.cpp:854
faiss::VectorTransform
Definition: VectorTransform.h:28
faiss::IndexPreTransform::own_fields
bool own_fields
! the sub-index
Definition: VectorTransform.h:235
faiss::OPQMatrix::niter_pq_0
int niter_pq_0
same, for the first outer iteration
Definition: VectorTransform.h:187
faiss::OPQMatrix::reverse_transform
void reverse_transform(idx_t n, const float *xt, float *x) const override
Definition: VectorTransform.cpp:708
faiss::VectorTransform::train
virtual void train(idx_t n, const float *x)
Definition: VectorTransform.cpp:80
faiss::RandomRotationMatrix::reverse_transform
void reverse_transform(idx_t n, const float *xt, float *x) const override
Definition: VectorTransform.cpp:189
faiss::VectorTransform::reverse_transform
virtual void reverse_transform(idx_t n, const float *xt, float *x) const
Definition: VectorTransform.cpp:85
faiss::RemapDimensionsTransform::reverse_transform
void reverse_transform(idx_t n, const float *xt, float *x) const override
reverse transform correct only when the mapping is a permuation
Definition: VectorTransform.cpp:923
faiss::RemapDimensionsTransform
Definition: VectorTransform.h:205
faiss::PCAMatrix::reverse_transform
void reverse_transform(idx_t n, const float *xt, float *x) const override
Definition: VectorTransform.cpp:521
faiss::OPQMatrix::max_train_points
size_t max_train_points
if there are too many training points, resample
Definition: VectorTransform.h:190
faiss::PCAMatrix::copy_from
void copy_from(const PCAMatrix &other)
copy pre-trained PCA matrix
Definition: VectorTransform.cpp:416
faiss::VectorTransform::d_out
int d_out
! input dimension
Definition: VectorTransform.h:33
faiss::OPQMatrix::OPQMatrix
OPQMatrix(int d=0, int M=1, int d2=-1)
if d2 != -1, output vectors of this dimension
Definition: VectorTransform.cpp:534
faiss::Index
Definition: Index.h:60
faiss::PCAMatrix::prepare_Ab
void prepare_Ab()
called after mean, PCAMat and eigenvalues are computed
Definition: VectorTransform.cpp:426
faiss::IndexPreTransform::add
void add(idx_t n, const float *x) override
Definition: VectorTransform.cpp:810
faiss::RemapDimensionsTransform::apply_noalloc
void apply_noalloc(idx_t n, const float *x, float *xt) const override
same as apply, but result is pre-allocated
Definition: VectorTransform.cpp:911
faiss::PCAMatrix::eigenvalues
std::vector< float > eigenvalues
eigenvalues of covariance matrix (= squared singular values)
Definition: VectorTransform.h:149
faiss::IndexPreTransform::search
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: VectorTransform.cpp:832
faiss::IndexPreTransform::add_with_ids
void add_with_ids(idx_t n, const float *x, const long *xids) override
Definition: VectorTransform.cpp:819
faiss::PCAMatrix::random_rotation
bool random_rotation
random rotation after PCA
Definition: VectorTransform.h:137
faiss::PCAMatrix::max_points_per_d
size_t max_points_per_d
ratio between # training vectors and dimension
Definition: VectorTransform.h:140
faiss::VectorTransform::apply
float * apply(idx_t n, const float *x) const
Definition: VectorTransform.cpp:72
faiss::IndexPreTransform::remove_ids
long remove_ids(const IDSelector &sel) override
Definition: VectorTransform.cpp:847
faiss::VectorTransform::apply_noalloc
virtual void apply_noalloc(idx_t n, const float *x, float *xt) const =0
same as apply, but result is pre-allocated
faiss::OPQMatrix::M
int M
nb of subquantizers
Definition: VectorTransform.h:184
faiss::LinearTransform::apply_noalloc
void apply_noalloc(idx_t n, const float *x, float *xt) const override
same as apply, but result is pre-allocated
Definition: VectorTransform.cpp:105