forked from harrisonzhao/plankton
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.lua
More file actions
48 lines (42 loc) · 1.52 KB
/
Copy pathtrain.lua
File metadata and controls
48 lines (42 loc) · 1.52 KB
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
----------------------------------------------------------------------
print '==> train.lua'
mdl:training()
parameters,gradParameters = mdl:getParameters()
print '==> defining training procedure'
function train()
local time = sys.clock()
print('<trainer> on training set:')
print("<trainer> online epoch # " .. epoch .. ' [batchSize = ' .. batchSize .. ']')
local epochError = 0
for t = 1,epochSize do
local batch, targets = getTrainSample()
-- create closure to evaluate f(X) and df/dX
local feval = function(x)
-- get new parameters
if x ~= parameters then
parameters:copy(x)
end
gradParameters:zero()
local f = 0;
local oHat = mdl:forward(batch)
f = f + criterion:forward(oHat,targets)
mdl:backward(batch,criterion:backward(oHat,targets)) --problem line
epochError = epochError + f
print('# of Examples:',t*batchSize,'Error:',f)
return f,gradParameters
end
optimMethod(feval, parameters, optimState)
collectgarbage()
end
-- time taken
time = sys.clock() - time
print("<trainer> time for 1 Epoch = " .. (time) .. 's')
epochError = epochError/epochSize
local errStr = string.format('Epoch: %g, Epoch Error: %g, Learning Rate: %g, Decay: %g',epoch,epochError,optimState.learningRate,optimState.weightDecay)
print(errStr)
local mdlErrFileName = string.format('modelLogs/model%d.err',nModel)
local errFile = io.open(mdlErrFileName,'a')
errFile:write(errStr)
errFile:close()
end
train()