结构体 faiss::IndexPQ
-
struct IndexPQ : public faiss::IndexFlatCodes
基于乘积量化器的Index。存储的向量通过 PQ 码近似。
公开类型
-
enum Search_type_t
如何在 search_core 中执行搜索
值
-
enumerator ST_PQ
非对称乘积量化器(默认)
-
enumerator ST_HE
码上的汉明距离。
-
enumerator ST_generalized_HE
相同码的数量
-
enumerator ST_SDC
对称乘积量化器 (SDC)
-
enumerator ST_polysemous
HE 过滤器(使用 ht)+ PQ 组合。
-
enumerator ST_polysemous_generalize
基于广义汉明距离的过滤。
-
enumerator ST_PQ
-
using component_t = float
-
using distance_t = float
公开函数
-
IndexPQ(int d, size_t M, size_t nbits, MetricType metric = METRIC_L2)
构造函数。
- 参数:
d – 输入向量的维度
M – 子量化器的数量
nbits – 每个子向量索引的位数
-
IndexPQ()
-
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
通过解码实现的搜索
-
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 FlatCodesDistanceComputer *get_FlatCodesDistanceComputer() const override
一个 FlatCodesDistanceComputer 提供了一个 distance_to_code 方法
默认实现显式地使用 sa_decode 解码向量。
-
void search_core_polysemous(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, int polysemous_ht, bool generalized_hamming) const
-
void hamming_distance_histogram(idx_t n, const float *x, idx_t nb, const float *xb, int64_t *dist_histogram)
为多义搜索准备查询,但不是计算结果,而是获取汉明距离的直方图。 如果 xb != NULL,则可以在提供的数据集上计算
- 参数:
dist_histogram – (M * nbits + 1)
-
void hamming_distance_table(idx_t n, const float *x, int32_t *dis) const
计算查询和数据库之间的成对距离
- 参数:
n – 查询向量的个数
x – 查询向量,大小为 n * d
dis – 输出距离,大小为 n * ntotal
-
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
重构存储的向量(如果是 lossy coding,则为近似值)
对于某些索引,此函数可能未定义
- 参数:
key – 要重构的向量的 ID
recons – 重构的向量(大小为 d)
-
virtual size_t sa_code_size() const override
生成的代码的大小(以字节为单位)
-
virtual size_t remove_ids(const IDSelector &sel) override
删除一些 ID。 请注意,由于索引的结构,此操作的语义与通常的语义不同:新的 ID 已移位
-
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
查询索引中维度为 d 的 n 个向量。
返回所有距离 < 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
将条目从另一个数据集移动到 self。在输出时,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 – 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 search_and_reconstruct(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, float *recons, const SearchParameters *params = nullptr) const
与 search 类似,但也会重建搜索结果的存储向量(如果是lossy coding,则为近似值)。
如果查询的结果不足,则结果数组将填充 -1。
- 参数:
n – 向量的数量
x – 要搜索的输入向量,大小为 n * d
k – 提取的向量数
distances – 输出成对距离,大小为 n*k
labels – NNs 的输出标签,大小为 n*k
recons – 重构向量,大小 (n, k, d)
公共成员
-
ProductQuantizer pq
用于编码向量的乘积量化器。
-
bool do_polysemous_training
false = 标准 PQ
-
PolysemousTraining polysemous_training
用于多义训练的参数
-
Search_type_t search_type
-
bool encode_signs
-
int polysemous_ht
用于多义的汉明阈值。
-
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
度量类型的参数
-
enum Search_type_t