-
Notifications
You must be signed in to change notification settings - Fork 1
/
compute_answers.py
211 lines (156 loc) · 7.27 KB
/
compute_answers.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#compute_answers.py
import sys
import os
import random
import math
import numpy as np
import tensorflow as tf
import json
import pandas as pd
import re
import string
os.system("pip install -q \"transformers==4.3\"")
import transformers
from transformers import TFBertModel, TFRobertaModel, TFElectraModel, TFLongformerModel
from transformers import AutoTokenizer
pd.set_option('display.max_colwidth', -1)
def build_model(bert_hf_layer):
input_word_ids = tf.keras.layers.Input(shape=(max_seq_length,), dtype=tf.int32, name='input_word_ids')
input_mask = tf.keras.layers.Input(shape=(max_seq_length,), dtype=tf.int32, name='input_mask')
input_type_ids = tf.keras.layers.Input(shape=(max_seq_length,), dtype=tf.int32, name='input_type_ids')
#pooled_output, sequence_output = bert_layer([input_word_ids, input_mask, input_type_ids])
#HUGGINGFACE 🤗🤗🤗🤗🤗🤗🤗🤗🤗🤗🤗🤗🤗
sequence_output = bert_hf_layer(input_ids=input_word_ids, attention_mask=input_mask,
token_type_ids=input_type_ids).last_hidden_state
#do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
start_logits = tf.keras.layers.Dense(1, name="start_logit", use_bias=False)(sequence_output)
start_logits = tf.keras.layers.Flatten(name="flatten_start")(start_logits)
end_logits = tf.keras.layers.Dense(1, name="end_logit", use_bias=False)(sequence_output)
end_logits = tf.keras.layers.Flatten(name="flatten_end")(end_logits)
start_probs = tf.keras.layers.Activation(tf.keras.activations.softmax, name="softmax_start")(start_logits)
end_probs = tf.keras.layers.Activation(tf.keras.activations.softmax, name="softmax_end")(end_logits)
model = tf.keras.Model(inputs=[input_word_ids, input_mask, input_type_ids],
outputs=[start_probs, end_probs],
name="BERT_QA")
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)
optimizer = tf.keras.optimizers.Adam(lr=1e-5, beta_1=0.9, beta_2=0.98, epsilon=1e-9)
model.summary(line_length=150)
return model
try:
path_to_json_file = sys.argv[1]
except:
print("the path to the json file is needed as argument of this script")
try:
huggingface_pretrained_model = sys.argv[2]
except:
huggingface_pretrained_model = "roberta-base"
# fix random seeds
seed_value = 42
os.environ['PYTHONHASHSEED'] = str(seed_value)
random.seed(seed_value)
np.random.seed(seed_value)
tf.compat.v1.set_random_seed(seed_value)
session_conf = tf.compat.v1.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)
sess = tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph(), config=session_conf)
tf.compat.v1.keras.backend.set_session(sess)
# BERT params
# hf model and input sequence max length
hf_Models = {"bert-base-uncased": (TFBertModel, 512, "https://api.wandb.ai/files/buio/SQUAD/2a1u1bxu/model-best.h5"),
"roberta-base" : (TFRobertaModel, 512, "https://api.wandb.ai/files/buio/SQUAD/184b7gum/model-best.h5"),
"google/electra-base-discriminator" : (TFElectraModel, 512, "https://api.wandb.ai/files/buio/SQUAD/2rab6oli/model-best.h5"),
"allenai/longformer-base-4096" : (TFLongformerModel, 1024, "not_yet_trained")}
TFHFModel, max_seq_length, weights_path = hf_Models[huggingface_pretrained_model]
# actual bert model
bert_hf_layer = TFHFModel.from_pretrained(huggingface_pretrained_model)
# actual tokenizer
tokenizer = AutoTokenizer.from_pretrained(huggingface_pretrained_model)
# load bert weights form the weights and biases platform
os.system(f"wget {weights_path}")
model = build_model(bert_hf_layer)
model.load_weights("model-best.h5")
# preprocess dev set
with open(path_to_json_file, "r") as f:
json_file = json.load(f)
data = json_file["data"]
rows = []
for document in data:
for par in document['paragraphs']:
for qas in par['qas']:
rows.append({
'id' : qas['id'],
'title': document["title"],
'passage': par['context'],
'question' : qas['question']
})
df_dev = pd.DataFrame(rows)
def preprocess_bert(text):
tokenized_text = tokenizer(list(text), return_offsets_mapping=True)
rows_out = [{'input_ids': tokenized_text.input_ids[i],
'offsets': tokenized_text.offset_mapping[i]} for i in range(len(text))]
return rows_out
def labeling(df):
skip = []
input_word_ids = []
input_type_ids = []
input_mask = []
context_token_to_char = []
for id in df.index:
tokenized_context = df.loc[id]['passage']
tokenized_question = df.loc[id]['question']
# create inputs as usual
input_ids = tokenized_context['input_ids'] + tokenized_question['input_ids'][1:] #removing CLS from the beginning of the question
token_type_ids = [0] * len(tokenized_context['input_ids']) + [1] * len(tokenized_question['input_ids'][1:])
attention_mask = [1] * len(input_ids)
padding_length = max_seq_length - len(input_ids)
# add padding if necessary
if padding_length > 0:
input_ids = input_ids + ([0] * padding_length)
attention_mask = attention_mask + ([0] * padding_length)
token_type_ids = token_type_ids + ([0] * padding_length)
elif padding_length < 0:
skip.append(id)
continue
input_word_ids.append(np.array(input_ids))
input_type_ids.append(np.array(token_type_ids))
input_mask.append(np.array(attention_mask))
context_token_to_char.append(np.array(tokenized_context["offsets"]))
df = df.drop(skip)
df['input_word_ids'] = input_word_ids
df['input_type_ids'] = input_type_ids
df['input_mask'] = input_mask
df['context_token_to_char'] = context_token_to_char
return df, skip
# pre-process passage and question text
df_dev = df_dev.set_index('id')
df_bert_dev = df_dev.copy()
df_bert_dev['passage'] = preprocess_bert(df_dev['passage'])
df_bert_dev['question'] = preprocess_bert(df_dev['question'])
df_bert_dev, skipped = labeling(df_bert_dev)
df_bert_dev.head(1)
x_test = [np.stack(df_bert_dev["input_word_ids"]),
np.stack(df_bert_dev["input_mask"]),
np.stack(df_bert_dev["input_type_ids"])]
predictions = model.predict(x_test, verbose=1)
# save predictions to file for script evaluation
num_samples = len(predictions[0])
start, end = list(np.argmax(predictions, axis=-1).squeeze())
lines_c = 0
with open("predictions.txt","w") as out:
out.write("{")
for id in skipped:
out.write(f'''"{id}": "error: sequence too long",\n''')
for ans_idx in range(num_samples):
# no answer
if end[ans_idx] == 0:
if ans_idx == num_samples-1:
out.write(f'''"{df_bert_dev.index[ans_idx]}": ""''')
else:
out.write(f'''"{df_bert_dev.index[ans_idx]}": "",\n''')
# extract answer text
else:
predicted_ans = tokenizer.decode(df_bert_dev.iloc[ans_idx]['passage']["input_ids"][start[ans_idx] : end[ans_idx]+1]).replace("\n"," ")
if ans_idx == num_samples-1:
out.write(f'''"{df_bert_dev.index[ans_idx]}": "{predicted_ans.replace('"',"")}"''')
else:
out.write(f'''"{df_bert_dev.index[ans_idx]}": "{predicted_ans.replace('"',"")}",\n''')
out.write("}")