结构体 faiss::IndexBinaryHash
-
struct IndexBinaryHash : public faiss::IndexBinary
仅使用前 b 位作为哈希值
公共类型
-
using InvertedListMap = std::unordered_map<idx_t, InvertedList>
-
using component_t = uint8_t
-
using distance_t = int32_t
公共函数
-
IndexBinaryHash(int d, int b)
-
IndexBinaryHash()
-
virtual void reset() override
从数据库中删除所有元素。
-
virtual void add(idx_t n, const uint8_t *x) override
将 n 个维度为 d 的向量添加到索引。
向量隐式分配标签 ntotal .. ntotal + n - 1
- 参数:
x – 输入矩阵,大小 n * d / 8
-
virtual void add_with_ids(idx_t n, const uint8_t *x, const idx_t *xids) override
与 add 相同,但存储 xids 而不是顺序 id。
默认实现会因断言而失败,因为它并非所有索引都支持。
- 参数:
xids – 如果非空,则为向量存储的 id(大小为 n)
-
virtual void range_search(idx_t n, const uint8_t *x, int radius, RangeSearchResult *result, const SearchParameters *params = nullptr) const override
查询 n 个维度为 d 的向量到索引。
返回距离 < radius 的所有向量。 请注意,许多索引未实现 range_search(只有 k-NN 搜索是强制性的)。 距离转换为 float 以重用 RangeSearchResult 结构,但它们是整数。 按照惯例,仅返回距离 < radius(严格比较)的距离,即 radius = 0 不返回任何结果,1 仅返回完全相同的向量。
- 参数:
x – 要搜索的输入向量,大小 n * d / 8
radius – 搜索半径
result – 结果表
-
virtual void search(idx_t n, const uint8_t *x, idx_t k, int32_t *distances, idx_t *labels, const SearchParameters *params = nullptr) const override
查询 n 个维度为 d 的向量到索引。
最多返回 k 个向量。 如果查询的结果不足,则结果数组将填充 -1。
- 参数:
x – 要搜索的输入向量,大小 n * d / 8
labels – NN 的输出标签,大小为 n*k
distances – 输出成对距离,大小为 n*k
-
void display() const
-
size_t hashtable_size() const
-
virtual void train(idx_t n, const uint8_t *x)
对具有代表性的向量集合执行训练。
- 参数:
n – 训练向量的数量
x – 训练向量,大小为 n * d / 8
-
void assign(idx_t n, const uint8_t *x, idx_t *labels, idx_t k = 1) const
返回与查询 x 最接近的 k 个向量的索引。
此函数与搜索相同,但仅返回邻居的标签。
- 参数:
x – 要搜索的输入向量,大小 n * d / 8
labels – NN 的输出标签,大小为 n*k
-
virtual size_t remove_ids(const IDSelector &sel)
从索引中移除 ID。并非所有索引都支持。
-
virtual void reconstruct(idx_t key, uint8_t *recons) const
重建存储的向量。
对于某些索引,可能未定义此函数。
- 参数:
key – 要重建的向量的 ID
recons – 重建的向量 (大小为 d / 8)
-
virtual void reconstruct_n(idx_t i0, idx_t ni, uint8_t *recons) const
重建向量 i0 到 i0 + ni - 1。
对于某些索引,可能未定义此函数。
- 参数:
recons – 重建的向量 (大小为 ni * d / 8)
-
virtual void search_and_reconstruct(idx_t n, const uint8_t *x, idx_t k, int32_t *distances, idx_t *labels, uint8_t *recons, const SearchParameters *params = nullptr) const
类似于搜索,但也会为搜索结果重建存储的向量(或者在有损编码的情况下进行近似)。
如果查询的结果不足,则结果数组会用 -1 填充。
- 参数:
recons – 重建的向量,大小为 (n, k, d)
-
virtual void merge_from(IndexBinary &otherIndex, idx_t add_id = 0)
将条目从另一个数据集移动到自身。 在输出时,other 为空。 add_id 将添加到所有移动的 id(对于顺序 id,这将是 this->ntotal)
-
virtual void check_compatible_for_merge(const IndexBinary &otherIndex) const
检查两个索引是否兼容(即,它们是否以相同的方式训练并且具有相同的参数)。 否则抛出异常。
-
virtual size_t sa_code_size() const
生成的代码的大小(以字节为单位)
-
virtual void add_sa_codes(idx_t n, const uint8_t *codes, const idx_t *xids)
与 IndexBinary 的 add_with_ids 相同。
公共成员
-
InvertedListMap invlists
-
int b
-
int nflip
-
int d = 0
向量维度
-
int code_size = 0
每个向量的字节数 ( = d / 8 )
-
idx_t ntotal = 0
索引向量的总数
-
bool verbose = false
详细级别
-
bool is_trained = true
设置 Index 是否需要训练,或者是否已经完成训练
-
MetricType metric_type = METRIC_L2
此索引用于搜索的度量类型
-
struct InvertedList
公共函数
-
void add(idx_t id, size_t code_size, const uint8_t *code)
-
void add(idx_t id, size_t code_size, const uint8_t *code)
-
using InvertedListMap = std::unordered_map<idx_t, InvertedList>