Sentence Transformers v2 集成 Hugging Face Hub
Sentence Transformers v2 版本发布,正式集成至 Hugging Face Hub。该更新支持模型共享、语义搜索及多语言零样本分类,并提供了嵌入与相似度计算的推理 API。
报道正文
本文目录5 个章节
Hugging Face Hub 中的 Sentence Transformers
发布于 2021 年 6 月 28 日 在 GitHub 上更新 点赞 - Omar Sanseviero osanseviero Nils Reimers nreimers
在过去几周里,我们与机器学习生态系统中的许多开源框架建立了合作。其中让我们特别兴奋的是 Sentence Transformers。
Sentence Transformers 是一个用于句子、段落和图像嵌入的框架。这使得我们可以生成具有语义意义的嵌入 (1),这对于语义搜索或多语言零样本分类等应用非常有用。作为 Sentence Transformers v2 版本发布的一部分,有许多令人兴奋的新特性:
Hub 中拥有超过 90 个预训练的 Sentence Transformers 模型,支持 100 多种语言,任何人都可以从中受益并轻松使用它们。只需几行代码即可加载并使用预训练模型:
from sentence_transformers import SentenceTransformer
sentences = ["Hello World", "Hallo Welt"]
model = SentenceTransformer('sentence-transformers/paraphrase-MiniLM-L6-v2')
embeddings = model.encode(sentences)
print(embeddings)不仅如此。人们可能希望演示他们的模型或轻松体验其他模型,因此我们很高兴地宣布在 Hub 中推出两个新的小部件!第一个是 feature-extraction 小部件,它展示了句子嵌入。
sentence-transformers/distilbert-base-nli-max-tokens Hosted inference API Feature Extraction This model is currently loaded and running on the Inference API.
不过,光看一堆数字对你来说可能用处不大(除非你能一眼看懂 embeddings,那可就太厉害了!)。我们还为 Sentence Transformers 的一个常见用例——计算句子相似度——引入了一个新的 widget。
sentence-transformers/paraphrase-MiniLM-L6-v2 Hosted inference API Sentence Similarity Source Sentence Sentences to compare to This model can be loaded on the Inference API on-demand.
当然,除了 widgets 之外,我们还在 Inference API 中提供了 API endpoints,你可以使用它们以编程方式调用你的模型!
import json
import requests
API_URL = "https://api-inference.huggingface.co/models/sentence-transformers/paraphrase-MiniLM-L6-v2"
headers = {"Authorization": "Bearer YOUR_TOKEN"}
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
data = query(
{
"inputs": {
"source_sentence": "That is a happy person",
"sentences": [
"That is a happy dog",
"That is a very happy person",
"Today is a sunny day"
]
}
}
)释放分享的力量
那么,为什么这很强大呢?只需几分钟,你就可以将训练好的模型分享给整个社区。
from sentence_transformers import SentenceTransformer
model.save_to_hub("my_new_model")现在,你将在 Hub 上拥有一个托管你模型的 repository。系统会自动创建一个 model card,其中列出了各层以描述架构,并展示了如何同时使用 Sentence Transformers 和 🤗 Transformers 来使用该模型。你还可以立即试用 widget 并使用 Inference API!
如果这还不够令人兴奋,你的模型还将通过筛选所有 Sentence Transformers 模型而轻松被他人发现。
下一步计划?
展望未来,我们希望让这一集成变得更有用。在我们的 roadmap 中,预计自动创建的 model card 将包含训练和评估数据,就像 transformers 从 v4.8 版本开始的情况一样。
And what's next for you? We're very excited to see your contributions! If you already have a Sentence Transformer repo in the Hub, you can now enable the widget and Inference API by changing the model card metadata.
---
tags:
- sentence-transformers
- sentence-similarity
---If you don't have any model in the Hub and want to learn more about Sentence Transformers, head to www.SBERT.net!
Would you like to integrate your library to the Hub?
This integration is possible thanks to the huggingface_hub library which has all our widgets and the API for all our supported libraries. If you would like to integrate your library to the Hub, we have a guide for you!
References
- Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks. https://arxiv.org/abs/1908.10084