-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
337 lines (254 loc) · 9.64 KB
/
utils.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import torch
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import yaml
import torch.nn.functional as F
from spacy.tokenizer import Tokenizer
from spacy.lang.en import English
from spacy.tokens import Doc
from spacy.vocab import Vocab
from spacy.lang.en import English
from spacy.tokens import DocBin
nlp=English()
with open('/home/config.yaml') as f:
config=yaml.safe_load(f)
device=['cuda' if torch.cuda.is_available() is True else 'cpu'][0]
#-------------------------------------------------------------------------------------------------
"""
#df_train=pd.read_csv('/home/datasets/squad.csv')
lis=[]
for it,i in enumerate(range(df_train.shape[0])):
start_no=df_train['answer_start'][it]
res = list(df_train['context'][it])
res.insert(start_no, '@@@')
res = ''.join(res)
lis.append(res)
df_train['edited_context']=lis
"""
def tokenizer_2(h):
"""
tokenizer for train set - using spacy tokenizer
"""
a=[[],[],[]]
nlp = English()
# Create a blank Tokenizer with just the English vocab
x=['edited_context','question','answer_text']
for k,i in enumerate(x):
for _ in range(h.shape[0]):
words = nlp(str(h[i][_]).lower())
tokens = [word.text for word in words]
a[k].append(tokens)
return a[0],a[1],a[2]
#c_t,q_t,a_t=tokenizer_2(df_train)
def ans_start_real(p,q):
p_,q_=[],[]
for i_ in range(len(p)):
for k,i in enumerate(p[i_]):
if "@@@" in i:
p_.append(k)
q_.append(k+len(q[i_])-1)
else:
pass
return p_,q_
"""
final,end=ans_start_real(c_t,a_t)
for k,i in enumerate(final):
if i>399:
final[k]=398
else:
pass
for k,i in enumerate(end):
if i>399:
end[k]=398
else:
pass
"""
#st=pickle.load(open('/home/pickle_objects/start_index.pkl','rb'))
#en=pickle.load(open('/home/pickle_objects/end_index.pkl','rb'))
#-------------------------------------------------------------------------------------------------
def spacytoken_to_string (context,start,end):
"""
returns a list of answers used to slice exact answer form question, takes in spacy tokenized
text object , answer start/end index.
"""
h=[]
for i in range(len(context)):
stri=''.join([token.text_with_ws for token in context[i][start[i]:end[i]+1]]).rstrip()
h.append(stri)
return h
#-------------------------------------------------------------------------------------------------
def answer_doc_list(p,answers_count):
#docs = list(p.get_docs(nlp.vocab))
c=0
ans_words=[]
for k,i in enumerate(answers_count):
ans_words.append(p[c:c+i])
c=i
return ans_words
#-------------------------------------------------------------------------------------------------
# USED FOR FINDING ANSWER START AND END INDEX FROM CONTEXT AND ANSWERS-
def sublist(x,y):
"""
helper function to compute answer start/end index in passage by performing match on each
sublist of passage.
"""
l_=len(y)
result=0,0
for ind in (i for i,e in enumerate(x) if e==y[0]):
if x[ind:ind+l_]==y:
result = ind,ind+l_-1
return result
def answer_processor(c,a):
"""
helper function to compute answer start/end index in passage
"""
start_vector=[]
end_vector=[]
for i in range(c.size(0)):
x,y=c[i].tolist(),a[i].tolist()
c_=[i for i in x if i!=0] # 0 is the int value of <pad> in vocab - removes pad values
a_=[i for i in y if i!=0]
start_index,end_index=sublist(c_,a_)
start_vector.append(start_index)
end_vector.append(end_index)
return start_vector,end_vector # shape- N same fir both start/end sets
#-------------------------------------------------------------------------------------------------
def get_answer_index(c_pad,ans_pad):
"""
returns answer start & end index in passage as tuple of torch tensors,
taking padded context and answer torch tensors as inputs
"""
holder=torch.zeros(c_pad.size(0),2)
s,e=answer_processor(c_pad,ans_pad)
a_start=torch.from_numpy(np.array(s)).unsqueeze(1)
a_end=torch.from_numpy(np.array(e)).unsqueeze(1)
#holder=torch.cat((a_start,a_end),dim=1)
return a_start,a_end
#-------------------------------------------------------------------------------------------------
def seq_length(p):
s_=[]
for k,i in enumerate(p):
s_.append(len(i[i!=0]))
return s_
#-------------------------------------------------------------------------------------------------
def last_saved_model(epoch,model,optimizer,train_losses
,val_losses,f1_scores,seed_value,batches_trained):
if True:
torch.save({'epoch': epoch, # FIX : EPOCH+1
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'train_loss_data':train_losses,
'val_loss_data': val_losses,
'f1_score_data':f1_scores,
'seed_value':seed_value,
'batches_trained':batches_trained},config['last_model_path'])
#-------------------------------------------------------------------------------------------------
def good_saved_model(epoch,model,optimizer,train_losses
,val_losses,f1_scores,best_f1,seed_value,batches_trained):
if True:
torch.save({'epoch': epoch, # FIX : EPOCH+1
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'train_loss_data':train_losses,
'val_loss_data': val_losses,
'f1_score_data':f1_scores,
'best_f1':best_f1,
'seed_value':seed_value,
'batches_trained':batches_trained},config['good_model_path'])
#-------------------------------------------------------------------------------------------------
# model checkpointing
class SaveBestModel:
"""
Class to save the best model while training. If the current iteration's
validation loss is less than the previous lowest loss, then save the
model state.
"""
def __init__(self,best_loss=float('inf')):
self.best_loss = best_loss
def __call__(self, current_loss,epoch,model, optimizer,
train_losses,val_losses,f1_scores,seed_value,batches_trained):
if current_loss < self.best_loss:
self.best_loss = current_loss
torch.save({'epoch': epoch, # FIX : EPOCH+1
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'train_loss_data':train_losses,
'val_loss_data': val_losses,
'f1_score_data':f1_scores,
'seed_value':seed_value,
'batches_trained':batches_trained},config['save_best_model_path']) # use config
#-------------------------------------------------------------------------------------------------
def load_checkpoint(model,optimizer):
checkpoint=torch.load(config['checkpoint_path'])
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
train_loss_=checkpoint['train_loss_data']
val_loss_=checkpoint['val_loss_data']
f1_loss_=checkpoint['f1_score_data']
ep=checkpoint['epoch']
seed_value=checkpoint['seed_value']
batches_completed=checkpoint['batches_trained']
return model,optimizer,train_loss_,val_loss_,f1_loss_,ep,seed_value,batches_completed
#-------------------------------------------------------------------------------------------------
class Early_Stopping:
"""
Performs early stopping if validation loss is above a certain threshold(min delta) from least
loss value for certain number of iterations(patience).
"""
def __init__(self, patience, min_delta):
self.patience=patience
self.min_delta=min_delta
self.best_loss= float('inf')
self.counter=0
def __call__(self,val_loss):
if val_loss< self.best_loss:
self.best_loss=val_loss
self.counter=0
elif val_loss> self.best_loss+self.min_delta:
self.counter+=1
if self.counter>self.patience:
return True
return False
#-------------------------------------------------------------------------------------------------
def save_graphs(train_loss,val_loss,f1_score):
plt.style.use('dark_background')
plt.rcParams["figure.figsize"] = [10, 7]
plt.figure(1)
plt.plot(train_loss,label='training loss',color='red')
plt.xlabel('iterations')
plt.ylabel('train loss')
plt.title('training loss')
plt.savefig('/home/training_data/train_loss.png')
plt.figure(2)
plt.plot(val_loss,label='validation loss',color='blue')
plt.xlabel('iterations')
plt.ylabel('val loss')
plt.title('validation loss')
plt.savefig('/home/training_data/val_loss.png')
plt.figure(3)
plt.plot(f1_score,label='F1-scores')
plt.xlabel('iterations')
plt.ylabel('f1-score')
plt.title('f1-scores/epoch')
plt.savefig('/home/training_data/f1_score.png')
#-------------------------------------------------------------------------------------------------
def exponential_mask(p,seq_le,partial=False):
"""
takes in attention with logits , applies softmax over the sequence
length & sets the probabilites of pad values to 0
"""
vect=torch.zeros(p.size(0),400,2,device=device)
for j in range(p.size(0)):
l_=seq_le[j]
vect[j][:l_,:]=p[j][:l_,:]
if partial is True:
for i in range(l_,400):
vect[j][i,:]=torch.tensor([-2,-2],device=device)
vect[j][:,0],vect[j][:,1]=F.softmax(vect[j][:,0],dim=0),F.softmax(vect[j][:,1],dim=0)
else:
vect[j][:l_,0],vect[j][:l_,1]=F.softmax(p[j][:l_,0],dim=0),F.softmax(p[j][:l_,1],dim=0)
return vect
#-------------------------------------------------------------------------------------------------
if __name__=='__main__':
print(sublist.__doc__)