🚀 hkunlp/instructor-large
我們推出了 Instructor👨🏫,這是一個經過指令微調的文本嵌入模型,它能夠在無需任何微調的情況下,通過簡單地提供任務指令,生成適用於任何任務(如分類、檢索、聚類、文本評估等)和領域(如科學、金融等)的文本嵌入。Instructor👨在70個不同的嵌入任務中達到了最優性能(MTEB排行榜)!
該模型可以通過我們定製的 sentence-transformer
庫輕鬆使用。更多詳細信息,請查看我們的論文和項目頁面!
**************************** 更新內容 ****************************
- 12月28日:我們發佈了一個新的檢查點,該檢查點使用了難負樣本進行訓練,性能更優。
- 12月21日:我們發佈了論文、代碼、檢查點和項目頁面!歡迎查看!
🚀 快速開始
📦 安裝指南
pip install InstructorEmbedding
💻 使用示例
基礎用法
計算自定義嵌入
from InstructorEmbedding import INSTRUCTOR
model = INSTRUCTOR('hkunlp/instructor-large')
sentence = "3D ActionSLAM: wearable person tracking in multi-floor environments"
instruction = "Represent the Science title:"
embeddings = model.encode([[instruction,sentence]])
print(embeddings)
高級用法
計算自定義文本的嵌入
如果你想為特定句子計算自定義嵌入,可以按照統一模板編寫指令:
Represent the domain
text_type
for task_objective
:
domain
是可選的,它指定了文本的領域,例如科學、金融、醫學等。
text_type
是必需的,它指定了編碼單元,例如句子、文檔、段落等。
task_objective
是可選的,它指定了嵌入的目標,例如檢索文檔、對句子進行分類等。
計算句子相似度
你可以進一步使用該模型,通過自定義嵌入來計算兩組句子之間的相似度。
from sklearn.metrics.pairwise import cosine_similarity
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
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)
聚類
使用自定義嵌入對文本進行分組聚類。
import sklearn.cluster
sentences = [['Represent the Medicine sentence for clustering: ','Dynamical Scalar Degree of Freedom in Horava-Lifshitz Gravity'],
['Represent the Medicine sentence for clustering: ','Comparison of Atmospheric Neutrino Flux Calculations at Low Energies'],
['Represent the Medicine sentence for clustering: ','Fermion Bags in the Massive Gross-Neveu Model'],
['Represent the Medicine sentence for clustering: ',"QCD corrections to Associated t-tbar-H production at the Tevatron"],
['Represent the Medicine sentence for clustering: ','A New Analysis of the R Measurements: Resonance Parameters of the Higher, Vector States of Charmonium']]
embeddings = model.encode(sentences)
clustering_model = sklearn.cluster.MiniBatchKMeans(n_clusters=2)
clustering_model.fit(embeddings)
cluster_assignment = clustering_model.labels_
print(cluster_assignment)
📚 詳細文檔
該模型在多個數據集和任務上進行了評估,以下是部分評估結果:
任務類型 |
數據集名稱 |
指標類型 |
指標值 |
Classification |
MTEB AmazonCounterfactualClassification (en) |
accuracy |
88.13432835820896 |
Classification |
MTEB AmazonCounterfactualClassification (en) |
ap |
59.298209334395665 |
Classification |
MTEB AmazonCounterfactualClassification (en) |
f1 |
83.31769058643586 |
... |
... |
... |
... |
📄 許可證
本項目採用 apache-2.0
許可證。