forked from jfsantos/seq2seq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AddBias.lua
93 lines (83 loc) · 2.57 KB
/
AddBias.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
require 'nn';
local AddBias, parent = torch.class('nn.AddBias', 'nn.Module')
function AddBias:__init(inputSize,scalar)
parent.__init(self)
local size = inputSize
if scalar then size=1 end
self.scalar = scalar
self.bias = torch.Tensor(size)
self.gradBias = torch.Tensor(size)
self._ones = torch.Tensor{1}
self._bias_grid = torch.Tensor()
self:reset()
end
function AddBias:reset(stdv)
if stdv then
stdv = stdv * math.sqrt(3)
else
stdv = 1./math.sqrt(self.bias:size(1))
end
self.bias:uniform(-stdv, stdv)
end
function AddBias:updateOutput(input)
self.output = self.output:type(input:type()):resizeAs(input):copy(input)
if self.scalar then
self.output:add(self.bias[1]);
else
if input:isSameSizeAs(self.bias) then
self.output:add(self.bias)
else
if input:nDimension() == 2 then
-- single SGD mode
local length = input:size(1)
if self._ones:size(1) ~= length then
self._ones:resize(length):fill(1)
end
local bias = self.bias:view(-1)
local output = self.output:view(length, -1)
output:addr(1, self._ones, bias)
elseif input:nDimension() == 3 then
-- batchMode
local length = input:size(2)
if self._ones:size(1) ~= length then
self._ones:resize(length):fill(1)
end
local bias = self.bias:view(-1)
self._bias_grid = self._bias_grid:type(input:type())
self._bias_grid = self._bias_grid:resizeAs(input[1]):fill(0):addr(1, self._ones, bias)
self.output:add(self._bias_grid:resize(1,input:size(2),input:size(3)):expandAs(input))
else
error('input must be a 1D, 2D, or 3D tensor')
end
end
end
return self.output
end
function AddBias:updateGradInput(input, gradOutput)
if self.gradInput then
self.gradInput = self.gradInput:type(gradOutput:type())
self.gradInput:resizeAs(gradOutput):copy(gradOutput)
return self.gradInput
end
end
function AddBias:accGradParameters(input, gradOutput, scale)
scale = scale or 1
if self.gradBias:size(1) == 1 then
self.gradBias[1] = self.gradBias[1] + scale*gradOutput:sum();
else
if input:isSameSizeAs(self.bias) then
self.gradBias:add(scale, gradOutput)
else
if input:nDimension() == 2 then
-- SGD
local gradOutput = gradOutput:view(input:size(1), -1)
self.gradBias:view(-1):addmv(scale, gradOutput:t(), self._ones)
elseif input:nDimension() == 3 then
-- batchMode
self.gradBias:addmv(scale, torch.sum(gradOutput,1):squeeze():t(), self._ones)
else
error('input must be 1D, 2D, or 3D tensor')
end
end
end
end