-
Notifications
You must be signed in to change notification settings - Fork 0
/
mst_postmulti.lua
449 lines (390 loc) · 14.2 KB
/
mst_postmulti.lua
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
require '.'
require 'shortcut'
require 'SelectNetRich'
require 'DepRichDataIter'
require 'PostDepGraph'
require 'ChuLiuEdmonds'
require 'Eisner'
local MST = torch.class('MSTPostMultiProcessor')
function MST:showOpts()
local tmp_vocab = self.opts.vocab
self.opts.vocab = nil
print(self.opts)
self.opts.vocab = tmp_vocab
end
function MST:load(modelPath)
self.opts = torch.load( modelPath:sub(1, -3) .. 'state.t7' )
local opts = self.opts
torch.manualSeed(opts.seed + 1)
if opts.useGPU then
require 'cutorch'
require 'cunn'
cutorch.manualSeed(opts.seed + 1)
end
local vocabPath = opts.vocabFile
local uDpos = opts.uDVocab .. '/ud_pos.vocab'
local recreateVocab = true
if paths.filep(vocabPath) then
opts.vocab = torch.load(vocabPath)
if opts.vocab.ignoreCase == opts.ignoreCase and opts.vocab.freqCut == opts.freqCut and opts.vocab.maxNVocab == opts.maxNVocab then
recreateVocab = false
DepRichDataIter.showVocab(opts.vocab)
print '****load from existing vocab!!!****\n\n'
end
end
assert(not recreateVocab, 'you should load existing vocabulary')
self.net = SelectNetRich(opts)
self:showOpts()
xprintln('load from %s ...', modelPath)
self.net:load(modelPath)
xprintln('load from %s done!', modelPath)
end
function MST:validConllx(validFile, outConllxFile)
local dataIter = DepRichDataIter.createBatch(self.opts.vocab, validFile, self.opts.batchSize, self.opts.feats, self.opts.maxTrainLen)
local totalCnt = 0
local totalLoss = 0
local cnt = 0
local sents_dep = {}
local y_tmp = torch.LongTensor(self.opts.maxTrainLen, self.opts.batchSize)
for x, x_mask, x_feats, y in dataIter do
local loss, y_preds = self.net:validBatch(x, x_mask, x_feats, y)
totalLoss = totalLoss + loss * x:size(2)
local y_mask = x_mask[{ {2, -1}, {} }]
local y_p = y_tmp:resize(y:size(1), y:size(2))
for t = 2, x:size(1) do
local _, mi = y_preds[t]:max(2)
if self.opts.useGPU then mi = mi:double() end
y_p[{ t-1, {} }] = mi
end
for i = 1, y_mask:size(2) do
local slen = y_mask[{ {}, i }]:sum()
local sent_dep = {}
for j = 1, slen do
sent_dep[#sent_dep + 1] = y_p[{ j, i }] - 1
end
sents_dep[#sents_dep + 1] = sent_dep
end
totalCnt = totalCnt + y_mask:sum()
cnt = cnt + 1
if cnt % 5 == 0 then
collectgarbage()
end
end
outConllxFile = outConllxFile or self.opts.modelDir .. '/tmp/__tmp__.dep'
local dep_iter = DepRichDataIter.conllx_iter(validFile)
local sent_idx = 0
local sys_out = outConllxFile
local fout = io.open(sys_out, 'w')
for dsent in dep_iter do
sent_idx = sent_idx + 1
local sent_dep = sents_dep[sent_idx]
assert(#sent_dep == #dsent)
for i, ditem in ipairs(dsent) do
-- '%d\t%s\t_\t_\t%s\t_\t%d\t%s\t_\t_\n'
-- 1 Influential _ JJ JJ _ 2 amod _ _
fout:write(string.format('%d\t%s\t_\t%s\t_\t%s\t%d\tprep\t_\t_\n', ditem.p1, ditem.wd, ditem.pos, ditem.xfeats, sent_dep[i]))
end
fout:write('\n')
end
fout:close()
-- local conllx_eval = require 'conllx_eval'
local conllx_eval = self.opts.evalType == 'stanford' and require 'conllx_eval' or require 'conllx2006_eval'
local LAS, UAS, noPunctLAS, noPunctUAS = conllx_eval.eval(sys_out, validFile)
local entropy = totalLoss / totalCnt
local ppl = torch.exp(entropy)
return {entropy = entropy, ppl = ppl, UAS = noPunctUAS}
end
function MST:validWithMSTPost(validFile, outConllxFile)
local dataIter = DepRichDataIter.createBatch(self.opts.vocab, validFile, self.opts.batchSize, self.opts.feats, self.opts.maxTrainLen)
local totalCnt = 0
local totalLoss = 0
local cnt = 0
local sents_dep = {}
local sents_graph = {}
local y_tmp = torch.LongTensor(self.opts.maxTrainLen, self.opts.batchSize)
for x, x_mask, x_feats, y in dataIter do
local loss, y_preds = self.net:validBatch(x, x_mask, x_feats, y)
totalLoss = totalLoss + loss * x:size(2)
local y_mask = x_mask[{ {2, -1}, {} }]
local y_p = y_tmp:resize(y:size(1), y:size(2))
-- WARNING: y_preds start from 2!
for t = 2, x:size(1) do
local _, mi = y_preds[t]:max(2)
if self.opts.useGPU then mi = mi:double() end
y_p[{ t-1, {} }] = mi
end
for i = 1, y_mask:size(2) do
local slen = y_mask[{ {}, i }]:sum()
local sent_dep = {}
local sent_graph = {}
for j = 1, slen do
sent_dep[#sent_dep + 1] = y_p[{ j, i }] - 1
local tmp = y_preds[j+1][{ i, {1, slen + 1} }]:double()
sent_graph[j] = tmp
end
sents_dep[#sents_dep + 1] = sent_dep
sents_graph[#sents_graph + 1] = sent_graph
end
totalCnt = totalCnt + y_mask:sum()
cnt = cnt + 1
if cnt % 5 == 0 then
collectgarbage()
end
end
outConllxFile = outConllxFile or '__tmp__.dep'
local dep_iter = DepRichDataIter.conllx_iter(validFile)
local sent_idx = 0
local connected_count = 0
local sys_out = outConllxFile
local fout = io.open(sys_out, 'w')
for dsent in dep_iter do
sent_idx = sent_idx + 1
local sent_dep = sents_dep[sent_idx]
assert(#sent_dep == #dsent)
local sent_graph = sents_graph[sent_idx]
assert(#sent_graph == #dsent)
local new_dsent = {}
for i, ditem in ipairs(dsent) do
local new_ditem = {p1 = ditem.p1, wd = ditem.wd, pos = ditem.pos, xfeats = ditem.xfeats, p2 = sent_dep[i]}
new_dsent[#new_dsent + 1] = new_ditem
end
-- check connectivity
local dgraph = PostDepGraph(new_dsent)
if not dgraph:checkConnectivity() then
local N = #sent_graph + 1
local edges = {}
for i, sp in ipairs(sent_graph) do
for j = 1, sp:size(1) do
edges[#edges + 1] = {j, i+1, sp[j]}
end
end
-- run ChuLiuEdmonds
local cle = ChuLiuEdmonds()
cle:load(N, edges)
local _, selectedEdges = cle:solve(1, N)
table.sort(selectedEdges, function(a, b) return a.v < b.v end)
for i, ditem in ipairs(new_dsent) do
local edge = selectedEdges[i]
assert(edge.v == i+1)
ditem.p2 = edge.u - 1
ditem.p1 = edge.v - 1
end
local dgraph2 = PostDepGraph(new_dsent)
assert(dgraph2:checkConnectivity())
else
connected_count = connected_count + 1
end
for i, ditem in ipairs(new_dsent) do
-- '%d\t%s\t_\t_\t%s\t_\t%d\t%s\t_\t_\n'
-- 1 Influential _ JJ JJ _ 2 amod _ _
fout:write(string.format('%d\t%s\t_\t%s\t_\t%s\t%d\tprep\t_\t_\n', ditem.p1, ditem.wd, ditem.pos, ditem.xfeats, ditem.p2))
end
fout:write('\n')
end
fout:close()
printf('%d/%d = %f are connected graph\n', connected_count, sent_idx, connected_count/sent_idx)
-- local conllx_eval = require 'conllx_eval'
printf('evalType = %s\n', self.opts.evalType)
local conllx_eval = self.opts.evalType == 'stanford' and require 'conllx_eval' or require 'conllx2006_eval'
local LAS, UAS, noPunctLAS, noPunctUAS = conllx_eval.eval(sys_out, validFile)
local entropy = totalLoss / totalCnt
local ppl = torch.exp(entropy)
return {entropy = entropy, ppl = ppl, UAS = noPunctUAS}
end
function MST:validWithMSTPostEisner(validFile, outConllxFile)
local dataIter = DepRichDataIter.createBatch(self.opts.vocab, validFile, self.opts.batchSize, self.opts.feats, self.opts.maxTrainLen)
local totalCnt = 0
local totalLoss = 0
local cnt = 0
local sents_dep = {}
local sents_graph = {}
local y_tmp = torch.LongTensor(self.opts.maxTrainLen, self.opts.batchSize)
for x, x_mask, x_feats, y in dataIter do
local loss, y_preds = self.net:validBatch(x, x_mask, x_feats, y)
totalLoss = totalLoss + loss * x:size(2)
local y_mask = x_mask[{ {2, -1}, {} }]
local y_p = y_tmp:resize(y:size(1), y:size(2))
-- WARNING: y_preds start from 2!
for t = 2, x:size(1) do
local _, mi = y_preds[t]:max(2)
if self.opts.useGPU then mi = mi:double() end
y_p[{ t-1, {} }] = mi
end
for i = 1, y_mask:size(2) do
local slen = y_mask[{ {}, i }]:sum()
local sent_dep = {}
local sent_graph = {}
for j = 1, slen do
sent_dep[#sent_dep + 1] = y_p[{ j, i }] - 1
local tmp = y_preds[j+1][{ i, {1, slen + 1} }]:double()
sent_graph[j] = tmp
end
sents_dep[#sents_dep + 1] = sent_dep
sents_graph[#sents_graph + 1] = sent_graph
end
totalCnt = totalCnt + y_mask:sum()
cnt = cnt + 1
if cnt % 5 == 0 then
collectgarbage()
end
end
outConllxFile = outConllxFile or '__tmp__.dep'
local dep_iter = DepRichDataIter.conllx_iter(validFile)
local sent_idx = 0
local connected_count = 0
local sys_out = outConllxFile
local fout = io.open(sys_out, 'w')
for dsent in dep_iter do
sent_idx = sent_idx + 1
local sent_dep = sents_dep[sent_idx]
assert(#sent_dep == #dsent)
local sent_graph = sents_graph[sent_idx]
assert(#sent_graph == #dsent)
local new_dsent = {}
for i, ditem in ipairs(dsent) do
local new_ditem = {p1 = ditem.p1, wd = ditem.wd, pos = ditem.pos, p2 = sent_dep[i]}
new_dsent[#new_dsent + 1] = new_ditem
end
-- check connectivity
local dgraph = PostDepGraph(new_dsent)
if not (dgraph:checkConnectivity() and dgraph:isProjective()) then
local N = #sent_graph + 1
local edges = {}
for i, sp in ipairs(sent_graph) do
for j = 1, sp:size(1) do
edges[#edges + 1] = {j, i+1, sp[j]}
end
end
-- run Eisner's algorithm
local eisner = Eisner()
eisner:load(N, edges)
local _, selectedEdges = eisner:solve()
table.sort(selectedEdges, function(a, b) return a.v < b.v end)
for i, ditem in ipairs(new_dsent) do
local edge = selectedEdges[i]
assert(edge.v == i+1)
ditem.p2 = edge.u - 1
ditem.p1 = edge.v - 1
end
-- local dgraph2 = PostDepGraph(new_dsent)
-- assert(dgraph2:checkConnectivity())
else
connected_count = connected_count + 1
end
for i, ditem in ipairs(new_dsent) do
-- '%d\t%s\t_\t_\t%s\t_\t%d\t%s\t_\t_\n'
-- 1 Influential _ JJ JJ _ 2 amod _ _
fout:write(string.format('%d\t%s\t_\t%s\t_\t%s\t%d\tprep\t_\t_\n', ditem.p1, ditem.wd, ditem.pos, ditem.xfeats, ditem.p2))
end
fout:write('\n')
end
fout:close()
printf('%d/%d = %f are projective trees\n', connected_count, sent_idx, connected_count/sent_idx)
-- local conllx_eval = require 'conllx_eval'
printf('evalType = %s\n', self.opts.evalType)
local conllx_eval = self.opts.evalType == 'stanford' and require 'conllx_eval' or require 'conllx2006_eval'
local LAS, UAS, noPunctLAS, noPunctUAS = conllx_eval.eval(sys_out, validFile)
local entropy = totalLoss / totalCnt
local ppl = torch.exp(entropy)
return {entropy = entropy, ppl = ppl, UAS = noPunctUAS}
end
local function getOpts()
local cmd = torch.CmdLine()
cmd:option('--modelPath', '/disk/scratch/XingxingZhang/dep_parse/experiments/we_select_ft_pos/ft/model_0.001.std.ft0.t7', 'model path')
cmd:option('--validout', 'valid', 'output conllx file for validation set')
cmd:option('--testout', 'test', 'output conllx file for test set')
cmd:option('--mstalg', 'ChuLiuEdmonds', 'MST algorithm: ChuLiuEdmonds or Eisner')
return cmd:parse(arg)
end
local function main()
local opts = getOpts()
local mst_post = MSTPostMultiProcessor()
mst_post:load(opts.modelPath)
-- load validation and test files
valid_files = listDir(mst_post.opts.valid)
test_files = listDir(mst_post.opts.test)
-- test performance on validation and test dataset
print '==Valid Performance=='
for _, f in ipairs(valid_files) do
local name = f:match( "([^/]+)$" )
local tokens = name:splitc('-')
local lang_id = tokens[2]
local validout = opts.validout .. '-' .. lang_id .. '.ori.dep'
print(f)
print(name)
print('Language:', lang_id)
local vret = mst_post:validConllx(f, validout)
print(vret)
end
print '--After post processing--'
if opts.mstalg == 'ChuLiuEdmonds' then
xprintln('Using ChuLiuEdmonds')
for _, f in ipairs(valid_files) do
local name = f:match( "([^/]+)$" )
local tokens = name:splitc('-')
local lang_id = tokens[2]
local validout = opts.validout .. '-' .. lang_id .. '.dep'
print('Language:', lang_id)
print('File = ', f)
local vret = mst_post:validWithMSTPost(f, validout)
print(vret)
end
elseif opts.mstalg == 'Eisner' then
xprintln('Using Eisner')
for _, f in ipairs(valid_files) do
local name = f:match( "([^/]+)$" )
local tokens = name:splitc('-')
local lang_id = tokens[2]
local validout = opts.validout .. '-' .. lang_id .. '.dep'
print('Language:', lang_id)
print('File = ', f)
local vret = mst_post:validWithMSTPostEisner(f, validout)
print(vret)
end
else
error(string.format('[%s] not supported!', opts.mstalg))
end
print('')
print '==Test Performance=='
for _, f in ipairs(test_files) do
local name = f:match( "([^/]+)$" )
local tokens = name:splitc('-')
local lang_id = tokens[2]
local testout = opts.testout .. '-' .. lang_id .. '.ori.dep'
print('Language:', lang_id)
print('File = ', f)
local tret = mst_post:validConllx(f, testout)
print(tret)
end
print '--After post processing--'
if opts.mstalg == 'ChuLiuEdmonds' then
xprintln('Using ChuLiuEdmonds')
for _, f in ipairs(test_files) do
local name = f:match( "([^/]+)$" )
local tokens = name:splitc('-')
local lang_id = tokens[2]
local testout = opts.testout .. '-' .. lang_id .. '.dep'
print('Language:', lang_id)
print('File = ', f)
local tret = mst_post:validWithMSTPost(f, testout)
print(tret)
end
elseif opts.mstalg == 'Eisner' then
xprintln('Using Eisner')
for _, f in ipairs(test_files) do
local name = f:match( "([^/]+)$" )
local tokens = name:splitc('-')
local lang_id = tokens[2]
local testout = opts.testout .. '-' .. lang_id .. '.dep'
print('Language:', lang_id)
print('File = ', f)
local tret = mst_post:validWithMSTPostEisner(f, testout)
print(tret)
end
else
error(string.format('[%s] not supported!', opts.mstalg))
end
end
if not package.loaded['mst_postmulti'] then
main()
end