结构体 faiss::IndexLSH

struct IndexLSH : public faiss::IndexFlatCodes

每个向量分量的符号被放入一个二进制签名中

公共类型

using component_t = float
using distance_t = float

公共函数

IndexLSH(idx_t d, int nbits, bool rotate_data = true, bool train_thresholds = false)
const float *apply_preprocess(idx_t n, const float *x) const

预处理并将输入调整为二值化数据所需的大小

参数:

x – 输入向量,大小为 n * d

返回值:

输出向量,大小为 n * bits。可能与 x 指向同一位置,否则应由调用者删除

virtual void train(idx_t n, const float *x) override

在具有代表性的向量集上执行训练

参数:
  • n – 训练向量的数量

  • x – 训练向量,大小为 n * d

virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, const SearchParameters *params = nullptr) const override

通过解码实现的搜索

void transfer_thresholds(LinearTransform *vt)

将阈值传递到预处理阶段(并取消设置 train_thresholds)

inline ~IndexLSH() override
IndexLSH()
virtual void sa_encode(idx_t n, const float *x, uint8_t *bytes) const override

编码一组向量

参数:
  • n – 向量的数量

  • x – 输入向量,大小为 n * d

  • bytes – 输出编码向量,大小为 n * sa_code_size()

virtual void sa_decode(idx_t n, const uint8_t *bytes, float *x) const override

解码一组向量

参数:
  • n – 向量的数量

  • bytes – 输入编码向量,大小为 n * sa_code_size()

  • x – 输出向量,大小为 n * d

virtual void add(idx_t n, const float *x) override

默认的 add 使用 sa_encode

virtual void reset() override

从数据库中移除所有元素。

virtual void reconstruct_n(idx_t i0, idx_t ni, float *recons) const override

重建向量 i0 到 i0 + ni - 1

这个函数可能对某些索引未定义

参数:
  • i0 – 序列中第一个向量的索引

  • ni – 序列中向量的数量

  • recons – 重建的向量 (大小 ni * d)

virtual void reconstruct(idx_t key, float *recons) const override

重建一个存储的向量 (如果是 有损编码,则重建一个近似向量)

这个函数可能对某些索引未定义

参数:
  • key – 要重建的向量的 id

  • recons – 重建的向量 (大小 d)

virtual size_t sa_code_size() const override

生成的代码的大小(以字节为单位)

virtual size_t remove_ids(const IDSelector &sel) override

移除一些 id。请注意,由于索引的结构,此操作的语义与通常的语义不同:新的 id 会被移动。

virtual FlatCodesDistanceComputer *get_FlatCodesDistanceComputer() const

FlatCodesDistanceComputer 提供了一个 distance_to_code 方法

默认实现使用 sa_decode 显式解码向量。

inline virtual DistanceComputer *get_distance_computer() const override

获取此索引类型的 DistanceComputer (在 AuxIndexStructures 中定义) 对象。

DistanceComputer 是为支持随机访问其向量的索引实现的。

virtual void range_search(idx_t n, const float *x, float radius, RangeSearchResult *result, const SearchParameters *params = nullptr) const override

查询 n 个维度为 d 的向量到索引。

返回所有距离 < radius 的向量。 请注意,许多索引未实现 range_search(只有 k-NN 搜索是强制性的)。

参数:
  • n – 向量的数量

  • x – 要搜索的输入向量,大小为 n * d

  • radius – 搜索半径

  • result – 结果表

CodePacker *get_CodePacker() const
virtual void check_compatible_for_merge(const Index &otherIndex) const override

检查两个索引是否兼容(即,它们是否以相同的方式训练并且具有相同的参数)。 否则,抛出异常。

virtual void merge_from(Index &otherIndex, idx_t add_id = 0) override

将条目从另一个数据集移动到自身。 在输出时,other 是空的。 add_id 将添加到所有移动的 id(对于顺序 id,这将是 this->ntotal)

virtual void add_sa_codes(idx_t n, const uint8_t *x, const idx_t *xids) override

添加使用独立编解码器计算的向量

参数:
  • codes – 要添加的代码,大小为 n * sa_code_size()

  • xids – 对应的 ID,大小为 n

void permute_entries(const idx_t *perm)
virtual void add_with_ids(idx_t n, const float *x, const idx_t *xids)

与 add 相同,但存储 xids 而不是顺序 ID。

默认实现会断言失败,因为它并非所有索引都支持。

参数:
  • n – 向量的数量

  • x – 输入向量,大小为 n * d

  • xids – 如果非空,则存储向量的 ID(大小为 n)

virtual void assign(idx_t n, const float *x, idx_t *labels, idx_t k = 1) const

返回与查询 x 最接近的 k 个向量的索引。

此函数与 search 相同,但仅返回邻居的标签。

参数:
  • n – 向量的数量

  • x – 要搜索的输入向量,大小为 n * d

  • labels – NN 的输出标签,大小为 n*k

  • k – 最近邻的数量

virtual void reconstruct_batch(idx_t n, const idx_t *keys, float *recons) const

重构多个存储的向量(如果是有损编码,则为近似值)

这个函数可能对某些索引未定义

参数:
  • n – 要重构的向量数

  • keys – 要重构的向量的 ID(大小为 n)

  • recons – 重构的向量(大小为 n * d)

virtual void search_and_reconstruct(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, float *recons, const SearchParameters *params = nullptr) const

类似于搜索,但也为搜索结果重构存储的向量(在有损编码的情况下为近似值)。

如果查询的结果不足,则生成的数组会用 -1 填充。

参数:
  • n – 向量的数量

  • x – 要搜索的输入向量,大小为 n * d

  • k – 提取的向量数

  • distances – 输出成对距离,大小为 n*k

  • labels – NN 的输出标签,大小为 n*k

  • recons – 重构的向量大小 (n, k, d)

virtual void compute_residual(const float *x, float *residual, idx_t key) const

计算索引编码后的残差向量。

残差向量是向量与可以从其在索引中的表示形式解码的重构之间的差异。 残差可用于多阶段索引方法,如 IndexIVF 的方法。

参数:
  • x – 输入向量,大小为 d

  • residual – 输出残差向量,大小为 d

  • key – 编码的索引,由 search 和 assign 返回

virtual void compute_residual_n(idx_t n, const float *xs, float *residuals, const idx_t *keys) const

计算索引编码后的残差向量(批量形式)。 相当于为每个向量调用 compute_residual。

残差向量是向量与可以从其在索引中的表示形式解码的重构之间的差异。 残差可用于多阶段索引方法,如 IndexIVF 的方法。

参数:
  • n – 向量的数量

  • xs – 输入向量,大小 (n x d)

  • residuals – 输出残差向量,大小 (n x d)

  • keys – 编码的索引,由 search 和 assign 返回

公共成员

int nbits

每个向量的位数

bool rotate_data

是否对输入应用随机旋转

bool train_thresholds

我们是否训练阈值或使用 0

RandomRotationMatrix rrot

可选的随机旋转

std::vector<float> thresholds

用于比较的阈值

size_t code_size
std::vector<uint8_t> codes

编码后的数据集,大小为 ntotal * code_size

int d

向量维度

idx_t ntotal

索引向量的总数

bool verbose

详细程度

bool is_trained

如果Index不需要训练,或者已经完成训练,则设置此项

MetricType metric_type

此索引用于搜索的度量类型

float metric_arg

度量类型的参数