-
Notifications
You must be signed in to change notification settings - Fork 2
/
public_python_engine.py
130 lines (105 loc) · 4.73 KB
/
public_python_engine.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
import utils
import torch
from tqdm import tqdm
import torch.nn as nn
import numpy as np
def loss_fn(o1,o2,t1,t2):
l = nn.CrossEntropyLoss()
loss_s = l(o1,t1)
loss_e = l(o2,t2)
return loss_s+loss_e
def train_fn(data_loader,model,optimizer,device,scheduler):
model.train()
losses = utils.AverageMeter()
jaccard = utils.AverageMeter()
tk0 = tqdm(data_loader,total = len(data_loader))
for bi,d in enumerate(tk0):
ids = d['ids']
offsets = d['offsets']
orig_tweet = d['orig_tweet']
orig_selected = d['orig_selected_text']
token_type_ids = d['token_type_ids']
sentiments = d['orig_sentiment']
mask = d['mask']
target_start = d['targets_start']
target_end = d['targets_end']
ids = ids.to(device,dtype = torch.long)
token_type_ids = token_type_ids.to(device,dtype = torch.long)
mask = mask.to(device,dtype = torch.long)
target_start = target_start.to(device,dtype = torch.long)
target_end = target_end.to(device,dtype = torch.long)
model.zero_grad()
out_start,out_end = model(
ids,
mask,
token_type_ids
)
loss = loss_fn(out_start,out_end,target_start,target_end)
loss.backward()
optimizer.step()
scheduler.step()
out_start = torch.softmax(out_start,dim = 1).cpu().detach().numpy()
out_end = torch.softmax(out_end,dim = 1).cpu().detach().numpy()
jac_scores = []
# print(sentiment)
# print(offsets,len(offsets),type(offsets))
for j,tweet in enumerate(orig_tweet):
# print(j)
offset = offsets[j]
selected_text = orig_selected[j]
sentiment = sentiments[j]
idx_start = np.argmax(out_start[j,:])
idx_end = np.argmax(out_end[j,:])
_,jac = utils.calculate_jaccard(tweet,offset,selected_text,
idx_start,idx_end,sentiment)
jac_scores.append(jac)
jaccard.update(np.mean(jac_scores),ids.size(0))
losses.update(loss.item(),ids.size(0))
tk0.set_postfix(loss = losses.avg,jaccard = jaccard.avg)
def eval_fn(data_loader,model,device):
model.eval()
losses = utils.AverageMeter()
jaccard = utils.AverageMeter()
with torch.no_grad():
losses = utils.AverageMeter()
jaccard = utils.AverageMeter()
tk0 = tqdm(data_loader,total = len(data_loader))
for bi,d in enumerate(tk0):
ids = d['ids']
offsets = d['offsets']
orig_selected = d['orig_selected_text']
token_type_ids = d['token_type_ids']
sentiments = d['orig_sentiment']
mask = d['mask']
target_start = d['targets_start']
target_end = d['targets_end']
orig_tweet = d['orig_tweet']
ids = ids.to(device,dtype = torch.long)
token_type_ids = token_type_ids.to(device,dtype = torch.long)
mask = mask.to(device,dtype = torch.long)
target_start = target_start.to(device,dtype = torch.long)
target_end = target_end.to(device,dtype = torch.long)
out_start,out_end = model(
ids,
mask,
token_type_ids
)
loss = loss_fn(out_start,out_end,target_start,target_end)
out_start = torch.softmax(out_start,dim = 1).cpu().detach().numpy()
out_end = torch.softmax(out_end,dim = 1).cpu().detach().numpy()
# print(out_start.shape,out_end.shape)
jac_scores = []
# print(offsets,len(offsets),type(offsets))
for j,tweet in enumerate(orig_tweet):
offset = offsets[j]
selected_text = orig_selected[j]
idx_start = np.argmax(out_start[j,:])
sentiment = sentiments[j]
idx_end = np.argmax(out_end[j,:])
_,jac = utils.calculate_jaccard(tweet,offset,selected_text,
idx_start,idx_end,sentiment)
jac_scores.append(jac)
jaccard.update(np.mean(jac_scores),ids.size(0))
losses.update(loss.item(),ids.size(0))
tk0.set_postfix(loss = losses.avg,jaccard = jaccard.avg)
return np.mean(jac_scores)