forked from clementfarabet/lua---nnx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpatialLinear.lua
65 lines (54 loc) · 1.81 KB
/
SpatialLinear.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
local SpatialLinear, parent = torch.class('nn.SpatialLinear', 'nn.Module')
function SpatialLinear:__init(fanin, fanout)
parent.__init(self)
self.fanin = fanin or 1
self.fanout = fanout or 1
self.weightDecay = 0
self.weight = torch.Tensor(self.fanout, self.fanin)
self.bias = torch.Tensor(self.fanout)
self.gradWeight = torch.Tensor(self.fanout, self.fanin)
self.gradBias = torch.Tensor(self.fanout)
self.output = torch.Tensor(fanout,1,1)
self.gradInput = torch.Tensor(fanin,1,1)
self:reset()
end
function SpatialLinear:reset(stdv)
if stdv then
stdv = stdv * math.sqrt(3)
else
stdv = 1./math.sqrt(self.weight:size(1))
end
for i=1,self.weight:size(1) do
self.weight:select(1, i):apply(function()
return torch.uniform(-stdv, stdv)
end)
self.bias[i] = torch.uniform(-stdv, stdv)
end
end
function SpatialLinear:zeroGradParameters(momentum)
if momentum then
self.gradWeight:mul(momentum)
self.gradBias:mul(momentum)
else
self.gradWeight:zero()
self.gradBias:zero()
end
end
function SpatialLinear:updateParameters(learningRate)
self.weight:add(-learningRate, self.gradWeight)
self.bias:add(-learningRate, self.gradBias)
end
function SpatialLinear:decayParameters(decay)
self.weight:add(-decay, self.weight)
self.bias:add(-decay, self.bias)
end
function SpatialLinear:updateOutput(input)
self.output:resize(self.fanout, input:size(2), input:size(3))
input.nn.SpatialLinear_updateOutput(self, input)
return self.output
end
function SpatialLinear:updateGradInput(input, gradOutput)
self.gradInput:resize(self.fanin, input:size(2), input:size(3))
input.nn.SpatialLinear_updateGradInput(self, input, gradOutput)
return self.gradInput
end