Sentence Similarity
sentence-transformers
PyTorch
Transformers
English
t5
text-embedding
embeddings
information-retrieval
beir
text-classification
language-model
text-clustering
text-semantic-similarity
text-evaluation
prompt-retrieval
text-reranking
feature-extraction
English
Sentence Similarity
natural_questions
ms_marco
fever
hotpot_qa
mteb
Eval Results
multi-train
commited on
Commit
•
0b2f225
1
Parent(s):
724f6ed
Update README.md
Browse files
README.md
CHANGED
@@ -2530,6 +2530,7 @@ The model is easy to use with **our customized** `sentence-transformer` library.
|
|
2530 |
|
2531 |
**************************** **Updates** ****************************
|
2532 |
|
|
|
2533 |
* 12/21: We released our [paper](https://arxiv.org/abs/2212.09741), [code](https://github.com/HKUNLP/instructor-embedding), [checkpoint](https://huggingface.co/hkunlp/instructor-base) and [project page](https://instructor-embedding.github.io/)! Check them out!
|
2534 |
|
2535 |
## Quick start
|
@@ -2537,20 +2538,17 @@ The model is easy to use with **our customized** `sentence-transformer` library.
|
|
2537 |
|
2538 |
## Installation
|
2539 |
```bash
|
2540 |
-
|
2541 |
-
cd instructor-embedding
|
2542 |
-
cd sentence-transformers
|
2543 |
-
pip install -e .
|
2544 |
```
|
2545 |
|
2546 |
## Compute your customized embeddings
|
2547 |
Then you can use the model like this to calculate domain-specific and task-aware embeddings:
|
2548 |
```python
|
2549 |
-
from
|
|
|
2550 |
sentence = "3D ActionSLAM: wearable person tracking in multi-floor environments"
|
2551 |
-
instruction = "Represent the Science title
|
2552 |
-
|
2553 |
-
embeddings = model.encode([[instruction,sentence,0]])
|
2554 |
print(embeddings)
|
2555 |
```
|
2556 |
|
@@ -2560,7 +2558,7 @@ print(embeddings)
|
|
2560 |
## Calculate embeddings for your customized texts
|
2561 |
If you want to calculate customized embeddings for specific sentences, you may follow the unified template to write instructions:
|
2562 |
|
2563 |
-
Represent the `domain` `text_type` for `task_objective
|
2564 |
* `domain` is optional, and it specifies the domain of the text, e.g., science, finance, medicine, etc.
|
2565 |
* `text_type` is required, and it specifies the encoding unit, e.g., sentence, document, paragraph, etc.
|
2566 |
* `task_objective` is optional, and it specifies the objective of embedding, e.g., retrieve a document, classify the sentence, etc.
|
@@ -2569,10 +2567,10 @@ If you want to calculate customized embeddings for specific sentences, you may f
|
|
2569 |
You can further use the model to compute similarities between two groups of sentences, with **customized embeddings**.
|
2570 |
```python
|
2571 |
from sklearn.metrics.pairwise import cosine_similarity
|
2572 |
-
sentences_a = [['Represent the Science sentence
|
2573 |
-
['Represent the Financial statement
|
2574 |
-
sentences_b = [['Represent the Science sentence
|
2575 |
-
['Represent the Financial statement
|
2576 |
embeddings_a = model.encode(sentences_a)
|
2577 |
embeddings_b = model.encode(sentences_b)
|
2578 |
similarities = cosine_similarity(embeddings_a,embeddings_b)
|
@@ -2584,10 +2582,10 @@ You can also use **customized embeddings** for information retrieval.
|
|
2584 |
```python
|
2585 |
import numpy as np
|
2586 |
from sklearn.metrics.pairwise import cosine_similarity
|
2587 |
-
query = [['Represent the Wikipedia question for retrieving supporting documents
|
2588 |
-
corpus = [['Represent the Wikipedia document for retrieval
|
2589 |
-
['Represent the Wikipedia document for retrieval
|
2590 |
-
['Represent the Wikipedia document for retrieval
|
2591 |
query_embeddings = model.encode(query)
|
2592 |
corpus_embeddings = model.encode(corpus)
|
2593 |
similarities = cosine_similarity(query_embeddings,corpus_embeddings)
|
@@ -2599,11 +2597,11 @@ print(retrieved_doc_id)
|
|
2599 |
Use **customized embeddings** for clustering texts in groups.
|
2600 |
```python
|
2601 |
import sklearn.cluster
|
2602 |
-
sentences = [['Represent the Medicine sentence for clustering
|
2603 |
-
['Represent the Medicine sentence for clustering
|
2604 |
-
['Represent the Medicine sentence for clustering
|
2605 |
-
['Represent the Medicine sentence for clustering
|
2606 |
-
['Represent the Medicine sentence for clustering
|
2607 |
embeddings = model.encode(sentences)
|
2608 |
clustering_model = sklearn.cluster.MiniBatchKMeans(n_clusters=2)
|
2609 |
clustering_model.fit(embeddings)
|
|
|
2530 |
|
2531 |
**************************** **Updates** ****************************
|
2532 |
|
2533 |
+
* 01/21: We released a new [checkpoint](https://huggingface.co/hkunlp/instructor-base) trained with hard negatives, which gives better performance.
|
2534 |
* 12/21: We released our [paper](https://arxiv.org/abs/2212.09741), [code](https://github.com/HKUNLP/instructor-embedding), [checkpoint](https://huggingface.co/hkunlp/instructor-base) and [project page](https://instructor-embedding.github.io/)! Check them out!
|
2535 |
|
2536 |
## Quick start
|
|
|
2538 |
|
2539 |
## Installation
|
2540 |
```bash
|
2541 |
+
pip install InstructorEmbedding
|
|
|
|
|
|
|
2542 |
```
|
2543 |
|
2544 |
## Compute your customized embeddings
|
2545 |
Then you can use the model like this to calculate domain-specific and task-aware embeddings:
|
2546 |
```python
|
2547 |
+
from InstructorEmbedding import INSTRUCTOR
|
2548 |
+
model = INSTRUCTOR('hkunlp/instructor-base')
|
2549 |
sentence = "3D ActionSLAM: wearable person tracking in multi-floor environments"
|
2550 |
+
instruction = "Represent the Science title:"
|
2551 |
+
embeddings = model.encode([[instruction,sentence]])
|
|
|
2552 |
print(embeddings)
|
2553 |
```
|
2554 |
|
|
|
2558 |
## Calculate embeddings for your customized texts
|
2559 |
If you want to calculate customized embeddings for specific sentences, you may follow the unified template to write instructions:
|
2560 |
|
2561 |
+
Represent the `domain` `text_type` for `task_objective`:
|
2562 |
* `domain` is optional, and it specifies the domain of the text, e.g., science, finance, medicine, etc.
|
2563 |
* `text_type` is required, and it specifies the encoding unit, e.g., sentence, document, paragraph, etc.
|
2564 |
* `task_objective` is optional, and it specifies the objective of embedding, e.g., retrieve a document, classify the sentence, etc.
|
|
|
2567 |
You can further use the model to compute similarities between two groups of sentences, with **customized embeddings**.
|
2568 |
```python
|
2569 |
from sklearn.metrics.pairwise import cosine_similarity
|
2570 |
+
sentences_a = [['Represent the Science sentence: ','Parton energy loss in QCD matter'],
|
2571 |
+
['Represent the Financial statement: ','The Federal Reserve on Wednesday raised its benchmark interest rate.']]
|
2572 |
+
sentences_b = [['Represent the Science sentence: ','The Chiral Phase Transition in Dissipative Dynamics'],
|
2573 |
+
['Represent the Financial statement: ','The funds rose less than 0.5 per cent on Friday']]
|
2574 |
embeddings_a = model.encode(sentences_a)
|
2575 |
embeddings_b = model.encode(sentences_b)
|
2576 |
similarities = cosine_similarity(embeddings_a,embeddings_b)
|
|
|
2582 |
```python
|
2583 |
import numpy as np
|
2584 |
from sklearn.metrics.pairwise import cosine_similarity
|
2585 |
+
query = [['Represent the Wikipedia question for retrieving supporting documents: ','where is the food stored in a yam plant']]
|
2586 |
+
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.'],
|
2587 |
+
['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"],
|
2588 |
+
['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.']]
|
2589 |
query_embeddings = model.encode(query)
|
2590 |
corpus_embeddings = model.encode(corpus)
|
2591 |
similarities = cosine_similarity(query_embeddings,corpus_embeddings)
|
|
|
2597 |
Use **customized embeddings** for clustering texts in groups.
|
2598 |
```python
|
2599 |
import sklearn.cluster
|
2600 |
+
sentences = [['Represent the Medicine sentence for clustering: ','Dynamical Scalar Degree of Freedom in Horava-Lifshitz Gravity'],
|
2601 |
+
['Represent the Medicine sentence for clustering: ','Comparison of Atmospheric Neutrino Flux Calculations at Low Energies'],
|
2602 |
+
['Represent the Medicine sentence for clustering: ','Fermion Bags in the Massive Gross-Neveu Model'],
|
2603 |
+
['Represent the Medicine sentence for clustering: ',"QCD corrections to Associated t-tbar-H production at the Tevatron"],
|
2604 |
+
['Represent the Medicine sentence for clustering: ','A New Analysis of the R Measurements: Resonance Parameters of the Higher, Vector States of Charmonium']]
|
2605 |
embeddings = model.encode(sentences)
|
2606 |
clustering_model = sklearn.cluster.MiniBatchKMeans(n_clusters=2)
|
2607 |
clustering_model.fit(embeddings)
|