forked from Element-Research/dpnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Container.lua
51 lines (48 loc) · 1.33 KB
/
Container.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
local Container = nn.Container
-- multi-add
function Container:extend(...)
for i,module in ipairs{...} do
self:add(module)
end
return self
end
function Container:sparseParameters()
local params = {}
local gradParams = {}
local scales = {}
local size = 0
for i=1,#self.modules do
local mParams, mGradParams, mScales, mSize = self.modules[i]:sparseParameters()
if mParams then
for k,param in pairs(mParams) do
assert(torch.type(param) ~= 'table')
params[size+k] = param
gradParams[size+k] = mGradParams[k]
scales[size+k] = mScales and mScales[k]
end
size = size + (mSize or #mParams)
end
end
return params, gradParams, scales, size
end
function Container:parameters()
local function tinsert(to, from)
if torch.type(from) == 'table' then -- we change this line so that it works with torch.MultiCudaTensor
for i=1,#from do
tinsert(to,from[i])
end
else
table.insert(to,from)
end
end
local w = {}
local gw = {}
for i=1,#self.modules do
local mw,mgw = self.modules[i]:parameters()
if mw then
tinsert(w,mw)
tinsert(gw,mgw)
end
end
return w,gw
end