Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
| 1 |
---
|
| 2 |
license: cc-by-3.0
|
|
|
|
|
|
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
license: cc-by-3.0
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
---
|
| 6 |
+
|
| 7 |
+
A model for mapping abstract sentence descriptions to sentences that fit the descriptions. Use ```load_finetuned_model``` to load the query and sentence encoder, and ```encode_batch()``` to encode a sentence with the model.
|
| 8 |
+
|
| 9 |
+
```python
|
| 10 |
+
|
| 11 |
+
from transformers import AutoTokenizer, AutoModel
|
| 12 |
+
import torch
|
| 13 |
+
|
| 14 |
+
def load_finetuned_model():
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
sentence_encoder = AutoModel.from_pretrained("ravfogs/description-transformer-mpnet-wiki-sentence-encoder")
|
| 18 |
+
query_encoder = AutoModel.from_pretrained("ravfogs/description-transformer-mpnet-wiki-query-encoder")
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained("ravfogs/description-transformer-mpnet-wiki-sentence-encoder")
|
| 20 |
+
|
| 21 |
+
return tokenizer, query_encoder, sentence_encoder
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def encode_batch(model, tokenizer, sentences, device):
|
| 25 |
+
input_ids = tokenizer(sentences, padding=True, max_length=512, truncation=True, return_tensors="pt",
|
| 26 |
+
add_special_tokens=True).to(device)
|
| 27 |
+
features = model(**input_ids)[0]
|
| 28 |
+
features = torch.sum(features[:,1:,:] * input_ids["attention_mask"][:,1:].unsqueeze(-1), dim=1) / torch.clamp(torch.sum(input_ids["attention_mask"][:,1:], dim=1, keepdims=True), min=1e-9)
|
| 29 |
+
return features
|
| 30 |
+
|
| 31 |
+
```
|