-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
259 lines (176 loc) · 6.96 KB
/
train.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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import math, copy, time
import matplotlib.pyplot as plt
import pickle
import sqlite3
import sys
from torch.autograd import Variable
from random import shuffle
from tqdm import tqdm
import model
def check_accuracy(net, dev_path):
dataint = open(dev_path, 'rb')
pos, neg, tie = 0,0,0
while dataint:
try:
pronoun_data = pickle.load(dataint)
ref_sen_len = len(pronoun_data['reference_sentence'])
ref_con_len = len(pronoun_data['reference_context'])
if ref_con_len - ref_sen_len <=0: #skip if there is no context; can comment out
continue
pos_context_input = [pronoun_data['reference_context']]
pos_sentence_input = [] #make context input blank and use this if running a no context version
##Getting pronouns only from the last sentence but adjusting indices for the full context
pos_context_pids = get_pronoun_idx(pronoun_data['reference_context'], pronoun_list)
pos_sentenceonly_pids = get_pronoun_idx(pronoun_data['reference_sentence'], pronoun_list) #use this for sentence pronoun-ids if there is no context
pos_sentence_pids = pos_context_pids[-len(pos_sentenceonly_pids):]
pos_pronoun_input = [pos_sentence_pids]
#common reference context
neg_context = pronoun_data['reference_context'][:-ref_sen_len]
neg_context.extend(pronoun_data['noisy_sentence'])
neg_context_pids = get_pronoun_idx(neg_context, pronoun_list)
neg_sentenceonly_pids = get_pronoun_idx(pronoun_data['noisy_sentence'], pronoun_list)
neg_sentence_pids = neg_context_pids[-len(neg_sentenceonly_pids):]
neg_context_input = [neg_context]
neg_sentence_input = []
neg_pronoun_input = [neg_sentence_pids]
try:
# order of calling does not matter
neg_score = net.forward(neg_context_input, neg_sentence_input, neg_pronoun_input, 1)
pos_score = net.forward(pos_context_input, pos_sentence_input, pos_pronoun_input, 1)
except:
print(pronoun_data)
raise
if pos_score > neg_score:
pos += 1
elif pos_score == neg_score:
tie += 1
elif neg_score > pos_score:
neg += 1
except EOFError:
break
dataint.close()
return pos, neg, tie, (pos+neg+tie)
def pairwise_loss(pos, neg, batch_size, margin=0.1, tie=False):
if tie:
return 0.0
else:
loss_zeros = torch.zeros(batch_size, 1, 1)
loss_zeros = loss_zeros.to(device)
loss = torch.max(margin + neg - pos, loss_zeros)
return torch.mean(loss)
def padding_and_mask(input_vector, max_size):
input_length = len(input_vector)
padding_size = max_size - input_length
padding = torch.zeros(padding_size, dtype=torch.long)
mask = []
mask.extend(torch.zeros(input_length))
mask.extend(torch.ones(padding_size))
input_vector = torch.cat((torch.LongTensor(input_vector), padding),0)
assert len(input_vector) == max_size
assert len(mask) == max_size
return input_vector, mask, input_length
def get_pronoun_idx(input_sent, pronoun_list):
return [i+1 for i in range(len(input_sent)) if input_sent[i] in pronoun_list.keys()]
dev_path = sys.argv[2]
#Adjust if needed
max_context_length = 304
max_sentence_length = 135
max_pronoun_length = 30
torch.manual_seed(100)
torch.cuda.manual_seed_all(100)
dimensions = 1024 #based on ELMo
epochs = 20
org_batch_size = 30
batch_size = org_batch_size
learning_rate = 0.01
device = torch.device('cuda') #('cpu')
start = time.time()
best_epoch, best_accuracy =0,0.0
margin = 0.1
net = model.PronounScoreShared(dimensions, max_context_length, max_sentence_length, max_pronoun_length, margin)
net = net.to(device)
count = 0
pint = open("pronoun_idx", 'rb') #list of pronouns
pronoun_list = pickle.load(pint)
for epoch in range(epochs):
count = 0
if epoch >= 3:
learning_rate = learning_rate/2
optimizer=torch.optim.SGD(net.parameters(), lr=learning_rate)
data_path = open(sys.argv[1], 'rb')
EOFFLAG=False
batch_size = org_batch_size
running_loss = 0.0
#Note that since the training data is large the pickle file is written to/read from one sample at a time
while data_path:
optimizer.zero_grad()
pos_context_input = []
pos_sentence_input = []
pos_pronoun_input = []
neg_context_input = []
neg_sentence_input = []
neg_pronoun_input = []
try:
for each_sample in range(batch_size):
pronoun_data = pickle.load(data_path)
ref_sen_len = len(pronoun_data['reference_sentence'])
pos_context = pronoun_data['reference_context']
pos_sentence = pronoun_data['reference_sentence']
pos_context_pids = get_pronoun_idx(pos_context, pronoun_list)
pos_sentenceonly_pids = get_pronoun_idx(pos_sentence, pronoun_list)
pos_sentence_pids = pos_context_pids[-len(pos_sentenceonly_pids):]
sys_sen_len = len(pronoun_data['system_sentence'])
neg_context = pronoun_data['reference_context'][:-ref_sen_len]
neg_context.extend(pronoun_data['system_sentence'])
neg_sentence = pronoun_data['system_sentence']
neg_context_pids = get_pronoun_idx(neg_context, pronoun_list)
neg_sentenceonly_pids = get_pronoun_idx(neg_sentence, pronoun_list)
neg_sentence_pids = neg_context_pids[-len(neg_sentenceonly_pids):]
neg_pronouns = neg_sentence_pids
pos_context_input.append(pos_context)
pos_sentence_input.append(pos_sentence)
pos_pronoun_input.append(pos_sentence_pids)
neg_context_input.append(neg_context)
neg_sentence_input.append(neg_sentence)
neg_pronoun_input.append(neg_sentence_pids)
except EOFError:
EOFFLAG=True
break
rand_idx = torch.randperm(batch_size)
pos_context_batch = [pos_context_input[i] for i in rand_idx]
pos_sentence_batch = []
pos_pronoun_batch = [pos_pronoun_input[i] for i in rand_idx]
neg_context_batch = [neg_context_input[i] for i in rand_idx]
neg_sentence_batch = []
neg_pronoun_batch = [neg_pronoun_input[i] for i in rand_idx]
pos_score = net.forward(pos_context_batch, pos_sentence_batch, pos_pronoun_batch, batch_size)
neg_score = net.forward(neg_context_batch, neg_sentence_batch, neg_pronoun_batch, batch_size)
loss = pairwise_loss(pos_score, neg_score, batch_size)
running_loss += loss.item()
loss.backward()
optimizer.step()
#Keeping track of progress
#if count%100==0:
# print(count, end=".", flush=True)
#count+=1
if EOFFLAG:
break
pos, neg, tie, total = check_accuracy(net, dev_path)
print(total)
data_path.close()
total_loss = running_loss/batch_size
elapsed = time.time()-start
recall = pos/total
precision = pos/(pos+neg)
f1 = 2 * precision * recall / (precision + recall)
accuracy = pos/total
if accuracy > best_accuracy:
best_accuracy = accuracy
best_epoch = epoch
print("epoch=", epoch, "\t time=", elapsed, "\t dev_accuracy=", accuracy, "\tbest_acc:", best_accuracy, "\tbest_ep:", best_epoch)
print()
torch.save({'epoch':epochs, 'dev_acc':accuracy,'model_state_dict':net.state_dict(), 'optimizer_state_dict':optimizer.state_dict(), 'loss': running_loss, 'bsize':batch_size, 'dimensions':dimensions},"anaphora_model_"+str(epoch))