Create README.MD
Browse files
README.md
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
erfan226/persian-t5-paraphraser
|
2 |
+
|
3 |
+
This is a paraphrase model that works on the Persian language. It is based on [the monolingual T5 model](https://huggingface.co/Ahmad/parsT5-base)
|
4 |
+
language:
|
5 |
+
- {fa}
|
6 |
+
# Usage (Sentence-Transformers)
|
7 |
+
|
8 |
+
```python
|
9 |
+
|
10 |
+
pip install -U sentence-transformers
|
11 |
+
Then you can use the model like this:
|
12 |
+
|
13 |
+
from sentence_transformers import SentenceTransformer
|
14 |
+
sentences = ["This is an example sentence", "Each sentence is converted"]
|
15 |
+
|
16 |
+
model = SentenceTransformer('sentence-transformers/paraphrase-MiniLM-L6-v2')
|
17 |
+
embeddings = model.encode(sentences)
|
18 |
+
print(embeddings)
|
19 |
+
Usage (HuggingFace Transformers)
|
20 |
+
Without sentence-transformers, 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.
|
21 |
+
|
22 |
+
from transformers import AutoTokenizer, AutoModel
|
23 |
+
import torch
|
24 |
+
|
25 |
+
|
26 |
+
#Mean Pooling - Take attention mask into account for correct averaging
|
27 |
+
def mean_pooling(model_output, attention_mask):
|
28 |
+
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
|
29 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
30 |
+
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
31 |
+
|
32 |
+
|
33 |
+
# Sentences we want sentence embeddings for
|
34 |
+
sentences = ['This is an example sentence', 'Each sentence is converted']
|
35 |
+
|
36 |
+
# Load model from HuggingFace Hub
|
37 |
+
tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/paraphrase-MiniLM-L6-v2')
|
38 |
+
model = AutoModel.from_pretrained('sentence-transformers/paraphrase-MiniLM-L6-v2')
|
39 |
+
|
40 |
+
# Tokenize sentences
|
41 |
+
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
|
42 |
+
|
43 |
+
# Compute token embeddings
|
44 |
+
with torch.no_grad():
|
45 |
+
model_output = model(**encoded_input)
|
46 |
+
|
47 |
+
# Perform pooling. In this case, max pooling.
|
48 |
+
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
|
49 |
+
|
50 |
+
print("Sentence embeddings:")
|
51 |
+
print(sentence_embeddings)
|
52 |
+
|
53 |
+
```
|