qinluo commited on
Commit
7ee236e
1 Parent(s): 9e1c1f2

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +32 -1
README.md CHANGED
@@ -17,7 +17,7 @@ pytorch 模型见: https://github.com/JunnYu/WoBERT_pytorch
17
  pip install git+https://github.com/JunnYu/WoBERT_pytorch.git
18
  ```
19
 
20
- ## 使用
21
  ```python
22
  from transformers import TFBertForMaskedLM as WoBertForMaskedLM
23
  from wobert import WoBertTokenizer
@@ -46,6 +46,37 @@ print(outputs_sentence)
46
  # 今天[天气|阳光|天|心情|空气]很好,我[想|要|打算|准备|就]去公园玩。
47
 
48
  ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  ## 引用
50
  Bibtex:
51
  ```tex
 
17
  pip install git+https://github.com/JunnYu/WoBERT_pytorch.git
18
  ```
19
 
20
+ ## TF Example
21
  ```python
22
  from transformers import TFBertForMaskedLM as WoBertForMaskedLM
23
  from wobert import WoBertTokenizer
 
46
  # 今天[天气|阳光|天|心情|空气]很好,我[想|要|打算|准备|就]去公园玩。
47
 
48
  ```
49
+
50
+ ## Pytorch Example
51
+ ```python
52
+ from transformers import BertForMaskedLM as WoBertForMaskedLM
53
+ from wobert import WoBertTokenizer
54
+
55
+
56
+ pretrained_model_or_path = 'qinluo/wobert-chinese-plus'
57
+
58
+ tokenizer = WoBertTokenizer.from_pretrained(pretrained_model_or_path)
59
+ model = WoBertForMaskedLM.from_pretrained(pretrained_model_or_path)
60
+
61
+ text = '今天[MASK]很好,我[MASK]去公园玩。'
62
+ inputs = tokenizer(text, return_tensors='pt')
63
+ outputs = model(**inputs).logits[0]
64
+
65
+ outputs_sentence = ''
66
+ for i, id in enumerate(tokenizer.encode(text)):
67
+ if id == tokenizer.mask_token_id:
68
+ tokens = tokenizer.convert_ids_to_tokens(outputs[i].topk(k=5)[1])
69
+ outputs_sentence += '[' + '|'.join(tokens) + ']'
70
+ else:
71
+ outputs_sentence += ''.join(tokenizer.convert_ids_to_tokens([id], skip_special_tokens=True))
72
+
73
+ print(outputs_sentence)
74
+
75
+ # 今天[天气|阳光|天|心情|空气]很好,我[想|要|打算|准备|就]去公园玩。
76
+
77
+ ```
78
+
79
+
80
  ## 引用
81
  Bibtex:
82
  ```tex