forked from jfsantos/seq2seq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TemporalConvolutionZeroBias.lua
58 lines (47 loc) · 1.69 KB
/
TemporalConvolutionZeroBias.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
local TemporalConvolutionZeroBias, parent = torch.class('nn.TemporalConvolutionZeroBias', 'nn.Module')
function TemporalConvolutionZeroBias:__init(inputFrameSize, outputFrameSize, kW, dW)
parent.__init(self)
dW = dW or 1
self.inputFrameSize = inputFrameSize
self.outputFrameSize = outputFrameSize
self.kW = kW
self.dW = dW
self.weight = torch.Tensor(outputFrameSize, inputFrameSize*kW)
self.bias = torch.Tensor(outputFrameSize):zero()
self.gradWeight = torch.Tensor(outputFrameSize, inputFrameSize*kW)
self.gradBias = torch.Tensor(outputFrameSize)
self:reset()
end
function TemporalConvolutionZeroBias: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
self.bias:zero()
end
function TemporalConvolutionZeroBias:updateOutput(input)
self.bias:zero()
return input.nn.TemporalConvolution_updateOutput(self, input)
end
function TemporalConvolutionZeroBias:updateGradInput(input, gradOutput)
self.bias:zero()
if self.gradInput then
return input.nn.TemporalConvolution_updateGradInput(self, input, gradOutput)
end
end
function TemporalConvolutionZeroBias:accGradParameters(input, gradOutput, scale)
scale = scale or 1
input.nn.TemporalConvolution_accGradParameters(self, input, gradOutput, scale)
self.gradBias:zero()
self.bias:zero()
end
-- we do not need to accumulate parameters when sharing
TemporalConvolutionZeroBias.sharedAccUpdateGradParameters = TemporalConvolutionZeroBias.accUpdateGradParameters