-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.lua
294 lines (258 loc) · 7.97 KB
/
Module.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
local Module = torch.class('nn.Module')
function Module:__init()
self.gradInput = torch.Tensor()
self.output = torch.Tensor()
end
function Module:parameters()
if self.weight and self.bias then
return {self.weight, self.bias}, {self.gradWeight, self.gradBias}
elseif self.weight then
return {self.weight}, {self.gradWeight}
elseif self.bias then
return {self.bias}, {self.gradBias}
else
return
end
end
function Module:updateOutput(input)
return self.output
end
function Module:forward(input)
return self:updateOutput(input)
end
function Module:backward(input, gradOutput, scale)
scale = scale or 1
self:updateGradInput(input, gradOutput)
self:accGradParameters(input, gradOutput, scale)
return self.gradInput
end
function Module:backwardUpdate(input, gradOutput, lr)
self:updateGradInput(input, gradOutput)
self:accUpdateGradParameters(input, gradOutput, lr)
return self.gradInput
end
function Module:updateGradInput(input, gradOutput)
return self.gradInput
end
function Module:accGradParameters(input, gradOutput, scale)
end
function Module:accUpdateGradParameters(input, gradOutput, lr)
local gradWeight = self.gradWeight
local gradBias = self.gradBias
self.gradWeight = self.weight
self.gradBias = self.bias
self:accGradParameters(input, gradOutput, -lr)
self.gradWeight = gradWeight
self.gradBias = gradBias
end
function Module:sharedAccUpdateGradParameters(input, gradOutput, lr)
if self:parameters() then
self:zeroGradParameters()
self:accGradParameters(input, gradOutput, 1)
self:updateParameters(lr)
end
end
function Module:zeroGradParameters()
local _,gradParams = self:parameters()
if gradParams then
for i=1,#gradParams do
gradParams[i]:zero()
end
end
end
function Module:updateParameters(learningRate)
local params, gradParams = self:parameters()
if params then
for i=1,#params do
params[i]:add(-learningRate, gradParams[i])
end
end
end
function Module:training()
self.train = true
end
function Module:evaluate()
self.train = false
end
function Module:share(mlp, ...)
local arg = {...}
for i,v in ipairs(arg) do
if self[v] ~= nil then
self[v]:set(mlp[v])
self.accUpdateGradParameters = self.sharedAccUpdateGradParameters
mlp.accUpdateGradParameters = mlp.sharedAccUpdateGradParameters
end
end
return self
end
function Module:clone(...)
local f = torch.MemoryFile("rw"):binary()
f:writeObject(self)
f:seek(1)
local clone = f:readObject()
f:close()
if select('#',...) > 0 then
clone:share(self,...)
end
return clone
end
function Module:type(type)
assert(type, 'Module: must provide a type to convert to')
-- find all tensors and convert them
for key,param in pairs(self) do
self[key] = nn.utils.recursiveType(param, type)
end
return self
end
function Module:float()
return self:type('torch.FloatTensor')
end
function Module:double()
return self:type('torch.DoubleTensor')
end
function Module:cuda()
return self:type('torch.CudaTensor')
end
function Module:reset()
end
function Module:getParameters()
-- get parameters
local parameters,gradParameters = self:parameters()
local function storageInSet(set, storage)
local storageAndOffset = set[torch.pointer(storage)]
if storageAndOffset == nil then
return nil
end
local _, offset = table.unpack(storageAndOffset)
return offset
end
-- this function flattens arbitrary lists of parameters,
-- even complex shared ones
local function flatten(parameters)
if not parameters or #parameters == 0 then
return torch.Tensor()
end
local Tensor = parameters[1].new
local dtype = parameters[1]:type()
local storages = {}
local nParameters = 0
for k = 1,#parameters do
if parameters[k]:type() ~= dtype then
error("Inconsistent parameter types. " .. parameters[k]:type() ..
" ~= " .. dtype)
end
local storage = parameters[k]:storage()
if not storageInSet(storages, storage) then
storages[torch.pointer(storage)] = {storage, nParameters}
nParameters = nParameters + storage:size()
end
end
local flatParameters = Tensor(nParameters):fill(1)
local flatStorage = flatParameters:storage()
for k = 1,#parameters do
local storageOffset = storageInSet(storages, parameters[k]:storage())
parameters[k]:set(flatStorage,
storageOffset + parameters[k]:storageOffset(),
parameters[k]:size(),
parameters[k]:stride())
parameters[k]:zero()
end
local maskParameters = flatParameters:float():clone()
local cumSumOfHoles = flatParameters:float():cumsum(1)
local nUsedParameters = nParameters - cumSumOfHoles[#cumSumOfHoles]
local flatUsedParameters = Tensor(nUsedParameters)
local flatUsedStorage = flatUsedParameters:storage()
for k = 1,#parameters do
local offset = cumSumOfHoles[parameters[k]:storageOffset()]
parameters[k]:set(flatUsedStorage,
parameters[k]:storageOffset() - offset,
parameters[k]:size(),
parameters[k]:stride())
end
for _, storageAndOffset in pairs(storages) do
local k, v = table.unpack(storageAndOffset)
flatParameters[{{v+1,v+k:size()}}]:copy(Tensor():set(k))
end
if cumSumOfHoles:sum() == 0 then
flatUsedParameters:copy(flatParameters)
else
local counter = 0
for k = 1,flatParameters:nElement() do
if maskParameters[k] == 0 then
counter = counter + 1
flatUsedParameters[counter] = flatParameters[counter+cumSumOfHoles[k]]
end
end
assert (counter == nUsedParameters)
end
return flatUsedParameters
end
-- flatten parameters and gradients
local flatParameters = flatten(parameters)
collectgarbage()
local flatGradParameters = flatten(gradParameters)
collectgarbage()
-- return new flat vector that contains all discrete parameters
return flatParameters, flatGradParameters
end
function Module:__call__(input, gradOutput)
self:forward(input)
if gradOutput then
self:backward(input, gradOutput)
return self.output, self.gradInput
else
return self.output
end
end
function Module:findModules(typename, container)
container = container or self
local nodes = {}
local containers = {}
local mod_type = torch.typename(self)
if mod_type == typename then
nodes[#nodes+1] = self
containers[#containers+1] = container
end
-- Recurse on nodes with 'modules'
if (self.modules ~= nil) then
if (torch.type(self.modules) == 'table') then
for i = 1, #self.modules do
local child = self.modules[i]
local cur_nodes, cur_containers =
child:findModules(typename, self)
assert(#cur_nodes == #cur_containers,
'Internal error: incorrect return length') -- This shouldn't happen
-- add the list items from our child to our list (ie return a
-- flattened table of the return nodes).
for j = 1, #cur_nodes do
nodes[#nodes+1] = cur_nodes[j]
containers[#containers+1] = cur_containers[j]
end
end
end
end
return nodes, containers
end
-- returns a list of modules
function Module:listModules()
local function tinsert(to, from)
if torch.type(from) == 'table' then
for i=1,#from do
tinsert(to,from[i])
end
else
table.insert(to,from)
end
end
-- include self first
local modules = {self}
if self.modules then
for i=1,#self.modules do
local modulas = self.modules[i]:listModules()
if modulas then
tinsert(modules,modulas)
end
end
end
return modules
end