Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
IndexLSH.h
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 // -*- c++ -*-
12 
13 #ifndef INDEX_LSH_H
14 #define INDEX_LSH_H
15 
16 #include <vector>
17 
18 #include "Index.h"
19 #include "VectorTransform.h"
20 
21 namespace faiss {
22 
23 
24 /** The sign of each vector component is put in a binary signature */
25 struct IndexLSH:Index {
26  typedef unsigned char uint8_t;
27 
28  int nbits; ///< nb of bits per vector
29  int bytes_per_vec; ///< nb of 8-bits per encoded vector
30  bool rotate_data; ///< whether to apply a random rotation to input
31  bool train_thresholds; ///< whether we train thresholds or use 0
32 
33  RandomRotationMatrix rrot; ///< optional random rotation
34 
35  std::vector <float> thresholds; ///< thresholds to compare with
36 
37  /// encoded dataset
38  std::vector<uint8_t> codes;
39 
40  IndexLSH (
41  idx_t d, int nbits,
42  bool rotate_data = true,
43  bool train_thresholds = false);
44 
45  /** Preprocesses and resizes the input to the size required to
46  * binarize the data
47  *
48  * @param x input vectors, size n * d
49  * @return output vectors, size n * bits. May be the same pointer
50  * as x, otherwise it should be deleted by the caller
51  */
52  const float *apply_preprocess (idx_t n, const float *x) const;
53 
54  virtual void set_typename () override;
55 
56  virtual void train (idx_t n, const float *x) override;
57 
58  virtual void add (idx_t n, const float *x) override;
59 
60  virtual void search (
61  idx_t n,
62  const float *x, idx_t k,
63  float *distances,
64  idx_t *labels) const override;
65 
66  virtual void reset() override;
67 
68  /// transfer the thresholds to a pre-processing stage (and unset
69  /// train_thresholds)
71 
72  virtual ~IndexLSH () {}
73 
74  IndexLSH ();
75 };
76 
77 
78 
79 }
80 
81 
82 
83 
84 
85 
86 #endif
Randomly rotate a set of vectors.
int bytes_per_vec
nb of 8-bits per encoded vector
Definition: IndexLSH.h:29
std::vector< float > thresholds
thresholds to compare with
Definition: IndexLSH.h:35
bool train_thresholds
whether we train thresholds or use 0
Definition: IndexLSH.h:31
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: IndexLSH.cpp:137
int d
vector dimension
Definition: Index.h:66
RandomRotationMatrix rrot
optional random rotation
Definition: IndexLSH.h:33
void transfer_thresholds(LinearTransform *vt)
Definition: IndexLSH.cpp:169
long idx_t
all indices are this type
Definition: Index.h:64
virtual void reset() override
removes all elements from the database.
Definition: IndexLSH.cpp:182
virtual void add(idx_t n, const float *x) override
Definition: IndexLSH.cpp:124
virtual void train(idx_t n, const float *x) override
Definition: IndexLSH.cpp:94
int nbits
nb of bits per vector
Definition: IndexLSH.h:28
const float * apply_preprocess(idx_t n, const float *x) const
Definition: IndexLSH.cpp:59
bool rotate_data
whether to apply a random rotation to input
Definition: IndexLSH.h:30
std::vector< uint8_t > codes
encoded dataset
Definition: IndexLSH.h:38