-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparseBlockTemporalConvolution.lua
More file actions
188 lines (144 loc) · 6.32 KB
/
SparseBlockTemporalConvolution.lua
File metadata and controls
188 lines (144 loc) · 6.32 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
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
local SparseBlockTemporalConvolution, parent = torch.class('nn.SparseBlockTemporalConvolution', 'nn.Module')
--[[ Module description:
current implementation assumes:
a) fullbatch
b) default input is allways zero which enables for sparse backpropagaion. (teDefault is not used at this point)
c) no bias in this layer
Note: isRelax means to allow this module to act as "identity" for column input in which the width becomes smaller than kW
It needs to be used carefully knowning the effects.
namely, if any given column is affected by relax, changes in frameSize will not be applied to it either.
therefore watch for "inconsistent tensor size" type of errors in subequent layers
--]]
function SparseBlockTemporalConvolution:__init(inputFrameSize, outputFrameSize, kW, dW, isRelax)
dW = dW or 1
self.inputFrameSize = inputFrameSize -- # of input channels
self.outputFrameSize = outputFrameSize -- # of output channels
self.kW = kW
self.dW = dW
self.isRelax = isRelax or false
self.weight = torch.Tensor(outputFrameSize, inputFrameSize*kW)
self.gradWeight = torch.Tensor(outputFrameSize, inputFrameSize*kW)
self.dummyBias = torch.zeros(outputFrameSize)
self:reset()
end
function SparseBlockTemporalConvolution:reset(stdv)
if stdv then
stdv = stdv * math.sqrt(3)
else
stdv = 1/math.sqrt(self.kW*self.inputFrameSize)
end
if nn.oldSeed then
self.weight:apply(function()
return torch.uniform(-stdv, stdv)
end)
else
self.weight:uniform(-stdv, stdv)
end
end
function SparseBlockTemporalConvolution:pri_ensureOutput(input)
if self.output ~= nil then
return
end
self.output = { nBatchSize = input.nBatchSize, taData = {} }
local nColumns = table.getn(input.taData)
for i=1, nColumns do
local taInputCurr = input.taData[i]
if self.isRelax and taInputCurr.teValue:size(2) < self.kW then -- when smaller then identity
taOutputCurr = { teValue = torch.Tensor(),
teRowIdx = torch.LongTensor() }
else
local nRows = taInputCurr.teValue:size(1)
local nWidth = (taInputCurr.teValue:size(2) - self.kW) / self.dW + 1;
taOutputCurr = { teValue = torch.zeros(nRows, nWidth, self.outputFrameSize),
teRowIdx = taInputCurr.teRowIdx }
end
table.insert(self.output.taData, taOutputCurr)
end
end
function SparseBlockTemporalConvolution:pri_ensureGradInput(input)
if self.gradInput ~= nil then
return
end
self.gradInput = { nBatchSize = input.nBatchSize, taData = {} }
local nColumns = table.getn(input.taData)
for i=1, nColumns do
local taInputCurr = input.taData[i]
if self.isRelax and taInputCurr.teValue:size(2) < self.kW then -- when smaller then identity
taGradInputCurr = { teValue = torch.Tensor(),
teRowIdx = torch.LongTensor() }
else
taGradInputCurr = { teValue = torch.zeros( taInputCurr.teValue:size() ),
teRowIdx = taInputCurr.teRowIdx }
end
table.insert(self.gradInput.taData, taGradInputCurr)
end
end
function SparseBlockTemporalConvolution:pri_updateOutput_column(taInput, taOutput)
if self.isRelax and taInput.teValue:size(2) < self.kW then -- when smaller then identity
taOutput.teValue:set(taInput.teValue)
taOutput.teRowIdx:set(taInput.teRowIdx)
else
local input = taInput.teValue
local output = taOutput.teValue
input.THNN.TemporalConvolution_updateOutput(input:cdata(), output:cdata(),
self.weight:cdata(), self.dummyBias:cdata(),
self.kW, self.dW,
self.inputFrameSize, self.outputFrameSize)
end
end
function SparseBlockTemporalConvolution:updateOutput(input)
-- ensure output created
self:pri_ensureOutput(input)
-- update data
local nColumns = table.getn(self.output.taData)
for i=1, nColumns do
self:pri_updateOutput_column(input.taData[i],
self.output.taData[i])
end
return self.output
end
function SparseBlockTemporalConvolution:pri_updateGradInput_column(taInput, taGradOutput, taGradInput)
-- Note: mimicing the logic of nDimension==3 in "TemporalConvolution_updateGradInput"
if self.isRelax and taInput.teValue:size(2) < self.kW then -- when smaller then identity
taGradInput.teValue:set(taGradOutput.teValue)
taGradInput.teRowIdx:set(taGradOutput.teRowIdx)
else
local input = taInput.teValue
local gradOutput = taGradOutput.teValue
local gradInput = taGradInput.teValue
input.THNN.TemporalConvolution_updateGradInput(input:cdata(), gradOutput:cdata(),
gradInput:cdata(), self.weight:cdata(),
self.kW, self.dW)
end
end
function SparseBlockTemporalConvolution:updateGradInput(input, gradOutput)
-- ensure GradInput created
self:pri_ensureGradInput(input)
-- update data
local nColumns = table.getn(self.gradInput.taData)
for i=1, nColumns do
self:pri_updateGradInput_column(input.taData[i],
gradOutput.taData[i],
self.gradInput.taData[i])
end
return self.gradInput
end
function SparseBlockTemporalConvolution:pri_accGradParameters_column(taInput, taGradOutput, scale)
if self.isRelax and taInput.teValue:size(2) < self.kW then -- when smaller then identity
return
end
local input = taInput.teValue
local gradOutput = taGradOutput.teValue
input.THNN.TemporalConvolution_accGradParameters(input:cdata(), gradOutput:cdata(),
self.gradWeight:cdata(), self.dummyBias:cdata(),
self.kW, self.dW, scale)
end
function SparseBlockTemporalConvolution:accGradParameters(input, gradOutput, scale)
local scale = scale or 1
local nColumns = table.getn(input.taData)
for i=1, nColumns do
self:pri_accGradParameters_column(input.taData[i],
gradOutput.taData[i],
scale)
end
end