-
Notifications
You must be signed in to change notification settings - Fork 2
/
Diag.lua
43 lines (37 loc) · 1009 Bytes
/
Diag.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
local Diag,parent = torch.class('nn.Diag','nn.Module')
function Diag:__init(nFeature)
parent.__init(self)
self.weight = torch.Tensor(nFeature)
self.gradWeight = torch.Tensor(nFeature)
self:reset()
end
function Diag:reset(stdv)
self.weight:fill(1)
end
function Diag:updateOutput(input)
self.output:resizeAs(input):copy(input)
if input:dim() > 1 then
for i=1,input:size(1) do
self.output[{{i}}]:mul(self.weight[i])
end
else
self.output:cmul(self.weight)
end
return self.output
end
function Diag:updateGradInput(input, gradOutput)
self.gradInput:resizeAs(gradOutput):copy(gradOutput)
if input:dim() > 1 then
for i=1,input:size(1) do
self.gradInput[{{i}}]:mul(self.weight[i])
end
else
self.gradInput:cmul(self.weight)
end
return self.gradInput
end
function Diag:accGradParameters(input, gradOutput)
for i=1,input:size(1) do
self.gradWeight[i] = self.gradWeight[i] + gradOutput[{{i}}]:dot(input[{{i}}])
end
end