-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
334 lines (275 loc) · 9.96 KB
/
test.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
import numpy as np
from queue import Queue
import torch.optim as optim
import re
import torch
import torch.optim
from torch.autograd import Variable
import torch.nn as nn
from dataset.vocab import Vocab
from dataset.dataset import NLIdataset
from dataset.vocab import Vocab
from trainer import Trainer
from config import parse_args
import os
from torch.utils.data import DataLoader
from model.model import TreeLSTMforNLI
from model.model import ESIM
from tqdm import tqdm
import utils
import torch.nn.functional as F
line = '(ROOT (S (NP (NP (DT A) (NN person)) (PP (IN on) (NP (DT a) (NN horse)))) (VP (VBZ jumps) (PP (IN over) (NP (DT a) (JJ broken) (JJ down) (NN airplane)))) (. .)))'
line = re.sub('\\([A-Z|.]+', '(', line)
line = re.sub('\\(', '( ', line)
line = re.sub('\\)', ' )', line)
line = re.sub('[ ]+', ' ', line)
#
class Tree(object):
def __init__(self,index):
self.root = None
self.node_array = {}
node = TreeNode(index,None)
node.set_word('(')
self.root = node
self.node_array[0] = node
def add_children(self,parent_index,index,word):
node = TreeNode(index, parent_index)
node.set_word(word)
self.node_array[index] = node
self.node_array[parent_index].add_children(node)
def resort_sequence(self):
new_array = []
tree_to_bfs_to_reverse = []
reverse_to_bfs_to_tree = {}
q = Queue()
q.put(self.root)
index = 0
while not q.empty():
node = q.get()
new_array.append(node)
tree_to_bfs_to_reverse.append(node.index)
index += 1
children = node.children
children.reverse()
for child in children:
q.put(child)
new_array.reverse()
tree_to_bfs_to_reverse.reverse()
for reverse,tree in enumerate(tree_to_bfs_to_reverse):
reverse_to_bfs_to_tree[tree]=reverse
self.new_array = new_array
self.reverse_to_bfs_to_tree = reverse_to_bfs_to_tree
return
def return_sequence(self):
sequence = ' '.join([node.get_word() for node in self.new_array])
return sequence
def convert_to_graph(self):
node_num = len(self.node_array.keys())
graph = np.zeros((node_num,node_num),dtype='float32')
for parent_index,node in enumerate(self.new_array):
for child in node.children:
child_index = self.reverse_to_bfs_to_tree[child.index]
graph[parent_index][child_index] = 1
return graph
def print_tree(tree):
if not tree.has_children():
print(tree.get_word())
else:
for child in tree.children:
print_tree(child)
print(tree.get_word())
return 0
class TreeNode(object):
def __init__(self,index,parent):
self.index = index
self.parent = parent
self.children = []
self.word = None
def add_children(self,Node):
self.children.append(Node)
def set_word(self,word):
self.word = word
def get_word(self):
return self.word
def has_children(self):
return not len(self.children)== 0
parts = line.split()
stack = []
tree = None
index = 0
for p_i,part in enumerate(parts):
if part =='(':#####父节点,不见 )推入栈
if tree is None:
tree = Tree(index)
elif parts[p_i+1]!='(':
continue
else:
tree.add_children(stack[-1],index,'(')
stack.append(index)
index+=1
elif part == ')' and parts[p_i-1]==')':
stack.pop(-1)
elif part != ')':
tree.add_children(stack[-1],index,part)
index+=1
# print(tree.resort_sequence())
# print(tree.return_sequence())
# print(parts)
# print_tree(tree.root)
# graph = tree.convert_to_graph()
# length = graph.shape[0]
# print(graph.shape)
# print(length)
graph = np.zeros((2,2),dtype='float32')
input = np.zeros((2,2),dtype='float32')
class TreeLSTM(nn.Module):
def __init__(self,idim,hdim):
super(TreeLSTM,self).__init__()
self.idim = idim
self.hdim = hdim
self.wi = nn.Linear(self.idim,self.hdim)
self.wf = nn.Linear(self.idim,self.hdim)
self.wo = nn.Linear(self.idim,self.hdim)
self.wu = nn.Linear(self.idim,self.hdim)
self.ui = nn.Linear(self.hdim,self.hdim)
self.uf = nn.Linear(self.hdim,self.hdim)
self.uo = nn.Linear(self.hdim,self.hdim)
self.uu = nn.Linear(self.hdim,self.hdim)
# self.lstm = nn.LSTM()
# self.wi = nn.Linear(idim,hdim)
def forward(self,input,graph):
print(input.size())
print(graph.size())
batch_size = input.size(0)
time_step = input.size(1)
h = torch.zeros(batch_size,time_step,self.hdim)
c = torch.zeros(batch_size,time_step,self.hdim)
for j in torch.range(0,time_step-1):
j = j.long()
x = input[:,j,:]
x = torch.unsqueeze(x,1)#[32, 1, 2])
# print('x',x)
mask = graph[:,j,:]
mask = torch.unsqueeze(mask,1)#[32, 1, 21])
# print('mask',mask)
h_ = torch.bmm(mask,h)#([32, 1, 21]) * (32,21,2) -> (32,1,2)
# print('h_',h_)
h_.backward()
i = self.wi(x)+self.ui(h_)#F.sigmoid(self.wi(x)+self.ui(h_))#(32,1,2)
# print('i',i)
o = self.wo(x)+self.uo(h_)#F.sigmoid(self.wo(x)+self.uo(h_))#(32,1,2)
# print('o',o)
u = self.wu(x)+self.uu(h_)#F.tanh(self.wu(x)+self.uu(h_))#(32,1,2)
# print('u',u)
x_= x.repeat(1,time_step,1)#(32,21,2)
x_ = self.wi(x_)#(32,21,2)->(32,21,2)
# print('x_',x_)
h__ = self.uf(h)#(32,21,2)->
# print('h',h__)
f = x_+h__ #F.sigmoid(x_+h_)#(32,21,2)->(32,21,2)
# print(f)
t = torch.bmm(mask, f * c) + i * u
# print('t',t)
c[:,j,:] = torch.squeeze(t)#(32,1,21)*(32,21,2)->(32,1,2)
h[:,j,:] = torch.squeeze(o*t)
return h,c
model = TreeLSTM(2,2)
# criterion = nn.CrossEntropyLoss()
input = torch.from_numpy(input)
graph = torch.from_numpy(graph)
# print(input.size())
# print(graph.size())
input = torch.unsqueeze(input,0).repeat(1,1,1)
graph = torch.unsqueeze(graph,0).repeat(1,1,1)
input,graph = Variable(input),Variable(graph)
global args
args = parse_args()
vocab_file = os.path.join(args.dtree, 'snli_vocab_cased.txt')
vocab = Vocab(filename=vocab_file)
# args.cuda = args.cuda and torch.cuda.is_available()
# device = torch.device("cuda:0" if args.cuda else "cpu")
l_dev_file = os.path.join(args.dtree, args.premise_dev)
r_dev_file = os.path.join(args.dtree, args.hypothesis_dev)
label_dev_file = os.path.join(args.dtree, args.label_dev)
l_dev_squence_file = os.path.join(args.ctree, args.premise_dev)
r_dev_squence_file = os.path.join(args.ctree, args.hypothesis_dev)
l_test_file = os.path.join(args.dtree, args.premise_test)
r_test_file = os.path.join(args.dtree, args.hypothesis_test)
label_test_file = os.path.join(args.dtree, args.label_test)
l_test_squence_file = os.path.join(args.ctree, args.premise_test)
r_test_squence_file = os.path.join(args.ctree, args.hypothesis_test)
# dev_dataset = NLIdataset(premise_tree=l_dev_file, hypothesis_tree=r_dev_file,
# premise_seq=l_dev_squence_file, hypothesis_seq=r_dev_squence_file,
# label=label_dev_file, vocab=vocab, num_classes=3, args=args)
# for i in dev_dataset:
test_file = os.path.join(args.data, 'test.pth')
# test_dataset = torch.load(test_file)
test_dataset = NLIdataset(premise_tree=l_test_file, hypothesis_tree=r_test_file,
premise_seq=l_test_squence_file, hypothesis_seq=r_test_squence_file,
label=label_test_file, vocab=vocab, num_classes=3, args=args)
# torch.save(test_dataset, test_file)
# print(i)
args.cuda = args.cuda and torch.cuda.is_available()
device = torch.device("cuda:2" if args.cuda else "cpu")
loader = DataLoader(test_dataset,batch_size=args.batchsize,shuffle=False)
model = TreeLSTMforNLI(
vocab.size(),
args.input_dim,
args.mem_dim,
args.hidden_dim,
args.num_classes,
args.sparse,
args.freeze_embed)
criterion = nn.CrossEntropyLoss()
emb_file = os.path.join(args.data, 'snli_embed.pth')
emb = torch.load(emb_file)
model.emb.weight.data.copy_(emb)
# model = ESIM(vocab.size(),
# args.input_dim,
# args.mem_dim,
# embeddings=emb,
# dropout=0.5,
# num_classes=args.num_classes,
# device=device,
# freeze=True
# ).to(device)
model.to(device), criterion.to(device)
optimizer = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=args.lr, weight_decay=args.wd)
optimizer.zero_grad()
total_loss = 0.0
accuracy = 0
model.train()
tqdm_loader = tqdm(loader)
for data in tqdm_loader:
# lsent, llen, rsent, rlen, label = data
lsent, lgraph, llen, rsent, rgraph, rlen, label = data
target = utils.map_label_to_target_classifcation(label,args.num_classes)
linput, rinput = lsent.to(device), rsent.to(device)
lgraph, rgraph = lgraph.to(device), rgraph.to(device)
llen, rlen = llen.to(device), rlen.to(device)
target = target.to(device)
label = label.to(device)
# output = model(linput, llen,rinput, rlen)
output = model(linput, lgraph, rinput, rgraph,device)
loss = criterion(output, target)
loss.backward()
total_loss+=loss.item()
description = "Avg. batch proc. loss: {:.4f}" \
.format(total_loss)
pred = torch.squeeze(output.data.max(1, keepdim=True)[1])
accuracy += pred.eq(label.data.view_as(pred)).cpu().sum().numpy()
# print(accuracy)
optimizer.step()
optimizer.zero_grad()
print(accuracy*1.0/len(loader.dataset))
# print(total_loss)
# print(lsent)
# print(lgraph)
# print(label)
# break
# print(len(loader))
# print(len(loader.dataset))
# h = model(input,graph)
# print(h)
a = [1,2,3,4,5]
b = [6,7,8,9,10]