forked from Element-Research/dpnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decorator.lua
48 lines (40 loc) · 1.54 KB
/
Decorator.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
local Decorator, parent = torch.class("nn.Decorator", "nn.Container")
function Decorator:__init(module)
parent.__init(self)
self.module = module
-- so that it can be handled like a Container
self.modules[1] = module
end
function Decorator:updateOutput(input)
self.output = self.module:updateOutput(input)
return self.output
end
function Decorator:updateGradInput(input, gradOutput)
self.gradInput = self.module:updateGradInput(input, gradOutput)
return self.gradInput
end
function Decorator:accGradParameters(input, gradOutput, scale)
self.module:accGradParameters(input, gradOutput, scale)
end
function Decorator:accUpdateGradParameters(input, gradOutput, lr)
self.module:accUpdateGradParameters(input, gradOutput, lr)
end
function Decorator:sharedAccUpdateGradParameters(input, gradOutput, lr)
self.module:sharedAccUpdateGradParameters(input, gradOutput, lr)
end
function Decorator:__tostring__()
if self.module.__tostring__ then
return torch.type(self) .. ' @ ' .. self.module:__tostring__()
else
return torch.type(self) .. ' @ ' .. torch.type(self.module)
end
end
-- useful for multiple-inheritance
function Decorator.decorate(class)
class.updateOutput = nn.Decorator.updateOutput
class.updateGradInput = nn.Decorator.updateGradInput
class.accGradParameters = nn.Decorator.accGradParameters
class.accUpdateGradParameters = nn.Decorator.accUpdateGradParameters
class.sharedAccUpdateGradParameters = nn.Decorator.sharedAccUpdateGradParameters
class.__tostring__ = nn.Decorator.__tostring__
end