milvus-logo
LFAI
首页
  • 模型

SPLADE

SPLADE嵌入是一种为文档和查询提供高度稀疏表示的模型,它继承了词袋(BOW)模型的理想特性,如精确的术语匹配和效率。

Milvus 通过SpladeEmbeddingFunction类与 SPLADE 模型集成。该类提供了对文档和查询进行编码的方法,并将嵌入返回为与 Milvus 索引兼容的稀疏向量。

要使用该功能,请安装必要的依赖项:

pip install --upgrade pymilvus
pip install "pymilvus[model]"

要实例化SpladeEmbeddingFunction,请使用以下命令:

from pymilvus import model

splade_ef = model.sparse.SpladeEmbeddingFunction(
    model_name="naver/splade-cocondenser-selfdistil", 
    device="cpu"
)

参数

  • model_name(字符串)

    用于编码的 SPLADE 模型名称。有效选项包括:naver/splade-cocondenser-ensembledistil(默认)、naver/splade_v2_maxnaver /splade _ v2_distilnaver/splade-cocondenser-selfdistil。有关更多信息,请参阅使用模型玩游戏

  • 设备(字符串)

    要使用的设备,cpu表示 CPU,cuda:n表示第 n 个 GPU 设备。

要创建文档嵌入,请使用encode_documents()方法:

docs = [
    "Artificial intelligence was founded as an academic discipline in 1956.",
    "Alan Turing was the first person to conduct substantial research in AI.",
    "Born in Maida Vale, London, Turing was raised in southern England.",
]

docs_embeddings = splade_ef.encode_documents(docs)

# Print embeddings
print("Embeddings:", docs_embeddings)
# since the output embeddings are in a 2D csr_array format, we convert them to a list for easier manipulation.
print("Sparse dim:", splade_ef.dim, list(docs_embeddings)[0].shape)

预期输出类似于下图:

Embeddings:   (0, 2001) 0.6392706036567688
  (0, 2034) 0.024093208834528923
  (0, 2082) 0.3230178654193878
...
  (2, 23602)    0.5671860575675964
  (2, 26757)    0.5770265460014343
  (2, 28639)    3.1990697383880615
Sparse dim: 30522 (1, 30522)

要为查询创建嵌入式代码,请使用encode_queries()方法:

queries = ["When was artificial intelligence founded", 
           "Where was Alan Turing born?"]

query_embeddings = splade_ef.encode_queries(queries)

# Print embeddings
print("Embeddings:", query_embeddings)
# since the output embeddings are in a 2D csr_array format, we convert them to a list for easier manipulation.
print("Sparse dim:", splade_ef.dim, list(query_embeddings)[0].shape)

预期输出类似于下图:

Embeddings:   (0, 2001)        0.6353746056556702
  (0, 2194)        0.015553371049463749
  (0, 2301)        0.2756537199020386
...
  (1, 18522)        0.1282549500465393
  (1, 23602)        0.13133203983306885
  (1, 28639)        2.8150033950805664
Sparse dim: 30522 (1, 30522)

翻译自DeepLogo

目录
反馈

此页对您是否有帮助?