Update README.md
Browse files
README.md
CHANGED
@@ -7,9 +7,72 @@ tags:
|
|
7 |
widget:
|
8 |
- text: "今天[MASK]很好,我想去公园玩!"
|
9 |
---
|
10 |
-
|
11 |
-
|
12 |
-
https://github.com/ZhuiyiTechnology/roformer
|
13 |
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
widget:
|
8 |
- text: "今天[MASK]很好,我想去公园玩!"
|
9 |
---
|
10 |
+
# 下载这个roformer代码
|
11 |
+
- https://github.com/JunnYu/RoFormer_pytorch/files/8402725/roformer.zip
|
|
|
12 |
|
13 |
+
# 使用
|
14 |
+
```python
|
15 |
+
import torch
|
16 |
+
import numpy as np
|
17 |
+
from roformer import RoFormerForCausalLM, RoFormerConfig
|
18 |
+
from transformers import BertTokenizer
|
19 |
+
|
20 |
+
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
21 |
+
pretrained_model = "junnyu/roformer_chinese_sim_char_base"
|
22 |
+
tokenizer = RoFormerTokenizer.from_pretrained(pretrained_model)
|
23 |
+
config = RoFormerConfig.from_pretrained(pretrained_model)
|
24 |
+
config.is_decoder = True
|
25 |
+
config.eos_token_id = tokenizer.sep_token_id
|
26 |
+
config.pooler_activation = "linear"
|
27 |
+
model = RoFormerForCausalLM.from_pretrained(pretrained_model, config=config)
|
28 |
+
model.to(device)
|
29 |
+
model.eval()
|
30 |
+
|
31 |
+
def gen_synonyms(text, n=100, k=20):
|
32 |
+
''''含义: 产生sent的n个相似句,然后返回最相似的k个。
|
33 |
+
做法:用seq2seq生成,并用encoder算相似度并排序。
|
34 |
+
'''
|
35 |
+
# 寻找所有相似的句子
|
36 |
+
r = []
|
37 |
+
inputs1 = tokenizer(text, return_tensors="pt")
|
38 |
+
for _ in range(n):
|
39 |
+
inputs1.to(device)
|
40 |
+
output = tokenizer.batch_decode(model.generate(**inputs1, top_p=0.95, do_sample=True, max_length=128), skip_special_tokens=True)[0].replace(" ","").replace(text, "") # 去除空格,去除原始text文本。
|
41 |
+
r.append(output)
|
42 |
+
|
43 |
+
# 对相似的句子进行排序
|
44 |
+
r = [i for i in set(r) if i != text and len(i) > 0]
|
45 |
+
r = [text] + r
|
46 |
+
inputs2 = tokenizer(r, padding=True, return_tensors="pt")
|
47 |
+
with torch.no_grad():
|
48 |
+
inputs2.to(device)
|
49 |
+
outputs = model(**inputs2)
|
50 |
+
Z = outputs.pooler_output.cpu().numpy()
|
51 |
+
Z /= (Z**2).sum(axis=1, keepdims=True)**0.5
|
52 |
+
argsort = np.dot(Z[1:], -Z[0]).argsort()
|
53 |
+
|
54 |
+
return [r[i + 1] for i in argsort[:k]]
|
55 |
+
|
56 |
+
out = gen_synonyms("广州和深圳哪个好?")
|
57 |
+
print(out)
|
58 |
+
# ['深圳和广州哪个好?',
|
59 |
+
# '广州和深圳哪个好',
|
60 |
+
# '深圳和广州哪个好',
|
61 |
+
# '深圳和广州哪个比较好。',
|
62 |
+
# '深圳和广州哪个最好?',
|
63 |
+
# '深圳和广州哪个比较好',
|
64 |
+
# '广州和深圳那个比较好',
|
65 |
+
# '深圳和广州哪个更好?',
|
66 |
+
# '深圳与广州哪个好',
|
67 |
+
# '深圳和广州,哪个比较好',
|
68 |
+
# '广州与深圳比较哪个好',
|
69 |
+
# '深圳和广州哪里比较好',
|
70 |
+
# '深圳还是广州比较好?',
|
71 |
+
# '广州和深圳哪个地方好一些?',
|
72 |
+
# '广州好还是深圳好?',
|
73 |
+
# '广州好还是深圳好呢?',
|
74 |
+
# '广州与深圳哪个地方好点?',
|
75 |
+
# '深圳好还是广州好',
|
76 |
+
# '广州好还是深圳好',
|
77 |
+
# '广州和深圳哪个城市好?']
|
78 |
+
```
|