-
-
Notifications
You must be signed in to change notification settings - Fork 608
/
Copy pathrecurrent.jl
356 lines (282 loc) · 9.3 KB
/
recurrent.jl
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
function cell_loss1(r, x, state)
for x_t in x
_, state = r(x_t, state)
end
return mean(state[1])
end
function cell_loss2(r, x, state)
y = [r(x_t, state)[1] for x_t in x]
return sum(mean, y)
end
function cell_loss3(r, x, state)
y = []
for x_t in x
y_t, state = r(x_t, state)
y = [y..., y_t]
end
return sum(mean, y)
end
function cell_loss4(r, x, state)
y = []
for x_t in x
y_t, state = r(x_t, state)
y = vcat(y, [y_t])
end
y = stack(y, dims=2) # [D, L] or [D, L, B]
return mean(y.^2)
end
@testset "RNNCell" begin
r = RNNCell(3 => 5)
@test length(Flux.trainables(r)) == 3
# An input sequence of length 6 and batch size 4.
x = [rand(Float32, 3, 4) for _ in 1:6]
# Initial State is a single vector
h = randn(Float32, 5)
test_gradients(r, x, h, loss=cell_loss1) # for loop
test_gradients(r, x, h, loss=cell_loss2) # comprehension
test_gradients(r, x, h, loss=cell_loss3) # splat
test_gradients(r, x, h, loss=cell_loss4) # vcat and stack
# initial states are zero
@test Flux.initialstates(r) ≈ zeros(Float32, 5)
# no initial state same as zero initial state
out, state = r(x[1])
@test out === state
@test out ≈ r(x[1], zeros(Float32, 5))[1]
# Now initial state has a batch dimension.
h = randn(Float32, 5, 4)
test_gradients(r, x, h, loss=cell_loss4)
# The input sequence has no batch dimension.
x = [rand(Float32, 3) for _ in 1:6]
h = randn(Float32, 5)
test_gradients(r, x, h, loss=cell_loss4)
# No Bias
r = RNNCell(3 => 5, bias=false)
@test length(Flux.trainables(r)) == 2
test_gradients(r, x, h, loss=cell_loss4)
end
@testset "RNN" begin
struct ModelRNN
rnn::RNN
h0::AbstractVector
end
Flux.@layer ModelRNN
(m::ModelRNN)(x) = m.rnn(x, m.h0)
model = ModelRNN(RNN(2 => 4), zeros(Float32, 4))
x = rand(Float32, 2, 3, 1)
y = model(x)
@test y isa Array{Float32, 3}
@test size(y) == (4, 3, 1)
test_gradients(model, x)
rnn = model.rnn
# initial states are zero
@test Flux.initialstates(rnn) ≈ zeros(Float32, 4)
# no initial state same as zero initial state
@test rnn(x) ≈ rnn(x, zeros(Float32, 4))
x = rand(Float32, 2, 3)
y = model(x)
@test y isa Array{Float32, 2}
@test size(y) == (4, 3)
test_gradients(model, x)
# testing return state
model = ModelRNN(RNN(2 => 4; return_state = true), zeros(Float32, 4))
x = rand(Float32, 2, 3, 1)
y, last_state = model(x)
@test y isa Array{Float32, 3}
@test size(y) == (4, 3, 1)
@test last_state isa Matrix{Float32}
@test size(last_state) == (4, 1)
end
@testset "LSTMCell" begin
cell = LSTMCell(3 => 5)
@test length(Flux.trainables(cell)) == 3
x = [rand(Float32, 3, 4) for _ in 1:6]
h = zeros(Float32, 5, 4)
c = zeros(Float32, 5, 4)
out, state = cell(x[1], (h, c))
hnew, cnew = state
@test out === hnew
@test hnew isa Matrix{Float32}
@test cnew isa Matrix{Float32}
@test size(hnew) == (5, 4)
@test size(cnew) == (5, 4)
test_gradients(cell, x[1], (h, c), loss = (m, x, hc) -> mean(m(x, hc)[1]))
test_gradients(cell, x, (h, c), loss = cell_loss4)
# initial states are zero
h0, c0 = Flux.initialstates(cell)
@test h0 ≈ zeros(Float32, 5)
@test c0 ≈ zeros(Float32, 5)
# no initial state same as zero initial state
_, (hnew1, cnew1) = cell(x[1])
_, (hnew2, cnew2) = cell(x[1], (zeros(Float32, 5), zeros(Float32, 5)))
@test hnew1 ≈ hnew2
@test cnew1 ≈ cnew2
# no bias
cell = LSTMCell(3 => 5, bias=false)
@test length(Flux.trainables(cell)) == 2
end
@testset "LSTM" begin
struct ModelLSTM
lstm::LSTM
h0::AbstractVector
c0::AbstractVector
end
Flux.@layer ModelLSTM
(m::ModelLSTM)(x) = m.lstm(x, (m.h0, m.c0))
model = ModelLSTM(LSTM(2 => 4), zeros(Float32, 4), zeros(Float32, 4))
x = rand(Float32, 2, 3, 1)
h = model(x)
@test h isa Array{Float32, 3}
@test size(h) == (4, 3, 1)
test_gradients(model, x)
x = rand(Float32, 2, 3)
h = model(x)
@test h isa Array{Float32, 2}
@test size(h) == (4, 3)
test_gradients(model, x, loss = (m, x) -> mean(m(x)[1]))
# test default initial states
lstm = model.lstm
h = lstm(x)
@test h isa Array{Float32, 2}
@test size(h) == (4, 3)
# initial states are zero
h0, c0 = Flux.initialstates(lstm)
@test h0 ≈ zeros(Float32, 4)
@test c0 ≈ zeros(Float32, 4)
# no initial state same as zero initial state
h1 = lstm(x, (zeros(Float32, 4), zeros(Float32, 4)))
@test h ≈ h1
# testing return state
model = ModelLSTM(LSTM(2 => 4; return_state = true), zeros(Float32, 4), zeros(Float32, 4))
x = rand(Float32, 2, 3, 1)
y, last_state = model(x)
@test y isa Array{Float32, 3}
@test size(y) == (4, 3, 1)
@test last_state[1] isa Matrix{Float32}
@test last_state[2] isa Matrix{Float32}
@test size(last_state[1]) == (4, 1)
@test size(last_state[2]) == (4, 1)
end
@testset "GRUCell" begin
r = GRUCell(3 => 5)
@test length(Flux.trainables(r)) == 3
# An input sequence of length 6 and batch size 4.
x = [rand(Float32, 3, 4) for _ in 1:6]
# Initial State is a single vector
h = randn(Float32, 5)
out, state = r(x[1], h)
@test out === state
test_gradients(r, x, h; loss = cell_loss4)
# initial states are zero
@test Flux.initialstates(r) ≈ zeros(Float32, 5)
# no initial state same as zero initial state
@test r(x[1])[1] ≈ r(x[1], zeros(Float32, 5))[1]
# Now initial state has a batch dimension.
h = randn(Float32, 5, 4)
test_gradients(r, x, h; loss = cell_loss4)
# The input sequence has no batch dimension.
x = [rand(Float32, 3) for _ in 1:6]
h = randn(Float32, 5)
test_gradients(r, x, h; loss = cell_loss4)
# No Bias
r = GRUCell(3 => 5, bias=false)
@test length(Flux.trainables(r)) == 2
end
@testset "GRU" begin
struct ModelGRU
gru::GRU
h0::AbstractVector
end
Flux.@layer ModelGRU
(m::ModelGRU)(x) = m.gru(x, m.h0)
model = ModelGRU(GRU(2 => 4), zeros(Float32, 4))
x = rand(Float32, 2, 3, 1)
y = model(x)
@test y isa Array{Float32, 3}
@test size(y) == (4, 3, 1)
test_gradients(model, x)
gru = model.gru
# initial states are zero
@test Flux.initialstates(gru) ≈ zeros(Float32, 4)
# no initial state same as zero initial state
@test gru(x) ≈ gru(x, zeros(Float32, 4))
# No Bias
gru = GRU(2 => 4, bias=false)
@test length(Flux.trainables(gru)) == 2
test_gradients(gru, x)
# testing return state
model = ModelGRU(GRU(2 => 4; return_state = true), zeros(Float32, 4))
x = rand(Float32, 2, 3, 1)
y, last_state = model(x)
@test y isa Array{Float32, 3}
@test size(y) == (4, 3, 1)
@test last_state isa Matrix{Float32}
@test size(last_state) == (4, 1)
end
@testset "GRUv3Cell" begin
r = GRUv3Cell(3 => 5)
@test length(Flux.trainables(r)) == 4
x = rand(Float32, 3)
# Initial State is a single vector
h = randn(Float32, 5)
out, state = r(x, h)
@test out === state
test_gradients(r, x, h, loss = (m, x, h) -> mean(m(x, h)[1]))
# initial states are zero
@test Flux.initialstates(r) ≈ zeros(Float32, 5)
# no initial state same as zero initial state
@test r(x)[1] ≈ r(x, zeros(Float32, 5))[1]
# Now initial state has a batch dimension.
h = randn(Float32, 5, 4)
test_gradients(r, x, h, loss = (m, x, h) -> mean(m(x, h)[1]))
# The input sequence has no batch dimension.
x = rand(Float32, 3)
h = randn(Float32, 5)
test_gradients(r, x, h, loss = (m, x, h) -> mean(m(x, h)[1]))
end
@testset "GRUv3" begin
struct ModelGRUv3
gru::GRUv3
h0::AbstractVector
end
Flux.@layer ModelGRUv3
(m::ModelGRUv3)(x) = m.gru(x, m.h0)
model = ModelGRUv3(GRUv3(2 => 4), zeros(Float32, 4))
x = rand(Float32, 2, 3, 1)
y = model(x)
@test y isa Array{Float32, 3}
@test size(y) == (4, 3, 1)
test_gradients(model, x)
gru = model.gru
# initial states are zero
@test Flux.initialstates(gru) ≈ zeros(Float32, 4)
# no initial state same as zero initial state
@test gru(x) ≈ gru(x, zeros(Float32, 4))
# testing return state
model = ModelGRUv3(GRUv3(2 => 4; return_state = true), zeros(Float32, 4))
x = rand(Float32, 2, 3, 1)
y, last_state = model(x)
@test y isa Array{Float32, 3}
@test size(y) == (4, 3, 1)
@test last_state isa Matrix{Float32}
@test size(last_state) == (4, 1)
end
@testset "Recurrence" begin
x = rand(Float32, 2, 3, 4)
for rnn in [RNN(2 => 3), LSTM(2 => 3), GRU(2 => 3), GRUv3(2 => 3)]
cell = rnn.cell
rec = Recurrence(cell)
@test rec(x) ≈ rnn(x)
end
for rnn in [RNN(2 => 3; return_state = true), LSTM(2 => 3; return_state = true),
GRU(2 => 3; return_state = true), GRUv3(2 => 3; return_state = true)]
cell = rnn.cell
rec = Recurrence(cell; return_state = true)
@test rec(x)[1] ≈ rnn(x)[1]
if !(typeof(rnn) <: LSTM)
@test rec(x)[2] ≈ rnn(x)[2]
else
@test rec(x)[2][1] ≈ rnn(x)[2][1]
@test rec(x)[2][2] ≈ rnn(x)[2][2]
end
end
end