结构体 faiss::IndexFlatL2
-
struct IndexFlatL2 : public faiss::IndexFlat
由 faiss::IndexFlat1D 继承
公共类型
-
using component_t = float
-
using distance_t = float
公共函数
-
inline explicit IndexFlatL2(idx_t d)
- 参数:
d – 输入向量的维度
-
inline IndexFlatL2()
-
virtual FlatCodesDistanceComputer *get_FlatCodesDistanceComputer() const override
FlatCodesDistanceComputer 提供一个 distance_to_code 方法
默认实现使用 sa_decode 显式解码向量。
-
void sync_l2norms()
-
void clear_l2norms()
-
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, const SearchParameters *params = nullptr) const override
将 n 个维度为 d 的向量查询到索引。
最多返回 k 个向量。 如果查询的结果不足,则结果数组将填充 -1。
- 参数:
n – 向量数
x – 要搜索的输入向量,大小为 n * d
k – 提取的向量数
distances – 输出成对距离,大小为 n*k
labels – NN 的输出标签,大小为 n*k
-
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 – 结果表
-
virtual void reconstruct(idx_t key, float *recons) const override
重建存储的向量(如果是有损编码,则重建近似向量)
对于某些索引,可能未定义此函数
- 参数:
key – 要重建的向量的 ID
recons – 重建的向量(大小为 d)
-
void compute_distance_subset(idx_t n, const float *x, idx_t k, float *distances, const idx_t *labels) const
计算与向量子集的距离
- 参数:
x – 查询向量,大小为 n * d
labels – 应该为每个查询向量比较的向量的索引,大小为 n * k
distances – 相应的输出距离,大小为 n * k
-
inline float *get_xb()
-
inline const float *get_xb() const
-
virtual void sa_encode(idx_t n, const float *x, uint8_t *bytes) const override
encode a set of vectors
- 参数:
n – 向量数
x – input vectors, size n * d
bytes – output encoded vectors, size n * sa_code_size()
-
virtual void sa_decode(idx_t n, const uint8_t *bytes, float *x) const override
decode a set of vectors
- 参数:
n – 向量数
bytes – input encoded vectors, size n * sa_code_size()
x – output vectors, size n * d
-
virtual void add(idx_t n, const float *x) override
default add uses sa_encode
-
virtual void reset() override
removes all elements from the database.
-
virtual void reconstruct_n(idx_t i0, idx_t ni, float *recons) const override
Reconstruct vectors i0 to i0 + ni - 1
对于某些索引,可能未定义此函数
- 参数:
i0 – index of the first vector in the sequence
ni – number of vectors in the sequence
recons – reconstucted vector (size ni * d)
-
virtual size_t sa_code_size() const override
size of the produced codes in bytes
-
virtual size_t remove_ids(const IDSelector &sel) override
remove some ids. NB that because of the structure of the index, the semantics of this operation are different from the usual ones: the new ids are shifted
-
inline virtual DistanceComputer *get_distance_computer() const override
Get a DistanceComputer (defined in AuxIndexStructures) object for this kind of index.
DistanceComputer is implemented for indexes that support random access of their vectors.
-
CodePacker *get_CodePacker() const
-
virtual void check_compatible_for_merge(const Index &otherIndex) const override
check that the two indexes are compatible (ie, they are trained in the same way and have the same parameters). Otherwise throw.
-
virtual void merge_from(Index &otherIndex, idx_t add_id = 0) override
moves the entries from another dataset to self. On output, other is empty. add_id is added to all moved ids (for sequential ids, this would be this->ntotal)
-
virtual void add_sa_codes(idx_t n, const uint8_t *x, const idx_t *xids) override
Add vectors that are computed with the standalone codec
- 参数:
codes – 要添加的代码,大小为 n * sa_code_size()
xids – 对应的 id,大小为 n
-
void permute_entries(const idx_t *perm)
-
virtual void train(idx_t n, const float *x)
在一个有代表性的向量集上执行训练
- 参数:
n – 训练向量的数量
x – 训练向量,大小为 n * d
-
virtual void add_with_ids(idx_t n, const float *x, const idx_t *xids)
与 add 相同,但存储 xids 而不是顺序 id。
默认实现会因断言而失败,因为它并非所有索引都支持。
- 参数:
n – 向量数
x – input vectors, size 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
与 search 类似,但也会重建存储的向量(如果是lossy编码,则是近似值)以获得搜索结果。
如果查询的结果不足,则结果数组会用 -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 返回
公共成员
-
std::vector<float> cached_l2norms
-
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
度量类型的参数
-
using component_t = float