-
Notifications
You must be signed in to change notification settings - Fork 1
/
SpatialSADMap.lua
96 lines (86 loc) · 2.83 KB
/
SpatialSADMap.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
local SpatialSADMap, parent = torch.class('nn.SpatialSADMap', 'nn.Module')
function constructTableRev(conMatrix)
local conMatrixL = conMatrix:type('torch.LongTensor')
-- Construct reverse lookup connection table
local thickness = conMatrixL:select(2,2):max()
-- approximate fanin check
if (#conMatrixL)[1] % thickness == 0 then
-- do a proper fanin check and set revTable
local fanin = (#conMatrixL)[1] / thickness
local revTable = torch.Tensor(thickness, fanin, 2)
for ii=1,thickness do
local tempf = fanin
for jj=1,(#conMatrixL)[1] do
if conMatrixL[jj][2] == ii then
if tempf <= 0 then break end
revTable[ii][tempf][1] = conMatrixL[jj][1]
revTable[ii][tempf][2] = jj
tempf = tempf - 1
end
end
if tempf ~= 0 then
fanin = -1
break
end
end
if fanin ~= -1 then
return revTable
end
end
return {}
end
function SpatialSADMap:__init(conMatrix, kW, kH, dW, dH)
parent.__init(self)
dW = dW or 1
dH = dH or 1
self.kW = kW
self.kH = kH
self.dW = dW
self.dH = dH
self.connTable = conMatrix
self.connTableRev = constructTableRev(conMatrix)
self.nInputPlane = self.connTable:select(2,1):max()
self.nOutputPlane = self.connTable:select(2,2):max()
self.weight = torch.Tensor(self.connTable:size(1), kH, kW)
self.bias = torch.Tensor(self.nOutputPlane)
self.gradWeight = torch.Tensor(self.connTable:size(1), kH, kW)
self.gradBias = torch.Tensor(self.nOutputPlane)
self:reset()
end
function SpatialSADMap:reset(stdv)
if stdv then
stdv = stdv * math.sqrt(3)
self.weight:apply(function()
return torch.uniform(-stdv, stdv)
end)
self.bias:apply(function()
return torch.uniform(-stdv, stdv)
end)
else
local ninp = torch.Tensor(self.nOutputPlane):zero()
for i=1,self.connTable:size(1) do ninp[self.connTable[i][2]] = ninp[self.connTable[i][2]]+1 end
for k=1,self.connTable:size(1) do
stdv = 1/math.sqrt(self.kW*self.kH*ninp[self.connTable[k][2]])
self.weight:select(1,k):apply(function() return torch.uniform(-stdv,stdv) end)
end
for k=1,self.bias:size(1) do
stdv = 1/math.sqrt(self.kW*self.kH*ninp[k])
self.bias[k] = torch.uniform(-stdv,stdv)
end
end
end
function SpatialSADMap:updateOutput(input)
input.nn.SpatialSADMap_updateOutput(self, input)
return self.output
end
function SpatialSADMap:updateGradInput(input, gradOutput)
input.nn.SpatialSADMap_updateGradInput(self, input, gradOutput)
return self.gradInput
end
function SpatialSADMap:accGradParameters(input, gradOutput, scale)
return input.nn.SpatialSADMap_accGradParameters(self, input, gradOutput, scale)
end
function SpatialSADMap:decayParameters(decay)
self.weight:add(-decay, self.weight)
self.bias:add(-decay, self.bias)
end