-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.lua
400 lines (329 loc) · 12.2 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
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
local nn = require('nn')
require('nngraph')
local optim = require('optim')
local threads = require('threads')
local tds = require('tds')
local torch = require('torch')
local utils = require('utils')
local train = {}
local _trainer = torch.class('Train.Trainer', train)
function _trainer:__init(fold, snapshot, dataLoader, opt)
self.snapshot = snapshot
self.dataLoader = dataLoader
self.trainData, self.validateData = dataLoader:getFold(fold)
self.epochs = opt.epochs
self.patience = opt.patience
self.threads = opt.threads
self.verbose = opt.verbose
end
function _trainer:setup(margin, reload)
if not self.criterion or reload then
self.criterion = train.setupCriterion(self.dataLoader, margin)
end
if not self.trainer or reload then
self.trainer = train.setupTrainer(self.dataLoader, self.snapshot.model)
self.parameters, self.gradients = self.trainer:getParameters()
if self.snapshot.epoch == 0 then
self.parameters:uniform(-0.08, 0.08)
end
end
end
function train.setupTrainer(dataLoader, model)
local inputModel = model()
local inputSplit = nn.SplitTable(1, 2)(inputModel)
local target = nn.Identity()()
local targetSplit = nn.SplitTable(1, 2)(target)
local numElements = dataLoader.targets:size(2)
local splitInputs = {inputSplit:split(numElements)}
local splitTargets = {targetSplit:split(numElements)}
local output = {}
for i=1,numElements do
table.insert(output, nn.Identity()({splitInputs[i], splitTargets[i]}))
end
local trainer = nn.gModule({inputModel, target}, output)
trainer.name = 'training.'..model.name
return trainer
end
function train.setupCriterion(dataLoader, margin)
local criterion = nn.ParallelCriterion(true)
local numElements = dataLoader.targets:size(2)
for _=1,numElements do
criterion:add(nn.CosineEmbeddingCriterion(margin))
end
return criterion
end
function train.evaluate(model, criterion, data, progress, threadnum)
model:evaluate()
local totalLoss = 0
local batchCount = data:batchCount()
progress.index = 0
progress.timer = torch.Timer()
progress.batchCount = batchCount
local pool = train.newPool(model, criterion, data, threadnum)
for i=1,batchCount do
pool:addjob(
function(index)
return _G.calculateLoss(index)
end,
function(loss)
totalLoss = totalLoss + loss
progress.loss = totalLoss
progress.index = progress.index + 1
utils.updateProgress(progress)
end,
i
)
end
pool:synchronize()
pool:terminate()
local loss = totalLoss/batchCount
return loss
end
function train.newPool(model, criterion, data, threadnum)
threads.serialization('threads.sharedserialize')
local rngState = torch.getRNGState()
local pool = threads.Threads(
threadnum,
function ()
require('dataset')
require('nn')
require('nngraph')
require('tds')
_G.threads = require('threads')
_G.torch = require('torch')
_G.utils = require('utils')
end,
function ()
local threadCriterion = criterion:clone()
local threadModel = model:clone('weight', 'bias')
local _, threadGradients = threadModel:parameters()
threadGradients = threadModel.flatten(threadGradients)
_G.torch.setRNGState(rngState)
_G.utils.collectAllGarbage()
_G.calculateLoss = function(batchIndex)
local inputs, targets, outcomes = data:getBatch(batchIndex)
local modelInput = {inputs, targets}
local predictions = threadModel:forward(modelInput)
local loss = threadCriterion:forward(predictions, outcomes)
return loss, predictions
end
_G.calculateGradient = function(batchIndex, predictions)
threadGradients:zero()
local inputs, targets, outcomes = data:getBatch(batchIndex)
local modelInput = {inputs, targets}
local dloss_dw = threadCriterion:backward(predictions, outcomes)
threadModel:backward(modelInput, dloss_dw)
-- Clip gradients element-wise
threadGradients:clamp(-5, 5)
return threadGradients
end
end
)
return pool
end
local function getLearningParameters(strategy)
local regimes
local optimState
if strategy <= 1 then
optimState = {
initialMomentum = 0.5,
momentumGrowth=2e-7,
dampening = 0,
nesterov = true,
learningRate=1e-2,
learningRateDecay=1e-7,
}
regimes = {
{learningRate=5e-3,initialMomentum=0.7},
{learningRate=1e-3,initialMomentum=0.9},
{learningRate=1e-4,initialMomentum=0.999},
}
elseif strategy <= 2 then
optimState = {
initialMomentum = 0.7,
momentumGrowth=6e-7,
dampening = 0,
nesterov = true,
learningRate=5e-1,
learningRateDecay=1e-6,
}
regimes = {
{learningRate=5e-3,initialMomentum=0.9},
{learningRate=1e-3,initialMomentum=0.95},
{learningRate=1e-4,initialMomentum=0.999},
}
elseif strategy >= 3 then
optimState = {
momentum = 0.9,
dampening = 0,
nesterov = true,
learningRate=1e-1,
learningRateDecay=0,
}
regimes = {
{learningRate=1e-2,momentum=0.95},
{learningRate=5e-3,momentum=0.995},
{learningRate=1e-3,momentum=0.999},
}
end
return optimState, {epoch=0,index=0,regimes=regimes}
end
local function printRegime(prefix, regime)
local attributes = {}
for k,v in pairs(regime) do
if type(v) == 'number' then
table.insert(attributes, tostring(k)..'='..tostring(v))
end
end
print(prefix..table.concat(attributes, ', '))
end
function _trainer:trainEpoch(epoch, optimState, updateFrequency)
local trainData = self.trainData
-- Make sure the model is in training mode and the data is shuffled
self.trainer:training()
trainData:shuffle()
local batchCount = trainData:batchCount()
local progress = {}
progress.epoch = epoch
progress.action = 'Train'
progress.loss = 0
progress.index = 0
progress.batchCount = batchCount
progress.timer = torch.Timer()
local parameters = self.parameters
local function updateParameters(loss, gradients)
if optimState.momentumGrowth then
local nevals = optimState.evalCounter or 0
local momentum = optimState.initialMomentum
optimState.momentum = math.min(.999999, momentum * (1+nevals*optimState.momentumGrowth))
end
optim.sgd(function() return loss, gradients end, parameters, optimState)
end
local updateLosses = tds.Vec()
local updateGradients = tds.Vec()
-- Need to preallocate the size of the vectors
-- such that no realloc occurs in the threads
-- as it seems that could cause incorrect memory
-- semantics since there is no way to synchronize
-- the realloc across threads in tds.Vec
for i=1,updateFrequency do
updateLosses[i] = 0
updateGradients[i] = self.gradients:clone()
end
local batchIndex = tds.AtomicCounter()
local updates = tds.AtomicCounter()
local updateMutex = threads.Mutex()
local updateMutexId = updateMutex:id()
local updateCondition = threads.Condition()
local updateConditionId = updateCondition:id()
local pool = train.newPool(self.trainer, self.criterion, trainData, self.threads)
for i=1,batchCount do
pool:addjob(
function()
-- Unfortunately to ensure deterministic training this synchronization
-- is needed, otherwise the model parameters could be changing in the
-- middle of a gradient update, which has undefined behavior. This
-- approach allows for completely deterministic results each time
-- given the same command line options allowing comparisons between
-- models and free parameters (useful together with cross validation).
local mutex = _G.threads.Mutex(updateMutexId)
local condition = _G.threads.Condition(updateConditionId)
mutex:lock()
while updates:get() >= updateFrequency do
condition:wait(mutex)
end
local index = batchIndex:inc() + 1
local updateIndex = (index-1) % updateFrequency + 1
updates:inc()
mutex:unlock()
local loss, predictions = _G.calculateLoss(index)
updateLosses[updateIndex] = loss
local gradients = _G.calculateGradient(index, predictions)
updateGradients[updateIndex]:copy(gradients)
return loss
end,
function(loss)
updateMutex:lock()
local index = progress.index + 1
local updateCount = updates:get()
if index % updateFrequency == 0 or index == batchCount then
for t=1,updateCount do
updateParameters(updateLosses[t], updateGradients[t])
end
updates:set(0)
-- Only signal after doing all the parameter updates
for _=1,updateCount do
updateCondition:signal()
end
end
updateMutex:unlock()
progress.index = index
progress.loss = progress.loss + loss
utils.updateProgress(progress)
end,
i
)
end
pool:synchronize()
pool:terminate()
progress.flush = true
progress.index = batchCount
utils.updateProgress(progress)
updateMutex:free()
updateCondition:free()
end
function _trainer:processEpoch(epoch, margin, frequency)
local regime = self.snapshot.regime
local optimState = self.snapshot.optimState
if epoch - regime.epoch > self.patience then
regime.index = regime.index + 1
local regimeChange = regime.regimes[regime.index]
if not regimeChange then
return
end
printRegime('\nChanging regime: ', regimeChange)
utils.progressHeader()
self.snapshot:load(true)
self:setup(margin, true)
for k,v in pairs(regimeChange) do
optimState[k] = v
end
regime.epoch = self.snapshot.best.epoch
epoch = regime.epoch + 1
utils.collectAllGarbage()
end
self:trainEpoch(epoch, optimState, frequency)
local progress = {}
progress.epoch = epoch
progress.action = 'Validate'
local snapshot = self.snapshot
local best = snapshot.best
local loss = train.evaluate(self.trainer, self.criterion, self.validateData, progress, self.threads)
if loss < best.loss then
best.loss = loss
best.epoch = epoch
regime.epoch = epoch
progress.best = true
end
progress.flush = true
utils.updateProgress(progress)
snapshot:update(best, epoch, regime, optimState)
snapshot:save()
return epoch + 1
end
function _trainer:train(margin, strategy, frequency)
local defaultOptimState, defaultRegime = getLearningParameters(strategy)
local regime = self.snapshot.regime or defaultRegime
local optimState = self.snapshot.optimState or defaultOptimState
self.snapshot:update(self.snapshot.best, self.snapshot.epoch, regime, optimState)
local epoch = self.snapshot.epoch + 1
printRegime(epoch == 1 and 'Initial regime: ' or 'Current regime: ', optimState)
self:setup(margin)
utils.progressHeader()
while epoch and epoch <= self.epochs do
epoch = self:processEpoch(epoch, margin, frequency)
utils.collectAllGarbage()
end
print('Fold training completed\n')
end
return train