forked from torch/nn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMulTable.lua
50 lines (41 loc) · 1.21 KB
/
CMulTable.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
local CMulTable, parent = torch.class('nn.CMulTable', 'nn.Module')
function CMulTable:__init()
parent.__init(self)
self.gradInput = {}
end
function CMulTable:updateOutput(input)
self.output:resizeAs(input[1]):copy(input[1])
for i=2,#input do
self.output:cmul(input[i])
end
return self.output
end
function CMulTable:updateGradInput_efficient(input, gradOutput)
self.tout = self.tout or input[1].new()
self.tout:resizeAs(self.output)
for i=1,#input do
self.gradInput[i] = self.gradInput[i] or input[1].new()
self.gradInput[i]:resizeAs(input[i]):copy(gradOutput)
self.tout:copy(self.output):cdiv(input[i])
self.gradInput[i]:cmul(self.tout)
end
for i=#input+1, #self.gradInput do
self.gradInput[i] = nil
end
return self.gradInput
end
function CMulTable:updateGradInput(input, gradOutput)
for i=1,#input do
self.gradInput[i] = self.gradInput[i] or input[1].new()
self.gradInput[i]:resizeAs(input[i]):copy(gradOutput)
for j=1,#input do
if i~=j then
self.gradInput[i]:cmul(input[j])
end
end
end
for i=#input+1, #self.gradInput do
self.gradInput[i] = nil
end
return self.gradInput
end