-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_train.lua
138 lines (114 loc) · 3.98 KB
/
post_train.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
require '.'
require 'shortcut'
require 'SelectNetPos'
require 'train'
local Trainer = torch.class('SelectNetPostTrainer', 'SelectNetTrainer')
local function getOpts()
local cmd = torch.CmdLine()
cmd:option('--load', '', 'model path')
cmd:option('--save', 'model.t7', 'save model path')
cmd:option('--lr', 0.001, 'learning rate')
cmd:option('--maxEpoch', 30, 'maximum number of epochs')
cmd:option('--optimMethod', 'SGD', 'optimization algorithm')
cmd:option('--decay', 1, 'decay learning rate')
local opts = cmd:parse(arg)
return opts
end
function Trainer:main()
local opts_ = getOpts()
self.opts = torch.load( opts_.load:sub(1, -3) .. 'state.t7' )
assert(self.opts.save ~= opts_.save)
self.opts.load = opts_.load
self.opts.save = opts_.save
self.opts.lr = opts_.lr
self.opts.maxEpoch = opts_.maxEpoch
self.opts.optimMethod = opts_.optimMethod
local opts = self.opts
torch.manualSeed(opts.seed + 1)
if opts.useGPU then
require 'cutorch'
require 'cunn'
cutorch.manualSeed(opts.seed + 1)
end
self.trainSize, self.validSize, self.testSize = unpack( DepPosDataIter.getDataSize({opts.train, opts.valid, opts.test}) )
xprintln('train size = %d, valid size = %d, test size = %d', self.trainSize, self.validSize, self.testSize)
-- local vocabPath = opts.train .. '.tmp.vocab.t7'
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
DepPosDataIter.showVocab(opts.vocab)
print '****load from existing vocab!!!****\n\n'
end
end
if recreateVocab then
opts.vocab = DepPosDataIter.createVocab(opts.train, opts.ignoreCase, opts.freqCut, opts.maxNVocab)
torch.save(vocabPath, opts.vocab)
xprintln('****create vocab from scratch****\n\n')
end
self.net = SelectNetPos(opts)
self:showOpts()
xprintln('load from %s ...', opts.load)
self.net:load(opts.load)
xprintln('load from %s done!', opts.load)
self.train_all_sents = DepPosDataIter.loadAllSents(opts.vocab, opts.train, opts.maxTrainLen)
local bestUAS = 0
local bestModel = torch.FloatTensor(self.net.params:size())
local timer = torch.Timer()
self.opts.sgdParam = {learningRate = opts.lr}
local v = self:validConllx(opts.valid)
print(v)
bestUAS = v.UAS
self.net:getModel(bestModel)
for epoch = 1, self.opts.maxEpoch do
self.iepoch = epoch
local startTime = timer:time().real
local train_nll, train_perp = self:train()
xprintln('\nepoch %d TRAIN %f (%f) ', epoch, train_nll, train_perp)
-- local vret = self:valid(opts.valid)
local vret = self:validConllx(opts.valid)
print 'Valid Performance'
print(vret)
local endTime = timer:time().real
xprintln('time spend %s', readableTime(endTime - startTime))
if bestUAS < vret.UAS then
bestUAS = vret.UAS
self.net:getModel(bestModel)
if opts.test and opts.test ~= '' then
local vret = self:validConllx(opts.test)
print 'Test Performance'
print(vret)
end
else
xprintln('UAS on valid not increase! early stopping!')
break
end
self.opts.sgdParam.learningRate = self.opts.sgdParam.learningRate * opts_.decay
end
-- save final model
self.net:setModel(bestModel)
opts.sgdParam = nil
self.net:save(opts.save, true)
xprintln('model saved at %s', opts.save)
-- show final perform
local vret = self:validConllx(opts.valid)
print 'Final Valid Performance'
print(vret)
if opts.test and opts.test ~= '' then
vret = self:validConllx(opts.test)
print 'Final Test Performance'
print(vret)
end
end
local function main()
local trainer = SelectNetPostTrainer()
trainer:main()
end
if not package.loaded['post_train'] then
main()
else
print '[post_train] loaded as package!'
end