-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathtrain.lua
177 lines (143 loc) · 5.04 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
----------------------------------------------------------------------
-- This script demonstrates how to define a training procedure,
-- irrespective of the model/loss functions chosen.
--
-- It shows how to:
-- + construct mini-batches on the fly
-- + define a closure to estimate (a noisy) loss
-- function, as well as its derivatives wrt the parameters of the
-- model to be trained
-- + optimize the function, according to several optmization
-- methods: SGD, L-BFGS.
--
-- Clement Farabet
----------------------------------------------------------------------
require 'torch' -- torch
require 'xlua' -- xlua provides useful tools, like progress bars
require 'optim' -- an optimization package, for online and batch methods
----------------------------------------------------------------------
-- Model + Loss:
local t = require 'model'
local model = t.model
local fwmodel = t.model
local loss = t.loss
----------------------------------------------------------------------
-- Save light network tools:
function nilling(module)
module.gradBias = nil
if module.finput then module.finput = torch.Tensor() end
module.gradWeight = nil
module.output = torch.Tensor()
if module.fgradInput then module.fgradInput = torch.Tensor() end
module.gradInput = nil
end
function netLighter(network)
nilling(network)
if network.modules then
for _,a in ipairs(network.modules) do
netLighter(a)
end
end
end
----------------------------------------------------------------------
print(sys.COLORS.red .. '==> defining some tools')
-- This matrix records the current confusion across classes
local confusion = optim.ConfusionMatrix(classes)
-- Log results to files
local trainLogger = optim.Logger(paths.concat(opt.save, 'train.log'))
----------------------------------------------------------------------
print(sys.COLORS.red .. '==> flattening model parameters')
-- Retrieve parameters and gradients:
-- this extracts and flattens all the trainable parameters of the mode
-- into a 1-dim vector
local w,dE_dw = model:getParameters()
----------------------------------------------------------------------
print(sys.COLORS.red .. '==> configuring optimizer')
local optimState = {
learningRate = opt.learningRate,
momentum = opt.momentum,
weightDecay = opt.weightDecay,
learningRateDecay = opt.learningRateDecay
}
----------------------------------------------------------------------
print(sys.COLORS.red .. '==> allocating minibatch memory')
local x = torch.Tensor(opt.batchSize,trainData.data:size(2),
trainData.data:size(3), trainData.data:size(4)) --faces data
local yt = torch.Tensor(opt.batchSize)
if opt.type == 'cuda' then
x = x:cuda()
yt = yt:cuda()
end
----------------------------------------------------------------------
print(sys.COLORS.red .. '==> defining training procedure')
local epoch
local function train(trainData)
-- epoch tracker
epoch = epoch or 1
-- local vars
local time = sys.clock()
-- shuffle at each epoch
local shuffle = torch.randperm(trainData:size())
-- do one epoch
print(sys.COLORS.green .. '==> doing epoch on training data:')
print("==> online epoch # " .. epoch .. ' [batchSize = ' .. opt.batchSize .. ']')
for t = 1,trainData:size(),opt.batchSize do
-- disp progress
xlua.progress(t, trainData:size())
collectgarbage()
-- batch fits?
if (t + opt.batchSize - 1) > trainData:size() then
break
end
-- create mini batch
local idx = 1
for i = t,t+opt.batchSize-1 do
x[idx] = trainData.data[shuffle[i]]
yt[idx] = trainData.labels[shuffle[i]]
idx = idx + 1
end
-- create closure to evaluate f(X) and df/dX
local eval_E = function(w)
-- reset gradients
dE_dw:zero()
-- evaluate function for complete mini batch
local y = model:forward(x)
local E = loss:forward(y,yt)
-- estimate df/dW
local dE_dy = loss:backward(y,yt)
model:backward(x,dE_dy)
-- update confusion
for i = 1,opt.batchSize do
confusion:add(y[i],yt[i])
end
-- return f and df/dX
return E,dE_dw
end
-- optimize on current mini-batch
optim.sgd(eval_E, w, optimState)
end
-- time taken
time = sys.clock() - time
time = time / trainData:size()
print("\n==> time to learn 1 sample = " .. (time*1000) .. 'ms')
-- print confusion matrix
print(confusion)
-- update logger/plot
trainLogger:add{['% mean class accuracy (train set)'] = confusion.totalValid * 100}
if opt.plot then
trainLogger:style{['% mean class accuracy (train set)'] = '-'}
trainLogger:plot()
end
-- save/log current net
local filename = paths.concat(opt.save, 'model.net')
os.execute('mkdir -p ' .. sys.dirname(filename))
print('==> saving model to '..filename)
model1 = model:clone()
netLighter(model1)
torch.save(filename, model1)
-- next epoch
confusion:zero()
epoch = epoch + 1
end
-- Export:
return train