-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmaptable.lua
144 lines (131 loc) · 4.48 KB
/
maptable.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
require 'nn'
require 'nngraph'
local MapTable, parent = torch.class('nn.maptable', 'nn.Container')
function MapTable:__init(module, shared,num_inputs)
parent.__init(self)
self.shared = (shared == nil) and true or shared
self.sharedparams = {'weight', 'bias', 'gradWeight', 'gradBias'}
self.output = {}
self.gradInput = {}
self:add(module)
self:_extend(num_inputs)
end
function MapTable:_extend(n)
self.sharedparams = self.sharedparams or {'weight', 'bias', 'gradWeight', 'gradBias'}
self.modules[1] = self.module
--assert(n==3,"provide 3 inputs")
p,g=self.module:getParameters()
print("size of original",p:size(1))
for num = 2, n do
if not self.modules[i] then
if self.shared then
self.modules[num] = self.module:clone()
for i=1,#self.module.forwardnodes do
if(self.module.forwardnodes[i].data.module~=nil and self.module.forwardnodes[i].data.module.weight~=nil) then
self.module.forwardnodes[i].data.module:share(self.modules[num].forwardnodes[i].data.module, 'weight','bias','gradWeight','gradBias');
-- print(self.module.forwardnodes[i].data.annotations.name)
end
if(self.module.forwardnodes[i].data.annotations.name=='attention_module') then
atten1=self.module.forwardnodes[i].data.module.lstm
atten2=self.modules[num].forwardnodes[i].data.module.lstm
for j=1,#atten1.forwardnodes do
if(atten1.forwardnodes[j].data.module~=nil and atten1.forwardnodes[j].data.module.weight~=nil) then
atten1.forwardnodes[j].data.module:share(atten2.forwardnodes[j].data.module, 'weight','bias','gradWeight','gradBias');
-- print(atten1.forwardnodes[j].data.annotations.name)
end
end
end
end
else
self.modules[num] = self.module:clone()
end
end
p,g=self.modules[num]:getParameters()
print(p:size(1),num)
end
end
function MapTable:resize(n)
-- self:_extend(n)
for i = n + 1, #self.modules do
-- It's not clear why this clearState call is necessary, but it fixes
-- https://github.com/torch/nn/issues/1141 .
self.modules[i]:clearState()
self.modules[i] = nil
end
end
function MapTable:add(module)
assert(not self.module, 'Single module required')
self.module = module
self.modules[1] = self.module
return self
end
function MapTable:updateOutput(input)
self.output = {}
-- self:_extend(#input)
for i = 1, #input do
self.output[i] = self:rethrowErrors(self.modules[i], i, 'updateOutput', input[i])
end
return self.output
end
function MapTable:updateGradInput(input, gradOutput)
self.gradInput = {}
-- self:_extend(#input)
for i = 1, #input do
self.gradInput[i] = self:rethrowErrors(self.modules[i], i, 'updateGradInput', input[i], gradOutput[i])
end
return self.gradInput
end
function MapTable:accGradParameters(input, gradOutput, scale)
scale = scale or 1
-- self:_extend(#input)
for i = 1, #input do
self:rethrowErrors(self.modules[i], i, 'accGradParameters', input[i], gradOutput[i], scale)
end
end
function MapTable:accUpdateGradParameters(input, gradOutput, lr)
lr = lr or 1
-- self:_extend(#input)
for i = 1, #input do
self:rethrowErrors(self.modules[i], i, 'accUpdateGradParameters', input[i], gradOutput[i], lr)
end
end
function MapTable:zeroGradParameters()
if self.module then
if self.shared then
self.module:zeroGradParameters()
else
parent.zeroGradParameters(self)
end
end
end
function MapTable:updateParameters(learningRate)
if self.module then
if self.shared then
self.module:updateParameters(learningRate)
else
parent.updateParameters(self, learningRate)
end
end
end
function MapTable:clearState()
for i = 2, #self.modules do
-- It's not clear why this clearState call is necessary, but it fixes
-- https://github.com/torch/nn/issues/1141 .
self.modules[i]:clearState()
self.modules[i] = nil
end
parent.clearState(self)
end
function MapTable:__tostring__()
local tab = ' '
local line = '\n'
local extlast = ' '
local str = torch.type(self)
if self.module then
str = str .. ' {' .. line .. tab
str = str .. tostring(self.module):gsub(line, line .. tab .. extlast) .. line .. '}'
else
str = str .. ' { }'
end
return str
end