We introduce Instructor👨🏫, an instruction-finetuned text embedding model that can generate text embeddings tailored to any task (e.g., classification, retrieval, clustering, text evaluation, etc.) and domains (e.g., science, finance, etc.) by simply providing the task instruction, without any finetuning. Instructor👨 achieves sota on 70 diverse embedding tasks!
The model is easy to use with our customized
sentence-transformer library. For more details, check out our paper and project page!
**************************** Updates ****************************
- 01/21: We released a new checkpoint trained with hard negatives, which gives better performance.
- 12/21: We released our paper, code, checkpoint and project page! Check them out!
pip install InstructorEmbedding
Compute your customized embeddings
Then you can use the model like this to calculate domain-specific and task-aware embeddings:
from InstructorEmbedding import INSTRUCTOR model = INSTRUCTOR('hkunlp/instructor-base') sentence = "3D ActionSLAM: wearable person tracking in multi-floor environments" instruction = "Represent the Science title:" embeddings = model.encode([[instruction,sentence]]) print(embeddings)
Calculate embeddings for your customized texts
If you want to calculate customized embeddings for specific sentences, you may follow the unified template to write instructions:
domainis optional, and it specifies the domain of the text, e.g., science, finance, medicine, etc.
text_typeis required, and it specifies the encoding unit, e.g., sentence, document, paragraph, etc.
task_objectiveis optional, and it specifies the objective of embedding, e.g., retrieve a document, classify the sentence, etc.
Calculate Sentence similarities
You can further use the model to compute similarities between two groups of sentences, with customized embeddings.
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)
You can also use customized embeddings for information retrieval.
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)
Use customized embeddings for clustering texts in groups.
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)
- Downloads last month
Inference API has been turned off for this model.
Spaces using hkunlp/instructor-base 8
- accuracy on MTEB AmazonCounterfactualClassification (en)test set self-reported86.209
- ap on MTEB AmazonCounterfactualClassification (en)test set self-reported55.763
- f1 on MTEB AmazonCounterfactualClassification (en)test set self-reported81.261
- accuracy on MTEB AmazonPolarityClassificationtest set self-reported88.360
- ap on MTEB AmazonPolarityClassificationtest set self-reported84.188
- f1 on MTEB AmazonPolarityClassificationtest set self-reported88.318
- accuracy on MTEB AmazonReviewsClassification (en)test set self-reported44.640
- f1 on MTEB AmazonReviewsClassification (en)test set self-reported42.487
- map_at_1 on MTEB ArguAnatest set self-reported27.383
- map_at_10 on MTEB ArguAnatest set self-reported43.024
- map_at_100 on MTEB ArguAnatest set self-reported44.023
- map_at_1000 on MTEB ArguAnatest set self-reported44.026
- map_at_3 on MTEB ArguAnatest set self-reported37.684
- map_at_5 on MTEB ArguAnatest set self-reported40.884
- mrr_at_1 on MTEB ArguAnatest set self-reported28.094
- mrr_at_10 on MTEB ArguAnatest set self-reported43.315
- mrr_at_100 on MTEB ArguAnatest set self-reported44.313
- mrr_at_1000 on MTEB ArguAnatest set self-reported44.317
- mrr_at_3 on MTEB ArguAnatest set self-reported37.862
- mrr_at_5 on MTEB ArguAnatest set self-reported41.155
- ndcg_at_1 on MTEB ArguAnatest set self-reported27.383
- ndcg_at_10 on MTEB ArguAnatest set self-reported52.032
- ndcg_at_100 on MTEB ArguAnatest set self-reported56.195
- ndcg_at_1000 on MTEB ArguAnatest set self-reported56.272
- ndcg_at_3 on MTEB ArguAnatest set self-reported41.166
- ndcg_at_5 on MTEB ArguAnatest set self-reported46.920
- precision_at_1 on MTEB ArguAnatest set self-reported27.383
- precision_at_10 on MTEB ArguAnatest set self-reported8.087
- precision_at_100 on MTEB ArguAnatest set self-reported0.989
- precision_at_1000 on MTEB ArguAnatest set self-reported0.099
- precision_at_3 on MTEB ArguAnatest set self-reported17.093
- precision_at_5 on MTEB ArguAnatest set self-reported13.044
- recall_at_1 on MTEB ArguAnatest set self-reported27.383
- recall_at_10 on MTEB ArguAnatest set self-reported80.868
- recall_at_100 on MTEB ArguAnatest set self-reported98.862
- recall_at_1000 on MTEB ArguAnatest set self-reported99.431
- recall_at_3 on MTEB ArguAnatest set self-reported51.280
- recall_at_5 on MTEB ArguAnatest set self-reported65.220
- v_measure on MTEB ArxivClusteringP2Ptest set self-reported39.684
- v_measure on MTEB ArxivClusteringS2Stest set self-reported29.189
- map on MTEB AskUbuntuDupQuestionstest set self-reported63.173
- mrr on MTEB AskUbuntuDupQuestionstest set self-reported76.189
- cos_sim_spearman on MTEB BIOSSEStest set self-reported82.308
- accuracy on MTEB Banking77Classificationtest set self-reported77.036
- f1 on MTEB Banking77Classificationtest set self-reported75.874
- v_measure on MTEB BiorxivClusteringP2Ptest set self-reported32.980