forked from jcjohnson/torch-rnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GRU.lua
265 lines (208 loc) · 7.14 KB
/
GRU.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
require 'torch'
require 'nn'
local lu = require 'util.layer_utils'
local layer, parent = torch.class('nn.GRU', 'nn.Module')
--[[
If we add up the sizes of all the tensors for output, gradInput, weights,
gradWeights, and temporary buffers, we get that a SequenceGRU stores this many
scalar values:
NTD + 4NTH + 5NH + 6H^2 + 6DH + 7H
Note that this class doesn't own input or gradOutput, so you'll
see a bit higher memory usage in practice.
--]]
function layer:__init(input_dim, hidden_dim)
parent.__init(self)
local D, H = input_dim, hidden_dim
self.input_dim, self.hidden_dim = D, H
self.weight = torch.Tensor(D + H, 3 * H)
self.gradWeight = torch.Tensor(D + H, 3 * H):zero()
self.bias = torch.Tensor(3 * H)
self.gradBias = torch.Tensor(3 * H):zero()
self:reset()
self.cell = torch.Tensor() -- This will be (N, T, H)
self.gates = torch.Tensor() -- This will be (N, T, 3H)
self.buffer1 = torch.Tensor() -- This will be (N, H)
self.buffer2 = torch.Tensor() -- This will be (N, H)
self.buffer3 = torch.Tensor() -- This will be (H,)
self.grad_a_buffer = torch.Tensor() -- This will be (N, 3H)
self.h0 = torch.Tensor()
self.remember_states = false
self.grad_h0 = torch.Tensor()
self.grad_x = torch.Tensor()
self.gradInput = {self.grad_c0, self.grad_h0, self.grad_x}
end
function layer:reset(std)
if not std then
std = 1.0 / math.sqrt(self.hidden_dim + self.input_dim)
end
--self.bias:zero()
self.bias:normal(0,std) --self.bias[{{self.hidden_dim + 1, 2 * self.hidden_dim}}]:fill(1)
self.weight:normal(0, std)
return self
end
function layer:resetStates()
self.h0 = self.h0.new()
end
local function check_dims(x, dims)
assert(x:dim() == #dims)
for i, d in ipairs(dims) do
assert(x:size(i) == d)
end
end
function layer:_unpack_input(input)
local h0, x = nil, nil
if torch.type(input) == 'table' and #input == 2 then
h0, x = unpack(input)
elseif torch.isTensor(input) then
x = input
else
assert(false, 'invalid input')
end
return h0, x
end
function layer:_get_sizes(input, gradOutput)
local h0, x = self:_unpack_input(input)
local N, T = x:size(1), x:size(2)
local H, D = self.hidden_dim, self.input_dim
check_dims(x, {N, T, D})
if h0 then
check_dims(h0, {N, H})
end
if gradOutput then
check_dims(gradOutput, {N, T, H})
end
return N, T, D, H
end
--[[
Input:
- h0: Initial hidden state, (N, H)
- x: Input sequence, (N, T, D)
Output:
- h: Sequence of hidden states, (N, T, H)
--]]
function layer:updateOutput(input)
local h0, x = self:_unpack_input(input)
local N, T, D, H = self:_get_sizes(input)
self._return_grad_h0 = (h0 ~= nil)
if not h0 then
h0 = self.h0
if h0:nElement() == 0 or not self.remember_states then
h0:resize(N, H):zero()
elseif self.remember_states then
local prev_N, prev_T = self.output:size(1), self.output:size(2)
assert(prev_N == N, 'batch sizes must be the same to remember states')
h0:copy(self.output[{{}, prev_T}])
end
end
local bias_expand = self.bias:view(1, 3 * H):expand(N * T, 3 * H)
local Wx = self.weight[{{1, D}}]
local Wh = self.weight[{{D + 1, D + H}}]
local h = self.output
h:resize(N, T, H):zero()
local prev_h = h0
self.gates:resize(N, T, 3 * H):zero()
self.gates:view(N * T, 3 * H):addmm(bias_expand, x:view(N * T, D), Wx)
for t = 1, T do
local next_h = h[{{}, t}]
local cur_gates = self.gates[{{}, t}]
cur_gates[{{}, {1, 2 * H}}]:addmm(prev_h, Wh[{{}, {1, 2 * H}}])
cur_gates[{{}, {1, 2 * H}}]:sigmoid()
local u = cur_gates[{{}, {1, H}}] --update gate : u = sig(Wx * x + Wh * prev_h + b)
local r = cur_gates[{{}, {H + 1, 2 * H}}] --reset gate : r = sig(Wx * x + Wh * prev_h + b)
next_h:cmul(r, prev_h) --temporary buffer : r . prev_h
cur_gates[{{}, {2 * H + 1, 3 * H}}]:addmm(next_h, Wh[{{}, {2 * H + 1, 3 * H}}]) -- hc += Wh * r . prev_h
local hc = cur_gates[{{}, {2 * H + 1, 3 * H}}]:tanh() --hidden candidate : hc = tanh(Wx * x + Wh * r . prev_h + b)
next_h:addcmul(prev_h,-1, u, prev_h)
next_h:addcmul(u,hc) --next_h = (1-u) . prev_h + u . hc
prev_h = next_h
end
return self.output
end
function layer:backward(input, gradOutput, scale)
scale = scale or 1.0
local h0, x = self:_unpack_input(input)
if not h0 then h0 = self.h0 end
local grad_h0, grad_x = self.grad_h0, self.grad_x
local h= self.output
local grad_h = gradOutput
local N, T, D, H = self:_get_sizes(input, gradOutput)
local Wx = self.weight[{{1, D}}]
local Wh = self.weight[{{D + 1, D + H}}]
local grad_Wx = self.gradWeight[{{1, D}}]
local grad_Wh = self.gradWeight[{{D + 1, D + H}}]
local grad_b = self.gradBias
grad_h0:resizeAs(h0):zero()
grad_x:resizeAs(x):zero()
local grad_next_h = self.buffer1:resizeAs(h0):zero()
local temp_buffer = self.buffer2:resizeAs(h0):zero()
for t = T, 1, -1 do
local prev_h
if t == 1 then
prev_h = h0
else
prev_h = h[{{}, t - 1}]
end
grad_next_h:add(grad_h[{{}, t}])
local u = self.gates[{{}, t, {1, H}}]
local r = self.gates[{{}, t, {H + 1, 2 * H}}]
local hc = self.gates[{{}, t, {2 * H + 1, 3 * H}}]
local grad_a = self.grad_a_buffer:resize(N, 3 * H):zero()
local grad_au = grad_a[{{}, {1, H}}]
local grad_ar = grad_a[{{}, {H + 1, 2 * H}}]
local grad_ahc = grad_a[{{}, {2 * H + 1, 3 * H}}]
-- We will use grad_au as temporary buffer
-- to compute grad_ahc.
local grad_hc = grad_au:cmul(grad_next_h, u)
lu.tanh_gradient(grad_ahc, hc, grad_hc)
local grad_r = grad_au:mm(grad_ahc, Wh[{{}, {2 * H + 1, 3 * H}}]:t() ):cmul(prev_h)
lu.sigmoid_gradient(grad_ar, r, grad_r)
temp_buffer:add(hc, -1, prev_h)
lu.sigmoid_gradient(grad_au, u, grad_next_h)
grad_au:cmul(temp_buffer)
grad_x[{{}, t}]:mm(grad_a, Wx:t())
grad_Wx:addmm(scale, x[{{}, t}]:t(), grad_a)
grad_Wh[{{}, {1, 2 * H}}]:addmm(scale, prev_h:t(), grad_a[{{}, {1, 2 * H}}])
local grad_a_sum = self.buffer3:resize(H):sum(grad_a, 1)
grad_b:add(scale, grad_a_sum)
temp_buffer:cmul(prev_h, r)
grad_Wh[{{}, {2 * H + 1, 3 * H}}]:addmm(scale, temp_buffer:t(), grad_ahc)
grad_next_h:addcmul(-1, u, grad_next_h)
grad_next_h:addmm(grad_a[{{}, {1, 2 * H}}], Wh[{{}, {1, 2 * H}}]:t())
temp_buffer:mm(grad_a[{{}, {2 * H + 1, 3 * H}}], Wh[{{}, {2 * H + 1, 3 * H}}]:t()):cmul(r)
grad_next_h:add(temp_buffer)
end
grad_h0:copy(grad_next_h)
if self._return_grad_h0 then
self.gradInput = {self.grad_h0, self.grad_x}
else
self.gradInput = self.grad_x
end
return self.gradInput
end
function layer:updateGradInput(input, gradOutput)
self:backward(input, gradOutput, 0)
end
function layer:accGradParameters(input, gradOutput, scale)
self:backward(input, gradOutput, scale)
end
function layer:clearState()
self.cell:set()
self.gates:set()
self.buffer1:set()
self.buffer2:set()
self.buffer3:set()
self.grad_a_buffer:set()
self.grad_h0:set()
self.grad_x:set()
self.output:set()
end
function layer:setBatchSize(N)
local H = self.hidden_dim
local T = self.output:size(2)
self.output:resize(N, T, H):zero()
self.h0:resize(N, H):zero()
end
function layer:getState(n)
local T = self.output:size(2)
return self.output[{n, T}]
end