Commit
·
ee1573a
1
Parent(s):
a374c77
Update README.md
Browse files
README.md
CHANGED
|
@@ -4,10 +4,10 @@ tags:
|
|
| 4 |
- sentence-transformers
|
| 5 |
- feature-extraction
|
| 6 |
- sentence-similarity
|
| 7 |
-
|
| 8 |
---
|
| 9 |
|
| 10 |
-
#
|
| 11 |
|
| 12 |
This is a [sentence-transformers](https://www.SBERT.net) model: It maps sentences & paragraphs to a 768 dimensional dense vector space and can be used for tasks like clustering or semantic search.
|
| 13 |
|
|
@@ -32,6 +32,41 @@ embeddings = model.encode(sentences)
|
|
| 32 |
print(embeddings)
|
| 33 |
```
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
## Evaluation Results
|
|
|
|
| 4 |
- sentence-transformers
|
| 5 |
- feature-extraction
|
| 6 |
- sentence-similarity
|
| 7 |
+
license: mit
|
| 8 |
---
|
| 9 |
|
| 10 |
+
# bhavyagiri/InLegal-Sbert
|
| 11 |
|
| 12 |
This is a [sentence-transformers](https://www.SBERT.net) model: It maps sentences & paragraphs to a 768 dimensional dense vector space and can be used for tasks like clustering or semantic search.
|
| 13 |
|
|
|
|
| 32 |
print(embeddings)
|
| 33 |
```
|
| 34 |
|
| 35 |
+
## Usage (HuggingFace Transformers)
|
| 36 |
+
Without [sentence-transformers](https://www.SBERT.net), you can use the model like this: First, you pass your input through the transformer model, then you have to apply the right pooling-operation on-top of the contextualized word embeddings.
|
| 37 |
+
|
| 38 |
+
```python
|
| 39 |
+
from transformers import AutoTokenizer, AutoModel
|
| 40 |
+
import torch
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
#Mean Pooling - Take attention mask into account for correct averaging
|
| 44 |
+
def mean_pooling(model_output, attention_mask):
|
| 45 |
+
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
|
| 46 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
| 47 |
+
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# Sentences we want sentence embeddings for
|
| 51 |
+
sentences = ['This is an example sentence', 'Each sentence is converted']
|
| 52 |
+
|
| 53 |
+
# Load model from HuggingFace Hub
|
| 54 |
+
tokenizer = AutoTokenizer.from_pretrained('bhavyagiri/InLegal-Sbert')
|
| 55 |
+
model = AutoModel.from_pretrained('bhavyagiri/InLegal-Sbert')
|
| 56 |
+
|
| 57 |
+
# Tokenize sentences
|
| 58 |
+
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
|
| 59 |
+
|
| 60 |
+
# Compute token embeddings
|
| 61 |
+
with torch.no_grad():
|
| 62 |
+
model_output = model(**encoded_input)
|
| 63 |
+
|
| 64 |
+
# Perform pooling. In this case, mean pooling.
|
| 65 |
+
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
|
| 66 |
+
|
| 67 |
+
print("Sentence embeddings:")
|
| 68 |
+
print(sentence_embeddings)
|
| 69 |
+
```
|
| 70 |
|
| 71 |
|
| 72 |
## Evaluation Results
|