-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy paththreadedtrain.lua
105 lines (87 loc) · 3.06 KB
/
threadedtrain.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
local Threads = require 'threads'
local function threadedTrain(module, criterion, data, label, params)
-- corner case: we are here to do batches
-- no bach, no threading
if params.batch == 1 then
print('! WARNING: no batch => no thread')
params.threads = 1
end
if params.threads == 1 then
print('! WARNING: if you use no thread, better not use a thread-aware code [overheads ahead]')
end
-- important because of possible sub-batch per thread
-- in the end, we normalize ourselves per batch-size
criterion.sizeAverage = false
Threads.serialization('threads.sharedserialize')
local threads = Threads(
params.threads,
function()
require 'nn'
end,
function()
local module = module:clone('weight', 'bias')
local weights, dweights = module:parameters()
local criterion = criterion:clone()
local data = data
local label = label
local dataset = {}
local nex = label:size(1)
if params.batch == 1 then
function dataset:size()
return nex
end
setmetatable(dataset, {__index =
function(self, index)
return {data[index], label[index]}
end})
else
assert(nex % params.batch == 0, '# of examples must be divisible with batch size')
local batch = params.batch
function dataset:size()
return nex/batch
end
setmetatable(dataset, {__index =
function(self, index)
return {
data:narrow(1,(index-1)*batch+1, batch),
label:narrow(1,(index-1)*batch+1, batch)
}
end})
end
function gupdate(idx)
local ex = dataset[idx]
local x, y = ex[1], ex[2]
local z = module:forward(x)
local err = criterion:forward(z, y)
module:zeroGradParameters()
module:updateGradInput(x, criterion:updateGradInput(module.output, y))
module:accGradParameters(x, criterion.gradInput)
return err, dweights
end
end
)
local weights = module:parameters()
for iter=1,params.iter do
local totalerr = 0
local idx = 1
while idx < label:size(1)/params.batch do
threads:addjob(
function(idx)
return gupdate(idx)
end,
function(err, dweights)
totalerr = totalerr + err
for i=1,#weights do
weights[i]:add(-0.01, dweights[i])
end
end,
idx
)
idx = idx + 1
end
threads:synchronize()
print('# current error = ', totalerr/label:size(1))
end
threads:terminate()
end
return threadedTrain