-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.lua
195 lines (168 loc) · 4.12 KB
/
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
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
print("set default tensor type to float")
torch.setdefaulttensortype('torch.FloatTensor')
function gradUpdate(mlpin, x, y, criterionin, learningRate)
local pred=mlpin:forward(x)
local err=criterionin:forward(pred, y)
sumErr=sumErr+err
local gradCriterion=criterionin:backward(pred, y)
mlpin:zeroGradParameters()
mlpin:backward(x, gradCriterion)
mlpin:updateGradParameters(0.875)
mlpin:updateParameters(learningRate)
mlpin:maxParamNorm(-1)
end
function evaDev(mlpin, x, y, criterionin)
mlpin:evaluate()
local serr=0
for curpot=1,ndev do
serr=serr+criterionin:forward(mlpin:forward(x[curpot]), y[curpot])
end
mlpin:training()
return serr/ndev
end
--[[function inirand(cyc)
cyc=cyc or 8
for i=1,cyc do
local sdata=math.random(nsam)
end
end]]
function saveObject(fname,objWrt)
local tmpod=nil
if not torch.isTensor(objWrt) then
tmpod=nn.Serial(objWrt)
tmpod:lightSerial()
else
tmpod=objWrt
end
local file=torch.DiskFile(fname,'w')
file:writeObject(tmpod)
file:close()
end
print("load settings")
require"conf"
print("load data")
require "dloader"
sumErr=0
crithis={}
cridev={}
erate=0
edevrate=0
storemini=1
storedevmini=1
minerrate=starterate
mindeverrate=minerrate
print("load packages")
require "nn"
require "rnn"
require "SeqBGRU"
--require "nngraph"
require "dpnn"
require "vecLookup"
require "maskZerovecLookup"
require "gnuplot"
function train()
print("design neural networks and criterion")
require "designn"
local nnmod=getnn()
print(nnmod)
nnmod:training()
local critmod=getcrit()
print("init train")
local epochs=1
local lr=modlr
--inirand()
print("Init model Dev:"..evaDev(nnmod,devin,devt,critmod))
collectgarbage()
print("start pre train")
for tmpi=1,warmcycle do
for tmpj=1,ieps do
for curpot=1,nsam do
gradUpdate(nnmod,mword[curpot],mwordt[curpot],critmod,lr)
end
end
local erate=sumErr/eaddtrain
table.insert(crithis,erate)
print("epoch:"..tostring(epochs)..",lr:"..lr..",Tra:"..erate)
sumErr=0
epochs=epochs+1
end
epochs=1
icycle=1
aminerr=0
lrdecayepochs=1
while true do
print("start innercycle:"..icycle)
for innercycle=1,gtraincycle do
for tmpi=1,ieps do
for curpot=1,nsam do
gradUpdate(nnmod,mword[curpot],mwordt[curpot],critmod,lr)
end
end
local erate=sumErr/eaddtrain
table.insert(crithis,erate)
local edevrate=evaDev(nnmod,devin,devt,critmod)
table.insert(cridev,edevrate)
print("epoch:"..tostring(epochs)..",lr:"..lr..",Tra:"..erate..",Dev:"..edevrate)
local modsavd=false
if edevrate<mindeverrate then
print("new minimal dev error found,save model")
mindeverrate=edevrate
saveObject("modrs/devnnmod"..storedevmini..".asc",nnmod)
storedevmini=storedevmini+1
if storedevmini>csave then
storedevmini=1
end
modsavd=true
end
if erate<minerrate then
minerrate=erate
aminerr=0
if not modsavd then
print("new minimal error found,save model")
saveObject("modrs/nnmod"..storemini..".asc",nnmod)
storemini=storemini+1
if storemini>csave then
storemini=1
end
end
else
if aminerr>=expdecaycycle then
aminerr=0
if lrdecayepochs>lrdecaycycle then
modlr=lr
lrdecayepochs=1
end
lrdecayepochs=lrdecayepochs+1
lr=modlr/(lrdecayepochs)
end
aminerr=aminerr+1
end
sumErr=0
epochs=epochs+1
end
print("save neural network trained")
saveObject("modrs/nnmod.asc",nnmod)
print("save criterion history trained")
local critensor=torch.Tensor(crithis)
saveObject("modrs/crit.asc",critensor)
local critdev=torch.Tensor(cridev)
saveObject("modrs/critdev.asc",critdev)
print("plot and save criterion")
gnuplot.plot(critensor)
gnuplot.figprint("modrs/crit.png")
gnuplot.figprint("modrs/crit.eps")
gnuplot.plotflush()
gnuplot.plot(critdev)
gnuplot.figprint("modrs/critdev.png")
gnuplot.figprint("modrs/critdev.eps")
gnuplot.plotflush()
critensor=torch.Tensor()
critdev=torch.Tensor()
print("task finished!Minimal error rate:"..minerrate.." "..mindeverrate)
print("wait for test, neural network saved at nnmod*.asc")
icycle=icycle+1
print("collect garbage")
collectgarbage()
end
end
train()