-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentencesTests.py
165 lines (143 loc) · 5.9 KB
/
sentencesTests.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
import argparse
from copy import copy
import torch
import numpy as np
import data
import model
parser = argparse.ArgumentParser(description='PyTorch Language Model')
# Model parameters
parser.add_argument('--data', type=str, default='./data/achemenet_data_20102019',
help='location of the data corpus')
parser.add_argument('--checkpoint', type=str, default='./model_best.pt',
help='model checkpoint to use')
parser.add_argument('--seed', type=int, default=1111,
help='random seed')
parser.add_argument('--cuda', action='store_true',
help='use CUDA')
parser.add_argument('--model', type=str, default='LSTM',
help='type of recurrent net (RNN_TANH, RNN_RELU, LSTM, GRU)')
parser.add_argument('--emsize', type=int, default=200,
help='size of word embeddings')
parser.add_argument('--nhid', type=int, default=350,
help='number of hidden units per layer')
parser.add_argument('--nlayers', type=int, default=2,
help='number of layers')
parser.add_argument('--tied', action='store_true',
help='tie the word embedding and softmax weights')
args = parser.parse_args()
args.cuda = torch.cuda.is_available()
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
np.random.seed(args.seed)
def filter_non_printable(str):
return ''.join([c for c in str if c != '\xa0'])
def clear_line(line):
return ' '.join(line.split(' ')[1:])
def get_data(location):
texts = []
with open(location) as f:
lines = f.read().split("\n")
while '' in lines:
lines.remove('')
for i, line in enumerate(lines):
line = filter_non_printable(line)
if "__" in line:
texts.append([line, clear_line(lines[i+1]), clear_line(lines[i+2]),
clear_line(lines[i + 3]), clear_line(lines[i+4])])
with open(location) as f:
lines = f.read().split("\n")
while '' in lines:
lines.remove('')
counter = 0
for line in lines:
line = filter_non_printable(line)
if line[0].isdigit() and len(line.split()) > 4:
texts[counter].append(clear_line(line))
counter += 1
return texts
count = 0
question_count = 0
texts = get_data("./testSentences/questionsDataFull")
log_file = "./testSentences/answers.txt"
wrong_log_file = "./testSentences/wrong_answers.txt"
net = model.RNNModel(args.model, 1549, args.emsize, args.nhid, args.nlayers, 0., args.tied)
net.load_state_dict(torch.load(args.checkpoint))
net.eval()
if args.cuda:
net.cuda()
else:
net.cpu()
softmax = torch.nn.Softmax(2)
# Loading the data
corpus = data.Corpus(args.data)
def get_token_index(sentence, token='__'):
counter = 0
idx = -1
for i,s in enumerate(sentence):
if s==token:
idx = i
counter += 1
assert(counter==1)
return idx
def tokenize(sentences, corpus):
tokenized_sentences = []
for sentence in sentences:
temp = []
for word in sentence:
temp.append(corpus.dictionary.word2idx[word])
tokenized_sentences.append(temp)
tokenized_sentences = np.array(tokenized_sentences).transpose()
return tokenized_sentences
def sentence_prob(sentence,output):
prob = 0.
for i,token in enumerate(sentence[1:]):
prob += output[i,token]
return prob.item()
with open(wrong_log_file,'w') as wf:
with open(log_file,'w') as f:
with torch.no_grad():
for idx, item in enumerate(texts[:]):
question_count += 1
# import ipdb; ipdb.set_trace()
print("Question", idx + 1)
s = item[0].replace('___','__')
sentence = s.split()
answer = item[-1].split()
print(sentence)
remove_index = get_token_index(sentence)
answer = answer[remove_index]
options = item[1:-1]
answer = get_token_index(options, answer)
sentences = [copy(sentence) for x in range(len(options))]
for i,option in enumerate(options):
sentences[i][remove_index] = option
tokenized_sentences = tokenize(sentences,corpus)
tokenized_sentences = torch.from_numpy(tokenized_sentences)
if args.cuda:
tokenized_sentences = tokenized_sentences.cuda()
hidden = net.init_hidden(len(options))
outputs, _ = net(tokenized_sentences, hidden)
outputs = torch.log(softmax(outputs))
probs = []
for i in range(len(options)):
probs.append(sentence_prob(tokenized_sentences[:,i],outputs[:,i,:]))
probs = np.array(probs)
order = np.argsort(-probs)
pred = probs.argmax()
if pred == answer:
count += 1
f.write('Question {}\n'.format(idx+1))
f.write(item[0]+'\n')
for i,option in enumerate(options):
f.write('{}) {}\n'.format(i,option))
f.write('Answer: {}. Correct answer: {}.\n'.format(pred,answer))
f.write('Order = '+str(order)+'\n\n')
## print to log
if pred != answer:
wf.write('Question {}\n'.format(idx + 1))
wf.write(item[0] + '\n')
for i, option in enumerate(options):
wf.write('{}) {}\n'.format(i, option))
wf.write('Answer: {}. Correct answer: {}.\n\n'.format(pred, answer))
f.write("Correct in total {}, out of {}. {:2f}%".format(count,question_count,count*100./question_count))
print("Correct in total {}, out of {}. {:2f}%".format(count,question_count,count*100./question_count))