junnyu commited on
Commit
c445c48
1 Parent(s): 0935f40

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +78 -0
README.md ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: zh
3
+ tags:
4
+ - roformer-v2
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 & tf2.0使用
16
+ ```python
17
+ import torch
18
+ import tensorflow as tf
19
+ from transformers import BertTokenizer
20
+ from roformer import RoFormerForMaskedLM, TFRoFormerForMaskedLM
21
+
22
+ text = "今天[MASK]很好,我[MASK]去公园玩。"
23
+ tokenizer = BertTokenizer.from_pretrained("junnyu/roformer_v2_chinese_char_base")
24
+ pt_model = RoFormerForMaskedLM.from_pretrained("junnyu/roformer_v2_chinese_char_base")
25
+ tf_model = TFRoFormerForMaskedLM.from_pretrained(
26
+ "junnyu/roformer_v2_chinese_char_base", from_pt=True
27
+ )
28
+ pt_inputs = tokenizer(text, return_tensors="pt")
29
+ tf_inputs = tokenizer(text, return_tensors="tf")
30
+ # pytorch
31
+ with torch.no_grad():
32
+ pt_outputs = pt_model(**pt_inputs).logits[0]
33
+ pt_outputs_sentence = "pytorch: "
34
+ for i, id in enumerate(tokenizer.encode(text)):
35
+ if id == tokenizer.mask_token_id:
36
+ tokens = tokenizer.convert_ids_to_tokens(pt_outputs[i].topk(k=5)[1])
37
+ pt_outputs_sentence += "[" + "||".join(tokens) + "]"
38
+ else:
39
+ pt_outputs_sentence += "".join(
40
+ tokenizer.convert_ids_to_tokens([id], skip_special_tokens=True)
41
+ )
42
+ print(pt_outputs_sentence)
43
+ # tf
44
+ tf_outputs = tf_model(**tf_inputs, training=False).logits[0]
45
+ tf_outputs_sentence = "tf: "
46
+ for i, id in enumerate(tokenizer.encode(text)):
47
+ if id == tokenizer.mask_token_id:
48
+ tokens = tokenizer.convert_ids_to_tokens(tf.math.top_k(tf_outputs[i], k=5)[1])
49
+ tf_outputs_sentence += "[" + "||".join(tokens) + "]"
50
+ else:
51
+ tf_outputs_sentence += "".join(
52
+ tokenizer.convert_ids_to_tokens([id], skip_special_tokens=True)
53
+ )
54
+ print(tf_outputs_sentence)
55
+ # small
56
+ # pytorch: 今天[的||,||是||很||也]很好,我[要||会||是||想||在]去公园玩。
57
+ # tf: 今天[的||,||是||很||也]很好,我[要||会||是||想||在]去公园玩。
58
+ # base
59
+ # pytorch: 今天[我||天||晴||园||玩]很好,我[想||要||会||就||带]去公园玩。
60
+ # tf: 今天[我||天||晴||园||玩]很好,我[想||要||会||就||带]去公园玩。
61
+ # large
62
+ # pytorch: 今天[天||气||我||空||阳]很好,我[又||想||会||就||爱]去公园玩。
63
+ # tf: 今天[天||气||我||空||阳]很好,我[又||想||会||就||爱]去公园玩。
64
+ ```
65
+
66
+ ```
67
+ ## 引用
68
+ Bibtex:
69
+ ```tex
70
+ @misc{su2021roformer,
71
+ title={RoFormer: Enhanced Transformer with Rotary Position Embedding},
72
+ author={Jianlin Su and Yu Lu and Shengfeng Pan and Bo Wen and Yunfeng Liu},
73
+ year={2021},
74
+ eprint={2104.09864},
75
+ archivePrefix={arXiv},
76
+ primaryClass={cs.CL}
77
+ }
78
+ ```