结构体 faiss::IndexPreTransform
-
struct IndexPreTransform : public faiss::Index
Index,在将向量传递给子索引之前,对向量应用 LinearTransform 变换
公共类型
-
using component_t = float
-
using distance_t = float
公共函数
-
explicit IndexPreTransform(Index *index)
! 指针是否在析构函数中被删除
-
IndexPreTransform()
-
IndexPreTransform(VectorTransform *ltrans, Index *index)
ltrans 是索引之前的最后一个变换
-
void prepend_transform(VectorTransform *ltrans)
-
virtual void train(idx_t n, const float *x) override
在代表性向量集上执行训练
- 参数:
n – 训练向量的数量
x – 训练向量,大小为 n * d
-
virtual void add(idx_t n, const float *x) override
将维度为 d 的 n 个向量添加到索引中。
向量被隐式地分配标签 ntotal .. ntotal + n - 1。此函数将输入向量切分成小于 blocksize_add 的块,并调用 add_core。
- 参数:
n – 向量的数量
x – 输入矩阵,大小为 n * d
-
virtual void add_with_ids(idx_t n, const float *x, const idx_t *xids) override
与 add 相同,但存储 xids 而不是顺序 id。
默认实现会因断言而失败,因为并非所有索引都支持它。
- 参数:
n – 向量的数量
x – 输入向量,大小为 n * d
xids – 如果非空,则要为向量存储的 id(大小为 n)
-
virtual void reset() override
从数据库中删除所有元素。
-
virtual size_t remove_ids(const IDSelector &sel) override
从索引中删除 ID。并非所有索引都支持。
-
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, const SearchParameters *params = nullptr) const override
将维度为 d 的 n 个向量查询到索引。
最多返回 k 个向量。如果查询的结果不足,则结果数组将填充 -1。
- 参数:
n – 向量的数量
x – 要搜索的输入向量,大小为 n * d
k – 提取的向量数
distances – 输出成对距离,大小为 n*k
labels – NNs 的输出标签,大小为 n*k
-
virtual void range_search(idx_t n, const float *x, float radius, RangeSearchResult *result, const SearchParameters *params = nullptr) const override
将维度为 d 的 n 个向量查询到索引。
返回所有距离小于半径的向量。请注意,许多索引没有实现 range_search(只有 k-NN 搜索是强制性的)。
- 参数:
n – 向量的数量
x – 要搜索的输入向量,大小为 n * d
radius – 搜索半径
result – 结果表
-
virtual void reconstruct(idx_t key, float *recons) const override
重建存储的向量(如果是 Lossy Coding,则重建近似向量)
此函数可能未针对某些索引定义
- 参数:
key – 要重建的向量的 ID
recons – 重建的向量(大小为 d)
-
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 search_and_reconstruct(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, float *recons, const SearchParameters *params = nullptr) const override
与搜索类似,但也会重建存储的向量(如果是 Lossy Coding,则重建近似向量),用于搜索结果。
如果查询的结果不足,则结果数组将填充 -1。
- 参数:
n – 向量的数量
x – 要搜索的输入向量,大小为 n * d
k – 提取的向量数
distances – 输出成对距离,大小为 n*k
labels – NNs 的输出标签,大小为 n*k
recons – 重建的向量大小 (n, k, d)
-
const float *apply_chain(idx_t n, const float *x) const
应用链中的转换。返回的 float * 可能等于 x,否则应取消分配。
-
void reverse_chain(idx_t n, const float *xt, float *x) const
反转链中的转换。对于链中的所有转换,可能未实现,或者可能返回近似结果。
-
virtual DistanceComputer *get_distance_computer() const override
获取此索引类型的 DistanceComputer(在 AuxIndexStructures 中定义)对象。
DistanceComputer 是为支持对其向量进行随机访问的索引实现的。
-
virtual size_t sa_code_size() const override
生成的代码的大小(以字节为单位)
-
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 merge_from(Index &otherIndex, idx_t add_id = 0) override
将条目从另一个数据集移动到 self。输出时,other 为空。 add_id 被添加到所有移动的 id (对于连续 id,这将是 this->ntotal)
-
virtual void check_compatible_for_merge(const Index &otherIndex) const override
检查两个索引是否兼容 (即,它们以相同的方式训练并具有相同的参数)。否则抛出异常。
-
~IndexPreTransform() override
-
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 – NNs 的输出标签,大小为 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 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 返回
-
virtual void add_sa_codes(idx_t n, const uint8_t *codes, const idx_t *xids)
添加使用独立编解码器计算的向量
- 参数:
codes – 要添加的代码,大小为 n * sa_code_size()
xids – 相应的 id,大小为 n
公共成员
-
std::vector<VectorTransform*> chain
-
Index *index
! 转换链
-
bool own_fields
! 子索引
-
int d
向量维度
-
idx_t ntotal
索引向量的总数
-
bool verbose
详细程度
-
bool is_trained
如果 Index 不需要训练,或者已经完成训练,则设置此项
-
MetricType metric_type
此索引用于搜索的度量类型
-
float metric_arg
度量类型的参数
-
using component_t = float