HuggingFace镜像/instructor-large
模型介绍文件和版本分析
下载使用量0

hkunlp/instructor-large 我们推出了Instructor👨‍🏫,这是一款指令微调文本嵌入模型。它只需提供任务指令,无需任何微调,就能为任何任务(例如分类、检索、聚类、文本评估等)和领域(例如科学、金融等)生成定制化的文本嵌入。Instructor👨‍在70项多样化的嵌入任务(MTEB排行榜)上均达到了SOTA水平!借助我们定制的sentence-transformer库,该模型易于使用。欲了解更多详情,请查阅我们的论文和项目页面!

**************************** 更新 ****************************

12月28日:我们发布了一个使用难负样本训练的新检查点,性能更优。 12月21日:我们发布了论文、代码、检查点和项目页面!欢迎查阅! 快速开始 安装 pip install InstructorEmbedding

hku-nlp/instructor-large

这是一个通用嵌入模型:在测试时,它能将任何文本片段(例如标题、句子、文档等)映射到固定长度的向量,无需进一步训练。通过指令,嵌入可以是领域特定的(例如专用于科学、金融等领域)和任务感知的(例如为分类、信息检索等任务定制)。

该模型可通过sentence-transformer库轻松使用。

安装

git clone https://github.com/HKUNLP/instructor-embedding
cd sentence-transformers
pip install -e .

计算句子相似度

您可以进一步使用该模型计算两组句子之间的相似度,并支持自定义嵌入。

import torch
import torch_npu
from InstructorEmbedding import INSTRUCTOR
from sklearn.metrics.pairwise import cosine_similarity

device = torch.device('npu:0')
model = INSTRUCTOR('./').to(device)
sentences_a = [['Represent the Science sentence: ','Parton energy loss in QCD matter'], 
               ['Represent the Financial statement: ','The Federal Reserve on Wednesday raised its benchmark interest rate.']]
sentences_b = [['Represent the Science sentence: ','The Chiral Phase Transition in Dissipative Dynamics'],
               ['Represent the Financial statement: ','The funds rose less than 0.5 per cent on Friday']]
embeddings_a = model.encode(sentences_a)
embeddings_b = model.encode(sentences_b)
similarities = cosine_similarity(embeddings_a,embeddings_b)
print(similarities)

信息检索

您还可以使用自定义嵌入进行信息检索。

from sklearn.metrics.pairwise import cosine_similarity
import torch
import torch_npu
from InstructorEmbedding import INSTRUCTOR

device = torch.device('npu:0')
model = INSTRUCTOR('./').to(device)
query  = [['Represent the Wikipedia question for retrieving supporting documents: ','where is the food stored in a yam plant']]
corpus = [['Represent the Wikipedia document for retrieval: ','Capitalism has been dominant in the Western world since the end of feudalism, but most feel[who?] that the term "mixed economies" more precisely describes most contemporary economies, due to their containing both private-owned and state-owned enterprises. In capitalism, prices determine the demand-supply scale. For example, higher demand for certain goods and services lead to higher prices and lower demand for certain goods lead to lower prices.'],
          ['Represent the Wikipedia document for retrieval: ',"The disparate impact theory is especially controversial under the Fair Housing Act because the Act regulates many activities relating to housing, insurance, and mortgage loans—and some scholars have argued that the theory's use under the Fair Housing Act, combined with extensions of the Community Reinvestment Act, contributed to rise of sub-prime lending and the crash of the U.S. housing market and ensuing global economic recession"],
          ['Represent the Wikipedia document for retrieval: ','Disparate impact in United States labor law refers to practices in employment, housing, and other areas that adversely affect one group of people of a protected characteristic more than another, even though rules applied by employers or landlords are formally neutral. Although the protected classes vary by statute, most federal civil rights laws protect based on race, color, religion, national origin, and sex as protected traits, and some laws include disability status and other traits as well.']]
query_embeddings = model.encode(query)
corpus_embeddings = model.encode(corpus)
similarities = cosine_similarity(query_embeddings,corpus_embeddings)
retrieved_doc_id = np.argmax(similarities)
print(retrieved_doc_id)