forked from jfsantos/seq2seq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinearZeroBias.lua
83 lines (70 loc) · 2.29 KB
/
LinearZeroBias.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
local LinearZeroBias, parent = torch.class('nn.LinearZeroBias', 'nn.Module')
function LinearZeroBias:__init(inputSize, outputSize)
parent.__init(self)
self.weight = torch.Tensor(outputSize, inputSize)
self.gradWeight = torch.Tensor(outputSize, inputSize)
self:reset()
end
function LinearZeroBias:reset(stdv)
if stdv then
stdv = stdv * math.sqrt(3)
else
stdv = 1./math.sqrt(self.weight:size(2))
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
else
self.weight:uniform(-stdv, stdv)
end
return self
end
function LinearZeroBias:updateOutput(input)
if input:dim() == 1 then
self.output:resize(self.weight:size(1)):zero()
self.output:addmv(1, self.weight, input)
elseif input:dim() == 2 then
local nframe = input:size(1)
local nElement = self.output:nElement()
self.output:resize(nframe, self.weight:size(1))
if self.output:nElement() ~= nElement then
self.output:zero()
end
self.output:addmm(0, self.output, 1, input, self.weight:t())
else
error('input must be vector or matrix')
end
return self.output
end
function LinearZeroBias: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
if input:dim() == 1 then
self.gradInput:addmv(0, 1, self.weight:t(), gradOutput)
elseif input:dim() == 2 then
self.gradInput:addmm(0, 1, gradOutput, self.weight)
end
return self.gradInput
end
end
function LinearZeroBias:accGradParameters(input, gradOutput, scale)
scale = scale or 1
if input:dim() == 1 then
self.gradWeight:addr(scale, gradOutput, input)
elseif input:dim() == 2 then
self.gradWeight:addmm(scale, gradOutput:t(), input)
end
end
-- we do not need to accumulate parameters when sharing
LinearZeroBias.sharedAccUpdateGradParameters = LinearZeroBias.accUpdateGradParameters
function LinearZeroBias:__tostring__()
return torch.type(self) ..
string.format('(%d -> %d)', self.weight:size(2), self.weight:size(1))
end