-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathClockLin.lua
More file actions
49 lines (41 loc) · 1.44 KB
/
ClockLin.lua
File metadata and controls
49 lines (41 loc) · 1.44 KB
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
local ClockLin, parent = torch.class('nn.ClockLin', 'nn.Module')
function ClockLin:__init(inputSize, outputSize)
parent.__init(self)
self.weight = torch.Tensor(outputSize, inputSize+outputSize)
self.bias = torch.Tensor(outputSize)
self.gradWeight = torch.Tensor(outputSize, inputSize+outputSize)
self.gradBias = torch.Tensor(outputSize)
self:reset()
end
function ClockLin:updateOutput(input)
self.output:resize(self.bias:size(1))
self.output:copy(self.bias)
self.output:addmv(1, torch.cmul(self.mask,self.weight), input)
return self.output
end
function ClockLin:updateGradInput(input, gradOutput)
if self.gradInput then
local nElement = self.gradInput:nElement()
self.gradInput:resizeAs(input)
if self.gradInput:nElement() ~= nElement then
self.gradInput:zero()
end
self.gradInput:addmv(0, 1,
torch.cmul(self.mask,self.weight):t(),
gradOutput)
return self.gradInput
else
print('what?')
end
end
function ClockLin:accGradParameters(input, gradOutput, scale)
scale = scale or 1
self.gradWeight:addr(scale, gradOutput, input)
self.gradBias:add(scale, gradOutput)
end
-- we do not need to accumulate parameters when sharing
ClockLin.sharedAccUpdateGradParameters = ClockLin.accUpdateGradParameters
function ClockLin:__tostring__()
return torch.type(self) ..
string.format('(%d -> %d)', self.weight:size(2), self.weight:size(1))
end