模型:
这是一个通用的嵌入模型:它可以在测试时将任意文本(例如标题、句子、文档等)映射为固定长度的向量,而无需进一步训练。在使用说明的情况下,这些嵌入是领域特定的(例如,专门针对科学、金融等)和任务感知的(例如,定制化用于分类、信息检索等)
使用sentence-transformer库非常简单。
git clone https://github.com/HKUNLP/instructor-embedding cd sentence-transformers pip install -e .
接下来,您可以像这样使用模型来计算领域特定和任务感知的嵌入:
from sentence_transformers import SentenceTransformer
sentence = "3D ActionSLAM: wearable person tracking in multi-floor environments"
instruction = "Represent the Science title; Input:"
model = SentenceTransformer('hku-nlp/instructor-xl')
embeddings = model.encode([[instruction,sentence,0]])
print(embeddings)
您可以进一步使用该模型计算两组句子之间的相似度,使用自定义嵌入。
from sklearn.metrics.pairwise import cosine_similarity
sentences_a = [['Represent the Science sentence; Input: ','Parton energy loss in QCD matter',0],
['Represent the Financial statement; Input: ','The Federal Reserve on Wednesday raised its benchmark interest rate.',0]
sentences_b = [['Represent the Science sentence; Input: ','The Chiral Phase Transition in Dissipative Dynamics', 0],
['Represent the Financial statement; Input: ','The funds rose less than 0.5 per cent on Friday',0]
embeddings_a = model.encode(sentences_a)
embeddings_b = model.encode(sentences_b)
similarities = cosine_similarity(embeddings_a,embeddings_b)
print(similarities)