-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbert.py
83 lines (63 loc) · 3.08 KB
/
bert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Adapted and packaged from http://mayhewsw.github.io/2019/01/16/can-bert-generate-text/
# Copyright Mayhew Stephen – 2019
# Copyright 2019 - UMONS
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You should have received a copy of the Apache License Version 2.0 along with this program.
# If not, see
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import torch
import argparse
from pytorch_pretrained_bert import BertTokenizer, BertForMaskedLM
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--model_name_or_path', type=str, default='bert-base-uncased',
help='pretrained model name or path to local checkpoint')
parser.add_argument("--text", type=str, default="This is the story of a little dog named Boo.",
help='The sentence to use for word suggestion')
parser.add_argument("--mask", type=str, default="dog",
help='The word to mask for suggestion')
args = parser.parse_args()
bert_generation(args.model_name_or_path, args.text, args.mask)
def bert_generation(model_name_or_path, text, mask):
tokenizer = BertTokenizer.from_pretrained(model_name_or_path)
model = BertForMaskedLM.from_pretrained(model_name_or_path)
tokenized_text = tokenizer.tokenize(text)
# Mask a token that we will try to predict back with `BertForMaskedLM`
try:
masked_index = tokenized_text.index(mask)
except ValueError:
print("Error : Masked word doesn't appear in sentence.")
return -1
tokenized_text[masked_index] = '[MASK]'
# Convert token to vocabulary indices
indexed_tokens = tokenizer.convert_tokens_to_ids(tokenized_text)
# Define sentence A and B indices associated to 1st and 2nd sentences (see paper)
segments_ids = [0] * len(tokenized_text)
# Convert inputs to PyTorch tensors
tokens_tensor = torch.tensor([indexed_tokens])
segments_tensors = torch.tensor([segments_ids])
model.eval()
# Predict all tokens
predictions = model(tokens_tensor, segments_tensors)
predicted_index = torch.argmax(predictions[0, masked_index]).item()
predicted_token = tokenizer.convert_ids_to_tokens([predicted_index])
print("Original:", text)
print("Masked:", " ".join(tokenized_text))
print("Predicted token:", predicted_token)
print("Other options:")
# just curious about what the next few options look like.
for i in range(10):
predictions[0, masked_index, predicted_index] = -11100000
predicted_index = torch.argmax(predictions[0, masked_index]).item()
predicted_token = tokenizer.convert_ids_to_tokens([predicted_index])
print(predicted_token)
if __name__ == '__main__':
main()