-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearSparse.lua
198 lines (159 loc) · 5.67 KB
/
LinearSparse.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
local THNN = require 'nn.THNN'
npy4th = require 'npy4th'
local LinearSparse, parent = torch.class('nn.LinearSparse', 'nn.Module')
function CreateRandomSparseWeightMatrix(density, inputSize, outputSize)
randElements = torch.randperm(inputSize*outputSize)
nElements = math.floor(density * inputSize* outputSize)
weights = torch.Tensor(nElements)
rows = torch.Tensor(nElements):int()
cols = torch.Tensor(nElements):int()
iRowStart = torch.Tensor(outputSize+1):int()
t = 1
for i = 0, outputSize - 1 do
for j = 0, inputSize - 1 do
rows[t] = i
cols[t] = j
t = t+1
end
end
-- for i = 1,nElements do
-- rows[i] = math.floor((randElements[i] - 1)/inputSize)
-- cols[i] = (randElements[i] - 1) % inputSize
-- end
rows, i = rows:sort()
cols = cols:index(1, i)
iRowStart[rows[1]+1] = 0
actRow = rows[1]
istart = 1
for i=2,nElements do
if actRow ~= rows[i] then
subVector = cols[{{istart,i-1}}]
cols[{{istart,i-1}}] = subVector:sort()
iRowStart[rows[i]+1] = i-1
actRow = rows[i]
istart = i
end
end
subVector = cols[{{istart,nElements}}]
cols[{{istart,nElements}}] = subVector:sort()
iRowStart[outputSize+1] = nElements
return weights, rows, cols, iRowStart
end
function CreateAndPrepareWeights(nRows, nCols, nChannels, nChannelsOut, rows, cols)
local outputSize = nCols*nRows*nChannelsOut
nElements = (#rows)[1]
local weights = torch.zeros(nElements)
local iRowStart = torch.Tensor(outputSize+1):int()
rows, i = rows:sort()
cols = cols:index(1, i)
iRowStart[rows[1]+1] = 0
actRow = rows[1]
istart = 1
for i=2,nElements do
if actRow ~= rows[i] then
subVector = cols[{{istart,i-1}}]
cols[{{istart,i-1}}] = subVector:sort()
iRowStart[rows[i]+1] = i-1
actRow = rows[i]
istart = i
end
end
subVector = cols[{{istart,nElements}}]
cols[{{istart,nElements}}] = subVector:sort()
iRowStart[outputSize+1] = nElements
return weights, rows, cols, iRowStart
end
function LinearSparse:__init(imgWidth, imgHeight, nChannels, nChannelsOut, rows, cols, bias)
parent.__init(self)
local bias = ((bias == nil) and true) or bias
local outputSize = imgWidth*imgHeight*nChannelsOut
self.weight, self.rows, self.cols, self.iRowStart = CreateAndPrepareWeights(imgHeight, imgWidth, nChannels, nChannelsOut, rows, cols)
self.nnz = self.weight:size(1)
self.gradWeight = torch.Tensor(self.nnz)
self.nRows = imgWidth*imgHeight*nChannelsOut
self.nCols = imgWidth*imgHeight*nChannels
if bias then
self.bias = torch.Tensor(outputSize)
self.gradBias = torch.Tensor(outputSize)
end
self:reset()
end
function LinearSparse:reset(stdv)
if stdv then
stdv = stdv * math.sqrt(3)
else
stdv = 1./math.sqrt(self.nCols)
end
if nn.oldSeed then
for i=1,self.weight:size(1) do
self.weight:select(1, i):apply(function()
return torch.uniform(-stdv, stdv)
end)
end
if self.bias then
for i=1,self.bias:nElement() do
self.bias[i] = torch.uniform(-stdv, stdv)
end
end
else
self.weight:uniform(-stdv, stdv)
if self.bias then self.bias:uniform(-stdv, stdv) end
end
return self
end
function LinearSparse:updateOutput(input)
if input:dim() == 1 then
self.output:resize(self.nRows)
if self.bias then self.output:copy(self.bias) else self.output:zero() end
elseif input:dim() == 2 then
local nframe = input:size(1)
local nElement = self.output:nElement()
self.output:resize(nframe, self.nRows)
if self.output:nElement() ~= nElement then
self.output:zero()
end
self.addBuffer = self.addBuffer or input.new()
if self.addBuffer:nElement() ~= nframe then
self.addBuffer:resize(nframe):fill(1)
end
else
error('input must be a matrix')
end
input.THNN.LinearSparse_updateOutput(input:cdata(), self.output:cdata(), self.weight:cdata(), self.rows:cdata(), self.cols:cdata(), self.iRowStart:cdata() ,self.nRows, self.nCols)
if self.bias and input:dim() == 2 then
self.output:addr(1, self.addBuffer, self.bias)
end
return self.output
end
function LinearSparse: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
input.THNN.LinearSparse_updateGradInput(input:cdata(), gradOutput:cdata(), self.gradInput:cdata(), self.weight:cdata(), self.rows:cdata(), self.cols:cdata(), self.nRows, self.nCols, self.iRowStart:cdata())
return self.gradInput
end
end
function LinearSparse:accGradParameters(input, gradOutput, scale)
scale = scale or 1
--input.THNN.LinearSparse_accGradParameters(input:cdata(),gradOutput:cdata(), self.gradWeight:cdata(), self.rows:cdata(), self.cols:cdata(), self.nnz, scale, self.iRowStart:cdata())
input.THNN.LinearSparse_accGradParameters(input:cdata(),gradOutput:cdata(), self.gradWeight:cdata(), self.rows:cdata(), self.cols:cdata(), self.nnz, scale)
if input:dim() == 1 then
if self.bias then self.gradBias:add(scale, gradOutput) end
elseif input:dim() == 2 then
if self.bias then
self.gradBias:addmv(scale, gradOutput:t(), self.addBuffer)
end
else
error('input must be a matrix')
end
end
-- we do not need to accumulate parameters when sharing
LinearSparse.sharedAccUpdateGradParameters = LinearSparse.accUpdateGradParameters
function LinearSparse:__tostring__()
return torch.type(self) ..
string.format('(%d -> %d)', self.nCols, self.nRows) ..
(self.bias == nil and ' without bias' or '')
end