-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_tense.py
313 lines (246 loc) · 10.7 KB
/
test_tense.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
# This code was adapted from the tutorial "Translation with a Sequence to
# Sequence Network and Attention" by Sean Robertson. It can be found at the
# following URL:
# http://pytorch.org/tutorials/intermediate/seq2seq_translation_tutorial.html
# You must have PyTorch installed to run this code.
# You can get it from: http://pytorch.org/
# Imports
from __future__ import unicode_literals, print_function, division
from io import open
import unicodedata
import string
import re
import random
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch import optim
import torch.nn.functional as F
import sys
import os
# Functions for tracking time
import time
import math
from numpy import median
from numpy import mean
from seq2seq import pos_to_parse, sent_to_pos, file_to_batches
from evaluation import *
from models import *
from parsing import *
from sent_evals import *
random.seed(7)
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--encoder", help="encoder type", type=str, default=None)
parser.add_argument("--decoder", help="decoder type", type=str, default=None)
parser.add_argument("--task", help="task", type=str, default=None)
parser.add_argument("--attention", help="attention type", type=str, default=None)
parser.add_argument("--parse_strategy", help="whether to parse correctly or right-branching", type=str, default="correct")
parser.add_argument("--lr", help="learning rate", type=float, default=None)
parser.add_argument("--hs", help="hidden size", type=int, default=None)
parser.add_argument("--seed", help="random seed", type=float, default=None)
args = parser.parse_args()
prefix = args.task
if args.parse_strategy == "right_branching":
directory = "models/" + args.task + "_" + args.encoder + "_" + args.decoder + "_" + "RB" + "_" + args.attention + "_" + str(args.lr) + "_" + str(args.hs)
else:
directory = "models/" + args.task + "_" + args.encoder + "_" + args.decoder + "_" + args.attention + "_" + str(args.lr) + "_" + str(args.hs)
# Reading the training data
trainingFile = 'data/' + prefix + '.train'
testFile = 'data/' + prefix + '.test'
devFile = 'data/' + prefix + '.dev'
genFile = 'data/' + prefix + '.gen'
# Determine if we are using a GPU
use_cuda = torch.cuda.is_available()
if use_cuda:
available_device = torch.device('cuda')
else:
available_device = torch.device('cpu')
# Dictionaries for converting words to indices, and vice versa
word2index = {}
index2word = {}
fi = open("index.txt", "r")
for line in fi:
parts = line.strip().split("\t")
word2index[parts[0]] = int(parts[1])
index2word[int(parts[1])] = parts[0]
MAX_LENGTH = 20
test_batches, MAX_LENGTH = file_to_batches(testFile, MAX_LENGTH, batch_size=1)
gen_batches, MAX_LENGTH = file_to_batches(genFile, MAX_LENGTH, batch_size=1)
# Show the output for a few randomly selected sentences
def evaluateRandomly(encoder, decoder, batches, index2word, n=10):
batch_size = batches[0][0].size()[1]
for i in range(math.ceil(n * 1.0 / batch_size)):
this_batch = random.choice(batches)
input_sents = logits_to_sentence(this_batch[0], index2word, end_at_punc=False)
target_sents = logits_to_sentence(this_batch[1], index2word)
pred_sents = logits_to_sentence(evaluate(encoder, decoder, this_batch), index2word)
for group in zip(input_sents, target_sents, pred_sents):
print(group[0])
print(group[1])
print(group[2])
print("")
# Initialize the encoder and the decoder
if args.encoder == "Tree":
encoder = TreeEncoderRNN(len(word2index.keys()), args.hs)
else:
encoder = EncoderRNN(len(word2index.keys()), args.hs, args.encoder, max_length=MAX_LENGTH)
if args.decoder == "Tree":
# Note that attention is not implemented for the tree decoder
decoder = TreeDecoderRNN(len(word2index.keys()), args.hs)
else:
decoder = DecoderRNN(args.hs, len(word2index.keys()), args.decoder, attn=args.attention, n_layers=1, dropout_p=0.1, max_length=MAX_LENGTH)
encoder = encoder.to(device=available_device)
decoder = decoder.to(device=available_device)
# Variables for iterating over directories
counter = 0
direcs_to_process = 1
# Lists for keeping track of the metrics for each model
this_gen_right = 0
this_gen_lin = 0
this_gen_main_right = 0
this_gen_main_lin = 0
this_gen_main_rightnum = 0
this_gen_main_wrongnum = 0
test_full_sent = []
test_full_sent_pos = []
gen_full_sent = []
gen_full_sent_pos = []
gen_right = []
gen_lin = []
gen_main_right = []
gen_main_lin = []
gen_main_rightnum = []
gen_main_wrongnum = []
# Iterate over all re-runs of the same model type that has been specified
while direcs_to_process:
if not os.path.exists(directory + "_" + str(counter)):
direcs_to_process = 0
else:
directory_now = directory + "_" + str(counter)
counter += 1
dec_list = sorted(os.listdir(directory_now))
dec = sorted(dec_list[:int(len(dec_list)/2)], key=lambda x:float(".".join(x.split(".")[2:4])))[0]
print("Directory being processed::", dec)
enc = dec.replace("decoder", "encoder")
encoder.load_state_dict(torch.load(directory_now + "/" + enc))
decoder.load_state_dict(torch.load(directory_now + "/" + dec))
print("Test set example outputs")
evaluateRandomly(encoder, decoder, test_batches, index2word)
print("Gen set example outputs")
evaluateRandomly(encoder, decoder, gen_batches, index2word)
print("Evaluation of model")
# Evaluation on the test set
right = 0
rightpos = 0
total = 0
for this_batch in test_batches:
input_sents = logits_to_sentence(this_batch[0], index2word, end_at_punc=False)
target_sents = logits_to_sentence(this_batch[1], index2word)
pred_sents = logits_to_sentence(evaluate(encoder, decoder, this_batch), index2word)
for trio in zip(input_sents, target_sents, pred_sents):
input_sent = sent_remove_brackets(trio[0])
target_sent = sent_remove_brackets(trio[1])
pred_sent = sent_remove_brackets(trio[2])
total += 1
if pred_sent == target_sent:
right += 1
if sent_to_pos(pred_sent) == sent_to_pos(target_sent):
rightpos += 1
print("Test number correct:", right)
print("Test total:", total)
test_full_sent.append(right * 1.0 / total)
test_full_sent_pos.append(rightpos * 1.0 / total)
# Evaluate on the generalization set
right = 0
first_aux = 0
other_aux = 0
other_word = 0
total = 0
other = 0
full_right = 0
full_right_pos = 0
this_gen_right = 0
this_gen_lin = 0
this_gen_main_right = 0
this_gen_main_lin = 0
this_gen_main_wrongnum = 0
this_gen_main_rightnum = 0
for this_batch in gen_batches:
input_sents = logits_to_sentence(this_batch[0], index2word, end_at_punc=False)
target_sents = logits_to_sentence(this_batch[1], index2word)
pred_sents = logits_to_sentence(evaluate(encoder, decoder, this_batch), index2word)
for trio in zip(input_sents, target_sents, pred_sents):
input_sent = sent_remove_brackets(trio[0])
target_sent = sent_remove_brackets(trio[1])
pred_sent = sent_remove_brackets(trio[2])
correct_words = target_sent.split()
total += 1
if pred_sent == target_sent:
full_right += 1
this_gen_right += 1
if pred_sent == tense_nearest(target_sent):
this_gen_lin += 1
if main_right_tense(target_sent, pred_sent):
this_gen_main_right += 1
if main_linear_tense(target_sent, pred_sent):
this_gen_main_lin += 1
if main_rightnum_tense(target_sent, pred_sent):
this_gen_main_rightnum += 1
if main_wrongnum_tense(target_sent, pred_sent):
this_gen_main_wrongnum += 1
if sent_to_pos(pred_sent) == sent_to_pos(target_sent):
full_right_pos += 1
gen_full_sent.append(full_right * 1.0 / total)
gen_full_sent_pos.append(full_right_pos * 1.0 / total)
gen_right.append(this_gen_right * 1.0 / total)
gen_lin.append(this_gen_lin * 1.0 / total)
gen_main_right.append(this_gen_main_right * 1.0 / total)
gen_main_lin.append(this_gen_main_lin * 1.0 / total)
gen_main_rightnum.append(this_gen_main_rightnum * 1.0 / total)
gen_main_wrongnum.append(this_gen_main_wrongnum * 1.0 / total)
print("Test full-sentence accuracy list:")
print(", ".join([str(x) for x in test_full_sent]))
print("Mean:", str(mean(test_full_sent)))
print("Median:", str(median(test_full_sent)))
print(" ")
print("Test full-sentence POS accuracy list:")
print(", ".join([str(x) for x in test_full_sent_pos]))
print("Mean:", str(mean(test_full_sent_pos)))
print("Median:", str(median(test_full_sent_pos)))
print(" ")
print("Gen full-sentence accuracy list:")
print(", ".join([str(x) for x in gen_full_sent]))
print("Mean:", str(mean(gen_full_sent)))
print("Median:", str(median(gen_full_sent)))
print(" ")
print("Gen full-sentence POS accuracy list:")
print(", ".join([str(x) for x in gen_full_sent_pos]))
print("Mean:", str(mean(gen_full_sent_pos)))
print("Median:", str(median(gen_full_sent_pos)))
print(" ")
print("Gen proportion of full-sentence outputs that follow agree-recent:")
print(", ".join([str(x) for x in gen_lin]))
print("Mean:", str(mean(gen_lin)))
print("Median:", str(median(gen_lin)))
print(" ")
print("Gen proportion of outputs that have the correct main verb:")
print(", ".join([str(x) for x in gen_main_right]))
print("Mean:", str(mean(gen_main_right)))
print("Median:", str(median(gen_main_right)))
print(" ")
print("Gen proportion of outputs that have the main verb predicted by agree-recent:")
print(", ".join([str(x) for x in gen_main_lin]))
print("Mean:", str(mean(gen_main_lin)))
print("Median:", str(median(gen_main_lin)))
print(" ")
print("Gen proportion of outputs that have the correct number for the main verb:")
print(", ".join([str(x) for x in gen_main_rightnum]))
print("Mean:", str(mean(gen_main_rightnum)))
print("Median:", str(median(gen_main_rightnum)))
print(" ")
print("Gen proportion of outputs that have the incorrect number for the main verb:")
print(", ".join([str(x) for x in gen_main_wrongnum]))
print("Mean:", str(mean(gen_main_wrongnum)))
print("Median:", str(median(gen_main_wrongnum)))
print(" ")