Yaxin commited on
Commit
6fc6ad1
1 Parent(s): 8184369

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +30 -0
README.md CHANGED
@@ -34,6 +34,36 @@ tokenizer = AutoTokenizer.from_pretrained("Yaxin/roberta-large-ernie2-skep-en")
34
  model = AutoModel.from_pretrained("Yaxin/roberta-large-ernie2-skep-en")
35
  ```
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  ## Citation
38
 
39
  ```bibtex
 
34
  model = AutoModel.from_pretrained("Yaxin/roberta-large-ernie2-skep-en")
35
  ```
36
 
37
+ ```
38
+ #!/usr/bin/env python
39
+ #encoding: utf-8
40
+ import torch
41
+ from transformers import RobertaTokenizer, RobertaForMaskedLM
42
+
43
+ tokenizer = RobertaTokenizer.from_pretrained('Yaxin/roberta-large-ernie2-skep-en')
44
+
45
+ input_tx = "<s> He like play with student, so he became a <mask> after graduation </s>"
46
+ # input_tx = "<s> He is a <mask> and likes to get along with his students </s>"
47
+
48
+ tokenized_text = tokenizer.tokenize(input_tx)
49
+ indexed_tokens = tokenizer.convert_tokens_to_ids(tokenized_text)
50
+
51
+ tokens_tensor = torch.tensor([indexed_tokens])
52
+ segments_tensors = torch.tensor([[0] * len(tokenized_text)])
53
+
54
+ model = RobertaForMaskedLM.from_pretrained('Yaxin/roberta-large-ernie2-skep-en')
55
+ model.eval()
56
+
57
+ with torch.no_grad():
58
+ outputs = model(tokens_tensor, token_type_ids=segments_tensors)
59
+ predictions = outputs[0]
60
+
61
+ predicted_index = [torch.argmax(predictions[0, i]).item() for i in range(0, (len(tokenized_text) - 1))]
62
+ predicted_token = [tokenizer.convert_ids_to_tokens([predicted_index[x]])[0] for x in
63
+ range(1, (len(tokenized_text) - 1))]
64
+
65
+ print('Predicted token is:', predicted_token)
66
+ ```
67
  ## Citation
68
 
69
  ```bibtex