hkunlp/instructor-xl 我们推出了Instructor👨🏫,这是一款经过指令微调的文本嵌入模型。它无需任何微调,只需提供任务指令,就能为任何任务(例如分类、检索、聚类、文本评估等)和领域(例如科学、金融等)生成量身定制的文本嵌入。Instructor👨在70项不同的嵌入任务上均达到了最先进水平!通过我们定制的sentence-transformer库,该模型易于使用。欲了解更多详情,请查阅我们的论文和项目页面!
**************************** 更新 ****************************
01/21:我们发布了一个使用难负样本训练的新检查点,性能更优。 12/21:我们发布了论文、代码、检查点和项目页面!欢迎查阅! 快速开始 安装 pip install InstructorEmbedding
您可以进一步使用该模型,通过定制化嵌入来计算两组句子之间的相似度。
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)您也可以使用自定义嵌入进行信息检索。
import numpy as np
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)