junnyu commited on
Commit
998ca11
1 Parent(s): 05197ca

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +73 -0
README.md ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: zh
3
+ tags:
4
+ - roformer
5
+ - pytorch
6
+ - tf2.0
7
+ ---
8
+ ## 介绍
9
+ ### tf版本
10
+ https://github.com/ZhuiyiTechnology/roformer
11
+
12
+ ### pytorch版本+tf2.0版本
13
+ https://github.com/JunnYu/RoFormer_pytorch
14
+
15
+ ## pytorch使用
16
+ ```python
17
+ import torch
18
+ from transformers import RoFormerForMaskedLM, RoFormerTokenizer
19
+
20
+ text = "今天[MASK]很好,我[MASK]去公园玩。"
21
+ tokenizer = RoFormerTokenizer.from_pretrained("junnyu/roformer_chinese_char_small")
22
+ pt_model = RoFormerForMaskedLM.from_pretrained("junnyu/roformer_chinese_char_small")
23
+ pt_inputs = tokenizer(text, return_tensors="pt")
24
+ with torch.no_grad():
25
+ pt_outputs = pt_model(**pt_inputs).logits[0]
26
+ pt_outputs_sentence = "pytorch: "
27
+ for i, id in enumerate(tokenizer.encode(text)):
28
+ if id == tokenizer.mask_token_id:
29
+ tokens = tokenizer.convert_ids_to_tokens(pt_outputs[i].topk(k=5)[1])
30
+ pt_outputs_sentence += "[" + "||".join(tokens) + "]"
31
+ else:
32
+ pt_outputs_sentence += "".join(
33
+ tokenizer.convert_ids_to_tokens([id], skip_special_tokens=True))
34
+ print(pt_outputs_sentence)
35
+ # pytorch: 今天[也||都||又||还||我]很好,我[就||想||去||也||又]去公园玩。
36
+ ```
37
+
38
+ ## tensorflow2.0使用
39
+ ```python
40
+ import tensorflow as tf
41
+ from transformers import RoFormerTokenizer, TFRoFormerForMaskedLM
42
+ text = "今天[MASK]很好,我[MASK]去公园玩。"
43
+ tokenizer = RoFormerTokenizer.from_pretrained("junnyu/roformer_chinese_char_small")
44
+ tf_model = TFRoFormerForMaskedLM.from_pretrained("junnyu/roformer_chinese_char_small")
45
+ tf_inputs = tokenizer(text, return_tensors="tf")
46
+ tf_outputs = tf_model(**tf_inputs, training=False).logits[0]
47
+ tf_outputs_sentence = "tf2.0: "
48
+ for i, id in enumerate(tokenizer.encode(text)):
49
+ if id == tokenizer.mask_token_id:
50
+ tokens = tokenizer.convert_ids_to_tokens(
51
+ tf.math.top_k(tf_outputs[i], k=5)[1])
52
+ tf_outputs_sentence += "[" + "||".join(tokens) + "]"
53
+ else:
54
+ tf_outputs_sentence += "".join(
55
+ tokenizer.convert_ids_to_tokens([id], skip_special_tokens=True))
56
+ print(tf_outputs_sentence)
57
+ # tf2.0: 今天[也||都||又||还||我]很好,我[就||想||去||也||又]去公园玩。
58
+ ```
59
+
60
+ ## 引用
61
+
62
+ Bibtex:
63
+
64
+ ```tex
65
+ @misc{su2021roformer,
66
+ title={RoFormer: Enhanced Transformer with Rotary Position Embedding},
67
+ author={Jianlin Su and Yu Lu and Shengfeng Pan and Bo Wen and Yunfeng Liu},
68
+ year={2021},
69
+ eprint={2104.09864},
70
+ archivePrefix={arXiv},
71
+ primaryClass={cs.CL}
72
+ }
73
+ ```